GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 454 457 99.3 %
Date: 2022-07-22 04:16:17 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
116989
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116989
    IsolateData* isolate_data)
47
116989
    : node_allocator_(isolate_data->node_allocator()) {
48
116989
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116989
}
50
51
233978
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116989
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116989
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1269578
inline uv_loop_t* IsolateData::event_loop() const {
60
1269578
  return event_loop_;
61
}
62
63
124099
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
124099
  return node_allocator_;
65
}
66
67
158938
inline MultiIsolatePlatform* IsolateData::platform() const {
68
158938
  return platform_;
69
}
70
71
1258
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
1258
  CHECK_NULL(worker_context_);  // Should be set only once.
73
1258
  worker_context_ = context;
74
1258
}
75
76
20180
inline worker::Worker* IsolateData::worker_context() const {
77
20180
  return worker_context_;
78
}
79
80
217566
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
217566
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2634502
inline AliasedUint32Array& AsyncHooks::fields() {
85
2634502
  return fields_;
86
}
87
88
1701317
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1701317
  return async_id_fields_;
90
}
91
92
1303
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
1303
  return async_ids_stack_;
94
}
95
96
821901
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
821901
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
1303
    js_execution_async_resources_.Reset(
99
2606
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
821901
  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
217218
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
217218
  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
434156
inline Environment* AsyncHooks::env() {
118
434156
  return Environment::ForAsyncHooks(this);
119
}
120
121
434156
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
434156
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
791382
inline size_t Environment::async_callback_scope_depth() const {
126
791382
  return async_callback_scope_depth_;
127
}
128
129
808604
inline void Environment::PushAsyncCallbackScope() {
130
808604
  async_callback_scope_depth_++;
131
808604
}
132
133
807992
inline void Environment::PopAsyncCallbackScope() {
134
807992
  async_callback_scope_depth_--;
135
807992
}
136
137
1303
inline AliasedUint32Array& ImmediateInfo::fields() {
138
1303
  return fields_;
139
}
140
141
187745
inline uint32_t ImmediateInfo::count() const {
142
187745
  return fields_[kCount];
143
}
144
145
285952
inline uint32_t ImmediateInfo::ref_count() const {
146
285952
  return fields_[kRefCount];
147
}
148
149
44596
inline bool ImmediateInfo::has_outstanding() const {
150
44596
  return fields_[kHasOutstanding] == 1;
151
}
152
153
34851
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
34851
  fields_[kRefCount] += increment;
155
34851
}
156
157
207457
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
207457
  fields_[kRefCount] -= decrement;
159
207457
}
160
161
1303
inline AliasedUint8Array& TickInfo::fields() {
162
1303
  return fields_;
163
}
164
165
1426292
inline bool TickInfo::has_tick_scheduled() const {
166
1426292
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
521945
inline bool TickInfo::has_rejection_to_warn() const {
170
521945
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
57278
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
57278
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
110800
  v8::HandleScope handle_scope(isolate);
176
55400
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
8091934
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
8091934
  if (UNLIKELY(context.IsEmpty())) {
181
1
    return nullptr;
182
  }
183
8091933
  if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184
8091933
               ContextEmbedderIndex::kContextTag)) {
185
11
    return nullptr;
186
  }
187
8091922
  if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188
                   ContextEmbedderIndex::kContextTag) !=
189
8091922
               Environment::kNodeContextTagPtr)) {
190
    return nullptr;
191
  }
192
  return static_cast<Environment*>(
193
8091922
      context->GetAlignedPointerFromEmbedderData(
194
8091922
          ContextEmbedderIndex::kEnvironment));
