GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 454 457 99.3 %
Date: 2022-07-18 04:15:58 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
115580
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
115580
    IsolateData* isolate_data)
47
115580
    : node_allocator_(isolate_data->node_allocator()) {
48
115580
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
115580
}
50
51
231160
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
115580
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
115580
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1271081
inline uv_loop_t* IsolateData::event_loop() const {
60
1271081
  return event_loop_;
61
}
62
63
122689
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
122689
  return node_allocator_;
65
}
66
67
159000
inline MultiIsolatePlatform* IsolateData::platform() const {
68
159000
  return platform_;
69
}
70
71
1260
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
1260
  CHECK_NULL(worker_context_);  // Should be set only once.
73
1260
  worker_context_ = context;
74
1260
}
75
76
20287
inline worker::Worker* IsolateData::worker_context() const {
77
20287
  return worker_context_;
78
}
79
80
218472
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
218472
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2630508
inline AliasedUint32Array& AsyncHooks::fields() {
85
2630508
  return fields_;
86
}
87
88
1700937
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1700937
  return async_id_fields_;
90
}
91
92
1308
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
1308
  return async_ids_stack_;
94
}
95
96
818484
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
818484
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
1308
    js_execution_async_resources_.Reset(
99
2616
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
818484
  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
218124
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
218124
  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
432660
inline Environment* AsyncHooks::env() {
118
432660
  return Environment::ForAsyncHooks(this);
119
}
120
121
432660
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
432660
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
788526
inline size_t Environment::async_callback_scope_depth() const {
126
788526
  return async_callback_scope_depth_;
127
}
128
129
805929
inline void Environment::PushAsyncCallbackScope() {
130
805929
  async_callback_scope_depth_++;
131
805929
}
132
133
805405
inline void Environment::PopAsyncCallbackScope() {
134
805405
  async_callback_scope_depth_--;
135
805405
}
136
137
1308
inline AliasedUint32Array& ImmediateInfo::fields() {
138
1308
  return fields_;
139
}
140
141
185662
inline uint32_t ImmediateInfo::count() const {
142
185662
  return fields_[kCount];
143
}
144
145
283537
inline uint32_t ImmediateInfo::ref_count() const {
146
283537
  return fields_[kRefCount];
147
}
148
149
44098
inline bool ImmediateInfo::has_outstanding() const {
150
44098
  return fields_[kHasOutstanding] == 1;
151
}
152
153
34737
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
34737
  fields_[kRefCount] += increment;
155
34737
}
156
157
205657
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
205657
  fields_[kRefCount] -= decrement;
159
205657
}
160
161
1308
inline AliasedUint8Array& TickInfo::fields() {
162
1308
  return fields_;
163
}
164
165
1420710
inline bool TickInfo::has_tick_scheduled() const {
166
1420710
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
520840
inline bool TickInfo::has_rejection_to_warn() const {
170
520840
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
57070
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
57070
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
110232
  v8::HandleScope handle_scope(isolate);
176
55116
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
8085361
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
8085361
  if (UNLIKELY(context.IsEmpty())) {
181
1
    return nullptr;
182
  }
183
8085360
  if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184
8085360
               ContextEmbedderIndex::kContextTag)) {
185
11
    return nullptr;
186
  }
187
8085349
  if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188
                   ContextEmbedderIndex::kContextTag) !=
189
8085349
               Environment::kNodeContextTagPtr)) {
190
    return nullptr;
191
  }
192
  return static_cast<Environment*>(
193
8085349
      context->GetAlignedPointerFromEmbedderData(
194
8085349
          ContextEmbedderIndex::kEnvironment));
