GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 451 453 99.6 %
Date: 2022-08-29 04:21:03 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
116900
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116900
    IsolateData* isolate_data)
47
116900
    : node_allocator_(isolate_data->node_allocator()) {
48
116900
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116900
}
50
51
233800
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116900
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116900
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1290539
inline uv_loop_t* IsolateData::event_loop() const {
60
1290539
  return event_loop_;
61
}
62
63
123639
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
123639
  return node_allocator_;
65
}
66
67
85235
inline MultiIsolatePlatform* IsolateData::platform() const {
68
85235
  return platform_;
69
}
70
71
733
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
733
  CHECK_NULL(worker_context_);  // Should be set only once.
73
733
  worker_context_ = context;
74
733
}
75
76
16257
inline worker::Worker* IsolateData::worker_context() const {
77
16257
  return worker_context_;
78
}
79
80
213089
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
213089
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2680760
inline AliasedUint32Array& AsyncHooks::fields() {
85
2680760
  return fields_;
86
}
87
88
1663987
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1663987
  return async_id_fields_;
90
}
91
92
781
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
781
  return async_ids_stack_;
94
}
95
96
843972
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
843972
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
781
    js_execution_async_resources_.Reset(
99
1562
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
843972
  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
212741
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
212741
  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
421375
inline Environment* AsyncHooks::env() {
118
421375
  return Environment::ForAsyncHooks(this);
119
}
120
121
421375
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
421375
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
815387
inline size_t Environment::async_callback_scope_depth() const {
126
815387
  return async_callback_scope_depth_;
127
}
128
129
829387
inline void Environment::PushAsyncCallbackScope() {
130
829387
  async_callback_scope_depth_++;
131
829387
}
132
133
828743
inline void Environment::PopAsyncCallbackScope() {
134
828743
  async_callback_scope_depth_--;
135
828743
}
136
137
781
inline AliasedUint32Array& ImmediateInfo::fields() {
138
781
  return fields_;
139
}
140
141
202048
inline uint32_t ImmediateInfo::count() const {
142
202048
  return fields_[kCount];
143
}
144
145
294634
inline uint32_t ImmediateInfo::ref_count() const {
146
294634
  return fields_[kRefCount];
147
}
148
149
42513
inline bool ImmediateInfo::has_outstanding() const {
150
42513
  return fields_[kHasOutstanding] == 1;
151
}
152
153
33635
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
33635
  fields_[kRefCount] += increment;
155
33635
}
156
157
219438
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
219438
  fields_[kRefCount] -= decrement;
159
219438
}
160
161
781
inline AliasedUint8Array& TickInfo::fields() {
162
781
  return fields_;
163
}
164
165
1485113
inline bool TickInfo::has_tick_scheduled() const {
166
1485113
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
537638
inline bool TickInfo::has_rejection_to_warn() const {
170
537638
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
78150
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
78150
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
152898
  v8::HandleScope handle_scope(isolate);
176
76449
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7746317
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7746317
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
181
12
    return nullptr;
182
  }
183
  return static_cast<Environment*>(
184
7746305
      context->GetAlignedPointerFromEmbedderData(
185
7746305
          ContextEmbedderIndex::kEnvironment));
186
}
187
188
5878784
inline Environment* Environment::GetCurrent(
189
    const v8::FunctionCallbackInfo<v8::Value>& info) {
190
5878784
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
191
}
192
193
template <typename T>
194
2163297
inline Environment* Environment::GetCurrent(
195
    const v8::PropertyCallbackInfo<T>& info) {
196
2163297
  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
1011972
inline T* Environment::GetBindingData(
206
    const v8::FunctionCallbackInfo<v8::Value>& info) {
207
1011972
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
208
}
209
210
template <typename T>
211
1011974
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
212
  BindingDataStore* map = static_cast<BindingDataStore*>(
213
1011974
      context->GetAlignedPointerFromEmbedderData(
214
          ContextEmbedderIndex::kBindingListIndex));
215
  DCHECK_NOT_NULL(map);
216
1011974
  auto it = map->find(T::type_name);
217
1011974
  if (UNLIKELY(it == map->end())) return nullptr;
218
1011974
  T* result = static_cast<T*>(it->second.get());
219
  DCHECK_NOT_NULL(result);
220
  DCHECK_EQ(result->env(), GetCurrent(context));
221
1011974
  return result;
222
}
223
224
template <typename T>
225
25949
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
51898
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
231
  BindingDataStore* map = static_cast<BindingDataStore*>(
232
25949
      context->GetAlignedPointerFromEmbedderData(
233
          ContextEmbedderIndex::kBindingListIndex));
