GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 451 453 99.6 %
Date: 2022-08-28 04:20:35 Branches: 57 72 79.2 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_ENV_INL_H_
23
#define SRC_ENV_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "aliased_buffer.h"
28
#include "callback_queue-inl.h"
29
#include "env.h"
30
#include "node.h"
31
#include "node_context_data.h"
32
#include "node_internals.h"
33
#include "node_perf_common.h"
34
#include "util-inl.h"
35
#include "uv.h"
36
#include "v8.h"
37
38
#include <cstddef>
39
#include <cstdint>
40
41
#include <utility>
42
43
namespace node {
44
45
116332
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116332
    IsolateData* isolate_data)
47
116332
    : node_allocator_(isolate_data->node_allocator()) {
48
116332
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116332
}
50
51
232664
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116332
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116332
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1281837
inline uv_loop_t* IsolateData::event_loop() const {
60
1281837
  return event_loop_;
61
}
62
63
123028
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
123028
  return node_allocator_;
65
}
66
67
85222
inline MultiIsolatePlatform* IsolateData::platform() const {
68
85222
  return platform_;
69
}
70
71
727
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
727
  CHECK_NULL(worker_context_);  // Should be set only once.
73
727
  worker_context_ = context;
74
727
}
75
76
16221
inline worker::Worker* IsolateData::worker_context() const {
77
16221
  return worker_context_;
78
}
79
80
213381
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
213381
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2684560
inline AliasedUint32Array& AsyncHooks::fields() {
85
2684560
  return fields_;
86
}
87
88
1670996
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1670996
  return async_id_fields_;
90
}
91
92
775
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
775
  return async_ids_stack_;
94
}
95
96
842472
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
842472
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
775
    js_execution_async_resources_.Reset(
99
1550
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
842472
  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
213033
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
213033
  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
424537
inline Environment* AsyncHooks::env() {
118
424537
  return Environment::ForAsyncHooks(this);
119
}
120
121
424537
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
424537
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
813818
inline size_t Environment::async_callback_scope_depth() const {
126
813818
  return async_callback_scope_depth_;
127
}
128
129
827957
inline void Environment::PushAsyncCallbackScope() {
130
827957
  async_callback_scope_depth_++;
131
827957
}
132
133
827402
inline void Environment::PopAsyncCallbackScope() {
134
827402
  async_callback_scope_depth_--;
135
827402
}
136
137
775
inline AliasedUint32Array& ImmediateInfo::fields() {
138
775
  return fields_;
139
}
140
141
200636
inline uint32_t ImmediateInfo::count() const {
142
200636
  return fields_[kCount];
143
}
144
145
294260
inline uint32_t ImmediateInfo::ref_count() const {
146
294260
  return fields_[kRefCount];
147
}
148
149
43249
inline bool ImmediateInfo::has_outstanding() const {
150
43249
  return fields_[kHasOutstanding] == 1;
151
}
152
153
33839
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
33839
  fields_[kRefCount] += increment;
155
33839
}
156
157
218123
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
218123
  fields_[kRefCount] -= decrement;
159
218123
}
160
161
775
inline AliasedUint8Array& TickInfo::fields() {
162
775
  return fields_;
163
}
164
165
1480651
inline bool TickInfo::has_tick_scheduled() const {
166
1480651
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
535146
inline bool TickInfo::has_rejection_to_warn() const {
170
535146
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
77840
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
77840
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
152364
  v8::HandleScope handle_scope(isolate);
176
76182
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7732157
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7732157
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
181
12
    return nullptr;
182
  }
183
  return static_cast<Environment*>(
184
7732145
      context->GetAlignedPointerFromEmbedderData(
185
7732145
          ContextEmbedderIndex::kEnvironment));
186
}
187
188
5872593
inline Environment* Environment::GetCurrent(
189
    const v8::FunctionCallbackInfo<v8::Value>& info) {
190
5872593
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
191
}
192
193
template <typename T>
194
2151070
inline Environment* Environment::GetCurrent(
195
    const v8::PropertyCallbackInfo<T>& info) {
196
2151070
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
197
}
198
199
template <typename T, typename U>
200
inline T* Environment::GetBindingData(const v8::PropertyCallbackInfo<U>& info) {
201
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
202
}
203
204
template <typename T>
205
1004919
inline T* Environment::GetBindingData(
206
    const v8::FunctionCallbackInfo<v8::Value>& info) {
207
1004919
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
208
}
209
210
template <typename T>
211
1004921
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
212
  BindingDataStore* map = static_cast<BindingDataStore*>(
213
1004921
      context->GetAlignedPointerFromEmbedderData(
214
          ContextEmbedderIndex::kBindingListIndex));
