GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 446 448 99.6 %
Date: 2022-12-31 04:22:30 Branches: 55 68 80.9 %

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
116060
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
47
116060
    IsolateData* isolate_data)
48
116060
    : node_allocator_(isolate_data->node_allocator()) {
49
116060
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
50
116060
}
51
52
232120
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
53
116060
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
54
116060
}
55
56
30020
inline v8::Isolate* IsolateData::isolate() const {
57
30020
  return isolate_;
58
}
59
60
1328743
inline uv_loop_t* IsolateData::event_loop() const {
61
1328743
  return event_loop_;
62
}
63
64
123906
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
65
123906
  return node_allocator_;
66
}
67
68
89730
inline MultiIsolatePlatform* IsolateData::platform() const {
69
89730
  return platform_;
70
}
71
72
768
inline void IsolateData::set_worker_context(worker::Worker* context) {
73
768
  CHECK_NULL(worker_context_);  // Should be set only once.
74
768
  worker_context_ = context;
75
768
}
76
77
17220
inline worker::Worker* IsolateData::worker_context() const {
78
17220
  return worker_context_;
79
}
80
81
216512
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
82
216512
  return async_wrap_providers_[index].Get(isolate_);
83
}
84
85
2764902
inline AliasedUint32Array& AsyncHooks::fields() {
86
2764902
  return fields_;
87
}
88
89
1631595
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
90
1631595
  return async_id_fields_;
91
}
92
93
827
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
94
827
  return async_ids_stack_;
95
}
96
97
894100
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
98
894100
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
99
827
    js_execution_async_resources_.Reset(
100
1654
        env()->isolate(), v8::Array::New(env()->isolate()));
101
  }
102
894100
  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
216106
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
111
216106
  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
468297
inline Environment* AsyncHooks::env() {
119
468297
  return Environment::ForAsyncHooks(this);
120
}
121
122
468297
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
123
468297
  return ContainerOf(&Environment::async_hooks_, hooks);
124
}
125
126
849884
inline size_t Environment::async_callback_scope_depth() const {
127
849884
  return async_callback_scope_depth_;
128
}
129
130
864505
inline void Environment::PushAsyncCallbackScope() {
131
864505
  async_callback_scope_depth_++;
132
864505
}
133
134
863890
inline void Environment::PopAsyncCallbackScope() {
135
863890
  async_callback_scope_depth_--;
136
863890
}
137
138
827
inline AliasedUint32Array& ImmediateInfo::fields() {
139
827
  return fields_;
140
}
141
142
214716
inline uint32_t ImmediateInfo::count() const {
143
214716
  return fields_[kCount];
144
}
145
146
334998
inline uint32_t ImmediateInfo::ref_count() const {
147
334998
  return fields_[kRefCount];
148
}
149
150
57508
inline bool ImmediateInfo::has_outstanding() const {
151
57508
  return fields_[kHasOutstanding] == 1;
152
}
153
154
45677
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
155
45677
  fields_[kRefCount] += increment;
156
45677
}
157
158
232780
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
159
232780
  fields_[kRefCount] -= decrement;
160
232780
}
161
162
827
inline AliasedUint8Array& TickInfo::fields() {
163
827
  return fields_;
164
}
165
166
1527076
inline bool TickInfo::has_tick_scheduled() const {
167
1527076
  return fields_[kHasTickScheduled] == 1;
168
}
169
170
551022
inline bool TickInfo::has_rejection_to_warn() const {
171
551022
  return fields_[kHasRejectionToWarn] == 1;
172
}
173
174
82042
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
175
82042
  if (UNLIKELY(!isolate->InContext())) return nullptr;
176
159098
  v8::HandleScope handle_scope(isolate);
177
79549
  return GetCurrent(isolate->GetCurrentContext());
178
}
179
180
7908043
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
181
7908043
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
182
13
    return nullptr;
183
  }
184
  return static_cast<Environment*>(
185
7908030
      context->GetAlignedPointerFromEmbedderData(
186
7908030
          ContextEmbedderIndex::kEnvironment));
