GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 454 457 99.3 %
Date: 2022-07-14 04:20:11 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
116789
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
46
116789
    IsolateData* isolate_data)
47
116789
    : node_allocator_(isolate_data->node_allocator()) {
48
116789
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
49
116789
}
50
51
233578
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
52
116789
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
53
116789
}
54
55
inline v8::Isolate* IsolateData::isolate() const {
56
  return isolate_;
57
}
58
59
1265434
inline uv_loop_t* IsolateData::event_loop() const {
60
1265434
  return event_loop_;
61
}
62
63
123884
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
64
123884
  return node_allocator_;
65
}
66
67
58824
inline MultiIsolatePlatform* IsolateData::platform() const {
68
58824
  return platform_;
69
}
70
71
1252
inline void IsolateData::set_worker_context(worker::Worker* context) {
72
1252
  CHECK_NULL(worker_context_);  // Should be set only once.
73
1252
  worker_context_ = context;
74
1252
}
75
76
20120
inline worker::Worker* IsolateData::worker_context() const {
77
20120
  return worker_context_;
78
}
79
80
217348
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
81
217348
  return async_wrap_providers_[index].Get(isolate_);
82
}
83
84
2632539
inline AliasedUint32Array& AsyncHooks::fields() {
85
2632539
  return fields_;
86
}
87
88
1697803
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
89
1697803
  return async_id_fields_;
90
}
91
92
1299
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
93
1299
  return async_ids_stack_;
94
}
95
96
823302
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
97
823302
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
98
1299
    js_execution_async_resources_.Reset(
99
2598
        env()->isolate(), v8::Array::New(env()->isolate()));
100
  }
101
823302
  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
217000
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
110
217000
  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
435040
inline Environment* AsyncHooks::env() {
118
435040
  return Environment::ForAsyncHooks(this);
119
}
120
121
435040
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
122
435040
  return ContainerOf(&Environment::async_hooks_, hooks);
123
}
124
125
792467
inline size_t Environment::async_callback_scope_depth() const {
126
792467
  return async_callback_scope_depth_;
127
}
128
129
809673
inline void Environment::PushAsyncCallbackScope() {
130
809673
  async_callback_scope_depth_++;
131
809673
}
132
133
809061
inline void Environment::PopAsyncCallbackScope() {
134
809061
  async_callback_scope_depth_--;
135
809061
}
136
137
1299
inline AliasedUint32Array& ImmediateInfo::fields() {
138
1299
  return fields_;
139
}
140
141
187964
inline uint32_t ImmediateInfo::count() const {
142
187964
  return fields_[kCount];
143
}
144
145
286246
inline uint32_t ImmediateInfo::ref_count() const {
146
286246
  return fields_[kRefCount];
147
}
148
149
44732
inline bool ImmediateInfo::has_outstanding() const {
150
44732
  return fields_[kHasOutstanding] == 1;
151
}
152
153
34650
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
154
34650
  fields_[kRefCount] += increment;
155
34650
}
156
157
207815
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
158
207815
  fields_[kRefCount] -= decrement;
159
207815
}
160
161
1299
inline AliasedUint8Array& TickInfo::fields() {
162
1299
  return fields_;
163
}
164
165
1428146
inline bool TickInfo::has_tick_scheduled() const {
166
1428146
  return fields_[kHasTickScheduled] == 1;
167
}
168
169
523364
inline bool TickInfo::has_rejection_to_warn() const {
170
523364
  return fields_[kHasRejectionToWarn] == 1;
171
}
172
173
56835
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
174
56835
  if (UNLIKELY(!isolate->InContext())) return nullptr;
175
109948
  v8::HandleScope handle_scope(isolate);
176
54974
  return GetCurrent(isolate->GetCurrentContext());
177
}
178
179
7659967
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180
7659967
  if (UNLIKELY(context.IsEmpty())) {
181
1
    return nullptr;
182
  }
183
7659966
  if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184
7659966
               ContextEmbedderIndex::kContextTag)) {
185
11
    return nullptr;
186
  }
187
7659955
  if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188
                   ContextEmbedderIndex::kContextTag) !=
189
7659955
               Environment::kNodeContextTagPtr)) {
190
    return nullptr;
191
  }
192
  return static_cast<Environment*>(
193
7659955
      context->GetAlignedPointerFromEmbedderData(
194
7659955
          ContextEmbedderIndex::kEnvironment));
