GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 451 453 99.6 %
Date: 2022-08-21 04:19:51 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
116481
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116481
    IsolateData* isolate_data)
47
116481
    : node_allocator_(isolate_data->node_allocator()) {
48
116481
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116481
}
50
51
232962
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116481
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116481
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1288325
inline uv_loop_t* IsolateData::event_loop() const {
60
1288325
  return event_loop_;
61
}
62
63
123154
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
123154
  return node_allocator_;
65
}
66
67
85223
inline MultiIsolatePlatform* IsolateData::platform() const {
68
85223
  return platform_;
69
}
70
71
726
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
726
  CHECK_NULL(worker_context_);  // Should be set only once.
73
726
  worker_context_ = context;
74
726
}
75
76
16185
inline worker::Worker* IsolateData::worker_context() const {
77
16185
  return worker_context_;
78
}
79
80
212746
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
212746
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2679747
inline AliasedUint32Array& AsyncHooks::fields() {
85
2679747
  return fields_;
86
}
87
88
1664557
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1664557
  return async_id_fields_;
90
}
91
92
775
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
775
  return async_ids_stack_;
94
}
95
96
846181
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
846181
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
775
    js_execution_async_resources_.Reset(
99
1550
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
846181
  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
212398
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
212398
  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
426432
inline Environment* AsyncHooks::env() {
118
426432
  return Environment::ForAsyncHooks(this);
119
}
120
121
426432
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
426432
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
816164
inline size_t Environment::async_callback_scope_depth() const {
126
816164
  return async_callback_scope_depth_;
127
}
128
129
830212
inline void Environment::PushAsyncCallbackScope() {
130
830212
  async_callback_scope_depth_++;
131
830212
}
132
133
829661
inline void Environment::PopAsyncCallbackScope() {
134
829661
  async_callback_scope_depth_--;
135
829661
}
136
137
775
inline AliasedUint32Array& ImmediateInfo::fields() {
138
775
  return fields_;
139
}
140
141
202105
inline uint32_t ImmediateInfo::count() const {
142
202105
  return fields_[kCount];
143
}
144
145
295203
inline uint32_t ImmediateInfo::ref_count() const {
146
295203
  return fields_[kRefCount];
147
}
148
149
42793
inline bool ImmediateInfo::has_outstanding() const {
150
42793
  return fields_[kHasOutstanding] == 1;
151
}
152
153
33800
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
33800
  fields_[kRefCount] += increment;
155
33800
}
156
157
219562
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
219562
  fields_[kRefCount] -= decrement;
159
219562
}
160
161
775
inline AliasedUint8Array& TickInfo::fields() {
162
775
  return fields_;
163
}
164
165
1486219
inline bool TickInfo::has_tick_scheduled() const {
166
1486219
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
538529
inline bool TickInfo::has_rejection_to_warn() const {
170
538529
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
78163
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
78163
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
152984
  v8::HandleScope handle_scope(isolate);
176
76492
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7740147
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7740147
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
181
12
    return nullptr;
182
  }
183
  return static_cast<Environment*>(
184
7740135
      context->GetAlignedPointerFromEmbedderData(
185
7740135
          ContextEmbedderIndex::kEnvironment));
186
}
187
188
5868445
inline Environment* Environment::GetCurrent(
189
    const v8::FunctionCallbackInfo<v8::Value>& info) {
190
5868445
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
191
}
192
193
template <typename T>
194
2169716
inline Environment* Environment::GetCurrent(
195
    const v8::PropertyCallbackInfo<T>& info) {
196
2169716
  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
1006275
inline T* Environment::GetBindingData(
206
    const v8::FunctionCallbackInfo<v8::Value>& info) {
207
1006275
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
208
}
209
210
template <typename T>
211
1006277
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
212
  BindingDataStore* map = static_cast<BindingDataStore*>(
213
1006277
      context->GetAlignedPointerFromEmbedderData(
214
          ContextEmbedderIndex::kBindingListIndex));
215
  DCHECK_NOT_NULL(map);
216
1006277
  auto it = map->find(T::type_name);
217
1006277
  if (UNLIKELY(it == map->end())) return nullptr;
218
1006277
  T* result = static_cast<T*>(it->second.get());
219
  DCHECK_NOT_NULL(result);
220
  DCHECK_EQ(result->env(), GetCurrent(context));
221
1006277
  return result;
222
}
223
224
template <typename T>
225
25692
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
51384
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
231
  BindingDataStore* map = static_cast<BindingDataStore*>(
232
25692
      context->GetAlignedPointerFromEmbedderData(
233
          ContextEmbedderIndex::kBindingListIndex));
