GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 455 458 99.3 %
Date: 2022-07-30 04:16:22 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
117267
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
117267
    IsolateData* isolate_data)
47
117267
    : node_allocator_(isolate_data->node_allocator()) {
48
117267
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
117267
}
50
51
234534
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
117267
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
117267
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1276159
inline uv_loop_t* IsolateData::event_loop() const {
60
1276159
  return event_loop_;
61
}
62
63
124400
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
124400
  return node_allocator_;
65
}
66
67
60013
inline MultiIsolatePlatform* IsolateData::platform() const {
68
60013
  return platform_;
69
}
70
71
1261
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
1261
  CHECK_NULL(worker_context_);  // Should be set only once.
73
1261
  worker_context_ = context;
74
1261
}
75
76
20330
inline worker::Worker* IsolateData::worker_context() const {
77
20330
  return worker_context_;
78
}
79
80
218082
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
218082
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2652695
inline AliasedUint32Array& AsyncHooks::fields() {
85
2652695
  return fields_;
86
}
87
88
1706560
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1706560
  return async_id_fields_;
90
}
91
92
1306
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
1306
  return async_ids_stack_;
94
}
95
96
825508
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
825508
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
1306
    js_execution_async_resources_.Reset(
99
2612
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
825508
  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
217734
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
217734
  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
439185
inline Environment* AsyncHooks::env() {
118
439185
  return Environment::ForAsyncHooks(this);
119
}
120
121
439185
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
439185
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
794760
inline size_t Environment::async_callback_scope_depth() const {
126
794760
  return async_callback_scope_depth_;
127
}
128
129
812252
inline void Environment::PushAsyncCallbackScope() {
130
812252
  async_callback_scope_depth_++;
131
812252
}
132
133
811726
inline void Environment::PopAsyncCallbackScope() {
134
811726
  async_callback_scope_depth_--;
135
811726
}
136
137
1306
inline AliasedUint32Array& ImmediateInfo::fields() {
138
1306
  return fields_;
139
}
140
141
188675
inline uint32_t ImmediateInfo::count() const {
142
188675
  return fields_[kCount];
143
}
144
145
286851
inline uint32_t ImmediateInfo::ref_count() const {
146
286851
  return fields_[kRefCount];
147
}
148
149
44099
inline bool ImmediateInfo::has_outstanding() const {
150
44099
  return fields_[kHasOutstanding] == 1;
151
}
152
153
34932
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
34932
  fields_[kRefCount] += increment;
155
34932
}
156
157
208771
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
208771
  fields_[kRefCount] -= decrement;
159
208771
}
160
161
1306
inline AliasedUint8Array& TickInfo::fields() {
162
1306
  return fields_;
163
}
164
165
1431977
inline bool TickInfo::has_tick_scheduled() const {
166
1431977
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
524778
inline bool TickInfo::has_rejection_to_warn() const {
170
524778
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
57232
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
57232
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
110774
  v8::HandleScope handle_scope(isolate);
176
55387
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7755466
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7755466
  if (UNLIKELY(context.IsEmpty())) {
181
1
    return nullptr;
182
  }
183
7755465
  if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184
7755465
               ContextEmbedderIndex::kContextTag)) {
185
11
    return nullptr;
186
  }
187
7755454
  if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188
                   ContextEmbedderIndex::kContextTag) !=
189
7755454
               Environment::kNodeContextTagPtr)) {
190
    return nullptr;
191
  }
192
  return static_cast<Environment*>(
193
7755454
      context->GetAlignedPointerFromEmbedderData(
194
7755454
          ContextEmbedderIndex::kEnvironment));