187
}
188
189
5889408
inline Environment* Environment::GetCurrent(
190
    const v8::FunctionCallbackInfo<v8::Value>& info) {
191
5889408
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
192
}
193
194
template <typename T>
195
2361528
inline Environment* Environment::GetCurrent(
196
    const v8::PropertyCallbackInfo<T>& info) {
197
2361528
  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
1048316
inline T* Environment::GetBindingData(
207
    const v8::FunctionCallbackInfo<v8::Value>& info) {
208
1048316
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
209
}
210
211
template <typename T>
212
1048318
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
213
  BindingDataStore* map = static_cast<BindingDataStore*>(
214
1048318
      context->GetAlignedPointerFromEmbedderData(
215
          ContextEmbedderIndex::kBindingListIndex));
216
  DCHECK_NOT_NULL(map);
217
1048318
  auto it = map->find(T::type_name);
218
1048318
  if (UNLIKELY(it == map->end())) return nullptr;
219
1048318
  T* result = static_cast<T*>(it->second.get());
220
  DCHECK_NOT_NULL(result);
221
  DCHECK_EQ(result->env(), GetCurrent(context));
222
1048318
  return result;
223
}
224
225
template <typename T>
226
21539
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
43078
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
232
  BindingDataStore* map = static_cast<BindingDataStore*>(
233
21539
      context->GetAlignedPointerFromEmbedderData(
234
          ContextEmbedderIndex::kBindingListIndex));
235
  DCHECK_NOT_NULL(map);
236
21539
  auto result = map->emplace(T::type_name, item);
237
21539
  CHECK(result.second);
238
  DCHECK_EQ(GetBindingData<T>(context), item.get());
239
21539
  return item.get();
240
}
241
242
24485616
inline v8::Isolate* Environment::isolate() const {
243
24485616
  return isolate_;
244
}
245
246
8475
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
247
8475
  return ContainerOf(&Environment::timer_handle_, handle);
248
}
249
250
34092
inline uv_timer_t* Environment::timer_handle() {
251
34092
  return &timer_handle_;
252
}
253
254
214716
inline Environment* Environment::from_immediate_check_handle(
255
    uv_check_t* handle) {
256
214716
  return ContainerOf(&Environment::immediate_check_handle_, handle);
257
}
258
259
25976
inline uv_check_t* Environment::immediate_check_handle() {
260
25976
  return &immediate_check_handle_;
261
}
262
263
259370
inline uv_idle_t* Environment::immediate_idle_handle() {
264
259370
  return &immediate_idle_handle_;
265
}
266
267
38964
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
268
                                               HandleCleanupCb cb,
269
                                               void* arg) {
270
38964
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
271
38964
}
272
273
template <typename T, typename OnCloseCallback>
274
40645
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
275
40645
  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
40645
  handle->data = new CloseData { this, callback, handle->data };
287
40853
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
288
81082
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
289
40541
    data->env->handle_cleanup_waiting_--;
290
40541
    handle->data = data->original_data;
291
40541
    data->callback(reinterpret_cast<T*>(handle));
292
  });
293
40645
}
294
295
95557
void Environment::IncreaseWaitingRequestCounter() {
296
95557
  request_waiting_++;
297
95557
}
298
299
95549
void Environment::DecreaseWaitingRequestCounter() {
300
95549
  request_waiting_--;
301
95549
  CHECK_GE(request_waiting_, 0);
302
95549
}
303
304
1322208
inline uv_loop_t* Environment::event_loop() const {
305
1322208
  return isolate_data()->event_loop();
306
}
307
308
#if HAVE_INSPECTOR
309
74690
inline bool Environment::is_in_inspector_console_call() const {
310
74690
  return is_in_inspector_console_call_;
311
}
312
313
149378
inline void Environment::set_is_in_inspector_console_call(bool value) {
314
149378
  is_in_inspector_console_call_ = value;
315
149378
}
316
#endif
317
318
6202454
inline AsyncHooks* Environment::async_hooks() {
319
6202454
  return &async_hooks_;
320
}
321
322
886506
inline ImmediateInfo* Environment::immediate_info() {
323
886506
  return &immediate_info_;
324
}
325
326
764402
inline TickInfo* Environment::tick_info() {
327
764402
  return &tick_info_;
328
}
329
330
104666
inline uint64_t Environment::timer_base() const {
331
104666
  return timer_base_;
332
}
333
334
1525154
inline std::shared_ptr<KVStore> Environment::env_vars() {
335
1525154
  return env_vars_;
336
}
337
338
7291
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
339
7291
  env_vars_ = env_vars;
