GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 445 447 99.6 %
Date: 2022-11-20 04:28:36 Branches: 54 68 79.4 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_ENV_INL_H_
23
#define SRC_ENV_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "aliased_buffer.h"
28
#include "callback_queue-inl.h"
29
#include "env.h"
30
#include "node.h"
31
#include "node_context_data.h"
32
#include "node_internals.h"
33
#include "node_perf_common.h"
34
#include "node_realm-inl.h"
35
#include "util-inl.h"
36
#include "uv.h"
37
#include "v8.h"
38
39
#include <cstddef>
40
#include <cstdint>
41
42
#include <utility>
43
44
namespace node {
45
46
115833
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
47
115833
    IsolateData* isolate_data)
48
115833
    : node_allocator_(isolate_data->node_allocator()) {
49
115833
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
50
115833
}
51
52
231666
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
53
115833
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
54
115833
}
55
56
26154
inline v8::Isolate* IsolateData::isolate() const {
57
26154
  return isolate_;
58
}
59
60
1312305
inline uv_loop_t* IsolateData::event_loop() const {
61
1312305
  return event_loop_;
62
}
63
64
123516
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
65
123516
  return node_allocator_;
66
}
67
68
87447
inline MultiIsolatePlatform* IsolateData::platform() const {
69
87447
  return platform_;
70
}
71
72
742
inline void IsolateData::set_worker_context(worker::Worker* context) {
73
742
  CHECK_NULL(worker_context_);  // Should be set only once.
74
742
  worker_context_ = context;
75
742
}
76
77
16779
inline worker::Worker* IsolateData::worker_context() const {
78
16779
  return worker_context_;
79
}
80
81
215563
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
82
215563
  return async_wrap_providers_[index].Get(isolate_);
83
}
84
85
2701995
inline AliasedUint32Array& AsyncHooks::fields() {
86
2701995
  return fields_;
87
}
88
89
1620700
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
90
1620700
  return async_id_fields_;
91
}
92
93
792
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
94
792
  return async_ids_stack_;
95
}
96
97
888229
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
98
888229
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
99
792
    js_execution_async_resources_.Reset(
100
1584
        env()->isolate(), v8::Array::New(env()->isolate()));
101
  }
102
888229
  return PersistentToLocal::Strong(js_execution_async_resources_);
103
}
104
105
2411
v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
106
2411
  if (i >= native_execution_async_resources_.size()) return {};
107
2411
  return native_execution_async_resources_[i];
108
}
109
110
215157
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
111
215157
  return env()->isolate_data()->async_wrap_provider(idx);
112
}
113
114
1
inline void AsyncHooks::no_force_checks() {
115
1
  fields_[kCheck] -= 1;
116
1
}
117
118
459416
inline Environment* AsyncHooks::env() {
119
459416
  return Environment::ForAsyncHooks(this);
120
}
121
122
459416
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
123
459416
  return ContainerOf(&Environment::async_hooks_, hooks);
124
}
125
126
845821
inline size_t Environment::async_callback_scope_depth() const {
127
845821
  return async_callback_scope_depth_;
128
}
129
130
860349
inline void Environment::PushAsyncCallbackScope() {
131
860349
  async_callback_scope_depth_++;
132
860349
}
133
134
859742
inline void Environment::PopAsyncCallbackScope() {
135
859742
  async_callback_scope_depth_--;
136
859742
}
137
138
792
inline AliasedUint32Array& ImmediateInfo::fields() {
139
792
  return fields_;
140
}
141
142
213678
inline uint32_t ImmediateInfo::count() const {
143
213678
  return fields_[kCount];
144
}
145
146
331696
inline uint32_t ImmediateInfo::ref_count() const {
147
331696
  return fields_[kRefCount];
148
}
149
150
54473
inline bool ImmediateInfo::has_outstanding() const {
151
54473
  return fields_[kHasOutstanding] == 1;
152
}
153
154
46537
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
155
46537
  fields_[kRefCount] += increment;
156
46537
}
157
158
231637
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
159
231637
  fields_[kRefCount] -= decrement;