234
  DCHECK_NOT_NULL(map);
235
25692
  auto result = map->emplace(T::type_name, item);
236
25692
  CHECK(result.second);
237
  DCHECK_EQ(GetBindingData<T>(context), item.get());
238
25692
  return item.get();
239
}
240
241
26202738
inline v8::Isolate* Environment::isolate() const {
242
26202738
  return isolate_;
243
}
244
245
9180
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
246
9180
  return ContainerOf(&Environment::timer_handle_, handle);
247
}
248
249
33176
inline uv_timer_t* Environment::timer_handle() {
250
33176
  return &timer_handle_;
251
}
252
253
202105
inline Environment* Environment::from_immediate_check_handle(
254
    uv_check_t* handle) {
255
202105
  return ContainerOf(&Environment::immediate_check_handle_, handle);
256
}
257
258
24356
inline uv_check_t* Environment::immediate_check_handle() {
259
24356
  return &immediate_check_handle_;
260
}
261
262
248233
inline uv_idle_t* Environment::immediate_idle_handle() {
263
248233
  return &immediate_idle_handle_;
264
}
265
266
36534
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
267
                                               HandleCleanupCb cb,
268
                                               void* arg) {
269
36534
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
270
36534
}
271
272
template <typename T, typename OnCloseCallback>
273
38258
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
274
38098
  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
38178
  handle->data = new CloseData { this, callback, handle->data };
286
38498
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
287
76196
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
288
38178
    data->env->handle_cleanup_waiting_--;
289
38178
    handle->data = data->original_data;
290
38178
    data->callback(reinterpret_cast<T*>(handle));
291
  });
292
38178
}
293
294
93571
void Environment::IncreaseWaitingRequestCounter() {
295
93571
  request_waiting_++;
296
93571
}
297
298
93562
void Environment::DecreaseWaitingRequestCounter() {
299
93562
  request_waiting_--;
300
93562
  CHECK_GE(request_waiting_, 0);
301
93562
}
302
303
1282200
inline uv_loop_t* Environment::event_loop() const {
304
1282200
  return isolate_data()->event_loop();
305
}
306
307
#if HAVE_INSPECTOR
308
70930
inline bool Environment::is_in_inspector_console_call() const {
309
70930
  return is_in_inspector_console_call_;
310
}
311
312
141858
inline void Environment::set_is_in_inspector_console_call(bool value) {
313
141858
  is_in_inspector_console_call_ = value;
314
141858
}
315
#endif
316
317
6026948
inline AsyncHooks* Environment::async_hooks() {
318
6026948
  return &async_hooks_;
319
}
320
321
794238
inline ImmediateInfo* Environment::immediate_info() {
322
794238
  return &immediate_info_;
323
}
324
325
743925
inline TickInfo* Environment::tick_info() {
326
743925
  return &tick_info_;
327
}
328
329
107704
inline uint64_t Environment::timer_base() const {
330
107704
  return timer_base_;
331
}
332
333
1407352
inline std::shared_ptr<KVStore> Environment::env_vars() {
334
1407352
  return env_vars_;
335
}
336
337
6839
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
338
6839
  env_vars_ = env_vars;
339
6839
}
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
10954
inline void Environment::set_trace_sync_io(bool value) {
350
10954
  trace_sync_io_ = value;
351
10954
}
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
41
inline bool Environment::force_context_aware() const {
362
41
  return options_->force_context_aware;
363
}
364
365
11313
inline void Environment::set_exiting(bool value) {
366
11313
  exiting_[0] = value ? 1 : 0;
367
11313
}
368
369
775
inline AliasedUint32Array& Environment::exiting() {
370
775
  return exiting_;
371
}
372
373
721
inline void Environment::set_abort_on_uncaught_exception(bool value) {
374
721
  options_->abort_on_uncaught_exception = value;
375
721
}
376
377
805
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
378
805
  return should_abort_on_uncaught_toggle_;
379
}
380
381
306523
inline AliasedInt32Array& Environment::stream_base_state() {
382
306523
  return stream_base_state_;
383
}
384
385
51084
inline uint32_t Environment::get_next_module_id() {
386
51084
  return module_id_counter_++;
387
}
388
7394
inline uint32_t Environment::get_next_script_id() {
389
7394
  return script_id_counter_++;
390
}
391
36273
inline uint32_t Environment::get_next_function_id() {
392
36273
  return function_id_counter_++;
393
}
394
395
109035
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
396
109035
    Environment* env)