340
7291
}
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
11666
inline void Environment::set_trace_sync_io(bool value) {
351
11666
  trace_sync_io_ = value;
352
11666
}
353
354
123
inline bool Environment::abort_on_uncaught_exception() const {
355
123
  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
12078
inline void Environment::set_exiting(bool value) {
367
12078
  exit_info_[kExiting] = value ? 1 : 0;
368
12078
}
369
370
16933
inline ExitCode Environment::exit_code(const ExitCode default_code) const {
371
16933
  return exit_info_[kHasExitCode] == 0
372
16933
             ? default_code
373
16933
             : static_cast<ExitCode>(exit_info_[kExitCode]);
374
}
375
376
827
inline AliasedInt32Array& Environment::exit_info() {
377
827
  return exit_info_;
378
}
379
380
769
inline void Environment::set_abort_on_uncaught_exception(bool value) {
381
769
  options_->abort_on_uncaught_exception = value;
382
769
}
383
384
857
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
385
857
  return should_abort_on_uncaught_toggle_;
386
}
387
388
279425
inline AliasedInt32Array& Environment::stream_base_state() {
389
279425
  return stream_base_state_;
390
}
391
392
53684
inline uint32_t Environment::get_next_module_id() {
393
53684
  return module_id_counter_++;
394
}
395
6155
inline uint32_t Environment::get_next_script_id() {
396
6155
  return script_id_counter_++;
397
}
398
36714
inline uint32_t Environment::get_next_function_id() {
399
36714
  return function_id_counter_++;
400
}
401
402
113130
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
403
113130
    Environment* env)
404
113130
    : env_(env) {
405
113130
  env_->PushShouldNotAbortOnUncaughtScope();
406
113130
}
407
408
226254
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
409
113127
  Close();
410
113127
}
411
412
113320
void ShouldNotAbortOnUncaughtScope::Close() {
413
113320
  if (env_ != nullptr) {
414
113127
    env_->PopShouldNotAbortOnUncaughtScope();
415
113127
    env_ = nullptr;
416
  }
417
113320
}
418
419
113130
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
420
113130
  should_not_abort_scope_counter_++;
421
113130
}
422
423
113127
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
424
113127
  should_not_abort_scope_counter_--;
425
113127
}
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
598774
inline std::vector<double>* Environment::destroy_async_id_list() {
432
598774
  return &destroy_async_id_list_;
433
}
434
435
216661
inline double Environment::new_async_id() {
436
216661
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
437
216661
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
438
}
439
440
248508
inline double Environment::execution_async_id() {
441
248508
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
442
}
443
444
65840
inline double Environment::trigger_async_id() {
445
65840
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
446
}
447
448
216658
inline double Environment::get_default_trigger_async_id() {
449
  double default_trigger_async_id =
450
216658
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
451
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
452
216658
  if (default_trigger_async_id < 0)
453
182514
    default_trigger_async_id = execution_async_id();
454
216658
  return default_trigger_async_id;
455
}
456
457
82832
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
458
82832
  return options_;
459
}
460
461
18375
inline const std::vector<std::string>& Environment::argv() {
462
18375
  return argv_;
463
}
464
465
7178
inline const std::vector<std::string>& Environment::exec_argv() {
466
7178
  return exec_argv_;
467
}
468
469
12990
inline const std::string& Environment::exec_path() const {
470
12990
  return exec_path_;
471
}
472
473
#if HAVE_INSPECTOR
474
6491
inline void Environment::set_coverage_directory(const char* dir) {
475
6491
  coverage_directory_ = std::string(dir);
476
6491
}
477
478
6519
inline void Environment::set_coverage_connection(
479
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
480
6519
  CHECK_NULL(coverage_connection_);
481
6519
  std::swap(coverage_connection_, connection);
482
6519
}
483
484
19549
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
485
19549
  return coverage_connection_.get();