234
  DCHECK_NOT_NULL(map);
235
25949
  auto result = map->emplace(T::type_name, item);
236
25949
  CHECK(result.second);
237
  DCHECK_EQ(GetBindingData<T>(context), item.get());
238
25949
  return item.get();
239
}
240
241
26213124
inline v8::Isolate* Environment::isolate() const {
242
26213124
  return isolate_;
243
}
244
245
8017
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
246
8017
  return ContainerOf(&Environment::timer_handle_, handle);
247
}
248
249
32239
inline uv_timer_t* Environment::timer_handle() {
250
32239
  return &timer_handle_;
251
}
252
253
202048
inline Environment* Environment::from_immediate_check_handle(
254
    uv_check_t* handle) {
255
202048
  return ContainerOf(&Environment::immediate_check_handle_, handle);
256
}
257
258
24624
inline uv_check_t* Environment::immediate_check_handle() {
259
24624
  return &immediate_check_handle_;
260
}
261
262
248208
inline uv_idle_t* Environment::immediate_idle_handle() {
263
248208
  return &immediate_idle_handle_;
264
}
265
266
36936
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
267
                                               HandleCleanupCb cb,
268
                                               void* arg) {
269
36936
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
270
36936
}
271
272
template <typename T, typename OnCloseCallback>
273
38069
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
274
37909
  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
37989
  handle->data = new CloseData { this, callback, handle->data };
286
38309
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
287
75818
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
288
37989
    data->env->handle_cleanup_waiting_--;
289
37989
    handle->data = data->original_data;
290
37989
    data->callback(reinterpret_cast<T*>(handle));
291
  });
292
37989
}
293
294
93926
void Environment::IncreaseWaitingRequestCounter() {
295
93926
  request_waiting_++;
296
93926
}
297
298
93917
void Environment::DecreaseWaitingRequestCounter() {
299
93917
  request_waiting_--;
300
93917
  CHECK_GE(request_waiting_, 0);
301
93917
}
302
303
1284350
inline uv_loop_t* Environment::event_loop() const {
304
1284350
  return isolate_data()->event_loop();
305
}
306
307
#if HAVE_INSPECTOR
308
70910
inline bool Environment::is_in_inspector_console_call() const {
309
70910
  return is_in_inspector_console_call_;
310
}
311
312
141818
inline void Environment::set_is_in_inspector_console_call(bool value) {
313
141818
  is_in_inspector_console_call_ = value;
314
141818
}
315
#endif
316
317
6027150
inline AsyncHooks* Environment::async_hooks() {
318
6027150
  return &async_hooks_;
319
}
320
321
793049
inline ImmediateInfo* Environment::immediate_info() {
322
793049
  return &immediate_info_;
323
}
324
325
743379
inline TickInfo* Environment::tick_info() {
326
743379
  return &tick_info_;
327
}
328
329
102468
inline uint64_t Environment::timer_base() const {
330
102468
  return timer_base_;
331
}
332
333
1403955
inline std::shared_ptr<KVStore> Environment::env_vars() {
334
1403955
  return env_vars_;
335
}
336
337
6910
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
338
6910
  env_vars_ = env_vars;
339
6910
}
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
10986
inline void Environment::set_trace_sync_io(bool value) {
350
10986
  trace_sync_io_ = value;
351
10986
}
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
11348
inline void Environment::set_exiting(bool value) {
366
11348
  exiting_[0] = value ? 1 : 0;
367
11348
}
368
369
781
inline AliasedUint32Array& Environment::exiting() {
370
781
  return exiting_;
371
}
372
373
725
inline void Environment::set_abort_on_uncaught_exception(bool value) {
374
725
  options_->abort_on_uncaught_exception = value;
375
725
}
376
377
811
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
378
811
  return should_abort_on_uncaught_toggle_;