195
}
196
197
5768831
inline Environment* Environment::GetCurrent(
198
    const v8::FunctionCallbackInfo<v8::Value>& info) {
199
5768831
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
200
}
201
202
template <typename T>
203
2764948
inline Environment* Environment::GetCurrent(
204
    const v8::PropertyCallbackInfo<T>& info) {
205
2764948
  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
996252
inline T* Environment::GetBindingData(
215
    const v8::FunctionCallbackInfo<v8::Value>& info) {
216
996252
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
217
}
218
219
template <typename T>
220
996254
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
221
  BindingDataStore* map = static_cast<BindingDataStore*>(
222
996254
      context->GetAlignedPointerFromEmbedderData(
223
          ContextEmbedderIndex::kBindingListIndex));
224
  DCHECK_NOT_NULL(map);
225
996254
  auto it = map->find(T::type_name);
226
996254
  if (UNLIKELY(it == map->end())) return nullptr;
227
996254
  T* result = static_cast<T*>(it->second.get());
228
  DCHECK_NOT_NULL(result);
229
  DCHECK_EQ(result->env(), GetCurrent(context));
230
996254
  return result;
231
}
232
233
template <typename T>
234
27717
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
55434
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
240
  BindingDataStore* map = static_cast<BindingDataStore*>(
241
27717
      context->GetAlignedPointerFromEmbedderData(
242
          ContextEmbedderIndex::kBindingListIndex));
243
  DCHECK_NOT_NULL(map);
244
27717
  auto result = map->emplace(T::type_name, item);
245
27717
  CHECK(result.second);
246
  DCHECK_EQ(GetBindingData<T>(context), item.get());
247
27717
  return item.get();
248
}
249
250
30337310
inline v8::Isolate* Environment::isolate() const {
251
30337310
  return isolate_;
252
}
253
254
7725
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
255
7725
  return ContainerOf(&Environment::timer_handle_, handle);
256
}
257
258
33061
inline uv_timer_t* Environment::timer_handle() {
259
33061
  return &timer_handle_;
260
}
261
262
187964
inline Environment* Environment::from_immediate_check_handle(
263
    uv_check_t* handle) {
264
187964
  return ContainerOf(&Environment::immediate_check_handle_, handle);
265
}
266
267
26052
inline uv_check_t* Environment::immediate_check_handle() {
268
26052
  return &immediate_check_handle_;
269
}
270
271
236407
inline uv_idle_t* Environment::immediate_idle_handle() {
272
236407
  return &immediate_idle_handle_;
273
}
274
275
39078
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
276
                                               HandleCleanupCb cb,
277
                                               void* arg) {
278
39078
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
279
39078
}
280
281
template <typename T, typename OnCloseCallback>
282
40269
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
283
40113
  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
40191
  handle->data = new CloseData { this, callback, handle->data };
295
40503
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
296
80226
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
297
40191
    data->env->handle_cleanup_waiting_--;
298
40191
    handle->data = data->original_data;
299
40191
    data->callback(reinterpret_cast<T*>(handle));
300
  });
301
40191
}
302
303
90091
void Environment::IncreaseWaitingRequestCounter() {
304
90091
  request_waiting_++;
305
90091
}
306
307
90083
void Environment::DecreaseWaitingRequestCounter() {
308
90083
  request_waiting_--;
309
90083
  CHECK_GE(request_waiting_, 0);
310
90083
}
311
312
1258886
inline uv_loop_t* Environment::event_loop() const {
313
1258886
  return isolate_data()->event_loop();
314
}
315
316
#if HAVE_INSPECTOR
317
43707
inline bool Environment::is_in_inspector_console_call() const {
318
43707
  return is_in_inspector_console_call_;
319
}
320
321
87412
inline void Environment::set_is_in_inspector_console_call(bool value) {
322
87412
  is_in_inspector_console_call_ = value;
323
87412
}
324
#endif
325
326
5953520
inline AsyncHooks* Environment::async_hooks() {
327
5953520
  return &async_hooks_;
328
}
329
330
762706
inline ImmediateInfo* Environment::immediate_info() {
331
762706
  return &immediate_info_;
332
}
333
334
715407
inline TickInfo* Environment::tick_info() {
335
715407
  return &tick_info_;
336
}
337
338
100748
inline uint64_t Environment::timer_base() const {
339
100748
  return timer_base_;
340
}
341
342
1402194
inline std::shared_ptr<KVStore> Environment::env_vars() {
343
1402194
  return env_vars_;
344
}
345
346
7788
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
347
7788
  env_vars_ = env_vars;