397
109035
    : env_(env) {
398
109035
  env_->PushShouldNotAbortOnUncaughtScope();
399
109035
}
400
401
218064
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
402
109032
  Close();
403
109032
}
404
405
109224
void ShouldNotAbortOnUncaughtScope::Close() {
406
109224
  if (env_ != nullptr) {
407
109032
    env_->PopShouldNotAbortOnUncaughtScope();
408
109032
    env_ = nullptr;
409
  }
410
109224
}
411
412
109035
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
413
109035
  should_not_abort_scope_counter_++;
414
109035
}
415
416
109032
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
417
109032
  should_not_abort_scope_counter_--;
418
109032
}
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
423069
inline std::vector<double>* Environment::destroy_async_id_list() {
425
423069
  return &destroy_async_id_list_;
426
}
427
428
212953
inline double Environment::new_async_id() {
429
212953
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
430
212953
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
431
}
432
433
244425
inline double Environment::execution_async_id() {
434
244425
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
435
}
436
437
65263
inline double Environment::trigger_async_id() {
438
65263
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
439
}
440
441
212950
inline double Environment::get_default_trigger_async_id() {
442
  double default_trigger_async_id =
443
212950
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
444
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
445
212950
  if (default_trigger_async_id < 0)
446
179096
    default_trigger_async_id = execution_async_id();
447
212950
  return default_trigger_async_id;
448
}
449
450
72856
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
451
72856
  return options_;
452
}
453
454
17270
inline const std::vector<std::string>& Environment::argv() {
455
17270
  return argv_;
456
}
457
458
6762
inline const std::vector<std::string>& Environment::exec_argv() {
459
6762
  return exec_argv_;
460
}
461
462
12178
inline const std::string& Environment::exec_path() const {
463
12178
  return exec_path_;
464
}
465
466
#if HAVE_INSPECTOR
467
6084
inline void Environment::set_coverage_directory(const char* dir) {
468
6084
  coverage_directory_ = std::string(dir);
469
6084
}
470
471
6110
inline void Environment::set_coverage_connection(
472
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
473
6110
  CHECK_NULL(coverage_connection_);
474
6110
  std::swap(coverage_connection_, connection);
475
6110
}
476
477
18329
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
478
18329
  return coverage_connection_.get();
479
}
480
481
6068
inline const std::string& Environment::coverage_directory() const {
482
6068
  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
6126
Environment::cpu_profiler_connection() {
493
6126
  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
6114
Environment::heap_profiler_connection() {
528
6114
  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
12462
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
559
12462
  return inspector_host_port_;
560
}
561
562
65676
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
563
65676
  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
92601
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
573
185202
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
574
92601
  native_immediates_.Push(std::move(callback));
575
576
92601
  if (flags & CallbackFlags::kRefed) {
577
67586
    if (immediate_info()->ref_count() == 0)
578
50370
      ToggleImmediateRef(true);
579
67586
    immediate_info()->ref_count_inc(1);
580
  }
581
92601
}
582
583
template <typename Fn>
584
2321
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
585
4642
  auto callback = native_immediates_threadsafe_.CreateCallback(
586
2321
      std::move(cb), flags);
587
  {
588
4642
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
589
2321
    native_immediates_threadsafe_.Push(std::move(callback));
590
2321
    if (task_queues_async_initialized_)
591
2312
      uv_async_send(&task_queues_async_);
592
  }
593
2321
}
594
595
template <typename Fn>
596
10519
void Environment::RequestInterrupt(Fn&& cb) {
597
21038
  auto callback = native_immediates_interrupts_.CreateCallback(
598
10519
      std::move(cb), CallbackFlags::kRefed);
599
  {
600
21038
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
601
10519
    native_immediates_interrupts_.Push(std::move(callback));
602
10519
    if (task_queues_async_initialized_)
603
4289
      uv_async_send(&task_queues_async_);
604
  }
605
10519
  RequestInterruptFromV8();
606
10519
}
607
608
3214993
inline bool Environment::can_call_into_js() const {
609

3214993
  return can_call_into_js_ && !is_stopping();
610
}
611
612
1638
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
613
1638
  can_call_into_js_ = can_call_into_js;