160
231637
}
161
162
792
inline AliasedUint8Array& TickInfo::fields() {
163
792
  return fields_;
164
}
165
166
1518717
inline bool TickInfo::has_tick_scheduled() const {
167
1518717
  return fields_[kHasTickScheduled] == 1;
168
}
169
170
549820
inline bool TickInfo::has_rejection_to_warn() const {
171
549820
  return fields_[kHasRejectionToWarn] == 1;
172
}
173
174
82063
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
175
82063
  if (UNLIKELY(!isolate->InContext())) return nullptr;
176
153274
  v8::HandleScope handle_scope(isolate);
177
76637
  return GetCurrent(isolate->GetCurrentContext());
178
}
179
180
8400655
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
181
8400655
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
182
13
    return nullptr;
183
  }
184
  return static_cast<Environment*>(
185
8400642
      context->GetAlignedPointerFromEmbedderData(
186
8400642
          ContextEmbedderIndex::kEnvironment));
187
}
188
189
6417021
inline Environment* Environment::GetCurrent(
190
    const v8::FunctionCallbackInfo<v8::Value>& info) {
191
6417021
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
192
}
193
194
template <typename T>
195
2329858
inline Environment* Environment::GetCurrent(
196
    const v8::PropertyCallbackInfo<T>& info) {
197
2329858
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
198
}
199
200
template <typename T, typename U>
201
inline T* Environment::GetBindingData(const v8::PropertyCallbackInfo<U>& info) {
202
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
203
}
204
205
template <typename T>
206
1033596
inline T* Environment::GetBindingData(
207
    const v8::FunctionCallbackInfo<v8::Value>& info) {
208
1033596
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
209
}
210
211
template <typename T>
212
1033598
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
213
  BindingDataStore* map = static_cast<BindingDataStore*>(
214
1033598
      context->GetAlignedPointerFromEmbedderData(
215
          ContextEmbedderIndex::kBindingListIndex));
216
  DCHECK_NOT_NULL(map);
217
1033598
  auto it = map->find(T::type_name);
218
1033598
  if (UNLIKELY(it == map->end())) return nullptr;
219
1033598
  T* result = static_cast<T*>(it->second.get());
220
  DCHECK_NOT_NULL(result);
221
  DCHECK_EQ(result->env(), GetCurrent(context));
222
1033598
  return result;
223
}
224
225
template <typename T>
226
26807
inline T* Environment::AddBindingData(
227
    v8::Local<v8::Context> context,
228
    v8::Local<v8::Object> target) {
229
  DCHECK_EQ(GetCurrent(context), this);
230
  // This won't compile if T is not a BaseObject subclass.
231
53614
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
232
  BindingDataStore* map = static_cast<BindingDataStore*>(
233
26807
      context->GetAlignedPointerFromEmbedderData(
234
          ContextEmbedderIndex::kBindingListIndex));
235
  DCHECK_NOT_NULL(map);
236
26807
  auto result = map->emplace(T::type_name, item);
237
26807
  CHECK(result.second);
238
  DCHECK_EQ(GetBindingData<T>(context), item.get());
239
26807
  return item.get();
240
}
241
242
24889581
inline v8::Isolate* Environment::isolate() const {
243
24889581
  return isolate_;
244
}
245
246
9793
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
247
9793
  return ContainerOf(&Environment::timer_handle_, handle);
248
}
249
250
34920
inline uv_timer_t* Environment::timer_handle() {
251
34920
  return &timer_handle_;
252
}
253
254
213678
inline Environment* Environment::from_immediate_check_handle(
255
    uv_check_t* handle) {
256
213678
  return ContainerOf(&Environment::immediate_check_handle_, handle);
257
}
258
259
25472
inline uv_check_t* Environment::immediate_check_handle() {
260
25472
  return &immediate_check_handle_;
261
}
262
263
258103
inline uv_idle_t* Environment::immediate_idle_handle() {
264
258103
  return &immediate_idle_handle_;
265
}
266
267
38208
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
268
                                               HandleCleanupCb cb,
269
                                               void* arg) {
270
38208
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
271
38208
}
272
273
template <typename T, typename OnCloseCallback>
274
39796
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
275
39796
  handle_cleanup_waiting_++;
