GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 454 457 99.3 %
Date: 2022-07-21 04:16:21 Branches: 60 76 78.9 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_ENV_INL_H_
23
#define SRC_ENV_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "aliased_buffer.h"
28
#include "callback_queue-inl.h"
29
#include "env.h"
30
#include "node.h"
31
#include "node_context_data.h"
32
#include "node_internals.h"
33
#include "node_perf_common.h"
34
#include "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
116438
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116438
    IsolateData* isolate_data)
47
116438
    : node_allocator_(isolate_data->node_allocator()) {
48
116438
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116438
}
50
51
232876
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116438
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116438
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1270085
inline uv_loop_t* IsolateData::event_loop() const {
60
1270085
  return event_loop_;
61
}
62
63
123546
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
123546
  return node_allocator_;
65
}
66
67
158993
inline MultiIsolatePlatform* IsolateData::platform() const {
68
158993
  return platform_;
69
}
70
71
1257
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
1257
  CHECK_NULL(worker_context_);  // Should be set only once.
73
1257
  worker_context_ = context;
74
1257
}
75
76
20258
inline worker::Worker* IsolateData::worker_context() const {
77
20258
  return worker_context_;
78
}
79
80
217418
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
217418
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2629882
inline AliasedUint32Array& AsyncHooks::fields() {
85
2629882
  return fields_;
86
}
87
88
1698883
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1698883
  return async_id_fields_;
90
}
91
92
1302
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
1302
  return async_ids_stack_;
94
}
95
96
813729
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
813729
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
1302
    js_execution_async_resources_.Reset(
99
2604
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
813729
  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
217070
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
217070
  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
421975
inline Environment* AsyncHooks::env() {
118
421975
  return Environment::ForAsyncHooks(this);
119
}
120
121
421975
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
421975
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
786201
inline size_t Environment::async_callback_scope_depth() const {
126
786201
  return async_callback_scope_depth_;
127
}
128
129
803657
inline void Environment::PushAsyncCallbackScope() {
130
803657
  async_callback_scope_depth_++;
131
803657
}
132
133
803133
inline void Environment::PopAsyncCallbackScope() {
134
803133
  async_callback_scope_depth_--;
135
803133
}
136
137
1302
inline AliasedUint32Array& ImmediateInfo::fields() {
138
1302
  return fields_;
139
}
140
141
185229
inline uint32_t ImmediateInfo::count() const {
142
185229
  return fields_[kCount];
143
}
144
145
280837
inline uint32_t ImmediateInfo::ref_count() const {
146
280837
  return fields_[kRefCount];
147
}
148
149
41852
inline bool ImmediateInfo::has_outstanding() const {
150
41852
  return fields_[kHasOutstanding] == 1;
151
}
152
153
34700
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
34700
  fields_[kRefCount] += increment;
155
34700
}
156
157
205237
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
205237
  fields_[kRefCount] -= decrement;
159
205237
}
160
161
1302
inline AliasedUint8Array& TickInfo::fields() {
162
1302
  return fields_;
163
}
164
165
1417004
inline bool TickInfo::has_tick_scheduled() const {
166
1417004
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
518401
inline bool TickInfo::has_rejection_to_warn() const {
170
518401
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
55294
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
55294
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
106712
  v8::HandleScope handle_scope(isolate);
176
53356
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
8084577
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
8084577
  if (UNLIKELY(context.IsEmpty())) {
181
1
    return nullptr;
182
  }
183
8084576
  if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184
8084576
               ContextEmbedderIndex::kContextTag)) {
185
11
    return nullptr;
186
  }
187
8084565
  if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188
                   ContextEmbedderIndex::kContextTag) !=
189
8084565
               Environment::kNodeContextTagPtr)) {
190
    return nullptr;
191
  }
192
  return static_cast<Environment*>(
193
8084565
      context->GetAlignedPointerFromEmbedderData(
194
8084565
          ContextEmbedderIndex::kEnvironment));