379
}
380
381
305261
inline AliasedInt32Array& Environment::stream_base_state() {
382
305261
  return stream_base_state_;
383
}
384
385
51379
inline uint32_t Environment::get_next_module_id() {
386
51379
  return module_id_counter_++;
387
}
388
6127
inline uint32_t Environment::get_next_script_id() {
389
6127
  return script_id_counter_++;
390
}
391
36667
inline uint32_t Environment::get_next_function_id() {
392
36667
  return function_id_counter_++;
393
}
394
395
108355
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
396
108355
    Environment* env)
397
108355
    : env_(env) {
398
108355
  env_->PushShouldNotAbortOnUncaughtScope();
399
108355
}
400
401
216704
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
402
108352
  Close();
403
108352
}
404
405
108544
void ShouldNotAbortOnUncaughtScope::Close() {
406
108544
  if (env_ != nullptr) {
407
108352
    env_->PopShouldNotAbortOnUncaughtScope();
408
108352
    env_ = nullptr;
409
  }
410
108544
}
411
412
108355
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
413
108355
  should_not_abort_scope_counter_++;
414
108355
}
415
416
108352
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
417
108352
  should_not_abort_scope_counter_--;
418
108352
}
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
424605
inline std::vector<double>* Environment::destroy_async_id_list() {
425
424605
  return &destroy_async_id_list_;
426
}
427
428
213296
inline double Environment::new_async_id() {
429
213296
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
430
213296
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
431
}
432
433
245281
inline double Environment::execution_async_id() {
434
245281
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
435
}
436
437
65778
inline double Environment::trigger_async_id() {
438
65778
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
439
}
440
441
213293
inline double Environment::get_default_trigger_async_id() {
442
  double default_trigger_async_id =
443
213293
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
444
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
445
213293
  if (default_trigger_async_id < 0)
446
179435
    default_trigger_async_id = execution_async_id();
447
213293
  return default_trigger_async_id;
448
}
449
450
73688
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
451
73688
  return options_;
452
}
453
454
17422
inline const std::vector<std::string>& Environment::argv() {
455
17422
  return argv_;
456
}
457
458
6829
inline const std::vector<std::string>& Environment::exec_argv() {
459
6829
  return exec_argv_;
460
}
461
462
12312
inline const std::string& Environment::exec_path() const {
463
12312
  return exec_path_;
464
}
465
466
#if HAVE_INSPECTOR
467
6151
inline void Environment::set_coverage_directory(const char* dir) {
468
6151
  coverage_directory_ = std::string(dir);
469
6151
}
470
471
6174
inline void Environment::set_coverage_connection(
472
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
473
6174
  CHECK_NULL(coverage_connection_);
474
6174
  std::swap(coverage_connection_, connection);
475
6174
}
476
477
18521
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
478
18521
  return coverage_connection_.get();
479
}
480
481
6135
inline const std::string& Environment::coverage_directory() const {
482
6135
  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
6190
Environment::cpu_profiler_connection() {
493
6190
  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
6178
Environment::heap_profiler_connection() {
528
6178
  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
12590
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
559
12590
  return inspector_host_port_;
560
}
561
562
65889
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
563
65889
  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
92285
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
573
184570
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
574
92285
  native_immediates_.Push(std::move(callback));
575
576
92285
  if (flags & CallbackFlags::kRefed) {
577
67256
    if (immediate_info()->ref_count() == 0)
578
50196
      ToggleImmediateRef(true);
579
67256
    immediate_info()->ref_count_inc(1);
580
  }
581
92285
}
582
583
template <typename Fn>
584
2316
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
585
4632
  auto callback = native_immediates_threadsafe_.CreateCallback(
586
2316
      std::move(cb), flags);
587
  {
588
4632
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
589
2316
    native_immediates_threadsafe_.Push(std::move(callback));
590
2316
    if (task_queues_async_initialized_)
591
2307
      uv_async_send(&task_queues_async_);
592
  }
593
2316
}
594
595
template <typename Fn>
596
10580
void Environment::RequestInterrupt(Fn&& cb) {
597
21160
  auto callback = native_immediates_interrupts_.CreateCallback(
598
10580
      std::move(cb), CallbackFlags::kRefed);
599
  {
600
21160
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
601
10580
    native_immediates_interrupts_.Push(std::move(callback));
602
10580
    if (task_queues_async_initialized_)
603
4286
      uv_async_send(&task_queues_async_);
604
  }
605
10580
  RequestInterruptFromV8();
606
10580
}
607
608
3211100
inline bool Environment::can_call_into_js() const {
609

3211100
  return can_call_into_js_ && !is_stopping();
610
}
611
612
1730
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
613
1730
  can_call_into_js_ = can_call_into_js;