486
}
487
488
6463
inline const std::string& Environment::coverage_directory() const {
489
6463
  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
6528
Environment::cpu_profiler_connection() {
500
6528
  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
6516
Environment::heap_profiler_connection() {
535
6516
  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
13327
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
566
13327
  return inspector_host_port_;
567
}
568
569
57096
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
570
57096
  return options_;
571
}
572
573
303
inline void IsolateData::set_options(
574
    std::shared_ptr<PerIsolateOptions> options) {
575
303
  options_ = std::move(options);
576
303
}
577
578
template <typename Fn>
579
116316
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
580
232632
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
581
116316
  native_immediates_.Push(std::move(callback));
582
583
116316
  if (flags & CallbackFlags::kRefed) {
584
91340
    if (immediate_info()->ref_count() == 0)
585
48062
      ToggleImmediateRef(true);
586
91340
    immediate_info()->ref_count_inc(1);
587
  }
588
116316
}
589
590
template <typename Fn>
591
2355
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
592
4710
  auto callback = native_immediates_threadsafe_.CreateCallback(
593
2355
      std::move(cb), flags);
594
  {
595
4710
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
596
2355
    native_immediates_threadsafe_.Push(std::move(callback));
597
2355
    if (task_queues_async_initialized_)
598
2346
      uv_async_send(&task_queues_async_);
599
  }
600
2355
}
601
602
template <typename Fn>
603
10753
void Environment::RequestInterrupt(Fn&& cb) {
604
21506
  auto callback = native_immediates_interrupts_.CreateCallback(
605
10753
      std::move(cb), CallbackFlags::kRefed);
606
  {
607
21506
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
608
10753
    native_immediates_interrupts_.Push(std::move(callback));
609
10753
    if (task_queues_async_initialized_)
610
4107
      uv_async_send(&task_queues_async_);
611
  }
612
10753
  RequestInterruptFromV8();
613
10753
}
614
615
3667552
inline bool Environment::can_call_into_js() const {
616

3667552
  return can_call_into_js_ && !is_stopping();
617
}
618
619
1728
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
620
1728
  can_call_into_js_ = can_call_into_js;
621
1728
}
622
623
1667696
inline bool Environment::has_run_bootstrapping_code() const {
624
1667696
  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
6593
inline void Environment::set_has_serialized_options(bool value) {
632
6593
  has_serialized_options_ = value;
633
6593
}
634
635
9977
inline bool Environment::is_main_thread() const {
636
9977
  return worker_context() == nullptr;
637
}
638
639
1179
inline bool Environment::no_native_addons() const {
640
2354
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
641
2354
          !options_->allow_native_addons;
642
}
643
644
6480
inline bool Environment::should_not_register_esm_loader() const {
645
6480
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
646
}
647
648
17033
inline bool Environment::owns_process_state() const {
649
17033
  return flags_ & EnvironmentFlags::kOwnsProcessState;
650
}
651
652
6531
inline bool Environment::owns_inspector() const {
653
6531
  return flags_ & EnvironmentFlags::kOwnsInspector;
654
}
655
656
17657
inline bool Environment::should_create_inspector() const {
657
35303
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0 &&
658

17657
         !options_->test_runner && !options_->watch_mode;
659
}
660
661
139058
inline bool Environment::tracks_unmanaged_fds() const {
662
139058
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
4131
inline bool Environment::hide_console_windows() const {
666
4131
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7463
inline bool Environment::no_global_search_paths() const {
670
14926
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14926
         !options_->global_search_paths;
672
}
673
674
1810
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1810
  return flags_ & EnvironmentFlags::kNoBrowserGlobals;
680
#endif
681
}
682
683
4
bool Environment::filehandle_close_warning() const {
684
4
  return emit_filehandle_warning_;
685
}
686
687
3
void Environment::set_filehandle_close_warning(bool on) {
688
3
  emit_filehandle_warning_ = on;
689
3
}
690
691
6499
void Environment::set_source_maps_enabled(bool on) {
692
6499
  source_maps_enabled_ = on;
693
6499
}
694
695
1717
bool Environment::source_maps_enabled() const {
696
1717
  return source_maps_enabled_;
697
}
698
699
8150
inline uint64_t Environment::thread_id() const {
700
8150
  return thread_id_;
701
}
702
703
17220
inline worker::Worker* Environment::worker_context() const {
704
17220
  return isolate_data()->worker_context();
705
}
706
707
980
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
980
  sub_worker_contexts_.insert(context);
709
980
}
710
711
1007
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
1007
  sub_worker_contexts_.erase(context);
713
1007
}
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
5331182
inline bool Environment::is_stopping() const {
721
5331182
  return is_stopping_.load();
722
}
723
724
6281
inline void Environment::set_stopping(bool value) {
725
6281
  is_stopping_.store(value);
726
6281
}
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
43706
inline performance::PerformanceState* Environment::performance_state() {
747
43706
  return performance_state_.get();
748
}
749
750
9110126
inline IsolateData* Environment::isolate_data() const {
751
9110126
  return isolate_data_;
752
}
753
754
template <typename T>
755
5543
inline void Environment::ForEachRealm(T&& iterator) const {
756
  // TODO(legendecas): iterate over more realms bound to the environment.
757
5543
  iterator(principal_realm());
758
5543
}
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
16
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
16
      UVException(isolate(), errorno, syscall, message, path, dest));