195
}
196
197
6198275
inline Environment* Environment::GetCurrent(
198
    const v8::FunctionCallbackInfo<v8::Value>& info) {
199
6198275
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
200
}
201
202
template <typename T>
203
2759176
inline Environment* Environment::GetCurrent(
204
    const v8::PropertyCallbackInfo<T>& info) {
205
2759176
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
206
}
207
208
template <typename T, typename U>
209
inline T* Environment::GetBindingData(const v8::PropertyCallbackInfo<U>& info) {
210
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
211
}
212
213
template <typename T>
214
1002913
inline T* Environment::GetBindingData(
215
    const v8::FunctionCallbackInfo<v8::Value>& info) {
216
1002913
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
217
}
218
219
template <typename T>
220
1002915
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
221
  BindingDataStore* map = static_cast<BindingDataStore*>(
222
1002915
      context->GetAlignedPointerFromEmbedderData(
223
          ContextEmbedderIndex::kBindingListIndex));
224
  DCHECK_NOT_NULL(map);
225
1002915
  auto it = map->find(T::type_name);
226
1002915
  if (UNLIKELY(it == map->end())) return nullptr;
227
1002915
  T* result = static_cast<T*>(it->second.get());
228
  DCHECK_NOT_NULL(result);
229
  DCHECK_EQ(result->env(), GetCurrent(context));
230
1002915
  return result;
231
}
232
233
template <typename T>
234
27769
inline T* Environment::AddBindingData(
235
    v8::Local<v8::Context> context,
236
    v8::Local<v8::Object> target) {
237
  DCHECK_EQ(GetCurrent(context), this);
238
  // This won't compile if T is not a BaseObject subclass.
239
55538
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
240
  BindingDataStore* map = static_cast<BindingDataStore*>(
241
27769
      context->GetAlignedPointerFromEmbedderData(
242
          ContextEmbedderIndex::kBindingListIndex));
243
  DCHECK_NOT_NULL(map);
244
27769
  auto result = map->emplace(T::type_name, item);
245
27769
  CHECK(result.second);
246
  DCHECK_EQ(GetBindingData<T>(context), item.get());
247
27769
  return item.get();
248
}
249
250
31436869
inline v8::Isolate* Environment::isolate() const {
251
31436869
  return isolate_;
252
}
253
254
7308
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
255
7308
  return ContainerOf(&Environment::timer_handle_, handle);
256
}
257
258
32715
inline uv_timer_t* Environment::timer_handle() {
259
32715
  return &timer_handle_;
260
}
261
262
185229
inline Environment* Environment::from_immediate_check_handle(
263
    uv_check_t* handle) {
264
185229
  return ContainerOf(&Environment::immediate_check_handle_, handle);
265
}
266
267
26112
inline uv_check_t* Environment::immediate_check_handle() {
268
26112
  return &immediate_check_handle_;
269
}
270
271
233630
inline uv_idle_t* Environment::immediate_idle_handle() {
272
233630
  return &immediate_idle_handle_;
273
}
274
275
39168
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
276
                                               HandleCleanupCb cb,
277
                                               void* arg) {
278
39168
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
279
39168
}
280
281
template <typename T, typename OnCloseCallback>
282
40985
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
283
40829
  handle_cleanup_waiting_++;
284
  static_assert(sizeof(T) >= sizeof(uv_handle_t), "T is a libuv handle");
285
  static_assert(offsetof(T, data) == offsetof(uv_handle_t, data),
286
                "T is a libuv handle");
287
  static_assert(offsetof(T, close_cb) == offsetof(uv_handle_t, close_cb),
288
                "T is a libuv handle");
289
  struct CloseData {
290
    Environment* env;
291
    OnCloseCallback callback;
292
    void* original_data;
293
  };
294
40907
  handle->data = new CloseData { this, callback, handle->data };
295
41219
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
296
81658
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
297
40907
    data->env->handle_cleanup_waiting_--;
298
40907
    handle->data = data->original_data;
299
40907
    data->callback(reinterpret_cast<T*>(handle));
300
  });
