GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 445 447 99.6 %
Date: 2022-12-07 04:23:16 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
116036
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
47
116036
    IsolateData* isolate_data)
48
116036
    : node_allocator_(isolate_data->node_allocator()) {
49
116036
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
50
116036
}
51
52
232072
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
53
116036
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
54
116036
}
55
56
26270
inline v8::Isolate* IsolateData::isolate() const {
57
26270
  return isolate_;
58
}
59
60
1317473
inline uv_loop_t* IsolateData::event_loop() const {
61
1317473
  return event_loop_;
62
}
63
64
123756
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
65
123756
  return node_allocator_;
66
}
67
68
89078
inline MultiIsolatePlatform* IsolateData::platform() const {
69
89078
  return platform_;
70
}
71
72
748
inline void IsolateData::set_worker_context(worker::Worker* context) {
73
748
  CHECK_NULL(worker_context_);  // Should be set only once.
74
748
  worker_context_ = context;
75
748
}
76
77
16788
inline worker::Worker* IsolateData::worker_context() const {
78
16788
  return worker_context_;
79
}
80
81
215482
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
82
215482
  return async_wrap_providers_[index].Get(isolate_);
83
}
84
85
2715122
inline AliasedUint32Array& AsyncHooks::fields() {
86
2715122
  return fields_;
87
}
88
89
1624597
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
90
1624597
  return async_id_fields_;
91
}
92
93
800
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
94
800
  return async_ids_stack_;
95
}
96
97
902275
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
98
902275
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
99
800
    js_execution_async_resources_.Reset(
100
1600
        env()->isolate(), v8::Array::New(env()->isolate()));
101
  }
102
902275
  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
215076
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
111
215076
  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
479001
inline Environment* AsyncHooks::env() {
119
479001
  return Environment::ForAsyncHooks(this);
120
}
121
122
479001
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
123
479001
  return ContainerOf(&Environment::async_hooks_, hooks);
124
}
125
126
854980
inline size_t Environment::async_callback_scope_depth() const {
127
854980
  return async_callback_scope_depth_;
128
}
129
130
869238
inline void Environment::PushAsyncCallbackScope() {
131
869238
  async_callback_scope_depth_++;
132
869238
}
133
134
868540
inline void Environment::PopAsyncCallbackScope() {
135
868540
  async_callback_scope_depth_--;
136
868540
}
137
138
800
inline AliasedUint32Array& ImmediateInfo::fields() {
139
800
  return fields_;
140
}
141
142
216169
inline uint32_t ImmediateInfo::count() const {
143
216169
  return fields_[kCount];
144
}
145
146
339563
inline uint32_t ImmediateInfo::ref_count() const {
147
339563
  return fields_[kRefCount];
148
}
149
150
60253
inline bool ImmediateInfo::has_outstanding() const {
151
60253
  return fields_[kHasOutstanding] == 1;
152
}
153
154
46385
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
155
46385
  fields_[kRefCount] += increment;
156
46385
}
157
158
233876
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
159
233876
  fields_[kRefCount] -= decrement;
160
233876
}
161
162
800
inline AliasedUint8Array& TickInfo::fields() {
163
800
  return fields_;
164
}
165
166
1535535
inline bool TickInfo::has_tick_scheduled() const {
167
1535535
  return fields_[kHasTickScheduled] == 1;
168
}
169
170
555916
inline bool TickInfo::has_rejection_to_warn() const {
171
555916
  return fields_[kHasRejectionToWarn] == 1;
172
}
173
174
87602
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
175
87602
  if (UNLIKELY(!isolate->InContext())) return nullptr;
176
164664
  v8::HandleScope handle_scope(isolate);
177
82332
  return GetCurrent(isolate->GetCurrentContext());
178
}
179
180
8446267
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
181
8446267
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
182
13
    return nullptr;
183
  }
184
  return static_cast<Environment*>(
185
8446254
      context->GetAlignedPointerFromEmbedderData(
186
8446254
          ContextEmbedderIndex::kEnvironment));
