GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 451 453 99.6 %
Date: 2022-08-31 04:20:31 Branches: 57 72 79.2 %

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 "util-inl.h"
35
#include "uv.h"
36
#include "v8.h"
37
38
#include <cstddef>
39
#include <cstdint>
40
41
#include <utility>
42
43
namespace node {
44
45
115600
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
115600
    IsolateData* isolate_data)
47
115600
    : node_allocator_(isolate_data->node_allocator()) {
48
115600
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
115600
}
50
51
231200
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
115600
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
115600
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1265823
inline uv_loop_t* IsolateData::event_loop() const {
60
1265823
  return event_loop_;
61
}
62
63
122344
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
122344
  return node_allocator_;
65
}
66
67
85297
inline MultiIsolatePlatform* IsolateData::platform() const {
68
85297
  return platform_;
69
}
70
71
731
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
731
  CHECK_NULL(worker_context_);  // Should be set only once.
73
731
  worker_context_ = context;
74
731
}
75
76
16342
inline worker::Worker* IsolateData::worker_context() const {
77
16342
  return worker_context_;
78
}
79
80
213533
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
213533
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2679293
inline AliasedUint32Array& AsyncHooks::fields() {
85
2679293
  return fields_;
86
}
87
88
1676364
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1676364
  return async_id_fields_;
90
}
91
92
779
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
779
  return async_ids_stack_;
94
}
95
96
836861
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
836861
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
779
    js_execution_async_resources_.Reset(
99
1558
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
836861
  return PersistentToLocal::Strong(js_execution_async_resources_);
102
}
103
104
2406
v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
105
2406
  if (i >= native_execution_async_resources_.size()) return {};
106
2406
  return native_execution_async_resources_[i];
107
}
108
109
213185
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
213185
  return env()->isolate_data()->async_wrap_provider(idx);
111
}
112
113
1
inline void AsyncHooks::no_force_checks() {
114
1
  fields_[kCheck] -= 1;
115
1
}
116
117
422644
inline Environment* AsyncHooks::env() {
118
422644
  return Environment::ForAsyncHooks(this);
119
}
120
121
422644
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
422644
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
808742
inline size_t Environment::async_callback_scope_depth() const {
126
808742
  return async_callback_scope_depth_;
127
}
128
129
823005
inline void Environment::PushAsyncCallbackScope() {
130
823005
  async_callback_scope_depth_++;
131
823005
}
132
133
822448
inline void Environment::PopAsyncCallbackScope() {
134
822448
  async_callback_scope_depth_--;
135
822448
}
136
137
779
inline AliasedUint32Array& ImmediateInfo::fields() {
138
779
  return fields_;
139
}
140
141
196725
inline uint32_t ImmediateInfo::count() const {
142
196725
  return fields_[kCount];
143
}
144
145
289716
inline uint32_t ImmediateInfo::ref_count() const {
146
289716
  return fields_[kRefCount];
147
}
148
149
42644
inline bool ImmediateInfo::has_outstanding() const {
150
42644
  return fields_[kHasOutstanding] == 1;
151
}
152
153
33804
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
33804
  fields_[kRefCount] += increment;
155
33804
}
156
157
214220
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
214220
  fields_[kRefCount] -= decrement;
159
214220
}
160
161
779
inline AliasedUint8Array& TickInfo::fields() {
162
779
  return fields_;
163
}
164
165
1470741
inline bool TickInfo::has_tick_scheduled() const {
166
1470741
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
529837
inline bool TickInfo::has_rejection_to_warn() const {
170
529837
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
76305
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
76305
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
149210
  v8::HandleScope handle_scope(isolate);
176
74605
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7738921
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7738921
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
181
12
    return nullptr;
182
  }
183
  return static_cast<Environment*>(
184
7738909
      context->GetAlignedPointerFromEmbedderData(
185
7738909
          ContextEmbedderIndex::kEnvironment));
186
}
187
188
5873132
inline Environment* Environment::GetCurrent(
189
    const v8::FunctionCallbackInfo<v8::Value>& info) {
190
5873132
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
191
}
192
193
template <typename T>
194
2163713
inline Environment* Environment::GetCurrent(
195
    const v8::PropertyCallbackInfo<T>& info) {
196
2163713
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
197
}
198
199
template <typename T, typename U>
200
inline T* Environment::GetBindingData(const v8::PropertyCallbackInfo<U>& info) {
201
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
202
}
203
204
template <typename T>
205
985190
inline T* Environment::GetBindingData(
206
    const v8::FunctionCallbackInfo<v8::Value>& info) {
207
985190
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
208
}
209
210
template <typename T>
211
985192
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
212
  BindingDataStore* map = static_cast<BindingDataStore*>(
213
985192
      context->GetAlignedPointerFromEmbedderData(
214
          ContextEmbedderIndex::kBindingListIndex));