614
1730
}
615
616
1545254
inline bool Environment::has_run_bootstrapping_code() const {
617
1545254
  return has_run_bootstrapping_code_;
618
}
619
620
6189
inline void Environment::DoneBootstrapping() {
621
6189
  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
6189
  base_object_created_by_bootstrap_ = base_object_count_;
626
6189
}
627
628
19
inline bool Environment::has_serialized_options() const {
629
19
  return has_serialized_options_;
630
}
631
632
6791
inline void Environment::set_has_serialized_options(bool value) {
633
6791
  has_serialized_options_ = value;
634
6791
}
635
636
9393
inline bool Environment::is_main_thread() const {
637
9393
  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
6153
inline bool Environment::should_not_register_esm_loader() const {
646
6153
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
647
}
648
649
16102
inline bool Environment::owns_process_state() const {
650
16102
  return flags_ & EnvironmentFlags::kOwnsProcessState;
651
}
652
653
6183
inline bool Environment::owns_inspector() const {
654
6183
  return flags_ & EnvironmentFlags::kOwnsInspector;
655
}
656
657
16792
inline bool Environment::should_create_inspector() const {
658
16792
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
659
}
660
661
136097
inline bool Environment::tracks_unmanaged_fds() const {
662
136097
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
3888
inline bool Environment::hide_console_windows() const {
666
3888
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7112
inline bool Environment::no_global_search_paths() const {
670
14224
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14224
         !options_->global_search_paths;
672
}
673
674
1740
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1740
  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
5988
void Environment::set_source_maps_enabled(bool on) {
692
5988
  source_maps_enabled_ = on;
693
5988
}
694
695
1716
bool Environment::source_maps_enabled() const {
696
1716
  return source_maps_enabled_;
697
}
698
699
7732
inline uint64_t Environment::thread_id() const {
700
7732
  return thread_id_;
701
}
702
703
16257
inline worker::Worker* Environment::worker_context() const {
704
16257
  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
984
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
984
  sub_worker_contexts_.erase(context);
713
984
}
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
4806644
inline bool Environment::is_stopping() const {
721
4806644
  return is_stopping_.load();
722
}
723
724
5920
inline void Environment::set_stopping(bool value) {
725
5920
  is_stopping_.store(value);
726
5920
}
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
42661
inline performance::PerformanceState* Environment::performance_state() {
747
42661
  return performance_state_.get();
748
}
749
750
9010904
inline IsolateData* Environment::isolate_data() const {
751
9010904
  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
1498348
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
791
2996696
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
792
1498348
    fn, arg, cleanup_hook_counter_++
793
1498348
  });
794
  // Make sure there was no existing element with these values.
795
1498348
  CHECK_EQ(insertion_info.second, true);
796
1498348
}
797
798
1465578
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
799
1465578
  CleanupHookCallback search { fn, arg, 0 };
800
1465578
  cleanup_hooks_.erase(search);
801
1465578
}
802
803
3319105
size_t CleanupHookCallback::Hash::operator()(
804
    const CleanupHookCallback& cb) const {
805
3319105
  return std::hash<void*>()(cb.arg_);
806
}
807
808
1659655
bool CleanupHookCallback::Equal::operator()(
809
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
810

1659655
  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
2876900
void Environment::modify_base_object_count(int64_t delta) {
830
2876900
  base_object_count_ += delta;
831
2876900
}
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
724
void Environment::set_process_exit_handler(
847
    std::function<void(Environment*, int)>&& handler) {
848
724
  process_exit_handler_ = std::move(handler);
849
724
}
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
60706
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
860
1186704
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
861
8215326
  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
1204244
  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
60706
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
885
1067790
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
886
11872331
  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
893767
  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
10714570
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
911
#undef V
912
913
9149119
v8::Local<v8::Context> Environment::context() const {
914
9149119
  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_