276
  static_assert(sizeof(T) >= sizeof(uv_handle_t), "T is a libuv handle");
277
  static_assert(offsetof(T, data) == offsetof(uv_handle_t, data),
278
                "T is a libuv handle");
279
  static_assert(offsetof(T, close_cb) == offsetof(uv_handle_t, close_cb),
280
                "T is a libuv handle");
281
  struct CloseData {
282
    Environment* env;
283
    OnCloseCallback callback;
284
    void* original_data;
285
  };
286
39796
  handle->data = new CloseData { this, callback, handle->data };
287
39954
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
288
79434
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
289
39717
    data->env->handle_cleanup_waiting_--;
290
39717
    handle->data = data->original_data;
291
39717
    data->callback(reinterpret_cast<T*>(handle));
292
  });
293
39796
}
294
295
95320
void Environment::IncreaseWaitingRequestCounter() {
296
95320
  request_waiting_++;
297
95320
}
298
299
95312
void Environment::DecreaseWaitingRequestCounter() {
300
95312
  request_waiting_--;
301
95312
  CHECK_GE(request_waiting_, 0);
302
95312
}
303
304
1305903
inline uv_loop_t* Environment::event_loop() const {
305
1305903
  return isolate_data()->event_loop();
306
}
307
308
#if HAVE_INSPECTOR
309
72676
inline bool Environment::is_in_inspector_console_call() const {
310
72676
  return is_in_inspector_console_call_;
311
}
312
313
145350
inline void Environment::set_is_in_inspector_console_call(bool value) {
314
145350
  is_in_inspector_console_call_ = value;
315
145350
}
316
#endif
317
318
6119740
inline AsyncHooks* Environment::async_hooks() {
319
6119740
  return &async_hooks_;
320
}
321
322
878813
inline ImmediateInfo* Environment::immediate_info() {
323
878813
  return &immediate_info_;
324
}
325
326
760190
inline TickInfo* Environment::tick_info() {
327
760190
  return &tick_info_;
328
}
329
330
111542
inline uint64_t Environment::timer_base() const {
331
111542
  return timer_base_;
332
}
333
334
1502509
inline std::shared_ptr<KVStore> Environment::env_vars() {
335
1502509
  return env_vars_;
336
}
337
338
7132
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
339
7132
  env_vars_ = env_vars;
340
7132
}
341
342
22
inline bool Environment::printed_error() const {
343
22
  return printed_error_;
344
}
345
346
22
inline void Environment::set_printed_error(bool value) {
347
22
  printed_error_ = value;
348
22
}
349
350
11430
inline void Environment::set_trace_sync_io(bool value) {
351
11430
  trace_sync_io_ = value;
352
11430
}
353
354
124
inline bool Environment::abort_on_uncaught_exception() const {
355
124
  return options_->abort_on_uncaught_exception;
356
}
357
358
inline void Environment::set_force_context_aware(bool value) {
359
  options_->force_context_aware = value;
360
}
361
362
44
inline bool Environment::force_context_aware() const {
363
44
  return options_->force_context_aware;
364
}
365
366
11831
inline void Environment::set_exiting(bool value) {
367
11831
  exit_info_[kExiting] = value ? 1 : 0;
368
11831
}
369
370
16590
inline ExitCode Environment::exit_code(const ExitCode default_code) const {
371
16590
  return exit_info_[kHasExitCode] == 0
372
16590
             ? default_code
373
16590
             : static_cast<ExitCode>(exit_info_[kExitCode]);
374
}
375
376
792
inline AliasedInt32Array& Environment::exit_info() {
377
792
  return exit_info_;
378
}
379
380
734
inline void Environment::set_abort_on_uncaught_exception(bool value) {
381
734
  options_->abort_on_uncaught_exception = value;
382
734
}
383
384
822
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
385
822
  return should_abort_on_uncaught_toggle_;
386
}
387
388
270122
inline AliasedInt32Array& Environment::stream_base_state() {
389
270122
  return stream_base_state_;
390
}
391
392
52810
inline uint32_t Environment::get_next_module_id() {
393
52810
  return module_id_counter_++;
394
}
395
7358
inline uint32_t Environment::get_next_script_id() {
396
7358
  return script_id_counter_++;
397
}
398
36318
inline uint32_t Environment::get_next_function_id() {
399
36318
  return function_id_counter_++;
400
}
401
402
112465
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
403
112465
    Environment* env)