215
  DCHECK_NOT_NULL(map);
216
985192
  auto it = map->find(T::type_name);
217
985192
  if (UNLIKELY(it == map->end())) return nullptr;
218
985192
  T* result = static_cast<T*>(it->second.get());
219
  DCHECK_NOT_NULL(result);
220
  DCHECK_EQ(result->env(), GetCurrent(context));
221
985192
  return result;
222
}
223
224
template <typename T>
225
25969
inline T* Environment::AddBindingData(
226
    v8::Local<v8::Context> context,
227
    v8::Local<v8::Object> target) {
228
  DCHECK_EQ(GetCurrent(context), this);
229
  // This won't compile if T is not a BaseObject subclass.
230
51938
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
231
  BindingDataStore* map = static_cast<BindingDataStore*>(
232
25969
      context->GetAlignedPointerFromEmbedderData(
233
          ContextEmbedderIndex::kBindingListIndex));
234
  DCHECK_NOT_NULL(map);
235
25969
  auto result = map->emplace(T::type_name, item);
236
25969
  CHECK(result.second);
237
  DCHECK_EQ(GetBindingData<T>(context), item.get());
238
25969
  return item.get();
239
}
240
241
26170987
inline v8::Isolate* Environment::isolate() const {
242
26170987
  return isolate_;
243
}
244
245
7410
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
246
7410
  return ContainerOf(&Environment::timer_handle_, handle);
247
}
248
249
31646
inline uv_timer_t* Environment::timer_handle() {
250
31646
  return &timer_handle_;
251
}
252
253
196725
inline Environment* Environment::from_immediate_check_handle(
254
    uv_check_t* handle) {
255
196725
  return ContainerOf(&Environment::immediate_check_handle_, handle);
256
}
257
258
24644
inline uv_check_t* Environment::immediate_check_handle() {
259
24644
  return &immediate_check_handle_;
260
}
261
262
242901
inline uv_idle_t* Environment::immediate_idle_handle() {
263
242901
  return &immediate_idle_handle_;
264
}
265
266
36966
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
267
                                               HandleCleanupCb cb,
268
                                               void* arg) {
269
36966
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
270
36966
}
271
272
template <typename T, typename OnCloseCallback>
273
38715
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
274
38555
  handle_cleanup_waiting_++;
275
  static_assert(sizeof(T) >= sizeof(uv_handle_t), "T is a libuv handle");
276
  static_assert(offsetof(T, data) == offsetof(uv_handle_t, data),
277
                "T is a libuv handle");
278
  static_assert(offsetof(T, close_cb) == offsetof(uv_handle_t, close_cb),
279
                "T is a libuv handle");
280
  struct CloseData {
281
    Environment* env;
282
    OnCloseCallback callback;
283
    void* original_data;
284
  };
285
38635
  handle->data = new CloseData { this, callback, handle->data };
286
38955
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
287
77110
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
288
38635
    data->env->handle_cleanup_waiting_--;
289
38635
    handle->data = data->original_data;
290
38635
    data->callback(reinterpret_cast<T*>(handle));
291
  });
292
38635
}
293
294
93948
void Environment::IncreaseWaitingRequestCounter() {
295
93948
  request_waiting_++;
296
93948
}
297
298
93939
void Environment::DecreaseWaitingRequestCounter() {
299
93939
  request_waiting_--;
300
93939
  CHECK_GE(request_waiting_, 0);
301
93939
}
302
303
1259630
inline uv_loop_t* Environment::event_loop() const {
304
1259630
  return isolate_data()->event_loop();
305
}
306
307
#if HAVE_INSPECTOR
308
70874
inline bool Environment::is_in_inspector_console_call() const {
309
70874
  return is_in_inspector_console_call_;
310
}
311
312
141746
inline void Environment::set_is_in_inspector_console_call(bool value) {
313
141746
  is_in_inspector_console_call_ = value;
314
141746
}
315
#endif
316
317
6016788
inline AsyncHooks* Environment::async_hooks() {
318
6016788
  return &async_hooks_;
319
}
320
321
777888
inline ImmediateInfo* Environment::immediate_info() {
322
777888
  return &immediate_info_;
323
}
324
325
736189
inline TickInfo* Environment::tick_info() {
326
736189
  return &tick_info_;
327
}
328
329
100589
inline uint64_t Environment::timer_base() const {
330
100589
  return timer_base_;
331
}
332
333
1404310
inline std::shared_ptr<KVStore> Environment::env_vars() {
334
1404310
  return env_vars_;
335
}
336
337
6913
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
338
6913
  env_vars_ = env_vars;