348
7788
}
349
350
19
inline bool Environment::printed_error() const {
351
19
  return printed_error_;
352
}
353
354
19
inline void Environment::set_printed_error(bool value) {
355
19
  printed_error_ = value;
356
19
}
357
358
11292
inline void Environment::set_trace_sync_io(bool value) {
359
11292
  trace_sync_io_ = value;
360
11292
}
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
1246
inline void Environment::set_abort_on_uncaught_exception(bool value) {
375
1246
  options_->abort_on_uncaught_exception = value;
376
1246
}
377
378
1329
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
379
1329
  return should_abort_on_uncaught_toggle_;
380
}
381
382
306687
inline AliasedInt32Array& Environment::stream_base_state() {
383
306687
  return stream_base_state_;
384
}
385
386
55112
inline uint32_t Environment::get_next_module_id() {
387
55112
  return module_id_counter_++;
388
}
389
3957
inline uint32_t Environment::get_next_script_id() {
390
3957
  return script_id_counter_++;
391
}
392
35750
inline uint32_t Environment::get_next_function_id() {
393
35750
  return function_id_counter_++;
394
}
395
396
113673
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
397
113673
    Environment* env)
398
113673
    : env_(env) {
399
113673
  env_->PushShouldNotAbortOnUncaughtScope();
400
113673
}
401
402
227340
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
403
113670
  Close();
404
113670
}
405
406
113862
void ShouldNotAbortOnUncaughtScope::Close() {
407
113862
  if (env_ != nullptr) {
408
113670
    env_->PopShouldNotAbortOnUncaughtScope();
409
113670
    env_ = nullptr;
410
  }
411
113862
}
412
413
113673
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
414
113673
  should_not_abort_scope_counter_++;
415
113673
}
416
417
113670
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
418
113670
  should_not_abort_scope_counter_--;
419
113670
}
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
388551
inline std::vector<double>* Environment::destroy_async_id_list() {
426
388551
  return &destroy_async_id_list_;
427
}
428
429
217555
inline double Environment::new_async_id() {
430
217555
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
431
217555
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
432
}
433
434
246311
inline double Environment::execution_async_id() {
435
246311
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
436
}
437
438
63600
inline double Environment::trigger_async_id() {
439
63600
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
440
}
441
442
217552
inline double Environment::get_default_trigger_async_id() {
443
  double default_trigger_async_id =
444
217552
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
445
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
446
217552
  if (default_trigger_async_id < 0)
447
182651
    default_trigger_async_id = execution_async_id();
448
217552
  return default_trigger_async_id;
449
}
450
451
74233
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
452
74233
  return options_;
453
}
454
455
18015
inline const std::vector<std::string>& Environment::argv() {
456
18015
  return argv_;
457
}
458
459
7697
inline const std::vector<std::string>& Environment::exec_argv() {
460
7697
  return exec_argv_;
461
}
462
463
13026
inline const std::string& Environment::exec_path() const {
464
13026
  return exec_path_;
465
}
466
467
#if HAVE_INSPECTOR
468
6508
inline void Environment::set_coverage_directory(const char* dir) {
469
6508
  coverage_directory_ = std::string(dir);
470
6508
}
471
472
6533
inline void Environment::set_coverage_connection(
473
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
474
6533
  CHECK_NULL(coverage_connection_);
475
6533
  std::swap(coverage_connection_, connection);
476
6533
}
477
478
19598
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
479
19598
  return coverage_connection_.get();
480
}
481
482
6493
inline const std::string& Environment::coverage_directory() const {
483
6493
  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
6549
Environment::cpu_profiler_connection() {
494
6549
  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
6537
Environment::heap_profiler_connection() {
529
6537
  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
13264
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
560
13264
  return inspector_host_port_;
561
}
562
563
63918
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
564
63918
  return options_;
565
}
566
567
289
inline void IsolateData::set_options(
568
    std::shared_ptr<PerIsolateOptions> options) {
569
289
  options_ = std::move(options);
570
289
}
571
572
template <typename Fn>
573
93853
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
574
187706
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
575
93853
  native_immediates_.Push(std::move(callback));
576
577
93853
  if (flags & CallbackFlags::kRefed) {
578
69286
    if (immediate_info()->ref_count() == 0)
579
50558
      ToggleImmediateRef(true);
580
69286
    immediate_info()->ref_count_inc(1);
581
  }
582
93853
}
583
584
template <typename Fn>
585
3794
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
586
7588
  auto callback = native_immediates_threadsafe_.CreateCallback(
587
3794
      std::move(cb), flags);