195
}
196
197
6201943
inline Environment* Environment::GetCurrent(
198
    const v8::FunctionCallbackInfo<v8::Value>& info) {
199
6201943
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
200
}
201
202
template <typename T>
203
2759770
inline Environment* Environment::GetCurrent(
204
    const v8::PropertyCallbackInfo<T>& info) {
205
2759770
  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
1002044
inline T* Environment::GetBindingData(
215
    const v8::FunctionCallbackInfo<v8::Value>& info) {
216
1002044
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
217
}
218
219
template <typename T>
220
1002046
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
221
  BindingDataStore* map = static_cast<BindingDataStore*>(
222
1002046
      context->GetAlignedPointerFromEmbedderData(
223
          ContextEmbedderIndex::kBindingListIndex));
224
  DCHECK_NOT_NULL(map);
225
1002046
  auto it = map->find(T::type_name);
226
1002046
  if (UNLIKELY(it == map->end())) return nullptr;
227
1002046
  T* result = static_cast<T*>(it->second.get());
228
  DCHECK_NOT_NULL(result);
229
  DCHECK_EQ(result->env(), GetCurrent(context));
230
1002046
  return result;
231
}
232
233
template <typename T>
234
27781
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
55562
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
240
  BindingDataStore* map = static_cast<BindingDataStore*>(
241
27781
      context->GetAlignedPointerFromEmbedderData(
242
          ContextEmbedderIndex::kBindingListIndex));
243
  DCHECK_NOT_NULL(map);
244
27781
  auto result = map->emplace(T::type_name, item);
245
27781
  CHECK(result.second);
246
  DCHECK_EQ(GetBindingData<T>(context), item.get());
247
27781
  return item.get();
248
}
249
250
31487410
inline v8::Isolate* Environment::isolate() const {
251
31487410
  return isolate_;
252
}
253
254
7550
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
255
7550
  return ContainerOf(&Environment::timer_handle_, handle);
256
}
257
258
32962
inline uv_timer_t* Environment::timer_handle() {
259
32962
  return &timer_handle_;
260
}
261
262
187745
inline Environment* Environment::from_immediate_check_handle(
263
    uv_check_t* handle) {
264
187745
  return ContainerOf(&Environment::immediate_check_handle_, handle);
265
}
266
267
26124
inline uv_check_t* Environment::immediate_check_handle() {
268
26124
  return &immediate_check_handle_;
269
}
270
271
236158
inline uv_idle_t* Environment::immediate_idle_handle() {
272
236158
  return &immediate_idle_handle_;
273
}
274
275
39186
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
276
                                               HandleCleanupCb cb,
277
                                               void* arg) {
278
39186
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
279
39186
}
280
281
template <typename T, typename OnCloseCallback>
282
40389
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
283
40233
  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
40311
  handle->data = new CloseData { this, callback, handle->data };
295
40623
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
296
80466
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
297
40311
    data->env->handle_cleanup_waiting_--;
298
40311
    handle->data = data->original_data;
299
40311
    data->callback(reinterpret_cast<T*>(handle));
300
  });
301
40311
}
302
303
90242
void Environment::IncreaseWaitingRequestCounter() {
304
90242
  request_waiting_++;
305
90242
}
306
307
90234
void Environment::DecreaseWaitingRequestCounter() {
308
90234
  request_waiting_--;
309
90234
  CHECK_GE(request_waiting_, 0);
310
90234
}
311
312
1263015
inline uv_loop_t* Environment::event_loop() const {
313
1263015
  return isolate_data()->event_loop();
314
}
315
316
#if HAVE_INSPECTOR
317
43768
inline bool Environment::is_in_inspector_console_call() const {
318
43768
  return is_in_inspector_console_call_;
319
}
320
321
87534
inline void Environment::set_is_in_inspector_console_call(bool value) {
322
87534
  is_in_inspector_console_call_ = value;
323
87534
}
324
#endif
325
326
5955164
inline AsyncHooks* Environment::async_hooks() {
327
5955164
  return &async_hooks_;
328
}
329
330
761904
inline ImmediateInfo* Environment::immediate_info() {
331
761904
  return &immediate_info_;
332
}
333
334
714481
inline TickInfo* Environment::tick_info() {
335
714481
  return &tick_info_;
336
}
337
338
99822
inline uint64_t Environment::timer_base() const {
339
99822
  return timer_base_;
340
}
341
342
1399682
inline std::shared_ptr<KVStore> Environment::env_vars() {
343
1399682
  return env_vars_;
344
}
345
346
7810
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
347
7810
  env_vars_ = env_vars;
348
7810
}
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
11321
inline void Environment::set_trace_sync_io(bool value) {
359
11321
  trace_sync_io_ = value;
360
11321
}
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
1250
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
1250
  options_->abort_on_uncaught_exception = value;