404
112465
    : env_(env) {
405
112465
  env_->PushShouldNotAbortOnUncaughtScope();
406
112465
}
407
408
224924
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
409
112462
  Close();
410
112462
}
411
412
112655
void ShouldNotAbortOnUncaughtScope::Close() {
413
112655
  if (env_ != nullptr) {
414
112462
    env_->PopShouldNotAbortOnUncaughtScope();
415
112462
    env_ = nullptr;
416
  }
417
112655
}
418
419
112465
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
420
112465
  should_not_abort_scope_counter_++;
421
112465
}
422
423
112462
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
424
112462
  should_not_abort_scope_counter_--;
425
112462
}
426
427
1
inline bool Environment::inside_should_not_abort_on_uncaught_scope() const {
428
1
  return should_not_abort_scope_counter_ > 0;
429
}
430
431
449926
inline std::vector<double>* Environment::destroy_async_id_list() {
432
449926
  return &destroy_async_id_list_;
433
}
434
435
215711
inline double Environment::new_async_id() {
436
215711
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
437
215711
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
438
}
439
440
247574
inline double Environment::execution_async_id() {
441
247574
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
442
}
443
444
65688
inline double Environment::trigger_async_id() {
445
65688
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
446
}
447
448
215708
inline double Environment::get_default_trigger_async_id() {
449
  double default_trigger_async_id =
450
215708
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
451
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
452
215708
  if (default_trigger_async_id < 0)
453
181795
    default_trigger_async_id = execution_async_id();
454
215708
  return default_trigger_async_id;
455
}
456
457
81992
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
458
81992
  return options_;
459
}
460
461
18043
inline const std::vector<std::string>& Environment::argv() {
462
18043
  return argv_;
463
}
464
465
7046
inline const std::vector<std::string>& Environment::exec_argv() {
466
7046
  return exec_argv_;
467
}
468
469
12738
inline const std::string& Environment::exec_path() const {
470
12738
  return exec_path_;
471
}
472
473
#if HAVE_INSPECTOR
474
6364
inline void Environment::set_coverage_directory(const char* dir) {
475
6364
  coverage_directory_ = std::string(dir);
476
6364
}
477
478
6385
inline void Environment::set_coverage_connection(
479
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
480
6385
  CHECK_NULL(coverage_connection_);
481
6385
  std::swap(coverage_connection_, connection);
482
6385
}
483
484
19152
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
485
19152
  return coverage_connection_.get();
486
}
487
488
6343
inline const std::string& Environment::coverage_directory() const {
489
6343
  return coverage_directory_;
490
}
491
492
12
inline void Environment::set_cpu_profiler_connection(
493
    std::unique_ptr<profiler::V8CpuProfilerConnection> connection) {
494
12
  CHECK_NULL(cpu_profiler_connection_);
495
12
  std::swap(cpu_profiler_connection_, connection);
496
12
}
497
498
inline profiler::V8CpuProfilerConnection*
499
6399
Environment::cpu_profiler_connection() {
500
6399
  return cpu_profiler_connection_.get();
501
}
502
503
12
inline void Environment::set_cpu_prof_interval(uint64_t interval) {
504
12
  cpu_prof_interval_ = interval;
505
12
}
506
507
12
inline uint64_t Environment::cpu_prof_interval() const {
508
12
  return cpu_prof_interval_;
509
}
510
511
12
inline void Environment::set_cpu_prof_name(const std::string& name) {
512
12
  cpu_prof_name_ = name;
513
12
}
514
515
12
inline const std::string& Environment::cpu_prof_name() const {
516
12
  return cpu_prof_name_;
517
}
518
519
12
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
520
12
  cpu_prof_dir_ = dir;
