GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 451 453 99.6 %
Date: 2022-09-22 04:22:24 Branches: 52 66 78.8 %

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
116565
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
47
116565
    IsolateData* isolate_data)
48
116565
    : node_allocator_(isolate_data->node_allocator()) {
49
116565
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
50
116565
}
51
52
233130
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
53
116565
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
54
116565
}
55
56
2382
inline v8::Isolate* IsolateData::isolate() const {
57
2382
  return isolate_;
58
}
59
60
1293786
inline uv_loop_t* IsolateData::event_loop() const {
61
1293786
  return event_loop_;
62
}
63
64
123422
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
65
123422
  return node_allocator_;
66
}
67
68
85411
inline MultiIsolatePlatform* IsolateData::platform() const {
69
85411
  return platform_;
70
}
71
72
734
inline void IsolateData::set_worker_context(worker::Worker* context) {
73
734
  CHECK_NULL(worker_context_);  // Should be set only once.
74
734
  worker_context_ = context;
75
734
}
76
77
16587
inline worker::Worker* IsolateData::worker_context() const {
78
16587
  return worker_context_;
79
}
80
81
214287
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
82
214287
  return async_wrap_providers_[index].Get(isolate_);
83
}
84
85
2717937
inline AliasedUint32Array& AsyncHooks::fields() {
86
2717937
  return fields_;
87
}
88
89
1676160
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
90
1676160
  return async_id_fields_;
91
}
92
93
789
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
94
789
  return async_ids_stack_;
95
}
96
97
888258
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
98
888258
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
99
789
    js_execution_async_resources_.Reset(
100
1578
        env()->isolate(), v8::Array::New(env()->isolate()));
101
  }
102
888258
  return PersistentToLocal::Strong(js_execution_async_resources_);
103
}
104
105
2406
v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
106
2406
  if (i >= native_execution_async_resources_.size()) return {};
107
2406
  return native_execution_async_resources_[i];
108
}
109
110
213939
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
111
213939
  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
484809
inline Environment* AsyncHooks::env() {
119
484809
  return Environment::ForAsyncHooks(this);
120
}
121
122
484809
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
123
484809
  return ContainerOf(&Environment::async_hooks_, hooks);
124
}
125
126
844508
inline size_t Environment::async_callback_scope_depth() const {
127
844508
  return async_callback_scope_depth_;
128
}
129
130
858714
inline void Environment::PushAsyncCallbackScope() {
131
858714
  async_callback_scope_depth_++;
132
858714
}
133
134
858138
inline void Environment::PopAsyncCallbackScope() {
135
858138
  async_callback_scope_depth_--;
136
858138
}
137
138
789
inline AliasedUint32Array& ImmediateInfo::fields() {
139
789
  return fields_;
140
}
141
142
215514
inline uint32_t ImmediateInfo::count() const {
143
215514
  return fields_[kCount];
144
}
145
146
324247
inline uint32_t ImmediateInfo::ref_count() const {
147
324247
  return fields_[kRefCount];
148
}
149
150
57933
inline bool ImmediateInfo::has_outstanding() const {
151
57933
  return fields_[kHasOutstanding] == 1;
152
}
153
154
33836
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
155
33836
  fields_[kRefCount] += increment;
156
33836
}
157
158
233433
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
159
233433
  fields_[kRefCount] -= decrement;
160
233433
}
161
162
789
inline AliasedUint8Array& TickInfo::fields() {
163
789
  return fields_;
164
}
165
166
1542981
inline bool TickInfo::has_tick_scheduled() const {
167
1542981
  return fields_[kHasTickScheduled] == 1;
168
}
169
170
565558
inline bool TickInfo::has_rejection_to_warn() const {
171
565558
  return fields_[kHasRejectionToWarn] == 1;
172
}
173
174
78527
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
175
78527
  if (UNLIKELY(!isolate->InContext())) return nullptr;
176
154458
  v8::HandleScope handle_scope(isolate);
177
77229
  return GetCurrent(isolate->GetCurrentContext());
178
}
179
180
7867401
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
181
7867401
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
182
13
    return nullptr;
183
  }
184
  return static_cast<Environment*>(
185
7867388
      context->GetAlignedPointerFromEmbedderData(
186
7867388
          ContextEmbedderIndex::kEnvironment));