376
1250
}
377
378
1333
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
1333
  return should_abort_on_uncaught_toggle_;
380
}
381
382
308153
inline AliasedInt32Array& Environment::stream_base_state() {
383
308153
  return stream_base_state_;
384
}
385
386
55314
inline uint32_t Environment::get_next_module_id() {
387
55314
  return module_id_counter_++;
388
}
389
104682
inline uint32_t Environment::get_next_script_id() {
390
104682
  return script_id_counter_++;
391
}
392
35806
inline uint32_t Environment::get_next_function_id() {
393
35806
  return function_id_counter_++;
394
}
395
396
214793
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
214793
    Environment* env)
398
214793
    : env_(env) {
399
214793
  env_->PushShouldNotAbortOnUncaughtScope();
400
214793
}
401
402
429580
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
214790
  Close();
404
214790
}
405
406
214982
void ShouldNotAbortOnUncaughtScope::Close() {
407
214982
  if (env_ != nullptr) {
408
214790
    env_->PopShouldNotAbortOnUncaughtScope();
409
214790
    env_ = nullptr;
410
  }
411
214982
}
412
413
214793
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
214793
  should_not_abort_scope_counter_++;
415
214793
}
416
417
214790
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
214790
  should_not_abort_scope_counter_--;
419
214790
}
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
397250
inline std::vector<double>* Environment::destroy_async_id_list() {
426
397250
  return &destroy_async_id_list_;
427
}
428
429
217773
inline double Environment::new_async_id() {
430
217773
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
217773
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
246357
inline double Environment::execution_async_id() {
435
246357
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
63593
inline double Environment::trigger_async_id() {
439
63593
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
217770
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
217770
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
217770
  if (default_trigger_async_id < 0)
447
182698
    default_trigger_async_id = execution_async_id();
448
217770
  return default_trigger_async_id;
449
}
450
451
74391
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
74391
  return options_;
453
}
454
455
18068
inline const std::vector<std::string>& Environment::argv() {
456
18068
  return argv_;
457
}
458
459
7726
inline const std::vector<std::string>& Environment::exec_argv() {
460
7726
  return exec_argv_;
461
}
462
463
13062
inline const std::string& Environment::exec_path() const {
464
13062
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6526
inline void Environment::set_coverage_directory(const char* dir) {
469
6526
  coverage_directory_ = std::string(dir);
470
6526
}
471
472
6549
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6549
  CHECK_NULL(coverage_connection_);
475
6549
  std::swap(coverage_connection_, connection);
476
6549
}
477
478
19646
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
19646
  return coverage_connection_.get();
480
}
481
482
6509
inline const std::string& Environment::coverage_directory() const {
483
6509
  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
6565
Environment::cpu_profiler_connection() {
494
6565
  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
6553
Environment::heap_profiler_connection() {
529
6553
  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
13298
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
13298
  return inspector_host_port_;
561
}
562
563
67730
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
67730
  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
94228
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
188456
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
94228
  native_immediates_.Push(std::move(callback));
576
577
94228
  if (flags & CallbackFlags::kRefed) {
578
69688
    if (immediate_info()->ref_count() == 0)
579
50714
      ToggleImmediateRef(true);
580
69688
    immediate_info()->ref_count_inc(1);
581
  }
582
94228
}
583
584
template <typename Fn>
585
3827
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
7654
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
3827
      std::move(cb), flags);
588
  {
589
7654
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
3827
    native_immediates_threadsafe_.Push(std::move(callback));
591
3827
    if (task_queues_async_initialized_)
592
3818
      uv_async_send(&task_queues_async_);
593
  }
594
3827
}
595
596
template <typename Fn>
597
11861
void Environment::RequestInterrupt(Fn&& cb) {
598
23722
  auto callback = native_immediates_interrupts_.CreateCallback(
599
11861
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
23722
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
11861
    native_immediates_interrupts_.Push(std::move(callback));
603
11861
    if (task_queues_async_initialized_)
604
5195
      uv_async_send(&task_queues_async_);
605
  }
606
11861
  RequestInterruptFromV8();
607
11861
}
608
609
3167602
inline bool Environment::can_call_into_js() const {
610

3167602
  return can_call_into_js_ && !is_stopping();
611
}
612
613
2688
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
2688
  can_call_into_js_ = can_call_into_js;