521
12
}
522
523
12
inline const std::string& Environment::cpu_prof_dir() const {
524
12
  return cpu_prof_dir_;
525
}
526
527
12
inline void Environment::set_heap_profiler_connection(
528
    std::unique_ptr<profiler::V8HeapProfilerConnection> connection) {
529
12
  CHECK_NULL(heap_profiler_connection_);
530
12
  std::swap(heap_profiler_connection_, connection);
531
12
}
532
533
inline profiler::V8HeapProfilerConnection*
534
6387
Environment::heap_profiler_connection() {
535
6387
  return heap_profiler_connection_.get();
536
}
537
538
12
inline void Environment::set_heap_prof_name(const std::string& name) {
539
12
  heap_prof_name_ = name;
540
12
}
541
542
12
inline const std::string& Environment::heap_prof_name() const {
543
12
  return heap_prof_name_;
544
}
545
546
12
inline void Environment::set_heap_prof_dir(const std::string& dir) {
547
12
  heap_prof_dir_ = dir;
548
12
}
549
550
12
inline const std::string& Environment::heap_prof_dir() const {
551
12
  return heap_prof_dir_;
552
}
553
554
12
inline void Environment::set_heap_prof_interval(uint64_t interval) {
555
12
  heap_prof_interval_ = interval;
556
12
}
557
558
12
inline uint64_t Environment::heap_prof_interval() const {
559
12
  return heap_prof_interval_;
560
}
561
562
#endif  // HAVE_INSPECTOR
563
564
inline
565
13043
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
566
13043
  return inspector_host_port_;
567
}
568
569
64800
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
570
64800
  return options_;
571
}
572
573
298
inline void IsolateData::set_options(
574
    std::shared_ptr<PerIsolateOptions> options) {
575
298
  options_ = std::move(options);
576
298
}
577
578
template <typename Fn>
579
118017
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
580
236034
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
581
118017
  native_immediates_.Push(std::move(callback));
582
583
118017
  if (flags & CallbackFlags::kRefed) {
584
93060
    if (immediate_info()->ref_count() == 0)
585
47834
      ToggleImmediateRef(true);
586
93060
    immediate_info()->ref_count_inc(1);
587
  }
588
118017
}
589
590
template <typename Fn>
591
2327
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
592
4654
  auto callback = native_immediates_threadsafe_.CreateCallback(
593
2327
      std::move(cb), flags);
594
  {
595
4654
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
596
2327
    native_immediates_threadsafe_.Push(std::move(callback));
597
2327
    if (task_queues_async_initialized_)
598
2318
      uv_async_send(&task_queues_async_);
599
  }
600
2327
}
601
602
template <typename Fn>
603
10820
void Environment::RequestInterrupt(Fn&& cb) {
604
21640
  auto callback = native_immediates_interrupts_.CreateCallback(
605
10820
      std::move(cb), CallbackFlags::kRefed);
606
  {
607
21640
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
608
10820
    native_immediates_interrupts_.Push(std::move(callback));
609
10820
    if (task_queues_async_initialized_)
610
4308
      uv_async_send(&task_queues_async_);
611
  }
612
10820
  RequestInterruptFromV8();
613
10820
}
614
615
3591807
inline bool Environment::can_call_into_js() const {
616

3591807
  return can_call_into_js_ && !is_stopping();
617
}
618
619
1682
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
620
1682
  can_call_into_js_ = can_call_into_js;