215
  DCHECK_NOT_NULL(map);
216
1004921
  auto it = map->find(T::type_name);
217
1004921
  if (UNLIKELY(it == map->end())) return nullptr;
218
1004921
  T* result = static_cast<T*>(it->second.get());
219
  DCHECK_NOT_NULL(result);
220
  DCHECK_EQ(result->env(), GetCurrent(context));
221
1004921
  return result;
222
}
223
224
template <typename T>
225
25775
inline T* Environment::AddBindingData(
226
    v8::Local<v8::Context> context,
227
    v8::Local<v8::Object> target) {
228
  DCHECK_EQ(GetCurrent(context), this);
229
  // This won't compile if T is not a BaseObject subclass.
230
51550
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
231
  BindingDataStore* map = static_cast<BindingDataStore*>(
232
25775
      context->GetAlignedPointerFromEmbedderData(
233
          ContextEmbedderIndex::kBindingListIndex));
234
  DCHECK_NOT_NULL(map);
235
25775
  auto result = map->emplace(T::type_name, item);
236
25775
  CHECK(result.second);
237
  DCHECK_EQ(GetBindingData<T>(context), item.get());
238
25775
  return item.get();
239
}
240
241
26166729
inline v8::Isolate* Environment::isolate() const {
242
26166729
  return isolate_;
243
}
244
245
7340
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
246
7340
  return ContainerOf(&Environment::timer_handle_, handle);
247
}
248
249
31424
inline uv_timer_t* Environment::timer_handle() {
250
31424
  return &timer_handle_;
251
}
252
253
200636
inline Environment* Environment::from_immediate_check_handle(
254
    uv_check_t* handle) {
255
200636
  return ContainerOf(&Environment::immediate_check_handle_, handle);
256
}
257
258
24448
inline uv_check_t* Environment::immediate_check_handle() {
259
24448
  return &immediate_check_handle_;
260
}
261
262
246820
inline uv_idle_t* Environment::immediate_idle_handle() {
263
246820
  return &immediate_idle_handle_;
264
}
265
266
36672
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
267
                                               HandleCleanupCb cb,
268
                                               void* arg) {
269
36672
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
270
36672
}
271
272
template <typename T, typename OnCloseCallback>
273
38391
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
274
38231
  handle_cleanup_waiting_++;
275
  static_assert(sizeof(T) >= sizeof(uv_handle_t), "T is a libuv handle");
276
  static_assert(offsetof(T, data) == offsetof(uv_handle_t, data),
277
                "T is a libuv handle");
278
  static_assert(offsetof(T, close_cb) == offsetof(uv_handle_t, close_cb),
279
                "T is a libuv handle");
280
  struct CloseData {
281
    Environment* env;
282
    OnCloseCallback callback;
283
    void* original_data;
284
  };
285
38311
  handle->data = new CloseData { this, callback, handle->data };
286
38631
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
287
76462
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
288
38311
    data->env->handle_cleanup_waiting_--;
289
38311
    handle->data = data->original_data;
290
38311
    data->callback(reinterpret_cast<T*>(handle));
291
  });
292
38311
}
293
294
93816
void Environment::IncreaseWaitingRequestCounter() {
295
93816
  request_waiting_++;
296
93816
}
297
298
93807
void Environment::DecreaseWaitingRequestCounter() {
299
93807
  request_waiting_--;
300
93807
  CHECK_GE(request_waiting_, 0);
301
93807
}
302
303
1275691
inline uv_loop_t* Environment::event_loop() const {
304
1275691
  return isolate_data()->event_loop();
305
}
306
307
#if HAVE_INSPECTOR
308
70895
inline bool Environment::is_in_inspector_console_call() const {
309
70895
  return is_in_inspector_console_call_;
310
}
311
312
141788
inline void Environment::set_is_in_inspector_console_call(bool value) {
313
141788
  is_in_inspector_console_call_ = value;
314
141788
}
315
#endif
316
317
6031029
inline AsyncHooks* Environment::async_hooks() {
318
6031029
  return &async_hooks_;
319
}
320
321
790882
inline ImmediateInfo* Environment::immediate_info() {
322
790882
  return &immediate_info_;
323
}
324
325
741143
inline TickInfo* Environment::tick_info() {
326
741143
  return &tick_info_;
327
}
328
329
100248
inline uint64_t Environment::timer_base() const {
330
100248
  return timer_base_;
331
}
332
333
1396211
inline std::shared_ptr<KVStore> Environment::env_vars() {
334
1396211
  return env_vars_;
335
}
336
337
6860
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
338
6860
  env_vars_ = env_vars;