195
}
196
197
5860929
inline Environment* Environment::GetCurrent(
198
    const v8::FunctionCallbackInfo<v8::Value>& info) {
199
5860929
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
200
}
201
202
template <typename T>
203
2763154
inline Environment* Environment::GetCurrent(
204
    const v8::PropertyCallbackInfo<T>& info) {
205
2763154
  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
1001327
inline T* Environment::GetBindingData(
215
    const v8::FunctionCallbackInfo<v8::Value>& info) {
216
1001327
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
217
}
218
219
template <typename T>
220
1001329
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
221
  BindingDataStore* map = static_cast<BindingDataStore*>(
222
1001329
      context->GetAlignedPointerFromEmbedderData(
223
          ContextEmbedderIndex::kBindingListIndex));
224
  DCHECK_NOT_NULL(map);
225
1001329
  auto it = map->find(T::type_name);
226
1001329
  if (UNLIKELY(it == map->end())) return nullptr;
227
1001329
  T* result = static_cast<T*>(it->second.get());
228
  DCHECK_NOT_NULL(result);
229
  DCHECK_EQ(result->env(), GetCurrent(context));
230
1001329
  return result;
231
}
232
233
template <typename T>
234
27874
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
55748
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
240
  BindingDataStore* map = static_cast<BindingDataStore*>(
241
27874
      context->GetAlignedPointerFromEmbedderData(
242
          ContextEmbedderIndex::kBindingListIndex));
243
  DCHECK_NOT_NULL(map);
244
27874
  auto result = map->emplace(T::type_name, item);
245
27874
  CHECK(result.second);
246
  DCHECK_EQ(GetBindingData<T>(context), item.get());
247
27874
  return item.get();
248
}
249
250
30346507
inline v8::Isolate* Environment::isolate() const {
251
30346507
  return isolate_;
252
}
253
254
8239
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
255
8239
  return ContainerOf(&Environment::timer_handle_, handle);
256
}
257
258
33751
inline uv_timer_t* Environment::timer_handle() {
259
33751
  return &timer_handle_;
260
}
261
262
188675
inline Environment* Environment::from_immediate_check_handle(
263
    uv_check_t* handle) {
264
188675
  return ContainerOf(&Environment::immediate_check_handle_, handle);
265
}
266
267
26212
inline uv_check_t* Environment::immediate_check_handle() {
268
26212
  return &immediate_check_handle_;
269
}
270
271
237137
inline uv_idle_t* Environment::immediate_idle_handle() {
272
237137
  return &immediate_idle_handle_;
273
}
274
275
39318
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
276
                                               HandleCleanupCb cb,
277
                                               void* arg) {
278
39318
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
279
39318
}
280
281
template <typename T, typename OnCloseCallback>
282
41146
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
283
40986
  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
41066
  handle->data = new CloseData { this, callback, handle->data };
295
41386
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
296
81972
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
297
41066
    data->env->handle_cleanup_waiting_--;
298
41066
    handle->data = data->original_data;
299
41066
    data->callback(reinterpret_cast<T*>(handle));
300
  });
301
41066
}
302
303
90177
void Environment::IncreaseWaitingRequestCounter() {
304
90177
  request_waiting_++;
305
90177
}
306
307
90168
void Environment::DecreaseWaitingRequestCounter() {
308
90168
  request_waiting_--;
309
90168
  CHECK_GE(request_waiting_, 0);
310
90168
}
311
312
1269573
inline uv_loop_t* Environment::event_loop() const {
313
1269573
  return isolate_data()->event_loop();
314
}
315
316
#if HAVE_INSPECTOR
317
43704
inline bool Environment::is_in_inspector_console_call() const {
318
43704
  return is_in_inspector_console_call_;
319
}
320
321
87406
inline void Environment::set_is_in_inspector_console_call(bool value) {
322
87406
  is_in_inspector_console_call_ = value;
323
87406
}
324
#endif
325
326
5982507
inline AsyncHooks* Environment::async_hooks() {
327
5982507
  return &async_hooks_;
328
}
329
330
764634
inline ImmediateInfo* Environment::immediate_info() {
331
764634
  return &immediate_info_;
332
}
333
334
717329
inline TickInfo* Environment::tick_info() {
335
717329
  return &tick_info_;
336
}
337
338
105145
inline uint64_t Environment::timer_base() const {
339
105145
  return timer_base_;
340
}
341
342
1401430
inline std::shared_ptr<KVStore> Environment::env_vars() {
343
1401430
  return env_vars_;
344
}
345
346
7835
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
347
7835
  env_vars_ = env_vars;