187
}
188
189
5948764
inline Environment* Environment::GetCurrent(
190
    const v8::FunctionCallbackInfo<v8::Value>& info) {
191
5948764
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
192
}
193
194
template <typename T>
195
2207262
inline Environment* Environment::GetCurrent(
196
    const v8::PropertyCallbackInfo<T>& info) {
197
2207262
  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
1011818
inline T* Environment::GetBindingData(
207
    const v8::FunctionCallbackInfo<v8::Value>& info) {
208
1011818
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
209
}
210
211
template <typename T>
212
1011820
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
213
  BindingDataStore* map = static_cast<BindingDataStore*>(
214
1011820
      context->GetAlignedPointerFromEmbedderData(
215
          ContextEmbedderIndex::kBindingListIndex));
216
  DCHECK_NOT_NULL(map);
217
1011820
  auto it = map->find(T::type_name);
218
1011820
  if (UNLIKELY(it == map->end())) return nullptr;
219
1011820
  T* result = static_cast<T*>(it->second.get());
220
  DCHECK_NOT_NULL(result);
221
  DCHECK_EQ(result->env(), GetCurrent(context));
222
1011820
  return result;
223
}
224
225
template <typename T>
226
26430
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
52860
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
232
  BindingDataStore* map = static_cast<BindingDataStore*>(
233
26430
      context->GetAlignedPointerFromEmbedderData(
234
          ContextEmbedderIndex::kBindingListIndex));
235
  DCHECK_NOT_NULL(map);
236
26430
  auto result = map->emplace(T::type_name, item);
237
26430
  CHECK(result.second);
238
  DCHECK_EQ(GetBindingData<T>(context), item.get());
239
26430
  return item.get();
240
}
241
242
26313123
inline v8::Isolate* Environment::isolate() const {
243
26313123
  return isolate_;
244
}
245
246
7713
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
247
7713
  return ContainerOf(&Environment::timer_handle_, handle);
248
}
249
250
32484
inline uv_timer_t* Environment::timer_handle() {
251
32484
  return &timer_handle_;
252
}
253
254
215514
inline Environment* Environment::from_immediate_check_handle(
255
    uv_check_t* handle) {
256
215514
  return ContainerOf(&Environment::immediate_check_handle_, handle);
257
}
258
259
25072
inline uv_check_t* Environment::immediate_check_handle() {
260
25072
  return &immediate_check_handle_;
261
}
262
263
262157
inline uv_idle_t* Environment::immediate_idle_handle() {
264
262157
  return &immediate_idle_handle_;
265
}
266
267
37608
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
268
                                               HandleCleanupCb cb,
269
                                               void* arg) {
270
37608
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
271
37608
}
272
273
template <typename T, typename OnCloseCallback>
274
39336
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
275
39176
  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
39256
  handle->data = new CloseData { this, callback, handle->data };
287
39576
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
288
78352
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
289
39256
    data->env->handle_cleanup_waiting_--;
290
39256
    handle->data = data->original_data;
291
39256
    data->callback(reinterpret_cast<T*>(handle));
292
  });
293
39256
}
294
295
94246
void Environment::IncreaseWaitingRequestCounter() {
296
94246
  request_waiting_++;
297
94246
}
298
299
94237
void Environment::DecreaseWaitingRequestCounter() {
300
94237
  request_waiting_--;
301
94237
  CHECK_GE(request_waiting_, 0);
302
94237
}
303
304
1287479
inline uv_loop_t* Environment::event_loop() const {
305
1287479
  return isolate_data()->event_loop();
306
}
307
308
#if HAVE_INSPECTOR
309
70798
inline bool Environment::is_in_inspector_console_call() const {
310
70798
  return is_in_inspector_console_call_;
311
}
312
313
141594
inline void Environment::set_is_in_inspector_console_call(bool value) {
314
141594
  is_in_inspector_console_call_ = value;
315
141594
}
316
#endif
317
318
6147102
inline AsyncHooks* Environment::async_hooks() {
319
6147102
  return &async_hooks_;
320
}
321
322
865747
inline ImmediateInfo* Environment::immediate_info() {
323
865747
  return &immediate_info_;
324
}
325
326
772323
inline TickInfo* Environment::tick_info() {
327
772323
  return &tick_info_;
328
}
329
330
101245
inline uint64_t Environment::timer_base() const {
331
101245
  return timer_base_;
332
}
333
334
1436748
inline std::shared_ptr<KVStore> Environment::env_vars() {
335
1436748
  return env_vars_;
336
}
337
338
7030
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
339
7030
  env_vars_ = env_vars;