195
}
196
197
6192435
inline Environment* Environment::GetCurrent(
198
    const v8::FunctionCallbackInfo<v8::Value>& info) {
199
6192435
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
200
}
201
202
template <typename T>
203
2766396
inline Environment* Environment::GetCurrent(
204
    const v8::PropertyCallbackInfo<T>& info) {
205
2766396
  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
1002443
inline T* Environment::GetBindingData(
215
    const v8::FunctionCallbackInfo<v8::Value>& info) {
216
1002443
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
217
}
218
219
template <typename T>
220
1002445
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
221
  BindingDataStore* map = static_cast<BindingDataStore*>(
222
1002445
      context->GetAlignedPointerFromEmbedderData(
223
          ContextEmbedderIndex::kBindingListIndex));
224
  DCHECK_NOT_NULL(map);
225
1002445
  auto it = map->find(T::type_name);
226
1002445
  if (UNLIKELY(it == map->end())) return nullptr;
227
1002445
  T* result = static_cast<T*>(it->second.get());
228
  DCHECK_NOT_NULL(result);
229
  DCHECK_EQ(result->env(), GetCurrent(context));
230
1002445
  return result;
231
}
232
233
template <typename T>
234
27776
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
55552
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
240
  BindingDataStore* map = static_cast<BindingDataStore*>(
241
27776
      context->GetAlignedPointerFromEmbedderData(
242
          ContextEmbedderIndex::kBindingListIndex));
243
  DCHECK_NOT_NULL(map);
244
27776
  auto result = map->emplace(T::type_name, item);
245
27776
  CHECK(result.second);
246
  DCHECK_EQ(GetBindingData<T>(context), item.get());
247
27776
  return item.get();
248
}
249
250
31470805
inline v8::Isolate* Environment::isolate() const {
251
31470805
  return isolate_;
252
}
253
254
7479
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
255
7479
  return ContainerOf(&Environment::timer_handle_, handle);
256
}
257
258
32877
inline uv_timer_t* Environment::timer_handle() {
259
32877
  return &timer_handle_;
260
}
261
262
185662
inline Environment* Environment::from_immediate_check_handle(
263
    uv_check_t* handle) {
264
185662
  return ContainerOf(&Environment::immediate_check_handle_, handle);
265
}
266
267
26108
inline uv_check_t* Environment::immediate_check_handle() {
268
26108
  return &immediate_check_handle_;
269
}
270
271
234248
inline uv_idle_t* Environment::immediate_idle_handle() {
272
234248
  return &immediate_idle_handle_;
273
}
274
275
39162
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
276
                                               HandleCleanupCb cb,
277
                                               void* arg) {
278
39162
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
279
39162
}
280
281
template <typename T, typename OnCloseCallback>
282
40975
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
283
40819
  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
40897
  handle->data = new CloseData { this, callback, handle->data };
295
41209
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
296
81638
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
297
40897
    data->env->handle_cleanup_waiting_--;
298
40897
    handle->data = data->original_data;
299
40897
    data->callback(reinterpret_cast<T*>(handle));
300
  });
301
40897
}
302
303
90787
void Environment::IncreaseWaitingRequestCounter() {
304
90787
  request_waiting_++;
305
90787
}
306
307
90779
void Environment::DecreaseWaitingRequestCounter() {
308
90779
  request_waiting_--;
309
90779
  CHECK_GE(request_waiting_, 0);
310
90779
}
311
312
1264519
inline uv_loop_t* Environment::event_loop() const {
313
1264519
  return isolate_data()->event_loop();
314
}
315
316
#if HAVE_INSPECTOR
317
43747
inline bool Environment::is_in_inspector_console_call() const {
318
43747
  return is_in_inspector_console_call_;
319
}
320
321
87492
inline void Environment::set_is_in_inspector_console_call(bool value) {
322
87492
  is_in_inspector_console_call_ = value;
323
87492
}
324
#endif
325
326
5946604
inline AsyncHooks* Environment::async_hooks() {
327
5946604
  return &async_hooks_;
328
}
329
330
754994
inline ImmediateInfo* Environment::immediate_info() {
331
754994
  return &immediate_info_;
332
}
333
334
711695
inline TickInfo* Environment::tick_info() {
335
711695
  return &tick_info_;
336
}
337
338
100063
inline uint64_t Environment::timer_base() const {
339
100063
  return timer_base_;
340
}
341
342
1402987
inline std::shared_ptr<KVStore> Environment::env_vars() {
343
1402987
  return env_vars_;
344
}
345
346
7811
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
347
7811
  env_vars_ = env_vars;
348
7811
}
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
11409
inline void Environment::set_trace_sync_io(bool value) {
359
11409
  trace_sync_io_ = value;
360
11409
}
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
1255
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
1255
  options_->abort_on_uncaught_exception = value;