614
1638
}
615
616
1548122
inline bool Environment::has_run_bootstrapping_code() const {
617
1548122
  return has_run_bootstrapping_code_;
618
}
619
620
6125
inline void Environment::DoneBootstrapping() {
621
6125
  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
6125
  base_object_created_by_bootstrap_ = base_object_count_;
626
6125
}
627
628
19
inline bool Environment::has_serialized_options() const {
629
19
  return has_serialized_options_;
630
}
631
632
6693
inline void Environment::set_has_serialized_options(bool value) {
633
6693
  has_serialized_options_ = value;
634
6693
}
635
636
9390
inline bool Environment::is_main_thread() const {
637
9390
  return worker_context() == nullptr;
638
}
639
640
1136
inline bool Environment::no_native_addons() const {
641
2268
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
642
2268
          !options_->allow_native_addons;
643
}
644
645
6086
inline bool Environment::should_not_register_esm_loader() const {
646
6086
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
647
}
648
649
15908
inline bool Environment::owns_process_state() const {
650
15908
  return flags_ & EnvironmentFlags::kOwnsProcessState;
651
}
652
653
6119
inline bool Environment::owns_inspector() const {
654
6119
  return flags_ & EnvironmentFlags::kOwnsInspector;
655
}
656
657
16654
inline bool Environment::should_create_inspector() const {
658
16654
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
659
}
660
661
134766
inline bool Environment::tracks_unmanaged_fds() const {
662
134766
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
3832
inline bool Environment::hide_console_windows() const {
666
3832
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7045
inline bool Environment::no_global_search_paths() const {
670
14090
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14090
         !options_->global_search_paths;
672
}
673
674
1734
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1734
  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
5923
void Environment::set_source_maps_enabled(bool on) {
692
5923
  source_maps_enabled_ = on;
693
5923
}
694
695
3004
bool Environment::source_maps_enabled() const {
696
3004
  return source_maps_enabled_;
697
}
698
699
7647
inline uint64_t Environment::thread_id() const {
700
7647
  return thread_id_;
701
}
702
703
16185
inline worker::Worker* Environment::worker_context() const {
704
16185
  return isolate_data()->worker_context();
705
}
706
707
956
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
956
  sub_worker_contexts_.insert(context);
709
956
}
710
711
984
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
984
  sub_worker_contexts_.erase(context);
713
984
}
714
715
template <typename Fn>
716
25
inline void Environment::ForEachWorker(Fn&& iterator) {
717
27
  for (worker::Worker* w : sub_worker_contexts_) iterator(w);
718
25
}
719
720
4812098
inline bool Environment::is_stopping() const {
721
4812098
  return is_stopping_.load();
722
}
723
724
5954
inline void Environment::set_stopping(bool value) {
725
5954
  is_stopping_.store(value);
726
5954
}
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
42538
inline performance::PerformanceState* Environment::performance_state() {
747
42538
  return performance_state_.get();
748
}
749
750
8925453
inline IsolateData* Environment::isolate_data() const {
751
8925453
  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
1499101
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
791
2998202
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
792
1499101
    fn, arg, cleanup_hook_counter_++
793
1499101
  });
794
  // Make sure there was no existing element with these values.
795
1499101
  CHECK_EQ(insertion_info.second, true);
796
1499101
}
797
798
1469824
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
799
1469824
  CleanupHookCallback search { fn, arg, 0 };
800
1469824
  cleanup_hooks_.erase(search);
801
1469824
}
802
803
3330041
size_t CleanupHookCallback::Hash::operator()(
804
    const CleanupHookCallback& cb) const {
805
3330041
  return std::hash<void*>()(cb.arg_);
806
}
807
808
1665668
bool CleanupHookCallback::Equal::operator()(
809
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
810

1665668
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
811
}
812
813
639
BaseObject* CleanupHookCallback::GetBaseObject() const {
814
639
  if (fn_ == BaseObject::DeleteMe)
815
611
    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
1308
  for (const auto& hook : cleanup_hooks_) {
823
1254
    BaseObject* obj = hook.GetBaseObject();
824
1254
    if (obj != nullptr)
825
1198
      iterator(obj);
826
  }
827
54
}
828
829
2882527
void Environment::modify_base_object_count(int64_t delta) {
830
2882527
  base_object_count_ += delta;
831
2882527
}
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
17
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
842
17
  CHECK(!main_utf16_);
843
17
  main_utf16_ = std::move(str);
844
17
}
845
846
717
void Environment::set_process_exit_handler(
847
    std::function<void(Environment*, int)>&& handler) {
848
717
  process_exit_handler_ = std::move(handler);
849
717
}
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
60824
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
860
1178096
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
861
8110928
  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
1184296
  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
60824
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
885
1059386
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
886
11721970
  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
889250
  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
10720201
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
911
#undef V
912
913
9155015
v8::Local<v8::Context> Environment::context() const {
914
9155015
  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_