348
7835
}
349
350
21
inline bool Environment::printed_error() const {
351
21
  return printed_error_;
352
}
353
354
21
inline void Environment::set_printed_error(bool value) {
355
21
  printed_error_ = value;
356
21
}
357
358
11451
inline void Environment::set_trace_sync_io(bool value) {
359
11451
  trace_sync_io_ = value;
360
11451
}
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
1253
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
1253
  options_->abort_on_uncaught_exception = value;
376
1253
}
377
378
1336
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
1336
  return should_abort_on_uncaught_toggle_;
380
}
381
382
306114
inline AliasedInt32Array& Environment::stream_base_state() {
383
306114
  return stream_base_state_;
384
}
385
386
55549
inline uint32_t Environment::get_next_module_id() {
387
55549
  return module_id_counter_++;
388
}
389
6418
inline uint32_t Environment::get_next_script_id() {
390
6418
  return script_id_counter_++;
391
}
392
35861
inline uint32_t Environment::get_next_function_id() {
393
35861
  return function_id_counter_++;
394
}
395
396
117009
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
117009
    Environment* env)
398
117009
    : env_(env) {
399
117009
  env_->PushShouldNotAbortOnUncaughtScope();
400
117009
}
401
402
234012
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
117006
  Close();
404
117006
}
405
406
117198
void ShouldNotAbortOnUncaughtScope::Close() {
407
117198
  if (env_ != nullptr) {
408
117006
    env_->PopShouldNotAbortOnUncaughtScope();
409
117006
    env_ = nullptr;
410
  }
411
117198
}
412
413
117009
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
117009
  should_not_abort_scope_counter_++;
415
117009
}
416
417
117006
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
117006
  should_not_abort_scope_counter_--;
419
117006
}
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
427092
inline std::vector<double>* Environment::destroy_async_id_list() {
426
427092
  return &destroy_async_id_list_;
427
}
428
429
218289
inline double Environment::new_async_id() {
430
218289
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
218289
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
248102
inline double Environment::execution_async_id() {
435
248102
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
65495
inline double Environment::trigger_async_id() {
439
65495
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
218286
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
218286
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
218286
  if (default_trigger_async_id < 0)
447
182537
    default_trigger_async_id = execution_async_id();
448
218286
  return default_trigger_async_id;
449
}
450
451
74787
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
74787
  return options_;
453
}
454
455
18126
inline const std::vector<std::string>& Environment::argv() {
456
18126
  return argv_;
457
}
458
459
7748
inline const std::vector<std::string>& Environment::exec_argv() {
460
7748
  return exec_argv_;
461
}
462
463
13106
inline const std::string& Environment::exec_path() const {
464
13106
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6548
inline void Environment::set_coverage_directory(const char* dir) {
469
6548
  coverage_directory_ = std::string(dir);
470
6548
}
471
472
6571
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6571
  CHECK_NULL(coverage_connection_);
475
6571
  std::swap(coverage_connection_, connection);
476
6571
}
477
478
19712
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
19712
  return coverage_connection_.get();
480
}
481
482
6532
inline const std::string& Environment::coverage_directory() const {
483
6532
  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
6587
Environment::cpu_profiler_connection() {
494
6587
  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
6575
Environment::heap_profiler_connection() {
529
6575
  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
13376
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
13376
  return inspector_host_port_;
561
}
562
563
67949
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
67949
  return options_;
565
}
566
567
291
inline void IsolateData::set_options(
568
    std::shared_ptr<PerIsolateOptions> options) {
569
291
  options_ = std::move(options);
570
291
}
571
572
template <typename Fn>
573
94723
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
189446
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
94723
  native_immediates_.Push(std::move(callback));
576
577
94723
  if (flags & CallbackFlags::kRefed) {
578
69850
    if (immediate_info()->ref_count() == 0)
579
50730
      ToggleImmediateRef(true);
580
69850
    immediate_info()->ref_count_inc(1);
581
  }
582
94723
}
583
584
template <typename Fn>
585
3830
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
7660
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
3830
      std::move(cb), flags);