301
40907
}
302
303
90255
void Environment::IncreaseWaitingRequestCounter() {
304
90255
  request_waiting_++;
305
90255
}
306
307
90247
void Environment::DecreaseWaitingRequestCounter() {
308
90247
  request_waiting_--;
309
90247
  CHECK_GE(request_waiting_, 0);
310
90247
}
311
312
1263524
inline uv_loop_t* Environment::event_loop() const {
313
1263524
  return isolate_data()->event_loop();
314
}
315
316
#if HAVE_INSPECTOR
317
43737
inline bool Environment::is_in_inspector_console_call() const {
318
43737
  return is_in_inspector_console_call_;
319
}
320
321
87472
inline void Environment::set_is_in_inspector_console_call(bool value) {
322
87472
  is_in_inspector_console_call_ = value;
323
87472
}
324
#endif
325
326
5935133
inline AsyncHooks* Environment::async_hooks() {
327
5935133
  return &async_hooks_;
328
}
329
330
749157
inline ImmediateInfo* Environment::immediate_info() {
331
749157
  return &immediate_info_;
332
}
333
334
709838
inline TickInfo* Environment::tick_info() {
335
709838
  return &tick_info_;
336
}
337
338
99037
inline uint64_t Environment::timer_base() const {
339
99037
  return timer_base_;
340
}
341
342
1399373
inline std::shared_ptr<KVStore> Environment::env_vars() {
343
1399373
  return env_vars_;
344
}
345
346
7806
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
347
7806
  env_vars_ = env_vars;
348
7806
}
349
350
19
inline bool Environment::printed_error() const {
351
19
  return printed_error_;
352
}
353
354
19
inline void Environment::set_printed_error(bool value) {
355
19
  printed_error_ = value;
356
19
}
357
358
11403
inline void Environment::set_trace_sync_io(bool value) {
359
11403
  trace_sync_io_ = value;
360
11403
}
361
362
118
inline bool Environment::abort_on_uncaught_exception() const {
363
118
  return options_->abort_on_uncaught_exception;
364
}
365
366
inline void Environment::set_force_context_aware(bool value) {
367
  options_->force_context_aware = value;
368
}
369
370
41
inline bool Environment::force_context_aware() const {
371
41
  return options_->force_context_aware;
372
}
373
374
1249
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
1249
  options_->abort_on_uncaught_exception = value;
376
1249
}
377
378
1332
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
1332
  return should_abort_on_uncaught_toggle_;
380
}
381
382
306270
inline AliasedInt32Array& Environment::stream_base_state() {
383
306270
  return stream_base_state_;
384
}
385
386
55308
inline uint32_t Environment::get_next_module_id() {
387
55308
  return module_id_counter_++;
388
}
389
104482
inline uint32_t Environment::get_next_script_id() {
390
104482
  return script_id_counter_++;
391
}
392
35801
inline uint32_t Environment::get_next_function_id() {
393
35801
  return function_id_counter_++;
394
}
395
396
214581
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
214581
    Environment* env)
398
214581
    : env_(env) {
399
214581
  env_->PushShouldNotAbortOnUncaughtScope();
400
214581
}
401
402
429156
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
214578
  Close();
404
214578
}
405
406
214770
void ShouldNotAbortOnUncaughtScope::Close() {
407
214770
  if (env_ != nullptr) {
408
214578
    env_->PopShouldNotAbortOnUncaughtScope();
409
214578
    env_ = nullptr;
410
  }
411
214770
}
412
413
214581
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
214581
  should_not_abort_scope_counter_++;
415
214581
}
416
417
214578
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
214578
  should_not_abort_scope_counter_--;