339
6913
}
340
341
22
inline bool Environment::printed_error() const {
342
22
  return printed_error_;
343
}
344
345
22
inline void Environment::set_printed_error(bool value) {
346
22
  printed_error_ = value;
347
22
}
348
349
11088
inline void Environment::set_trace_sync_io(bool value) {
350
11088
  trace_sync_io_ = value;
351
11088
}
352
353
124
inline bool Environment::abort_on_uncaught_exception() const {
354
124
  return options_->abort_on_uncaught_exception;
355
}
356
357
inline void Environment::set_force_context_aware(bool value) {
358
  options_->force_context_aware = value;
359
}
360
361
42
inline bool Environment::force_context_aware() const {
362
42
  return options_->force_context_aware;
363
}
364
365
11452
inline void Environment::set_exiting(bool value) {
366
11452
  exiting_[0] = value ? 1 : 0;
367
11452
}
368
369
779
inline AliasedUint32Array& Environment::exiting() {
370
779
  return exiting_;
371
}
372
373
723
inline void Environment::set_abort_on_uncaught_exception(bool value) {
374
723
  options_->abort_on_uncaught_exception = value;
375
723
}
376
377
809
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
378
809
  return should_abort_on_uncaught_toggle_;
379
}
380
381
308397
inline AliasedInt32Array& Environment::stream_base_state() {
382
308397
  return stream_base_state_;
383
}
384
385
51456
inline uint32_t Environment::get_next_module_id() {
386
51456
  return module_id_counter_++;
387
}
388
5627
inline uint32_t Environment::get_next_script_id() {
389
5627
  return script_id_counter_++;
390
}
391
36694
inline uint32_t Environment::get_next_function_id() {
392
36694
  return function_id_counter_++;
393
}
394
395
108009
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
396
108009
    Environment* env)
397
108009
    : env_(env) {
398
108009
  env_->PushShouldNotAbortOnUncaughtScope();
399
108009
}
400
401
216012
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
402
108006
  Close();
403
108006
}
404
405
108198
void ShouldNotAbortOnUncaughtScope::Close() {
406
108198
  if (env_ != nullptr) {
407
108006
    env_->PopShouldNotAbortOnUncaughtScope();
408
108006
    env_ = nullptr;
409
  }
410
108198
}
411
412
108009
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
413
108009
  should_not_abort_scope_counter_++;
414
108009
}
415
416
108006
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
417
108006
  should_not_abort_scope_counter_--;
418
108006
}
419
420
1
inline bool Environment::inside_should_not_abort_on_uncaught_scope() const {
421
1
  return should_not_abort_scope_counter_ > 0;
422
}
423
424
432611
inline std::vector<double>* Environment::destroy_async_id_list() {
425
432611
  return &destroy_async_id_list_;
426
}
427
428
213740
inline double Environment::new_async_id() {
429
213740
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
430
213740
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
431
}
432
433
245631
inline double Environment::execution_async_id() {
434
245631
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
435
}
436
437
66035
inline double Environment::trigger_async_id() {
438
66035
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
439
}
440
441
213737
inline double Environment::get_default_trigger_async_id() {
442
  double default_trigger_async_id =
443
213737
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
444
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
445
213737
  if (default_trigger_async_id < 0)
446
179531
    default_trigger_async_id = execution_async_id();
447
213737
  return default_trigger_async_id;
448
}
449
450
73834
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
451
73834
  return options_;
452
}
453
454
17442
inline const std::vector<std::string>& Environment::argv() {
455
17442
  return argv_;
456
}
457
458
6835
inline const std::vector<std::string>& Environment::exec_argv() {
459
6835
  return exec_argv_;
460
}
461
462
12322
inline const std::string& Environment::exec_path() const {
463
12322
  return exec_path_;
464
}
465
466
#if HAVE_INSPECTOR
467
6156
inline void Environment::set_coverage_directory(const char* dir) {
468
6156
  coverage_directory_ = std::string(dir);
469
6156
}
470
471
6179
inline void Environment::set_coverage_connection(
472
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
473
6179
  CHECK_NULL(coverage_connection_);
474
6179
  std::swap(coverage_connection_, connection);
475
6179
}
476
477
18536
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
478
18536
  return coverage_connection_.get();