588
  {
589
7660
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
3830
    native_immediates_threadsafe_.Push(std::move(callback));
591
3830
    if (task_queues_async_initialized_)
592
3821
      uv_async_send(&task_queues_async_);
593
  }
594
3830
}
595
596
template <typename Fn>
597
11982
void Environment::RequestInterrupt(Fn&& cb) {
598
23964
  auto callback = native_immediates_interrupts_.CreateCallback(
599
11982
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
23964
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
11982
    native_immediates_interrupts_.Push(std::move(callback));
603
11982
    if (task_queues_async_initialized_)
604
5291
      uv_async_send(&task_queues_async_);
605
  }
606
11982
  RequestInterruptFromV8();
607
11982
}
608
609
3090322
inline bool Environment::can_call_into_js() const {
610

3090322
  return can_call_into_js_ && !is_stopping();
611
}
612
613
2604
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
2604
  can_call_into_js_ = can_call_into_js;
615
2604
}
616
617
1545604
inline bool Environment::has_run_bootstrapping_code() const {
618
1545604
  return has_run_bootstrapping_code_;
619
}
620
621
6586
inline void Environment::DoneBootstrapping() {
622
6586
  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
6586
  base_object_created_by_bootstrap_ = base_object_count_;
627
6586
}
628
629
19
inline bool Environment::has_serialized_options() const {
630
19
  return has_serialized_options_;
631
}
632
633
7138
inline void Environment::set_has_serialized_options(bool value) {
634
7138
  has_serialized_options_ = value;
635
7138
}
636
637
12539
inline bool Environment::is_main_thread() const {
638
12539
  return worker_context() == nullptr;
639
}
640
641
1661
inline bool Environment::no_native_addons() const {
642
3318
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
643
3318
          !options_->allow_native_addons;
644
}
645
646
6551
inline bool Environment::should_not_register_esm_loader() const {
647
6551
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
648
}
649
650
18942
inline bool Environment::owns_process_state() const {
651
18942
  return flags_ & EnvironmentFlags::kOwnsProcessState;
652
}
653
654
6580
inline bool Environment::owns_inspector() const {
655
6580
  return flags_ & EnvironmentFlags::kOwnsInspector;
656
}
657
658
19690
inline bool Environment::should_create_inspector() const {
659
19690
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
660
}
661
662
135166
inline bool Environment::tracks_unmanaged_fds() const {
663
135166
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
664
}
665
666
4290
inline bool Environment::hide_console_windows() const {
667
4290
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
668
}
669
670
8035
inline bool Environment::no_global_search_paths() const {
671
16070
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
672
16070
         !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
6392
void Environment::set_source_maps_enabled(bool on) {
693
6392
  source_maps_enabled_ = on;
694
6392
}
695
696
2122
bool Environment::source_maps_enabled() const {
697
2122
  return source_maps_enabled_;
698
}
699
700
9174
inline uint64_t Environment::thread_id() const {
701
9174
  return thread_id_;
702
}
703
704
20330
inline worker::Worker* Environment::worker_context() const {
705
20330
  return isolate_data()->worker_context();
706
}
707
708
1481
inline void Environment::add_sub_worker_context(worker::Worker* context) {
709
1481
  sub_worker_contexts_.insert(context);
710
1481
}
711
712
1509
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
713
1509
  sub_worker_contexts_.erase(context);
714
1509
}
715
716
template <typename Fn>
717
25
inline void Environment::ForEachWorker(Fn&& iterator) {
718
27
  for (worker::Worker* w : sub_worker_contexts_) iterator(w);
719
25
}
720
721
4630483
inline bool Environment::is_stopping() const {
722
4630483
  return is_stopping_.load();
723
}
724
725
6899
inline void Environment::set_stopping(bool value) {
726
6899
  is_stopping_.store(value);
727
6899
}
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
44613
inline performance::PerformanceState* Environment::performance_state() {
748
44613
  return performance_state_.get();
749
}
750
751
9160268
inline IsolateData* Environment::isolate_data() const {
752
9160268
  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
1502774
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
792
3005548
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
793
1502774
    fn, arg, cleanup_hook_counter_++
794
1502774
  });