340
7030
}
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
11260
inline void Environment::set_trace_sync_io(bool value) {
351
11260
  trace_sync_io_ = value;
352
11260
}
353
354
125
inline bool Environment::abort_on_uncaught_exception() const {
355
125
  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
11637
inline void Environment::set_exiting(bool value) {
367
11637
  exiting_[0] = value ? 1 : 0;
368
11637
}
369
370
789
inline AliasedUint32Array& Environment::exiting() {
371
789
  return exiting_;
372
}
373
374
732
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
732
  options_->abort_on_uncaught_exception = value;
376
732
}
377
378
819
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
819
  return should_abort_on_uncaught_toggle_;
380
}
381
382
308277
inline AliasedInt32Array& Environment::stream_base_state() {
383
308277
  return stream_base_state_;
384
}
385
386
52038
inline uint32_t Environment::get_next_module_id() {
387
52038
  return module_id_counter_++;
388
}
389
5822
inline uint32_t Environment::get_next_script_id() {
390
5822
  return script_id_counter_++;
391
}
392
37105
inline uint32_t Environment::get_next_function_id() {
393
37105
  return function_id_counter_++;
394
}
395
396
109358
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
109358
    Environment* env)
398
109358
    : env_(env) {
399
109358
  env_->PushShouldNotAbortOnUncaughtScope();
400
109358
}
401
402
218710
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
109355
  Close();
404
109355
}
405
406
109548
void ShouldNotAbortOnUncaughtScope::Close() {
407
109548
  if (env_ != nullptr) {
408
109355
    env_->PopShouldNotAbortOnUncaughtScope();
409
109355
    env_ = nullptr;
410
  }
411
109548
}
412
413
109358
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
109358
  should_not_abort_scope_counter_++;
415
109358
}
416
417
109355
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
109355
  should_not_abort_scope_counter_--;
419
109355
}
420
421
1
inline bool Environment::inside_should_not_abort_on_uncaught_scope() const {
422
1
  return should_not_abort_scope_counter_ > 0;
423
}
424
425
430834
inline std::vector<double>* Environment::destroy_async_id_list() {
426
430834
  return &destroy_async_id_list_;
427
}
428
429
214494
inline double Environment::new_async_id() {
430
214494
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
214494
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
246769
inline double Environment::execution_async_id() {
435
246769
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
66351
inline double Environment::trigger_async_id() {
439
66351
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
214491
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
214491
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
214491
  if (default_trigger_async_id < 0)
447
180339
    default_trigger_async_id = execution_async_id();
448
214491
  return default_trigger_async_id;
449
}
450
451
79884
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
79884
  return options_;
453
}
454
455
17750
inline const std::vector<std::string>& Environment::argv() {
456
17750
  return argv_;
457
}
458
459
6944
inline const std::vector<std::string>& Environment::exec_argv() {
460
6944
  return exec_argv_;
461
}
462
463
12538
inline const std::string& Environment::exec_path() const {
464
12538
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6264
inline void Environment::set_coverage_directory(const char* dir) {
469
6264
  coverage_directory_ = std::string(dir);
470
6264
}
471
472
6294
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6294
  CHECK_NULL(coverage_connection_);
475
6294
  std::swap(coverage_connection_, connection);
476
6294
}
477
478
18879
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
18879
  return coverage_connection_.get();
480
}
481
482
6247
inline const std::string& Environment::coverage_directory() const {
483
6247
  return coverage_directory_;
484
}
485
486
12
inline void Environment::set_cpu_profiler_connection(
487
    std::unique_ptr<profiler::V8CpuProfilerConnection> connection) {
488
12
  CHECK_NULL(cpu_profiler_connection_);
489
12
  std::swap(cpu_profiler_connection_, connection);
490
12
}
491
492
inline profiler::V8CpuProfilerConnection*
493
6308
Environment::cpu_profiler_connection() {
494
6308
  return cpu_profiler_connection_.get();
495
}
496
497
12
inline void Environment::set_cpu_prof_interval(uint64_t interval) {
498
12
  cpu_prof_interval_ = interval;
499
12
}
500
501
12
inline uint64_t Environment::cpu_prof_interval() const {
502
12
  return cpu_prof_interval_;
503
}
504
505
12
inline void Environment::set_cpu_prof_name(const std::string& name) {
506
12
  cpu_prof_name_ = name;
507
12
}
508
509
12
inline const std::string& Environment::cpu_prof_name() const {
510
12
  return cpu_prof_name_;
511
}
512
513
12
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
514
12
  cpu_prof_dir_ = dir;