479
}
480
481
6137
inline const std::string& Environment::coverage_directory() const {
482
6137
  return coverage_directory_;
483
}
484
485
12
inline void Environment::set_cpu_profiler_connection(
486
    std::unique_ptr<profiler::V8CpuProfilerConnection> connection) {
487
12
  CHECK_NULL(cpu_profiler_connection_);
488
12
  std::swap(cpu_profiler_connection_, connection);
489
12
}
490
491
inline profiler::V8CpuProfilerConnection*
492
6195
Environment::cpu_profiler_connection() {
493
6195
  return cpu_profiler_connection_.get();
494
}
495
496
12
inline void Environment::set_cpu_prof_interval(uint64_t interval) {
497
12
  cpu_prof_interval_ = interval;
498
12
}
499
500
12
inline uint64_t Environment::cpu_prof_interval() const {
501
12
  return cpu_prof_interval_;
502
}
503
504
12
inline void Environment::set_cpu_prof_name(const std::string& name) {
505
12
  cpu_prof_name_ = name;
506
12
}
507
508
12
inline const std::string& Environment::cpu_prof_name() const {
509
12
  return cpu_prof_name_;
510
}
511
512
12
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
513
12
  cpu_prof_dir_ = dir;
514
12
}
515
516
12
inline const std::string& Environment::cpu_prof_dir() const {
517
12
  return cpu_prof_dir_;
518
}
519
520
12
inline void Environment::set_heap_profiler_connection(
521
    std::unique_ptr<profiler::V8HeapProfilerConnection> connection) {
522
12
  CHECK_NULL(heap_profiler_connection_);
523
12
  std::swap(heap_profiler_connection_, connection);
524
12
}
525
526
inline profiler::V8HeapProfilerConnection*
527
6183
Environment::heap_profiler_connection() {
528
6183
  return heap_profiler_connection_.get();
529
}
530
531
12
inline void Environment::set_heap_prof_name(const std::string& name) {
532
12
  heap_prof_name_ = name;
533
12
}
534
535
12
inline const std::string& Environment::heap_prof_name() const {
536
12
  return heap_prof_name_;
537
}
538
539
12
inline void Environment::set_heap_prof_dir(const std::string& dir) {
540
12
  heap_prof_dir_ = dir;
541
12
}
542
543
12
inline const std::string& Environment::heap_prof_dir() const {
544
12
  return heap_prof_dir_;
545
}
546
547
12
inline void Environment::set_heap_prof_interval(uint64_t interval) {
548
12
  heap_prof_interval_ = interval;
549
12
}
550
551
12
inline uint64_t Environment::heap_prof_interval() const {
552
12
  return heap_prof_interval_;
553
}
554
555
#endif  // HAVE_INSPECTOR
556
557
inline
558
12600
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
559
12600
  return inspector_host_port_;
560
}
561
562
65887
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
563
65887
  return options_;
564
}
565
566
288
inline void IsolateData::set_options(
567
    std::shared_ptr<PerIsolateOptions> options) {
568
288
  options_ = std::move(options);
569
288
}
570
571
template <typename Fn>
572
92606
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
573
185212
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
574
92606
  native_immediates_.Push(std::move(callback));
575
576
92606
  if (flags & CallbackFlags::kRefed) {
577
67594
    if (immediate_info()->ref_count() == 0)
578
50370
      ToggleImmediateRef(true);
579
67594
    immediate_info()->ref_count_inc(1);
580
  }
581
92606
}
582
583
template <typename Fn>
584
2320
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
585
4640
  auto callback = native_immediates_threadsafe_.CreateCallback(
586
2320
      std::move(cb), flags);
587
  {
588
4640
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
589
2320
    native_immediates_threadsafe_.Push(std::move(callback));
590
2320
    if (task_queues_async_initialized_)
591
2311
      uv_async_send(&task_queues_async_);
592
  }