794
16
}
795
796
107515
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
797
107515
  cleanup_queue_.Add(fn, arg);
798
107515
}
799
800
67753
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
801
67753
  cleanup_queue_.Remove(fn, arg);
802
67753
}
803
804
20
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
805
20
  CHECK(!main_utf16_);
806
20
  main_utf16_ = std::move(str);
807
20
}
808
809
759
void Environment::set_process_exit_handler(
810
    std::function<void(Environment*, ExitCode)>&& handler) {
811
759
  process_exit_handler_ = std::move(handler);
812
759
}
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
50875
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
823
1220556
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
824
8074819
  PER_ISOLATE_STRING_PROPERTIES(VS)
825
#undef V
826
#undef VS
827
#undef VY
828
#undef VP
829
830
#define VM(PropertyName) V(PropertyName##_binding, v8::FunctionTemplate)
831
#define V(PropertyName, TypeName)                                              \
832
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
833
    return PropertyName##_.Get(isolate_);                                      \
834
  }                                                                            \
835
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
836
    PropertyName##_.Set(isolate_, value);                                      \
837
  }
838
1498562
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
839
14782
  NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM)
840
#undef V
841
#undef VM
842
843
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
844
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
845
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
846
#define V(TypeName, PropertyName)                                             \
847
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
848
    return isolate_data()->PropertyName();                                    \
849
  }
850
50875
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
851
1099078
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
852
11697964
  PER_ISOLATE_STRING_PROPERTIES(VS)
853
#undef V
854
#undef VS
855
#undef VY
856
#undef VP
857
858
#define V(PropertyName, TypeName)                                              \
859
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
860
    return isolate_data()->PropertyName();                                     \
861
  }                                                                            \
862
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
863
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
864
    isolate_data()->set_##PropertyName(value);                                 \
865
  }
866
555571
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
867
#undef V
868
869
#define V(PropertyName, TypeName)                                              \
870
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
871
    DCHECK_NOT_NULL(principal_realm_);                                         \
872
    return principal_realm_->PropertyName();                                   \
873
  }                                                                            \
874
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
875
    DCHECK_NOT_NULL(principal_realm_);                                         \
876
    principal_realm_->set_##PropertyName(value);                               \
877
  }
878
9676534
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
879
#undef V
880
881
8929490
v8::Local<v8::Context> Environment::context() const {
882
8929490
  return principal_realm()->context();
883
}
884
885
10352126
Realm* Environment::principal_realm() const {
886
10352126
  return principal_realm_.get();
887
}
888
889
1
inline void Environment::set_heap_snapshot_near_heap_limit(uint32_t limit) {
890
1
  heap_snapshot_near_heap_limit_ = limit;
891
1
}
892
893
inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
894
  return is_in_heapsnapshot_heap_limit_callback_;
895
}
896
897
2
inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
898
  DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
899
2
  heapsnapshot_near_heap_limit_callback_added_ = true;
900
2
  isolate_->AddNearHeapLimitCallback(Environment::NearHeapLimitCallback, this);
901
2
}
902
903
2
inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
904
    size_t heap_limit) {
905
  DCHECK(heapsnapshot_near_heap_limit_callback_added_);
906
2
  heapsnapshot_near_heap_limit_callback_added_ = false;
907
2
  isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback,
908
                                        heap_limit);
909
2
}
910
911
}  // namespace node
912
913
// These two files depend on each other. Including base_object-inl.h after this
914
// file is the easiest way to avoid issues with that circular dependency.
915
#include "base_object-inl.h"
916
917
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
918
919
#endif  // SRC_ENV_INL_H_