187
}
188
189
6447852
inline Environment* Environment::GetCurrent(
190
    const v8::FunctionCallbackInfo<v8::Value>& info) {
191
6447852
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
192
}
193
194
template <typename T>
195
2334178
inline Environment* Environment::GetCurrent(
196
    const v8::PropertyCallbackInfo<T>& info) {
197
2334178
  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
1037689
inline T* Environment::GetBindingData(
207
    const v8::FunctionCallbackInfo<v8::Value>& info) {
208
1037689
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
209
}
210
211
template <typename T>
212
1037691
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
213
  BindingDataStore* map = static_cast<BindingDataStore*>(
214
1037691
      context->GetAlignedPointerFromEmbedderData(
215
          ContextEmbedderIndex::kBindingListIndex));
216
  DCHECK_NOT_NULL(map);
217
1037691
  auto it = map->find(T::type_name);
218
1037691
  if (UNLIKELY(it == map->end())) return nullptr;
219
1037691
  T* result = static_cast<T*>(it->second.get());
220
  DCHECK_NOT_NULL(result);
221
  DCHECK_EQ(result->env(), GetCurrent(context));
222
1037691
  return result;
223
}
224
225
template <typename T>
226
26924
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
53848
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
232
  BindingDataStore* map = static_cast<BindingDataStore*>(
233
26924
      context->GetAlignedPointerFromEmbedderData(
234
          ContextEmbedderIndex::kBindingListIndex));
235
  DCHECK_NOT_NULL(map);
236
26924
  auto result = map->emplace(T::type_name, item);
237
26924
  CHECK(result.second);
238
  DCHECK_EQ(GetBindingData<T>(context), item.get());
239
26924
  return item.get();
240
}
241
242
24993228
inline v8::Isolate* Environment::isolate() const {
243
24993228
  return isolate_;
244
}
245
246
8893
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
247
8893
  return ContainerOf(&Environment::timer_handle_, handle);
248
}
249
250
34123
inline uv_timer_t* Environment::timer_handle() {
251
34123
  return &timer_handle_;
252
}
253
254
216169
inline Environment* Environment::from_immediate_check_handle(
255
    uv_check_t* handle) {
256
216169
  return ContainerOf(&Environment::immediate_check_handle_, handle);
257
}
258
259
25580
inline uv_check_t* Environment::immediate_check_handle() {
260
25580
  return &immediate_check_handle_;
261
}
262
263
260501
inline uv_idle_t* Environment::immediate_idle_handle() {
264
260501
  return &immediate_idle_handle_;
265
}
266
267
38370
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
268
                                               HandleCleanupCb cb,
269
                                               void* arg) {
270
38370
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
271
38370
}
272
273
template <typename T, typename OnCloseCallback>
274
39379
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
275
39379
  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
39379
  handle->data = new CloseData { this, callback, handle->data };
287
39575
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
288
78562
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
289
39281
    data->env->handle_cleanup_waiting_--;
290
39281
    handle->data = data->original_data;
291
39281
    data->callback(reinterpret_cast<T*>(handle));
292
  });
293
39379
}
294
295
95145
void Environment::IncreaseWaitingRequestCounter() {
296
95145
  request_waiting_++;
297
95145
}
298
299
95137
void Environment::DecreaseWaitingRequestCounter() {
300
95137
  request_waiting_--;
301
95137
  CHECK_GE(request_waiting_, 0);
302
95137
}
303
304
1311042
inline uv_loop_t* Environment::event_loop() const {
305
1311042
  return isolate_data()->event_loop();
306
}
307
308
#if HAVE_INSPECTOR
309
74331
inline bool Environment::is_in_inspector_console_call() const {
310
74331
  return is_in_inspector_console_call_;
311
}
312
313
148660
inline void Environment::set_is_in_inspector_console_call(bool value) {
314
148660
  is_in_inspector_console_call_ = value;
315
148660
}
316
#endif
317
318
6159319
inline AsyncHooks* Environment::async_hooks() {
319
6159319
  return &async_hooks_;
320
}
321
322
897046
inline ImmediateInfo* Environment::immediate_info() {
323
897046
  return &immediate_info_;
324
}
325
326
768606
inline TickInfo* Environment::tick_info() {
327
768606
  return &tick_info_;
328
}
329
330
107114
inline uint64_t Environment::timer_base() const {
331
107114
  return timer_base_;
332
}
333
334
1505539
inline std::shared_ptr<KVStore> Environment::env_vars() {
335
1505539
  return env_vars_;
336
}
337
338
7167
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
339
7167
  env_vars_ = env_vars;