593
2320
}
594
595
template <typename Fn>
596
10494
void Environment::RequestInterrupt(Fn&& cb) {
597
20988
  auto callback = native_immediates_interrupts_.CreateCallback(
598
10494
      std::move(cb), CallbackFlags::kRefed);
599
  {
600
20988
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
601
10494
    native_immediates_interrupts_.Push(std::move(callback));
602
10494
    if (task_queues_async_initialized_)
603
4196
      uv_async_send(&task_queues_async_);
604
  }
605
10494
  RequestInterruptFromV8();
606
10494
}
607
608
3191668
inline bool Environment::can_call_into_js() const {
609

3191668
  return can_call_into_js_ && !is_stopping();
610
}
611
612
1643
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
613
1643
  can_call_into_js_ = can_call_into_js;
614
1643
}
615
616
1545669
inline bool Environment::has_run_bootstrapping_code() const {
617
1545669
  return has_run_bootstrapping_code_;
618
}
619
620
6194
inline void Environment::DoneBootstrapping() {
621
6194
  has_run_bootstrapping_code_ = true;
622
  // This adjusts the return value of base_object_created_after_bootstrap() so
623
  // that tests that check the count do not have to account for internally
624
  // created BaseObjects.
625
6194
  base_object_created_by_bootstrap_ = base_object_count_;
626
6194
}
627
628
19
inline bool Environment::has_serialized_options() const {
629
19
  return has_serialized_options_;
630
}
631
632
6796
inline void Environment::set_has_serialized_options(bool value) {
633
6796
  has_serialized_options_ = value;
634
6796
}
635
636
9475
inline bool Environment::is_main_thread() const {
637
9475
  return worker_context() == nullptr;
638
}
639
640
1150
inline bool Environment::no_native_addons() const {
641
2296
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
642
2296
          !options_->allow_native_addons;
643
}
644
645
6158
inline bool Environment::should_not_register_esm_loader() const {
646
6158
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
647
}
648
649
16108
inline bool Environment::owns_process_state() const {
650
16108
  return flags_ & EnvironmentFlags::kOwnsProcessState;
651
}
652
653
6188
inline bool Environment::owns_inspector() const {
654
6188
  return flags_ & EnvironmentFlags::kOwnsInspector;
655
}
656
657
16801
inline bool Environment::should_create_inspector() const {
658
16801
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
659
}
660
661
128152
inline bool Environment::tracks_unmanaged_fds() const {
662
128152
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
3890
inline bool Environment::hide_console_windows() const {
666
3890
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7118
inline bool Environment::no_global_search_paths() const {
670
14236
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14236
         !options_->global_search_paths;
672
}
673
674
1739
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1739
  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
5992
void Environment::set_source_maps_enabled(bool on) {
692
5992
  source_maps_enabled_ = on;
693
5992
}
694
695
1201
bool Environment::source_maps_enabled() const {
696
1201
  return source_maps_enabled_;
697
}
698
699
7730
inline uint64_t Environment::thread_id() const {
700
7730
  return thread_id_;
701
}
702
703
16342
inline worker::Worker* Environment::worker_context() const {
704
16342
  return isolate_data()->worker_context();
705
}
706
707
957
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
957
  sub_worker_contexts_.insert(context);
709
957
}
710
711
985
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
985
  sub_worker_contexts_.erase(context);
713
985
}
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
4773937
inline bool Environment::is_stopping() const {
721
4773937
  return is_stopping_.load();
722
}
723
724
6014
inline void Environment::set_stopping(bool value) {
725
6014
  is_stopping_.store(value);
726
6014
}
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
42747
inline performance::PerformanceState* Environment::performance_state() {
747
42747
  return performance_state_.get();
748
}
749
750
8977107
inline IsolateData* Environment::isolate_data() const {
751
8977107
  return isolate_data_;
752
}
753
754
8
inline void Environment::ThrowError(const char* errmsg) {
755
8
  ThrowError(v8::Exception::Error, errmsg);
756
8
}
757
758
inline void Environment::ThrowTypeError(const char* errmsg) {
759
  ThrowError(v8::Exception::TypeError, errmsg);
760
}
761
762
inline void Environment::ThrowRangeError(const char* errmsg) {
763
  ThrowError(v8::Exception::RangeError, errmsg);
764
}
765
766
8
inline void Environment::ThrowError(
767
    v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
768
    const char* errmsg) {
769
16
  v8::HandleScope handle_scope(isolate());
770
8
  isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
771
8
}
772
773
7
inline void Environment::ThrowErrnoException(int errorno,
774
                                             const char* syscall,
775
                                             const char* message,
776
                                             const char* path) {
777
  isolate()->ThrowException(
778
7
      ErrnoException(isolate(), errorno, syscall, message, path));
779
7
}
780
781
15
inline void Environment::ThrowUVException(int errorno,
782
                                          const char* syscall,
783
                                          const char* message,
784
                                          const char* path,
785
                                          const char* dest) {
786
  isolate()->ThrowException(
787
15
      UVException(isolate(), errorno, syscall, message, path, dest));
788
15
}
789
790
1507452
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
791
3014904
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
792
1507452
    fn, arg, cleanup_hook_counter_++