419
214578
}
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
398523
inline std::vector<double>* Environment::destroy_async_id_list() {
426
398523
  return &destroy_async_id_list_;
427
}
428
429
217625
inline double Environment::new_async_id() {
430
217625
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
217625
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
246413
inline double Environment::execution_async_id() {
435
246413
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
63906
inline double Environment::trigger_async_id() {
439
63906
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
217622
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
217622
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
217622
  if (default_trigger_async_id < 0)
447
182438
    default_trigger_async_id = execution_async_id();
448
217622
  return default_trigger_async_id;
449
}
450
451
74378
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
74378
  return options_;
453
}
454
455
18061
inline const std::vector<std::string>& Environment::argv() {
456
18061
  return argv_;
457
}
458
459
7723
inline const std::vector<std::string>& Environment::exec_argv() {
460
7723
  return exec_argv_;
461
}
462
463
13056
inline const std::string& Environment::exec_path() const {
464
13056
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6523
inline void Environment::set_coverage_directory(const char* dir) {
469
6523
  coverage_directory_ = std::string(dir);
470
6523
}
471
472
6546
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6546
  CHECK_NULL(coverage_connection_);
475
6546
  std::swap(coverage_connection_, connection);
476
6546
}
477
478
19637
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
19637
  return coverage_connection_.get();
480
}
481
482
6508
inline const std::string& Environment::coverage_directory() const {
483
6508
  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
6562
Environment::cpu_profiler_connection() {
494
6562
  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
6550
Environment::heap_profiler_connection() {
529
6550
  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
13292
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
13292
  return inspector_host_port_;
561
}
562
563
67607
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
67607
  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
93898
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
187796
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
93898
  native_immediates_.Push(std::move(callback));
576
577
93898
  if (flags & CallbackFlags::kRefed) {
578
69386
    if (immediate_info()->ref_count() == 0)
579
50626
      ToggleImmediateRef(true);
580
69386
    immediate_info()->ref_count_inc(1);
581
  }
582
93898
}
583
584
template <typename Fn>
585
3825
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
7650
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
3825
      std::move(cb), flags);
588
  {
589
7650
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
3825
    native_immediates_threadsafe_.Push(std::move(callback));
591
3825
    if (task_queues_async_initialized_)
592
3816
      uv_async_send(&task_queues_async_);
593
  }
594
3825
}
595
596
template <typename Fn>
597
11836
void Environment::RequestInterrupt(Fn&& cb) {
598
23672
  auto callback = native_immediates_interrupts_.CreateCallback(
599
11836
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
23672
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
11836
    native_immediates_interrupts_.Push(std::move(callback));
603
11836
    if (task_queues_async_initialized_)
604
5172
      uv_async_send(&task_queues_async_);
605
  }
606
11836
  RequestInterruptFromV8();
607
11836
}
608
609
3147895
inline bool Environment::can_call_into_js() const {
610

3147895
  return can_call_into_js_ && !is_stopping();
611
}
612
613
2597
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
2597
  can_call_into_js_ = can_call_into_js;
615
2597
}
616
617
1543305
inline bool Environment::has_run_bootstrapping_code() const {
618
1543305
  return has_run_bootstrapping_code_;
619
}
620
621
6561
inline void Environment::DoneBootstrapping() {
622
6561
  has_run_bootstrapping_code_ = true;
623
  // This adjusts the return value of base_object_created_after_bootstrap() so
624
  // that tests that check the count do not have to account for internally
625
  // created BaseObjects.
626
6561
  base_object_created_by_bootstrap_ = base_object_count_;
627
6561
}
628
629
19
inline bool Environment::has_serialized_options() const {
630
19
  return has_serialized_options_;
631
}
632
633
7089
inline void Environment::set_has_serialized_options(bool value) {
634
7089
  has_serialized_options_ = value;
635
7089
}
636
637
12496
inline bool Environment::is_main_thread() const {
638
12496
  return worker_context() == nullptr;
639
}
640
641
1659
inline bool Environment::no_native_addons() const {
642
3314
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
643
3314
          !options_->allow_native_addons;
644
}
645
646
6526
inline bool Environment::should_not_register_esm_loader() const {
647
6526
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
648
}
649
650
18873
inline bool Environment::owns_process_state() const {
651
18873
  return flags_ & EnvironmentFlags::kOwnsProcessState;
652
}
653
654
6555
inline bool Environment::owns_inspector() const {
655
6555
  return flags_ & EnvironmentFlags::kOwnsInspector;
656
}
657
658
19628
inline bool Environment::should_create_inspector() const {
659
19628
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
660
}
661
662
135872
inline bool Environment::tracks_unmanaged_fds() const {
663
135872
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
664
}
665
666
4281
inline bool Environment::hide_console_windows() const {
667
4281
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
668
}
669
670
8008
inline bool Environment::no_global_search_paths() const {
671
16016
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
672
16016
         !options_->global_search_paths;
673
}
674
675
2784
inline bool Environment::no_browser_globals() const {
676
  // configure --no-browser-globals
677
#ifdef NODE_NO_BROWSER_GLOBALS
678
  return true;
679
#else
680
2784
  return flags_ & EnvironmentFlags::kNoBrowserGlobals;
681
#endif
682
}
683
684
4
bool Environment::filehandle_close_warning() const {
685
4
  return emit_filehandle_warning_;
686
}
687
688
3
void Environment::set_filehandle_close_warning(bool on) {
689
3
  emit_filehandle_warning_ = on;
690
3
}
691
692
6372
void Environment::set_source_maps_enabled(bool on) {
693
6372
  source_maps_enabled_ = on;
694
6372
}
695
696
2111
bool Environment::source_maps_enabled() const {
697
2111
  return source_maps_enabled_;
698
}
699
700
9141
inline uint64_t Environment::thread_id() const {
701
9141
  return thread_id_;
702
}
703
704
20258
inline worker::Worker* Environment::worker_context() const {
705
20258
  return isolate_data()->worker_context();
706
}
707
708
1479
inline void Environment::add_sub_worker_context(worker::Worker* context) {
709
1479
  sub_worker_contexts_.insert(context);
710
1479
}
711
712
1506
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
713
1506
  sub_worker_contexts_.erase(context);