376
1255
}
377
378
1338
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
1338
  return should_abort_on_uncaught_toggle_;
380
}
381
382
302518
inline AliasedInt32Array& Environment::stream_base_state() {
383
302518
  return stream_base_state_;
384
}
385
386
55256
inline uint32_t Environment::get_next_module_id() {
387
55256
  return module_id_counter_++;
388
}
389
104659
inline uint32_t Environment::get_next_script_id() {
390
104659
  return script_id_counter_++;
391
}
392
35794
inline uint32_t Environment::get_next_function_id() {
393
35794
  return function_id_counter_++;
394
}
395
396
214661
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
214661
    Environment* env)
398
214661
    : env_(env) {
399
214661
  env_->PushShouldNotAbortOnUncaughtScope();
400
214661
}
401
402
429316
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
214658
  Close();
404
214658
}
405
406
214850
void ShouldNotAbortOnUncaughtScope::Close() {
407
214850
  if (env_ != nullptr) {
408
214658
    env_->PopShouldNotAbortOnUncaughtScope();
409
214658
    env_ = nullptr;
410
  }
411
214850
}
412
413
214661
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
214661
  should_not_abort_scope_counter_++;
415
214661
}
416
417
214658
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
214658
  should_not_abort_scope_counter_--;
419
214658
}
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
394581
inline std::vector<double>* Environment::destroy_async_id_list() {
426
394581
  return &destroy_async_id_list_;
427
}
428
429
218679
inline double Environment::new_async_id() {
430
218679
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
218679
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
247451
inline double Environment::execution_async_id() {
435
247451
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
64142
inline double Environment::trigger_async_id() {
439
64142
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
218676
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
218676
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
218676
  if (default_trigger_async_id < 0)
447
183245
    default_trigger_async_id = execution_async_id();
448
218676
  return default_trigger_async_id;
449
}
450
451
74426
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
74426
  return options_;
453
}
454
455
18052
inline const std::vector<std::string>& Environment::argv() {
456
18052
  return argv_;
457
}
458
459
7722
inline const std::vector<std::string>& Environment::exec_argv() {
460
7722
  return exec_argv_;
461
}
462
463
13054
inline const std::string& Environment::exec_path() const {
464
13054
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6522
inline void Environment::set_coverage_directory(const char* dir) {
469
6522
  coverage_directory_ = std::string(dir);
470
6522
}
471
472
6548
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6548
  CHECK_NULL(coverage_connection_);
475
6548
  std::swap(coverage_connection_, connection);
476
6548
}
477
478
19643
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
19643
  return coverage_connection_.get();
480
}
481
482
6506
inline const std::string& Environment::coverage_directory() const {
483
6506
  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
6564
Environment::cpu_profiler_connection() {
494
6564
  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
6552
Environment::heap_profiler_connection() {
529
6552
  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
13294
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
13294
  return inspector_host_port_;
561
}
562
563
67441
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
67441
  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
94012
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
188024
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
94012
  native_immediates_.Push(std::move(callback));
576
577
94012
  if (flags & CallbackFlags::kRefed) {
578
69460
    if (immediate_info()->ref_count() == 0)
579
50724
      ToggleImmediateRef(true);
580
69460
    immediate_info()->ref_count_inc(1);
581
  }
582
94012
}
583
584
template <typename Fn>
585
3818
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
7636
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
3818
      std::move(cb), flags);
588
  {
589
7636
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
3818
    native_immediates_threadsafe_.Push(std::move(callback));
591
3818
    if (task_queues_async_initialized_)
592
3809
      uv_async_send(&task_queues_async_);
593
  }
594
3818
}
595
596
template <typename Fn>
597
11926
void Environment::RequestInterrupt(Fn&& cb) {
598
23852
  auto callback = native_immediates_interrupts_.CreateCallback(
599
11926
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
23852
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
11926
    native_immediates_interrupts_.Push(std::move(callback));
603
11926
    if (task_queues_async_initialized_)
604
5254
      uv_async_send(&task_queues_async_);
605
  }
606
11926
  RequestInterruptFromV8();
607
11926
}
608
609
3157270
inline bool Environment::can_call_into_js() const {
610

3157270
  return can_call_into_js_ && !is_stopping();
611
}
612
613
2596
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
2596
  can_call_into_js_ = can_call_into_js;