621
1682
}
622
623
1645097
inline bool Environment::has_run_bootstrapping_code() const {
624
1645097
  return principal_realm_->has_run_bootstrapping_code();
625
}
626
627
20
inline bool Environment::has_serialized_options() const {
628
20
  return has_serialized_options_;
629
}
630
631
7030
inline void Environment::set_has_serialized_options(bool value) {
632
7030
  has_serialized_options_ = value;
633
7030
}
634
635
9694
inline bool Environment::is_main_thread() const {
636
9694
  return worker_context() == nullptr;
637
}
638
639
1165
inline bool Environment::no_native_addons() const {
640
2326
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
641
2326
          !options_->allow_native_addons;
642
}
643
644
6354
inline bool Environment::should_not_register_esm_loader() const {
645
6354
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
646
}
647
648
16613
inline bool Environment::owns_process_state() const {
649
16613
  return flags_ & EnvironmentFlags::kOwnsProcessState;
650
}
651
652
6397
inline bool Environment::owns_inspector() const {
653
6397
  return flags_ & EnvironmentFlags::kOwnsInspector;
654
}
655
656
17295
inline bool Environment::should_create_inspector() const {
657
34590
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0 &&
658

17295
         !options_->test_runner && !options_->watch_mode;
659
}
660
661
137457
inline bool Environment::tracks_unmanaged_fds() const {
662
137457
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
4051
inline bool Environment::hide_console_windows() const {
666
4051
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7326
inline bool Environment::no_global_search_paths() const {
670
14652
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14652
         !options_->global_search_paths;
672
}
673
674
1764
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1764
  return flags_ & EnvironmentFlags::kNoBrowserGlobals;
680
#endif
681
}
682
683
5
bool Environment::filehandle_close_warning() const {
684
5
  return emit_filehandle_warning_;
685
}
686
687
4
void Environment::set_filehandle_close_warning(bool on) {
688
4
  emit_filehandle_warning_ = on;
689
4
}
690
691
6372
void Environment::set_source_maps_enabled(bool on) {
692
6372
  source_maps_enabled_ = on;
693
6372
}
694
695
3007
bool Environment::source_maps_enabled() const {
696
3007
  return source_maps_enabled_;
697
}
698
699
7962
inline uint64_t Environment::thread_id() const {
700
7962
  return thread_id_;
701
}
702
703
16779
inline worker::Worker* Environment::worker_context() const {
704
16779
  return isolate_data()->worker_context();
705
}
706
707
969
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
969
  sub_worker_contexts_.insert(context);
709
969
}
710
711
997
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
997
  sub_worker_contexts_.erase(context);
713
997
}
714
715
template <typename Fn>
716
29
inline void Environment::ForEachWorker(Fn&& iterator) {
717
31
  for (worker::Worker* w : sub_worker_contexts_) iterator(w);
718
29
}
719
720
5246193
inline bool Environment::is_stopping() const {
721
5246193
  return is_stopping_.load();
722
}
723
724
6150
inline void Environment::set_stopping(bool value) {
725
6150
  is_stopping_.store(value);
726
6150
}
727
728
14
inline std::list<node_module>* Environment::extra_linked_bindings() {
729
14
  return &extra_linked_bindings_;
730
}
731
732
10
inline node_module* Environment::extra_linked_bindings_head() {
733
10
  return extra_linked_bindings_.size() > 0 ?
734
10
      &extra_linked_bindings_.front() : nullptr;
735
}
736
737
9
inline node_module* Environment::extra_linked_bindings_tail() {
738
9
  return extra_linked_bindings_.size() > 0 ?
739
9
      &extra_linked_bindings_.back() : nullptr;
740
}
741
742
19
inline const Mutex& Environment::extra_linked_bindings_mutex() const {
743
19
  return extra_linked_bindings_mutex_;
744
}
745
746
43292
inline performance::PerformanceState* Environment::performance_state() {
747
43292
  return performance_state_.get();
748
}
749
750
9212431
inline IsolateData* Environment::isolate_data() const {
751
9212431
  return isolate_data_;
752
}
753
754
template <typename T>
755
5429
inline void Environment::ForEachRealm(T&& iterator) const {
756
  // TODO(legendecas): iterate over more realms bound to the environment.
757
5429
  iterator(principal_realm());
758
5429
}
759
760
8
inline void Environment::ThrowError(const char* errmsg) {
761
8
  ThrowError(v8::Exception::Error, errmsg);
762
8
}
763
764
inline void Environment::ThrowTypeError(const char* errmsg) {
765
  ThrowError(v8::Exception::TypeError, errmsg);
766
}
767
768
inline void Environment::ThrowRangeError(const char* errmsg) {
769
  ThrowError(v8::Exception::RangeError, errmsg);
770
}
771
772
8
inline void Environment::ThrowError(
773
    v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
774
    const char* errmsg) {
775
16
  v8::HandleScope handle_scope(isolate());
776
8
  isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
777
8
}
778
779
11
inline void Environment::ThrowErrnoException(int errorno,
780
                                             const char* syscall,
781
                                             const char* message,
782
                                             const char* path) {
783
  isolate()->ThrowException(
784
11
      ErrnoException(isolate(), errorno, syscall, message, path));
785
11
}
786
787
15
inline void Environment::ThrowUVException(int errorno,
788
                                          const char* syscall,
789
                                          const char* message,
790
                                          const char* path,
791
                                          const char* dest) {
792
  isolate()->ThrowException(
793
15
      UVException(isolate(), errorno, syscall, message, path, dest));
794
15
}
795
796
58117
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
797
58117
  cleanup_queue_.Add(fn, arg);