588
  {
589
7588
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
590
3794
    native_immediates_threadsafe_.Push(std::move(callback));
591
3794
    if (task_queues_async_initialized_)
592
3785
      uv_async_send(&task_queues_async_);
593
  }
594
3794
}
595
596
template <typename Fn>
597
11923
void Environment::RequestInterrupt(Fn&& cb) {
598
23846
  auto callback = native_immediates_interrupts_.CreateCallback(
599
11923
      std::move(cb), CallbackFlags::kRefed);
600
  {
601
23846
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
602
11923
    native_immediates_interrupts_.Push(std::move(callback));
603
11923
    if (task_queues_async_initialized_)
604
5273
      uv_async_send(&task_queues_async_);
605
  }
606
11923
  RequestInterruptFromV8();
607
11923
}
608
609
3068690
inline bool Environment::can_call_into_js() const {
610

3068690
  return can_call_into_js_ && !is_stopping();
611
}
612
613
2673
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614
2673
  can_call_into_js_ = can_call_into_js;
615
2673
}
616
617
1545790
inline bool Environment::has_run_bootstrapping_code() const {
618
1545790
  return has_run_bootstrapping_code_;
619
}
620
621
6548
inline void Environment::DoneBootstrapping() {
622
6548
  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
6548
  base_object_created_by_bootstrap_ = base_object_count_;
627
6548
}
628
629
19
inline bool Environment::has_serialized_options() const {
630
19
  return has_serialized_options_;
631
}
632
633
7073
inline void Environment::set_has_serialized_options(bool value) {
634
7073
  has_serialized_options_ = value;
635
7073
}
636
637
12376
inline bool Environment::is_main_thread() const {
638
12376
  return worker_context() == nullptr;
639
}
640
641
1648
inline bool Environment::no_native_addons() const {
642
3292
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
643
3292
          !options_->allow_native_addons;
644
}
645
646
6511
inline bool Environment::should_not_register_esm_loader() const {
647
6511
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
648
}
649
650
18830
inline bool Environment::owns_process_state() const {
651
18830
  return flags_ & EnvironmentFlags::kOwnsProcessState;
652
}
653
654
6542
inline bool Environment::owns_inspector() const {
655
6542
  return flags_ & EnvironmentFlags::kOwnsInspector;
656
}
657
658
19574
inline bool Environment::should_create_inspector() const {
659
19574
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0;
660
}
661
662
134472
inline bool Environment::tracks_unmanaged_fds() const {
663
134472
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
664
}
665
666
4261
inline bool Environment::hide_console_windows() const {
667
4261
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
668
}
669
670
7982
inline bool Environment::no_global_search_paths() const {
671
15964
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
672
15964
         !options_->global_search_paths;
673
}
674
675
2770
inline bool Environment::no_browser_globals() const {
676
  // configure --no-browser-globals
677
#ifdef NODE_NO_BROWSER_GLOBALS
678
  return true;
679
#else
680
2770
  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
6356
void Environment::set_source_maps_enabled(bool on) {
693
6356
  source_maps_enabled_ = on;
694
6356
}
695
696
2581
bool Environment::source_maps_enabled() const {
697
2581
  return source_maps_enabled_;
698
}
699
700
9120
inline uint64_t Environment::thread_id() const {
701
9120
  return thread_id_;
702
}
703
704
20120
inline worker::Worker* Environment::worker_context() const {
705
20120
  return isolate_data()->worker_context();
706
}
707
708
1468
inline void Environment::add_sub_worker_context(worker::Worker* context) {
709
1468
  sub_worker_contexts_.insert(context);
710
1468
}
711
712
1495
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
713
1495
  sub_worker_contexts_.erase(context);
714
1495
}
715
716
template <typename Fn>
717
24
inline void Environment::ForEachWorker(Fn&& iterator) {
718
26
  for (worker::Worker* w : sub_worker_contexts_) iterator(w);
719
24
}
720
721
4603802
inline bool Environment::is_stopping() const {
722
4603802
  return is_stopping_.load();
723
}
724
725
6765
inline void Environment::set_stopping(bool value) {
726
6765
  is_stopping_.store(value);
727
6765
}
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
44361
inline performance::PerformanceState* Environment::performance_state() {
748
44361
  return performance_state_.get();
749
}
750
751
8458641
inline IsolateData* Environment::isolate_data() const {
752
8458641
  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
1463138
void Environment::AddCleanupHook(CleanupCallback fn, void* arg) {
792
2926276
  auto insertion_info = cleanup_hooks_.emplace(CleanupHookCallback {
793
1463138
    fn, arg, cleanup_hook_counter_++
794
1463138
  });
795
  // Make sure there was no existing element with these values.
796
1463138
  CHECK_EQ(insertion_info.second, true);
797
1463138
}
798
799
1444006
void Environment::RemoveCleanupHook(CleanupCallback fn, void* arg) {
800
1444006
  CleanupHookCallback search { fn, arg, 0 };
801
1444006
  cleanup_hooks_.erase(search);
802
1444006
}
803
804
3216028
size_t CleanupHookCallback::Hash::operator()(
805
    const CleanupHookCallback& cb) const {
806
3216028
  return std::hash<void*>()(cb.arg_);
807
}
808
809
1603674
bool CleanupHookCallback::Equal::operator()(
810
    const CleanupHookCallback& a, const CleanupHookCallback& b) const {
811

1603674
  return a.fn_ == b.fn_ && a.arg_ == b.arg_;
812
}
813
814
545
BaseObject* CleanupHookCallback::GetBaseObject() const {
815
545
  if (fn_ == BaseObject::DeleteMe)
816
517
    return static_cast<BaseObject*>(arg_);
817
  else
818
28
    return nullptr;
819
}
820
821
template <typename T>
822
48
void Environment::ForEachBaseObject(T&& iterator) {
823
1138
  for (const auto& hook : cleanup_hooks_) {
824
1090
    BaseObject* obj = hook.GetBaseObject();
825
1090
    if (obj != nullptr)
826
1034
      iterator(obj);
827
  }
828
}
829
830
template <typename T>
831
6
void Environment::ForEachBindingData(T&& iterator) {
832
  BindingDataStore* map = static_cast<BindingDataStore*>(
833
12
      context()->GetAlignedPointerFromEmbedderData(
834
          ContextEmbedderIndex::kBindingListIndex));
835
  DCHECK_NOT_NULL(map);
836
30
  for (auto& it : *map) {
837
24
    iterator(it.first, it.second);
838
  }
839
6
}
840
841
2846018
void Environment::modify_base_object_count(int64_t delta) {
842
2846018
  base_object_count_ += delta;
843
2846018
}
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
1243
void Environment::set_process_exit_handler(
859
    std::function<void(Environment*, int)>&& handler) {
860
1243
  process_exit_handler_ = std::move(handler);
861
1243
}
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
44048
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
872
979176
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
873
8349390
  PER_ISOLATE_STRING_PROPERTIES(VS)
874
#undef V
875
#undef VS
876
#undef VY
877
#undef VP
878
879
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
880
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
881
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
882
#define V(TypeName, PropertyName)                                             \
883
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
884
    return isolate_data()->PropertyName();                                    \
885
  }
886
44048
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
887
855389
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
888
12053942
  PER_ISOLATE_STRING_PROPERTIES(VS)
889
#undef V
890
#undef VS
891
#undef VY
892
#undef VP
893
894
#define V(PropertyName, TypeName)                                             \
895
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
896
    return PersistentToLocal::Strong(PropertyName ## _);                      \
897
  }                                                                           \
898
  inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) {  \
899
    PropertyName ## _.Reset(isolate(), value);                                \
900
  }
901
1365188
  ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V)
902
6249889
  ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
903
#undef V
904
905
9390172
v8::Local<v8::Context> Environment::context() const {
906
9390172
  return PersistentToLocal::Strong(context_);
907
}
908
909
}  // namespace node
910
911
// These two files depend on each other. Including base_object-inl.h after this
912
// file is the easiest way to avoid issues with that circular dependency.
913
#include "base_object-inl.h"
914
915
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
916
917
#endif  // SRC_ENV_INL_H_