714
1506
}
715
716
template <typename Fn>
717
24
inline void Environment::ForEachWorker(Fn&& iterator) {
718
26
  for (worker::Worker* w : sub_worker_contexts_) iterator(w);
719
24
}
720
721
4671495
inline bool Environment::is_stopping() const {
722
4671495
  return is_stopping_.load();
723
}
724
725
6875
inline void Environment::set_stopping(bool value) {
726
6875
  is_stopping_.store(value);
727
6875
}
728
729
14
inline std::list<node_module>* Environment::extra_linked_bindings() {
730
14
  return &extra_linked_bindings_;
731
}
732
733
10
inline node_module* Environment::extra_linked_bindings_head() {
734
10
  return extra_linked_bindings_.size() > 0 ?
735
10
      &extra_linked_bindings_.front() : nullptr;
736
}
737
738
9
inline node_module* Environment::extra_linked_bindings_tail() {
739
9
  return extra_linked_bindings_.size() > 0 ?
740
9
      &extra_linked_bindings_.back() : nullptr;
741
}
742
743
19
inline const Mutex& Environment::extra_linked_bindings_mutex() const {
744
19
  return extra_linked_bindings_mutex_;
745
}
746
747
44485
inline performance::PerformanceState* Environment::performance_state() {
748
44485
  return performance_state_.get();
749
}
750
751
8974845
inline IsolateData* Environment::isolate_data() const {
752
8974845
  return isolate_data_;
753
}
754
755
8
inline void Environment::ThrowError(const char* errmsg) {
756
8
  ThrowError(v8::Exception::Error, errmsg);
757
8
}
758
759
inline void Environment::ThrowTypeError(const char* errmsg) {
760
  ThrowError(v8::Exception::TypeError, errmsg);
761
}
762
763
inline void Environment::ThrowRangeError(const char* errmsg) {
764
  ThrowError(v8::Exception::RangeError, errmsg);
765
}
766
767
8
inline void Environment::ThrowError(
768
    v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
769
    const char* errmsg) {
770
16
  v8::HandleScope handle_scope(isolate());
771
8
  isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
772
8
}
773
774
7
inline void Environment::ThrowErrnoException(int errorno,
775
                                             const char* syscall,
776
                                             const char* message,
777
                                             const char* path) {
778
  isolate()->ThrowException(
779
7
      ErrnoException(isolate(), errorno, syscall, message, path));
780
7
}
781
782
15
inline void Environment::ThrowUVException(int errorno,
783
                                          const char* syscall,
784
                                          const char* message,
785
                                          const char* path,
786
                                          const char* dest) {
787
  isolate()->ThrowException(
788
15
      UVException(isolate(), errorno, syscall, message, path, dest));
789
15
}
790
791
1571564
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
792
3143128
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
793
1571564
    fn, arg, cleanup_hook_counter_++