615
2688
}
616
617
1543496
inline bool Environment::has_run_bootstrapping_code() const {
618
1543496
  return has_run_bootstrapping_code_;
619
}
620
621
6564
inline void Environment::DoneBootstrapping() {
622
6564
  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
6564
  base_object_created_by_bootstrap_ = base_object_count_;
627
6564
}
628
629
19
inline bool Environment::has_serialized_options() const {
630
19
  return has_serialized_options_;
631
}
632
633
7093
inline void Environment::set_has_serialized_options(bool value) {
634
7093
  has_serialized_options_ = value;
635
7093
}
636
637
12415
inline bool Environment::is_main_thread() const {
638
12415
  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
6529
inline bool Environment::should_not_register_esm_loader() const {
647
6529
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
648
}
649
650
18883
inline bool Environment::owns_process_state() const {
651
18883
  return flags_ & EnvironmentFlags::kOwnsProcessState;
652
}
653
654
6558
inline bool Environment::owns_inspector() const {
655
6558
  return flags_ & EnvironmentFlags::kOwnsInspector;
656
}
657
658
19634
inline bool Environment::should_create_inspector() const {
659
19634
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
660
}
661
662
135590
inline bool Environment::tracks_unmanaged_fds() const {
663
135590
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
664
}
665
666
4282
inline bool Environment::hide_console_windows() const {
667
4282
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
668
}
669
670
8011
inline bool Environment::no_global_search_paths() const {
671
16022
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
672
16022
         !options_->global_search_paths;
673
}
674
675
2785
inline bool Environment::no_browser_globals() const {
676
  // configure --no-browser-globals
677
#ifdef NODE_NO_BROWSER_GLOBALS
678
  return true;
679
#else
680
2785
  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
6374
void Environment::set_source_maps_enabled(bool on) {
693
6374
  source_maps_enabled_ = on;
694
6374
}
695
696
2305
bool Environment::source_maps_enabled() const {
697
2305
  return source_maps_enabled_;
698
}
699
700
9143
inline uint64_t Environment::thread_id() const {
701
9143
  return thread_id_;
702
}
703
704
20180
inline worker::Worker* Environment::worker_context() const {
705
20180
  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
1507
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
713
1507
  sub_worker_contexts_.erase(context);
714
1507
}
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
4700854
inline bool Environment::is_stopping() const {
722
4700854
  return is_stopping_.load();
723
}
724
725
6791
inline void Environment::set_stopping(bool value) {
726
6791
  is_stopping_.store(value);
727
6791
}
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
44434
inline performance::PerformanceState* Environment::performance_state() {
748
44434
  return performance_state_.get();
749
}
750
751
8989154
inline IsolateData* Environment::isolate_data() const {
752
8989154
  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
1572415
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
792
3144830
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
793
1572415
    fn, arg, cleanup_hook_counter_++
794
1572415
  });
795
  // Make sure there was no existing element with these values.
796
1572415
  CHECK_EQ(insertion_info.second, true);
797
1572415
}
798
799
1549118
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
800
1549118
  CleanupHookCallback search { fn, arg, 0 };
801
1549118
  cleanup_hooks_.erase(search);
802
1549118
}
803
804
3440370
size_t CleanupHookCallback::Hash::operator()(
805
    const CleanupHookCallback& cb) const {
806
3440370
  return std::hash<void*>()(cb.arg_);
807
}
808
809
1717967
bool CleanupHookCallback::Equal::operator()(
810
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
811

1717967
  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
3050273
void Environment::modify_base_object_count(int64_t delta) {
842
3050273
  base_object_count_ += delta;
843
3050273
}
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
1249
void Environment::set_process_exit_handler(
859
    std::function<void(Environment*, int)>&& handler) {
860
1249
  process_exit_handler_ = std::move(handler);
861
1249
}
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
852227
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
872
979252
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
873
8376232
  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
852227
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
887
855303
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
888
12090872
  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
1569490
  ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
902
6258180
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
903
#undef V
904
905
10199831
v8::Local<v8::Context> Environment::context() const {
906
10199831
  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_