340
7167
}
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
11401
inline void Environment::set_trace_sync_io(bool value) {
351
11401
  trace_sync_io_ = value;
352
11401
}
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
11804
inline void Environment::set_exiting(bool value) {
367
11804
  exit_info_[kExiting] = value ? 1 : 0;
368
11804
}
369
370
16416
inline ExitCode Environment::exit_code(const ExitCode default_code) const {
371
16416
  return exit_info_[kHasExitCode] == 0
372
16416
             ? default_code
373
16416
             : static_cast<ExitCode>(exit_info_[kExitCode]);
374
}
375
376
800
inline AliasedInt32Array& Environment::exit_info() {
377
800
  return exit_info_;
378
}
379
380
742
inline void Environment::set_abort_on_uncaught_exception(bool value) {
381
742
  options_->abort_on_uncaught_exception = value;
382
742
}
383
384
830
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
385
830
  return should_abort_on_uncaught_toggle_;
386
}
387
388
273654
inline AliasedInt32Array& Environment::stream_base_state() {
389
273654
  return stream_base_state_;
390
}
391
392
52983
inline uint32_t Environment::get_next_module_id() {
393
52983
  return module_id_counter_++;
394
}
395
6444
inline uint32_t Environment::get_next_script_id() {
396
6444
  return script_id_counter_++;
397
}
398
36348
inline uint32_t Environment::get_next_function_id() {
399
36348
  return function_id_counter_++;
400
}
401
402
111898
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
403
111898
    Environment* env)
404
111898
    : env_(env) {
405
111898
  env_->PushShouldNotAbortOnUncaughtScope();
406
111898
}
407
408
223790
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
409
111895
  Close();
410
111895
}
411
412
112088
void ShouldNotAbortOnUncaughtScope::Close() {
413
112088
  if (env_ != nullptr) {
414
111895
    env_->PopShouldNotAbortOnUncaughtScope();
415
111895
    env_ = nullptr;
416
  }
417
112088
}
418
419
111898
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
420
111898
  should_not_abort_scope_counter_++;
421
111898
}
422
423
111895
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
424
111895
  should_not_abort_scope_counter_--;
425
111895
}
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
442659
inline std::vector<double>* Environment::destroy_async_id_list() {
432
442659
  return &destroy_async_id_list_;
433
}
434
435
215631
inline double Environment::new_async_id() {
436
215631
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
437
215631
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
438
}
439
440
247629
inline double Environment::execution_async_id() {
441
247629
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
442
}
443
444
65781
inline double Environment::trigger_async_id() {
445
65781
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
446
}
447
448
215628
inline double Environment::get_default_trigger_async_id() {
449
  double default_trigger_async_id =
450
215628
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
451
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
452
215628
  if (default_trigger_async_id < 0)
453
181750
    default_trigger_async_id = execution_async_id();
454
215628
  return default_trigger_async_id;
455
}
456
457
82219
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
458
82219
  return options_;
459
}
460
461
18112
inline const std::vector<std::string>& Environment::argv() {
462
18112
  return argv_;
463
}
464
465
7072
inline const std::vector<std::string>& Environment::exec_argv() {
466
7072
  return exec_argv_;
467
}
468
469
12790
inline const std::string& Environment::exec_path() const {
470
12790
  return exec_path_;
471
}
472
473
#if HAVE_INSPECTOR
474
6390
inline void Environment::set_coverage_directory(const char* dir) {
475
6390
  coverage_directory_ = std::string(dir);
476
6390
}
477
478
6413
inline void Environment::set_coverage_connection(
479
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
480
6413
  CHECK_NULL(coverage_connection_);
481
6413
  std::swap(coverage_connection_, connection);
482
6413
}
483
484
19236
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
485
19236
  return coverage_connection_.get();
486
}
487
488
6370
inline const std::string& Environment::coverage_directory() const {
489
6370
  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
6427
Environment::cpu_profiler_connection() {
500
6427
  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
6415
Environment::heap_profiler_connection() {
535
6415
  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
13117
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
566
13117
  return inspector_host_port_;
567
}
568
569
64965
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
570
64965
  return options_;
571
}
572
573
302
inline void IsolateData::set_options(
574
    std::shared_ptr<PerIsolateOptions> options) {
575
302
  options_ = std::move(options);
576
302
}
577
578
template <typename Fn>
579
117745
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
580
235490
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
581
117745
  native_immediates_.Push(std::move(callback));
582
583
117745
  if (flags & CallbackFlags::kRefed) {
584
92756
    if (immediate_info()->ref_count() == 0)
585
47780
      ToggleImmediateRef(true);
586
92756
    immediate_info()->ref_count_inc(1);
587
  }
588
117745
}
589
590
template <typename Fn>
591
2332
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
592
4664
  auto callback = native_immediates_threadsafe_.CreateCallback(
593
2332
      std::move(cb), flags);