795
  // Make sure there was no existing element with these values.
796
1502774
  CHECK_EQ(insertion_info.second, true);
797
1502774
}
798
799
1476159
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
800
1476159
  CleanupHookCallback search { fn, arg, 0 };
801
1476159
  cleanup_hooks_.erase(search);
802
1476159
}
803
804
3347009
size_t CleanupHookCallback::Hash::operator()(
805
    const CleanupHookCallback& cb) const {
806
3347009
  return std::hash<void*>()(cb.arg_);
807
}
808
809
1673306
bool CleanupHookCallback::Equal::operator()(
810
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
811

1673306
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
812
}
813
814
615
BaseObject* CleanupHookCallback::GetBaseObject() const {
815
615
  if (fn_ == BaseObject::DeleteMe)
816
587
    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
1278
  for (const auto& hook : cleanup_hooks_) {
824
1230
    BaseObject* obj = hook.GetBaseObject();
825
1230
    if (obj != nullptr)
826
1174
      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
2895920
void Environment::modify_base_object_count(int64_t delta) {
842
2895920
  base_object_count_ += delta;
843
2895920
}
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
1252
void Environment::set_process_exit_handler(
859
    std::function<void(Environment*, int)>&& handler) {
860
1252
  process_exit_handler_ = std::move(handler);
861
1252
}
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
60196
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
872
983842
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
873
8470607
  PER_ISOLATE_STRING_PROPERTIES(VS)
874
#undef V
875
#undef VS
876
#undef VY
877
#undef VP
878
879
#define V(PropertyName, TypeName)                                              \
880
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
881
    return PropertyName##_.Get(isolate_);                                      \
882
  }                                                                            \
883
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
884
    PropertyName##_.Set(isolate_, value);                                      \
885
  }
886
1376010
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
887
#undef V
888
889
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
890
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
891
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
892
#define V(TypeName, PropertyName)                                             \
893
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
894
    return isolate_data()->PropertyName();                                    \
895
  }
896
60196
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
897
858765
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
898
12225367
  PER_ISOLATE_STRING_PROPERTIES(VS)
899
#undef V
900
#undef VS
901
#undef VY
902
#undef VP
903
904
#define V(PropertyName, TypeName)                                              \
905
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
906
    return isolate_data()->PropertyName();                                     \
907
  }                                                                            \
908
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
909
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
910
    isolate_data()->set_##PropertyName(value);                                 \
911
  }
912
1059359
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
913
#undef V
914
915
#define V(PropertyName, TypeName)                                              \
916
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
917
    return PersistentToLocal::Strong(PropertyName##_);                         \
918
  }                                                                            \
919
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
920
    PropertyName##_.Reset(isolate(), value);                                   \
921
  }
922
10594971
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
923
#undef V
924
925
9467743
v8::Local<v8::Context> Environment::context() const {
926
9467743
  return PersistentToLocal::Strong(context_);
927
}
928
929
}  // namespace node
930
931
// These two files depend on each other. Including base_object-inl.h after this
932
// file is the easiest way to avoid issues with that circular dependency.
933
#include "base_object-inl.h"
934
935
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
936
937
#endif  // SRC_ENV_INL_H_