798
58117
}
799
800
39271
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
801
39271
  cleanup_queue_.Remove(fn, arg);
802
39271
}
803
804
19
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
805
19
  CHECK(!main_utf16_);
806
19
  main_utf16_ = std::move(str);
807
19
}
808
809
733
void Environment::set_process_exit_handler(
810
    std::function<void(Environment*, ExitCode)>&& handler) {
811
733
  process_exit_handler_ = std::move(handler);
812
733
}
813
814
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
815
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
816
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
817
#define V(TypeName, PropertyName)                                             \
818
  inline                                                                      \
819
  v8::Local<TypeName> IsolateData::PropertyName() const {                     \
820
    return PropertyName ## _ .Get(isolate_);                                  \
821
  }
822
49813
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
823
1185228
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
824
8446989
  PER_ISOLATE_STRING_PROPERTIES(VS)
825
#undef V
826
#undef VS
827
#undef VY
828
#undef VP
829
830
#define V(PropertyName, TypeName)                                              \
831
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
832
    return PropertyName##_.Get(isolate_);                                      \
833
  }                                                                            \
834
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
835
    PropertyName##_.Set(isolate_, value);                                      \
836
  }
837
1251154
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
838
#undef V
839
840
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
841
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
842
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
843
#define V(TypeName, PropertyName)                                             \
844
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
845
    return isolate_data()->PropertyName();                                    \
846
  }
847
49813
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
848
1064564
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
849
12219130
  PER_ISOLATE_STRING_PROPERTIES(VS)
850
#undef V
851
#undef VS
852
#undef VY
853
#undef VP
854
855
#define V(PropertyName, TypeName)                                              \
856
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
857
    return isolate_data()->PropertyName();                                     \
858
  }                                                                            \
859
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
860
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
861
    isolate_data()->set_##PropertyName(value);                                 \
862
  }
863
795183
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
864
#undef V
865
866
#define V(PropertyName, TypeName)                                              \
867
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
868
    DCHECK_NOT_NULL(principal_realm_);                                         \
869
    return principal_realm_->PropertyName();                                   \
870
  }                                                                            \
871
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
872
    DCHECK_NOT_NULL(principal_realm_);                                         \
873
    principal_realm_->set_##PropertyName(value);                               \
874
  }
875
9578032
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
876
#undef V
877
878
9528587
v8::Local<v8::Context> Environment::context() const {
879
9528587
  return principal_realm()->context();
880
}
881
882
11008175
Realm* Environment::principal_realm() const {
883
11008175
  return principal_realm_.get();
884
}
885
886
1
inline void Environment::set_heap_snapshot_near_heap_limit(uint32_t limit) {
887
1
  heap_snapshot_near_heap_limit_ = limit;
888
1
}
889
890
inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
891
  return is_in_heapsnapshot_heap_limit_callback_;
892
}
893
894
2
inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
895
  DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
896
2
  heapsnapshot_near_heap_limit_callback_added_ = true;
897
2
  isolate_->AddNearHeapLimitCallback(Environment::NearHeapLimitCallback, this);
898
2
}
899
900
2
inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
901
    size_t heap_limit) {
902
  DCHECK(heapsnapshot_near_heap_limit_callback_added_);
903
2
  heapsnapshot_near_heap_limit_callback_added_ = false;
904
2
  isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback,
905
                                        heap_limit);
906
2
}
907
908
}  // namespace node
909
910
// These two files depend on each other. Including base_object-inl.h after this
911
// file is the easiest way to avoid issues with that circular dependency.
912
#include "base_object-inl.h"
913
914
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
915
916
#endif  // SRC_ENV_INL_H_