594
  {
595
4664
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
596
2332
    native_immediates_threadsafe_.Push(std::move(callback));
597
2332
    if (task_queues_async_initialized_)
598
2323
      uv_async_send(&task_queues_async_);
599
  }
600
2332
}
601
602
template <typename Fn>
603
10670
void Environment::RequestInterrupt(Fn&& cb) {
604
21340
  auto callback = native_immediates_interrupts_.CreateCallback(
605
10670
      std::move(cb), CallbackFlags::kRefed);
606
  {
607
21340
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
608
10670
    native_immediates_interrupts_.Push(std::move(callback));
609
10670
    if (task_queues_async_initialized_)
610
4129
      uv_async_send(&task_queues_async_);
611
  }
612
10670
  RequestInterruptFromV8();
613
10670
}
614
615
3634001
inline bool Environment::can_call_into_js() const {
616

3634001
  return can_call_into_js_ && !is_stopping();
617
}
618
619
1778
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
620
1778
  can_call_into_js_ = can_call_into_js;
621
1778
}
622
623
1647978
inline bool Environment::has_run_bootstrapping_code() const {
624
1647978
  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
7035
inline void Environment::set_has_serialized_options(bool value) {
632
7035
  has_serialized_options_ = value;
633
7035
}
634
635
9668
inline bool Environment::is_main_thread() const {
636
9668
  return worker_context() == nullptr;
637
}
638
639
1169
inline bool Environment::no_native_addons() const {
640
2334
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
641
2334
          !options_->allow_native_addons;
642
}
643
644
6381
inline bool Environment::should_not_register_esm_loader() const {
645
6381
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
646
}
647
648
16699
inline bool Environment::owns_process_state() const {
649
16699
  return flags_ & EnvironmentFlags::kOwnsProcessState;
650
}
651
652
6425
inline bool Environment::owns_inspector() const {
653
6425
  return flags_ & EnvironmentFlags::kOwnsInspector;
654
}
655
656
17377
inline bool Environment::should_create_inspector() const {
657
34754
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0 &&
658

17377
         !options_->test_runner && !options_->watch_mode;
659
}
660
661
137936
inline bool Environment::tracks_unmanaged_fds() const {
662
137936
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
4065
inline bool Environment::hide_console_windows() const {
666
4065
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7357
inline bool Environment::no_global_search_paths() const {
670
14714
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14714
         !options_->global_search_paths;
672
}
673
674
1776
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1776
  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
6399
void Environment::set_source_maps_enabled(bool on) {
692
6399
  source_maps_enabled_ = on;
693
6399
}
694
695
2058
bool Environment::source_maps_enabled() const {
696
2058
  return source_maps_enabled_;
697
}
698
699
8006
inline uint64_t Environment::thread_id() const {
700
8006
  return thread_id_;
701
}
702
703
16788
inline worker::Worker* Environment::worker_context() const {
704
16788
  return isolate_data()->worker_context();
705
}
706
707
973
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
973
  sub_worker_contexts_.insert(context);
709
973
}
710
711
1000
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
1000
  sub_worker_contexts_.erase(context);
713
1000
}
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
5306043
inline bool Environment::is_stopping() const {
721
5306043
  return is_stopping_.load();
722
}
723
724
6085
inline void Environment::set_stopping(bool value) {
725
6085
  is_stopping_.store(value);
726
6085
}
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
43285
inline performance::PerformanceState* Environment::performance_state() {
747
43285
  return performance_state_.get();
748
}
749
750
9257087
inline IsolateData* Environment::isolate_data() const {
751
9257087
  return isolate_data_;
752
}
753
754
template <typename T>
755
5373
inline void Environment::ForEachRealm(T&& iterator) const {
756
  // TODO(legendecas): iterate over more realms bound to the environment.
757
5373
  iterator(principal_realm());
758
5373
}
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
58521
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
797
58521
  cleanup_queue_.Add(fn, arg);
798
58521
}
799
800
37248
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
801
37248
  cleanup_queue_.Remove(fn, arg);
802
37248
}
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
739
void Environment::set_process_exit_handler(
810
    std::function<void(Environment*, ExitCode)>&& handler) {
811
739
  process_exit_handler_ = std::move(handler);
812
739
}
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
50019
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
823
1235480
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
824
8457583
  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
1255284
  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
50019
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
848
1114641
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
849
12240205
  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
797127
  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
9647235
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
876
#undef V
877
878
9593194
v8::Local<v8::Context> Environment::context() const {
879
9593194
  return principal_realm()->context();
880
}
881
882
11072158
Realm* Environment::principal_realm() const {
883
11072158
  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_