515
12
}
516
517
12
inline const std::string& Environment::cpu_prof_dir() const {
518
12
  return cpu_prof_dir_;
519
}
520
521
12
inline void Environment::set_heap_profiler_connection(
522
    std::unique_ptr<profiler::V8HeapProfilerConnection> connection) {
523
12
  CHECK_NULL(heap_profiler_connection_);
524
12
  std::swap(heap_profiler_connection_, connection);
525
12
}
526
527
inline profiler::V8HeapProfilerConnection*
528
6296
Environment::heap_profiler_connection() {
529
6296
  return heap_profiler_connection_.get();
530
}
531
532
12
inline void Environment::set_heap_prof_name(const std::string& name) {
533
12
  heap_prof_name_ = name;
534
12
}
535
536
12
inline const std::string& Environment::heap_prof_name() const {
537
12
  return heap_prof_name_;
538
}
539
540
12
inline void Environment::set_heap_prof_dir(const std::string& dir) {
541
12
  heap_prof_dir_ = dir;
542
12
}
543
544
12
inline const std::string& Environment::heap_prof_dir() const {
545
12
  return heap_prof_dir_;
546
}
547
548
12
inline void Environment::set_heap_prof_interval(uint64_t interval) {
549
12
  heap_prof_interval_ = interval;
550
12
}
551
552
12
inline uint64_t Environment::heap_prof_interval() const {
553
12
  return heap_prof_interval_;
554
}
555
556
#endif  // HAVE_INSPECTOR
557
558
inline
559
12859
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
12859
  return inspector_host_port_;
561
}
562
563
66881
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
66881
  return options_;
565
}
566
567
289
inline void IsolateData::set_options(
568
    std::shared_ptr<PerIsolateOptions> options) {
569
289
  options_ = std::move(options);
570
289
}
571
572
template <typename Fn>
573
92871
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
185742
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
92871
  native_immediates_.Push(std::move(callback));
576
577
92871
  if (flags & CallbackFlags::kRefed) {
578
67658
    if (immediate_info()->ref_count() == 0)
579
50392
      ToggleImmediateRef(true);
580
67658
    immediate_info()->ref_count_inc(1);
581
  }
582
92871
}
583
584
template <typename Fn>
585
2331
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
4662
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
2331
      std::move(cb), flags);
588
  {
589
4662
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
2331
    native_immediates_threadsafe_.Push(std::move(callback));
591
2331
    if (task_queues_async_initialized_)
592
2322
      uv_async_send(&task_queues_async_);
593
  }
594
2331
}
595
596
template <typename Fn>
597
10735
void Environment::RequestInterrupt(Fn&& cb) {
598
21470
  auto callback = native_immediates_interrupts_.CreateCallback(
599
10735
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
21470
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
10735
    native_immediates_interrupts_.Push(std::move(callback));
603
10735
    if (task_queues_async_initialized_)
604
4313
      uv_async_send(&task_queues_async_);
605
  }
606
10735
  RequestInterruptFromV8();
607
10735
}
608
609
3580006
inline bool Environment::can_call_into_js() const {
610

3580006
  return can_call_into_js_ && !is_stopping();
611
}
612
613
1678
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
1678
  can_call_into_js_ = can_call_into_js;
615
1678
}
616
617
1577967
inline bool Environment::has_run_bootstrapping_code() const {
618
1577967
  return principal_realm_->has_run_bootstrapping_code();
619
}
620
621
20
inline bool Environment::has_serialized_options() const {
622
20
  return has_serialized_options_;
623
}
624
625
6929
inline void Environment::set_has_serialized_options(bool value) {
626
6929
  has_serialized_options_ = value;
627
6929
}
628
629
9605
inline bool Environment::is_main_thread() const {
630
9605
  return worker_context() == nullptr;
631
}
632
633
1153
inline bool Environment::no_native_addons() const {
634
2302
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
635
2302
          !options_->allow_native_addons;
636
}
637
638
6254
inline bool Environment::should_not_register_esm_loader() const {
639
6254
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
640
}
641
642
16379
inline bool Environment::owns_process_state() const {
643
16379
  return flags_ & EnvironmentFlags::kOwnsProcessState;
644
}
645
646
6303
inline bool Environment::owns_inspector() const {
647
6303
  return flags_ & EnvironmentFlags::kOwnsInspector;
648
}
649
650
17081
inline bool Environment::should_create_inspector() const {
651
34162
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0 &&
652

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