339
6860
}
340
341
22
inline bool Environment::printed_error() const {
342
22
  return printed_error_;
343
}
344
345
22
inline void Environment::set_printed_error(bool value) {
346
22
  printed_error_ = value;
347
22
}
348
349
11003
inline void Environment::set_trace_sync_io(bool value) {
350
11003
  trace_sync_io_ = value;
351
11003
}
352
353
124
inline bool Environment::abort_on_uncaught_exception() const {
354
124
  return options_->abort_on_uncaught_exception;
355
}
356
357
inline void Environment::set_force_context_aware(bool value) {
358
  options_->force_context_aware = value;
359
}
360
361
42
inline bool Environment::force_context_aware() const {
362
42
  return options_->force_context_aware;
363
}
364
365
11366
inline void Environment::set_exiting(bool value) {
366
11366
  exiting_[0] = value ? 1 : 0;
367
11366
}
368
369
775
inline AliasedUint32Array& Environment::exiting() {
370
775
  return exiting_;
371
}
372
373
719
inline void Environment::set_abort_on_uncaught_exception(bool value) {
374
719
  options_->abort_on_uncaught_exception = value;
375
719
}
376
377
805
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
378
805
  return should_abort_on_uncaught_toggle_;
379
}
380
381
306955
inline AliasedInt32Array& Environment::stream_base_state() {
382
306955
  return stream_base_state_;
383
}
384
385
51238
inline uint32_t Environment::get_next_module_id() {
386
51238
  return module_id_counter_++;
387
}
388
5559
inline uint32_t Environment::get_next_script_id() {
389
5559
  return script_id_counter_++;
390
}
391
36611
inline uint32_t Environment::get_next_function_id() {
392
36611
  return function_id_counter_++;
393
}
394
395
107505
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
396
107505
    Environment* env)
397
107505
    : env_(env) {
398
107505
  env_->PushShouldNotAbortOnUncaughtScope();
399
107505
}
400
401
215004
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
402
107502
  Close();
403
107502
}
404
405
107694
void ShouldNotAbortOnUncaughtScope::Close() {
406
107694
  if (env_ != nullptr) {
407
107502
    env_->PopShouldNotAbortOnUncaughtScope();
408
107502
    env_ = nullptr;
409
  }
410
107694
}
411
412
107505
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
413
107505
  should_not_abort_scope_counter_++;
414
107505
}
415
416
107502
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
417
107502
  should_not_abort_scope_counter_--;
418
107502
}
419
420
1
inline bool Environment::inside_should_not_abort_on_uncaught_scope() const {
421
1
  return should_not_abort_scope_counter_ > 0;
422
}
423
424
434662
inline std::vector<double>* Environment::destroy_async_id_list() {
425
434662
  return &destroy_async_id_list_;
426
}
427
428
213588
inline double Environment::new_async_id() {
429
213588
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
430
213588
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
431
}
432
433
245587
inline double Environment::execution_async_id() {
434
245587
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
435
}
436
437
66061
inline double Environment::trigger_async_id() {
438
66061
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
439
}
440
441
213585
inline double Environment::get_default_trigger_async_id() {
442
  double default_trigger_async_id =
443
213585
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
444
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
445
213585
  if (default_trigger_async_id < 0)
446
179458
    default_trigger_async_id = execution_async_id();
447
213585
  return default_trigger_async_id;
448
}
449
450
73330
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
451
73330
  return options_;