793
1507452
  });
794
  // Make sure there was no existing element with these values.
795
1507452
  CHECK_EQ(insertion_info.second, true);
796
1507452
}
797
798
1475789
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
799
1475789
  CleanupHookCallback search { fn, arg, 0 };
800
1475789
  cleanup_hooks_.erase(search);
801
1475789
}
802
803
3359000
size_t CleanupHookCallback::Hash::operator()(
804
    const CleanupHookCallback& cb) const {
805
3359000
  return std::hash<void*>()(cb.arg_);
806
}
807
808
1680540
bool CleanupHookCallback::Equal::operator()(
809
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
810

1680540
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
811
}
812
813
685
BaseObject* CleanupHookCallback::GetBaseObject() const {
814
685
  if (fn_ == BaseObject::DeleteMe)
815
657
    return static_cast<BaseObject*>(arg_);
816
  else
817
28
    return nullptr;
818
}
819
820
template <typename T>
821
54
void Environment::ForEachBaseObject(T&& iterator) {
822
1400
  for (const auto& hook : cleanup_hooks_) {
823
1346
    BaseObject* obj = hook.GetBaseObject();
824
1346
    if (obj != nullptr)
825
1290
      iterator(obj);
826
  }
827
54
}
828
829
2896144
void Environment::modify_base_object_count(int64_t delta) {
830
2896144
  base_object_count_ += delta;
831
2896144
}
832
833
14
int64_t Environment::base_object_created_after_bootstrap() const {
834
14
  return base_object_count_ - base_object_created_by_bootstrap_;
835
}
836
837
2
int64_t Environment::base_object_count() const {
838
2
  return base_object_count_;
839
}
840
841
19
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
842
19
  CHECK(!main_utf16_);
843
19
  main_utf16_ = std::move(str);
844
19
}
845
846
722
void Environment::set_process_exit_handler(
847
    std::function<void(Environment*, int)>&& handler) {
848
722
  process_exit_handler_ = std::move(handler);
849
722
}
850
851
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
852
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
853
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
854
#define V(TypeName, PropertyName)                                             \
855
  inline                                                                      \
856
  v8::Local<TypeName> IsolateData::PropertyName() const {                     \
857
    return PropertyName ## _ .Get(isolate_);                                  \
858
  }
859
60991
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
860
1164774
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
861
8220940
  PER_ISOLATE_STRING_PROPERTIES(VS)
862
#undef V
863
#undef VS
864
#undef VY
865
#undef VP
866
867
#define V(PropertyName, TypeName)                                              \
868
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
869
    return PropertyName##_.Get(isolate_);                                      \
870
  }                                                                            \
871
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
872
    PropertyName##_.Set(isolate_, value);                                      \
873
  }
874
1203826
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
875
#undef V
876
877
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
878
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
879
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
880
#define V(TypeName, PropertyName)                                             \
881
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
882
    return isolate_data()->PropertyName();                                    \
883
  }
884
60991
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
885
1045610
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
886
11879362
  PER_ISOLATE_STRING_PROPERTIES(VS)
887
#undef V
888
#undef VS
889
#undef VY
890
#undef VP
891
892
#define V(PropertyName, TypeName)                                              \
893
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
894
    return isolate_data()->PropertyName();                                     \
895
  }                                                                            \
896
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
897
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
898
    isolate_data()->set_##PropertyName(value);                                 \
899
  }
900
893072
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
901
#undef V
902
903
#define V(PropertyName, TypeName)                                              \
904
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
905
    return PersistentToLocal::Strong(PropertyName##_);                         \
906
  }                                                                            \
907
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
908
    PropertyName##_.Reset(isolate(), value);                                   \
909
  }
910
10666730
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
911
#undef V
912
913
9109615
v8::Local<v8::Context> Environment::context() const {
914
9109615
  return PersistentToLocal::Strong(context_);
915
}
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_