794
1571564
  });
795
  // Make sure there was no existing element with these values.
796
1571564
  CHECK_EQ(insertion_info.second, true);
797
1571564
}
798
799
1549889
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
800
1549889
  CleanupHookCallback search { fn, arg, 0 };
801
1549889
  cleanup_hooks_.erase(search);
802
1549889
}
803
804
3442916
size_t CleanupHookCallback::Hash::operator()(
805
    const CleanupHookCallback& cb) const {
806
3442916
  return std::hash<void*>()(cb.arg_);
807
}
808
809
1720263
bool CleanupHookCallback::Equal::operator()(
810
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
811

1720263
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
812
}
813
814
545
BaseObject* CleanupHookCallback::GetBaseObject() const {
815
545
  if (fn_ == BaseObject::DeleteMe)
816
517
    return static_cast<BaseObject*>(arg_);
817
  else
818
28
    return nullptr;
819
}
820
821
template <typename T>
822
48
void Environment::ForEachBaseObject(T&& iterator) {
823
1138
  for (const auto& hook : cleanup_hooks_) {
824
1090
    BaseObject* obj = hook.GetBaseObject();
825
1090
    if (obj != nullptr)
826
1034
      iterator(obj);
827
  }
828
}
829
830
template <typename T>
831
6
void Environment::ForEachBindingData(T&& iterator) {
832
  BindingDataStore* map = static_cast<BindingDataStore*>(
833
12
      context()->GetAlignedPointerFromEmbedderData(
834
          ContextEmbedderIndex::kBindingListIndex));
835
  DCHECK_NOT_NULL(map);
836
30
  for (auto& it : *map) {
837
24
    iterator(it.first, it.second);
838
  }
839
6
}
840
841
3050465
void Environment::modify_base_object_count(int64_t delta) {
842
3050465
  base_object_count_ += delta;
843
3050465
}
844
845
14
int64_t Environment::base_object_created_after_bootstrap() const {
846
14
  return base_object_count_ - base_object_created_by_bootstrap_;
847
}
848
849
2
int64_t Environment::base_object_count() const {
850
2
  return base_object_count_;
851
}
852
853
17
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
854
17
  CHECK(!main_utf16_);
855
17
  main_utf16_ = std::move(str);
856
17
}
857
858
1248
void Environment::set_process_exit_handler(
859
    std::function<void(Environment*, int)>&& handler) {
860
1248
  process_exit_handler_ = std::move(handler);
861
1248
}
862
863
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
864
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
865
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
866
#define V(TypeName, PropertyName)                                             \
867
  inline                                                                      \
868
  v8::Local<TypeName> IsolateData::PropertyName() const {                     \
869
    return PropertyName ## _ .Get(isolate_);                                  \
870
  }
871
852127
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
872
963062
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
873
8368341
  PER_ISOLATE_STRING_PROPERTIES(VS)
874
#undef V
875
#undef VS
876
#undef VY
877
#undef VP
878
879
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
880
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
881
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
882
#define V(TypeName, PropertyName)                                             \
883
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
884
    return isolate_data()->PropertyName();                                    \
885
  }
886
852127
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
887
838722
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
888
12077419
  PER_ISOLATE_STRING_PROPERTIES(VS)
889
#undef V
890
#undef VS
891
#undef VY
892
#undef VP
893
894
#define V(PropertyName, TypeName)                                             \
895
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
896
    return PersistentToLocal::Strong(PropertyName ## _);                      \
897
  }                                                                           \
898
  inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) {  \
899
    PropertyName ## _.Reset(isolate(), value);                                \
900
  }
901
1568138
  ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
902
6237610
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
903
#undef V
904
905
10171487
v8::Local<v8::Context> Environment::context() const {
906
10171487
  return PersistentToLocal::Strong(context_);
907
}
908
909
}  // namespace node
910
911
// These two files depend on each other. Including base_object-inl.h after this
912
// file is the easiest way to avoid issues with that circular dependency.
913
#include "base_object-inl.h"
914
915
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
916
917
#endif  // SRC_ENV_INL_H_