452
}
453
454
17335
inline const std::vector<std::string>& Environment::argv() {
455
17335
  return argv_;
456
}
457
458
6785
inline const std::vector<std::string>& Environment::exec_argv() {
459
6785
  return exec_argv_;
460
}
461
462
12224
inline const std::string& Environment::exec_path() const {
463
12224
  return exec_path_;
464
}
465
466
#if HAVE_INSPECTOR
467
6107
inline void Environment::set_coverage_directory(const char* dir) {
468
6107
  coverage_directory_ = std::string(dir);
469
6107
}
470
471
6131
inline void Environment::set_coverage_connection(
472
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
473
6131
  CHECK_NULL(coverage_connection_);
474
6131
  std::swap(coverage_connection_, connection);
475
6131
}
476
477
18392
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
478
18392
  return coverage_connection_.get();
479
}
480
481
6091
inline const std::string& Environment::coverage_directory() const {
482
6091
  return coverage_directory_;
483
}
484
485
12
inline void Environment::set_cpu_profiler_connection(
486
    std::unique_ptr<profiler::V8CpuProfilerConnection> connection) {
487
12
  CHECK_NULL(cpu_profiler_connection_);
488
12
  std::swap(cpu_profiler_connection_, connection);
489
12
}
490
491
inline profiler::V8CpuProfilerConnection*
492
6147
Environment::cpu_profiler_connection() {
493
6147
  return cpu_profiler_connection_.get();
494
}
495
496
12
inline void Environment::set_cpu_prof_interval(uint64_t interval) {
497
12
  cpu_prof_interval_ = interval;
498
12
}
499
500
12
inline uint64_t Environment::cpu_prof_interval() const {
501
12
  return cpu_prof_interval_;
502
}
503
504
12
inline void Environment::set_cpu_prof_name(const std::string& name) {
505
12
  cpu_prof_name_ = name;
506
12
}
507
508
12
inline const std::string& Environment::cpu_prof_name() const {
509
12
  return cpu_prof_name_;
510
}
511
512
12
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
513
12
  cpu_prof_dir_ = dir;
514
12
}
515
516
12
inline const std::string& Environment::cpu_prof_dir() const {
517
12
  return cpu_prof_dir_;
518
}
519
520
12
inline void Environment::set_heap_profiler_connection(
521
    std::unique_ptr<profiler::V8HeapProfilerConnection> connection) {
522
12
  CHECK_NULL(heap_profiler_connection_);
523
12
  std::swap(heap_profiler_connection_, connection);
524
12
}
525
526
inline profiler::V8HeapProfilerConnection*
527
6135
Environment::heap_profiler_connection() {
528
6135
  return heap_profiler_connection_.get();
529
}
530
531
12
inline void Environment::set_heap_prof_name(const std::string& name) {
532
12
  heap_prof_name_ = name;
533
12
}
534
535
12
inline const std::string& Environment::heap_prof_name() const {
536
12
  return heap_prof_name_;
537
}
538
539
12
inline void Environment::set_heap_prof_dir(const std::string& dir) {
540
12
  heap_prof_dir_ = dir;
541
12
}
542
543
12
inline const std::string& Environment::heap_prof_dir() const {
544
12
  return heap_prof_dir_;
545
}
546
547
12
inline void Environment::set_heap_prof_interval(uint64_t interval) {
548
12
  heap_prof_interval_ = interval;
549
12
}
550
551
12
inline uint64_t Environment::heap_prof_interval() const {
552
12
  return heap_prof_interval_;
553
}
554
555
#endif  // HAVE_INSPECTOR
556
557
inline
558
12504
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
559
12504
  return inspector_host_port_;
560
}
561
562
65691
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
563
65691
  return options_;
564
}
565
566
288
inline void IsolateData::set_options(
567
    std::shared_ptr<PerIsolateOptions> options) {
568
288
  options_ = std::move(options);
569
288
}
570
571
template <typename Fn>
572
92681
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
573
185362
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
574
92681
  native_immediates_.Push(std::move(callback));
575
576
92681
  if (flags & CallbackFlags::kRefed) {
577
67664
    if (immediate_info()->ref_count() == 0)
578
50414
      ToggleImmediateRef(true);
579
67664
    immediate_info()->ref_count_inc(1);
580
  }
581
92681
}
582
583
template <typename Fn>
584
2308
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
585
4616
  auto callback = native_immediates_threadsafe_.CreateCallback(
586
2308
      std::move(cb), flags);