615
2596
}
616
617
1547239
inline bool Environment::has_run_bootstrapping_code() const {
618
1547239
  return has_run_bootstrapping_code_;
619
}
620
621
6563
inline void Environment::DoneBootstrapping() {
622
6563
  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
6563
  base_object_created_by_bootstrap_ = base_object_count_;
627
6563
}
628
629
19
inline bool Environment::has_serialized_options() const {
630
19
  return has_serialized_options_;
631
}
632
633
7087
inline void Environment::set_has_serialized_options(bool value) {
634
7087
  has_serialized_options_ = value;
635
7087
}
636
637
12521
inline bool Environment::is_main_thread() const {
638
12521
  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
6525
inline bool Environment::should_not_register_esm_loader() const {
647
6525
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
648
}
649
650
18894
inline bool Environment::owns_process_state() const {
651
18894
  return flags_ & EnvironmentFlags::kOwnsProcessState;
652
}
653
654
6557
inline bool Environment::owns_inspector() const {
655
6557
  return flags_ & EnvironmentFlags::kOwnsInspector;
656
}
657
658
19642
inline bool Environment::should_create_inspector() const {
659
19642
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
660
}
661
662
135854
inline bool Environment::tracks_unmanaged_fds() const {
663
135854
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
664
}
665
666
4274
inline bool Environment::hide_console_windows() const {
667
4274
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
668
}
669
670
8007
inline bool Environment::no_global_search_paths() const {
671
16014
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
672
16014
         !options_->global_search_paths;
673
}
674
675
2790
inline bool Environment::no_browser_globals() const {
676
  // configure --no-browser-globals
677
#ifdef NODE_NO_BROWSER_GLOBALS
678
  return true;
679
#else
680
2790
  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
6371
void Environment::set_source_maps_enabled(bool on) {
693
6371
  source_maps_enabled_ = on;
694
6371
}
695
696
2298
bool Environment::source_maps_enabled() const {
697
2298
  return source_maps_enabled_;
698
}
699
700
9150
inline uint64_t Environment::thread_id() const {
701
9150
  return thread_id_;
702
}
703
704
20287
inline worker::Worker* Environment::worker_context() const {
705
20287
  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
4685317
inline bool Environment::is_stopping() const {
722
4685317
  return is_stopping_.load();
723
}
724
725
6869
inline void Environment::set_stopping(bool value) {
726
6869
  is_stopping_.store(value);
727
6869
}
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
44467
inline performance::PerformanceState* Environment::performance_state() {
748
44467
  return performance_state_.get();
749
}
750
751
8982247
inline IsolateData* Environment::isolate_data() const {
752
8982247
  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
1568236
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
792
3136472
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
793
1568236
    fn, arg, cleanup_hook_counter_++
794
1568236
  });
795
  // Make sure there was no existing element with these values.
796
1568236
  CHECK_EQ(insertion_info.second, true);
797
1568236
}
798
799
1549845
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
800
1549845
  CleanupHookCallback search { fn, arg, 0 };
801
1549845
  cleanup_hooks_.erase(search);
802
1549845
}
803
804
3434883
size_t CleanupHookCallback::Hash::operator()(
805
    const CleanupHookCallback& cb) const {
806
3434883
  return std::hash<void*>()(cb.arg_);
807
}
808
809
1714536
bool CleanupHookCallback::Equal::operator()(
810
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
811

1714536
  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
3053664
void Environment::modify_base_object_count(int64_t delta) {
842
3053664
  base_object_count_ += delta;
843
3053664
}
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
1251
void Environment::set_process_exit_handler(
859
    std::function<void(Environment*, int)>&& handler) {
860
1251
  process_exit_handler_ = std::move(handler);
861
1251
}
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
851912
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
872
978288
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
873
8365529
  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
851912
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
887
853825
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
888
12076217
  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
1571364
  ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
902
6247154
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
903
#undef V
904
905
10185621
v8::Local<v8::Context> Environment::context() const {
906
10185621
  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_