587
  {
588
4616
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
589
2308
    native_immediates_threadsafe_.Push(std::move(callback));
590
2308
    if (task_queues_async_initialized_)
591
2299
      uv_async_send(&task_queues_async_);
592
  }
593
2308
}
594
595
template <typename Fn>
596
10537
void Environment::RequestInterrupt(Fn&& cb) {
597
21074
  auto callback = native_immediates_interrupts_.CreateCallback(
598
10537
      std::move(cb), CallbackFlags::kRefed);
599
  {
600
21074
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
601
10537
    native_immediates_interrupts_.Push(std::move(callback));
602
10537
    if (task_queues_async_initialized_)
603
4286
      uv_async_send(&task_queues_async_);
604
  }
605
10537
  RequestInterruptFromV8();
606
10537
}
607
608
3209019
inline bool Environment::can_call_into_js() const {
609

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

1659110
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
811
}
812
813
639
BaseObject* CleanupHookCallback::GetBaseObject() const {
814
639
  if (fn_ == BaseObject::DeleteMe)
815
611
    return static_cast<BaseObject*>(arg_);
816
  else
817
28
    return nullptr;
818
}
819
820
template <typename T>
821
54
void Environment::ForEachBaseObject(T&& iterator) {
822
1308
  for (const auto& hook : cleanup_hooks_) {
823
1254
    BaseObject* obj = hook.GetBaseObject();
824
1254
    if (obj != nullptr)
825
1198
      iterator(obj);
826
  }
827
54
}
828
829
2874473
void Environment::modify_base_object_count(int64_t delta) {
830
2874473
  base_object_count_ += delta;
831
2874473
}
832
833
14
int64_t Environment::base_object_created_after_bootstrap() const {
834
14
  return base_object_count_ - base_object_created_by_bootstrap_;
835
}
836
837
2
int64_t Environment::base_object_count() const {
838
2
  return base_object_count_;
839
}
840
841
19
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
842
19
  CHECK(!main_utf16_);
843
19
  main_utf16_ = std::move(str);
844
19
}
845
846
717
void Environment::set_process_exit_handler(
847
    std::function<void(Environment*, int)>&& handler) {
848
717
  process_exit_handler_ = std::move(handler);
849
717
}
850
851
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
852
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
853
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
854
#define V(TypeName, PropertyName)                                             \
855
  inline                                                                      \
856
  v8::Local<TypeName> IsolateData::PropertyName() const {                     \
857
    return PropertyName ## _ .Get(isolate_);                                  \
858
  }
859
60639
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
860
1176002
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
861
8138041
  PER_ISOLATE_STRING_PROPERTIES(VS)
862
#undef V
863
#undef VS
864
#undef VY
865
#undef VP
866
867
#define V(PropertyName, TypeName)                                              \
868
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
869
    return PropertyName##_.Get(isolate_);                                      \
870
  }                                                                            \
871
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
872
    PropertyName##_.Set(isolate_, value);                                      \
873
  }
874
1197826
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
875
#undef V
876
877
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
878
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
879
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
880
#define V(TypeName, PropertyName)                                             \
881
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
882
    return isolate_data()->PropertyName();                                    \
883
  }
884
60639
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
885
1056927
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
886
11760711
  PER_ISOLATE_STRING_PROPERTIES(VS)
887
#undef V
888
#undef VS
889
#undef VY
890
#undef VP
891
892
#define V(PropertyName, TypeName)                                              \
893
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
894
    return isolate_data()->PropertyName();                                     \
895
  }                                                                            \
896
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
897
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
898
    isolate_data()->set_##PropertyName(value);                                 \
899
  }
900
889351
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
901
#undef V
902
903
#define V(PropertyName, TypeName)                                              \
904
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
905
    return PersistentToLocal::Strong(PropertyName##_);                         \
906
  }                                                                            \
907
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
908
    PropertyName##_.Reset(isolate(), value);                                   \
909
  }
910
10698861
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
911
#undef V
912
913
9133699
v8::Local<v8::Context> Environment::context() const {
914
9133699
  return PersistentToLocal::Strong(context_);
915
}
916
917
}  // namespace node
918
919
// These two files depend on each other. Including base_object-inl.h after this
920
// file is the easiest way to avoid issues with that circular dependency.
921
#include "base_object-inl.h"
922
923
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
924
925
#endif  // SRC_ENV_INL_H_