GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env-inl.h Lines: 445 447 99.6 %
Date: 2022-11-10 04:21:28 Branches: 54 68 79.4 %

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 "node_realm-inl.h"
35
#include "util-inl.h"
36
#include "uv.h"
37
#include "v8.h"
38
39
#include <cstddef>
40
#include <cstdint>
41
42
#include <utility>
43
44
namespace node {
45
46
115721
NoArrayBufferZeroFillScope::NoArrayBufferZeroFillScope(
47
115721
    IsolateData* isolate_data)
48
115721
    : node_allocator_(isolate_data->node_allocator()) {
49
115721
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 0;
50
115721
}
51
52
231442
NoArrayBufferZeroFillScope::~NoArrayBufferZeroFillScope() {
53
115721
  if (node_allocator_ != nullptr) node_allocator_->zero_fill_field()[0] = 1;
54
115721
}
55
56
26069
inline v8::Isolate* IsolateData::isolate() const {
57
26069
  return isolate_;
58
}
59
60
1305188
inline uv_loop_t* IsolateData::event_loop() const {
61
1305188
  return event_loop_;
62
}
63
64
123377
inline NodeArrayBufferAllocator* IsolateData::node_allocator() const {
65
123377
  return node_allocator_;
66
}
67
68
86993
inline MultiIsolatePlatform* IsolateData::platform() const {
69
86993
  return platform_;
70
}
71
72
737
inline void IsolateData::set_worker_context(worker::Worker* context) {
73
737
  CHECK_NULL(worker_context_);  // Should be set only once.
74
737
  worker_context_ = context;
75
737
}
76
77
16620
inline worker::Worker* IsolateData::worker_context() const {
78
16620
  return worker_context_;
79
}
80
81
215407
inline v8::Local<v8::String> IsolateData::async_wrap_provider(int index) const {
82
215407
  return async_wrap_providers_[index].Get(isolate_);
83
}
84
85
2705108
inline AliasedUint32Array& AsyncHooks::fields() {
86
2705108
  return fields_;
87
}
88
89
1618707
inline AliasedFloat64Array& AsyncHooks::async_id_fields() {
90
1618707
  return async_id_fields_;
91
}
92
93
787
inline AliasedFloat64Array& AsyncHooks::async_ids_stack() {
94
787
  return async_ids_stack_;
95
}
96
97
897712
v8::Local<v8::Array> AsyncHooks::js_execution_async_resources() {
98
897712
  if (UNLIKELY(js_execution_async_resources_.IsEmpty())) {
99
787
    js_execution_async_resources_.Reset(
100
1574
        env()->isolate(), v8::Array::New(env()->isolate()));
101
  }
102
897712
  return PersistentToLocal::Strong(js_execution_async_resources_);
103
}
104
105
2406
v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
106
2406
  if (i >= native_execution_async_resources_.size()) return {};
107
2406
  return native_execution_async_resources_[i];
108
}
109
110
215001
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
111
215001
  return env()->isolate_data()->async_wrap_provider(idx);
112
}
113
114
1
inline void AsyncHooks::no_force_checks() {
115
1
  fields_[kCheck] -= 1;
116
1
}
117
118
473845
inline Environment* AsyncHooks::env() {
119
473845
  return Environment::ForAsyncHooks(this);
120
}
121
122
473845
Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
123
473845
  return ContainerOf(&Environment::async_hooks_, hooks);
124
}
125
126
851633
inline size_t Environment::async_callback_scope_depth() const {
127
851633
  return async_callback_scope_depth_;
128
}
129
130
865928
inline void Environment::PushAsyncCallbackScope() {
131
865928
  async_callback_scope_depth_++;
132
865928
}
133
134
865236
inline void Environment::PopAsyncCallbackScope() {
135
865236
  async_callback_scope_depth_--;
136
865236
}
137
138
787
inline AliasedUint32Array& ImmediateInfo::fields() {
139
787
  return fields_;
140
}
141
142
216229
inline uint32_t ImmediateInfo::count() const {
143
216229
  return fields_[kCount];
144
}
145
146
338615
inline uint32_t ImmediateInfo::ref_count() const {
147
338615
  return fields_[kRefCount];
148
}
149
150
59028
inline bool ImmediateInfo::has_outstanding() const {
151
59028
  return fields_[kHasOutstanding] == 1;
152
}
153
154
46722
inline void ImmediateInfo::ref_count_inc(uint32_t increment) {
155
46722
  fields_[kRefCount] += increment;
156
46722
}
157
158
233816
inline void ImmediateInfo::ref_count_dec(uint32_t decrement) {
159
233816
  fields_[kRefCount] -= decrement;
160
233816
}
161
162
787
inline AliasedUint8Array& TickInfo::fields() {
163
787
  return fields_;
164
}
165
166
1530133
inline bool TickInfo::has_tick_scheduled() const {
167
1530133
  return fields_[kHasTickScheduled] == 1;
168
}
169
170
554608
inline bool TickInfo::has_rejection_to_warn() const {
171
554608
  return fields_[kHasRejectionToWarn] == 1;
172
}
173
174
85290
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
175
85290
  if (UNLIKELY(!isolate->InContext())) return nullptr;
176
159944
  v8::HandleScope handle_scope(isolate);
177
79972
  return GetCurrent(isolate->GetCurrentContext());
178
}
179
180
8400950
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
181
8400950
  if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
182
13
    return nullptr;
183
  }
184
  return static_cast<Environment*>(
185
8400937
      context->GetAlignedPointerFromEmbedderData(
186
8400937
          ContextEmbedderIndex::kEnvironment));
187
}
188
189
6413972
inline Environment* Environment::GetCurrent(
190
    const v8::FunctionCallbackInfo<v8::Value>& info) {
191
6413972
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
192
}
193
194
template <typename T>
195
2329141
inline Environment* Environment::GetCurrent(
196
    const v8::PropertyCallbackInfo<T>& info) {
197
2329141
  return GetCurrent(info.GetIsolate()->GetCurrentContext());
198
}
199
200
template <typename T, typename U>
201
inline T* Environment::GetBindingData(const v8::PropertyCallbackInfo<U>& info) {
202
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
203
}
204
205
template <typename T>
206
1027819
inline T* Environment::GetBindingData(
207
    const v8::FunctionCallbackInfo<v8::Value>& info) {
208
1027819
  return GetBindingData<T>(info.GetIsolate()->GetCurrentContext());
209
}
210
211
template <typename T>
212
1027821
inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
213
  BindingDataStore* map = static_cast<BindingDataStore*>(
214
1027821
      context->GetAlignedPointerFromEmbedderData(
215
          ContextEmbedderIndex::kBindingListIndex));
216
  DCHECK_NOT_NULL(map);
217
1027821
  auto it = map->find(T::type_name);
218
1027821
  if (UNLIKELY(it == map->end())) return nullptr;
219
1027821
  T* result = static_cast<T*>(it->second.get());
220
  DCHECK_NOT_NULL(result);
221
  DCHECK_EQ(result->env(), GetCurrent(context));
222
1027821
  return result;
223
}
224
225
template <typename T>
226
26717
inline T* Environment::AddBindingData(
227
    v8::Local<v8::Context> context,
228
    v8::Local<v8::Object> target) {
229
  DCHECK_EQ(GetCurrent(context), this);
230
  // This won't compile if T is not a BaseObject subclass.
231
53434
  BaseObjectPtr<T> item = MakeDetachedBaseObject<T>(this, target);
232
  BindingDataStore* map = static_cast<BindingDataStore*>(
233
26717
      context->GetAlignedPointerFromEmbedderData(
234
          ContextEmbedderIndex::kBindingListIndex));
235
  DCHECK_NOT_NULL(map);
236
26717
  auto result = map->emplace(T::type_name, item);
237
26717
  CHECK(result.second);
238
  DCHECK_EQ(GetBindingData<T>(context), item.get());
239
26717
  return item.get();
240
}
241
242
24897840
inline v8::Isolate* Environment::isolate() const {
243
24897840
  return isolate_;
244
}
245
246
8939
inline Environment* Environment::from_timer_handle(uv_timer_t* handle) {
247
8939
  return ContainerOf(&Environment::timer_handle_, handle);
248
}
249
250
33986
inline uv_timer_t* Environment::timer_handle() {
251
33986
  return &timer_handle_;
252
}
253
254
216229
inline Environment* Environment::from_immediate_check_handle(
255
    uv_check_t* handle) {
256
216229
  return ContainerOf(&Environment::immediate_check_handle_, handle);
257
}
258
259
25384
inline uv_check_t* Environment::immediate_check_handle() {
260
25384
  return &immediate_check_handle_;
261
}
262
263
260574
inline uv_idle_t* Environment::immediate_idle_handle() {
264
260574
  return &immediate_idle_handle_;
265
}
266
267
38076
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
268
                                               HandleCleanupCb cb,
269
                                               void* arg) {
270
38076
  handle_cleanup_queue_.push_back(HandleCleanup{handle, cb, arg});
271
38076
}
272
273
template <typename T, typename OnCloseCallback>
274
39053
inline void Environment::CloseHandle(T* handle, OnCloseCallback callback) {
275
39053
  handle_cleanup_waiting_++;
276
  static_assert(sizeof(T) >= sizeof(uv_handle_t), "T is a libuv handle");
277
  static_assert(offsetof(T, data) == offsetof(uv_handle_t, data),
278
                "T is a libuv handle");
279
  static_assert(offsetof(T, close_cb) == offsetof(uv_handle_t, close_cb),
280
                "T is a libuv handle");
281
  struct CloseData {
282
    Environment* env;
283
    OnCloseCallback callback;
284
    void* original_data;
285
  };
286
39053
  handle->data = new CloseData { this, callback, handle->data };
287
39213
  uv_close(reinterpret_cast<uv_handle_t*>(handle), [](uv_handle_t* handle) {
288
77946
    std::unique_ptr<CloseData> data { static_cast<CloseData*>(handle->data) };
289
38973
    data->env->handle_cleanup_waiting_--;
290
38973
    handle->data = data->original_data;
291
38973
    data->callback(reinterpret_cast<T*>(handle));
292
  });
293
39053
}
294
295
95206
void Environment::IncreaseWaitingRequestCounter() {
296
95206
  request_waiting_++;
297
95206
}
298
299
95198
void Environment::DecreaseWaitingRequestCounter() {
300
95198
  request_waiting_--;
301
95198
  CHECK_GE(request_waiting_, 0);
302
95198
}
303
304
1298808
inline uv_loop_t* Environment::event_loop() const {
305
1298808
  return isolate_data()->event_loop();
306
}
307
308
#if HAVE_INSPECTOR
309
72354
inline bool Environment::is_in_inspector_console_call() const {
310
72354
  return is_in_inspector_console_call_;
311
}
312
313
144706
inline void Environment::set_is_in_inspector_console_call(bool value) {
314
144706
  is_in_inspector_console_call_ = value;
315
144706
}
316
#endif
317
318
6137460
inline AsyncHooks* Environment::async_hooks() {
319
6137460
  return &async_hooks_;
320
}
321
322
895197
inline ImmediateInfo* Environment::immediate_info() {
323
895197
  return &immediate_info_;
324
}
325
326
765890
inline TickInfo* Environment::tick_info() {
327
765890
  return &tick_info_;
328
}
329
330
107149
inline uint64_t Environment::timer_base() const {
331
107149
  return timer_base_;
332
}
333
334
1499426
inline std::shared_ptr<KVStore> Environment::env_vars() {
335
1499426
  return env_vars_;
336
}
337
338
7105
inline void Environment::set_env_vars(std::shared_ptr<KVStore> env_vars) {
339
7105
  env_vars_ = env_vars;
340
7105
}
341
342
22
inline bool Environment::printed_error() const {
343
22
  return printed_error_;
344
}
345
346
22
inline void Environment::set_printed_error(bool value) {
347
22
  printed_error_ = value;
348
22
}
349
350
11298
inline void Environment::set_trace_sync_io(bool value) {
351
11298
  trace_sync_io_ = value;
352
11298
}
353
354
124
inline bool Environment::abort_on_uncaught_exception() const {
355
124
  return options_->abort_on_uncaught_exception;
356
}
357
358
inline void Environment::set_force_context_aware(bool value) {
359
  options_->force_context_aware = value;
360
}
361
362
44
inline bool Environment::force_context_aware() const {
363
44
  return options_->force_context_aware;
364
}
365
366
11699
inline void Environment::set_exiting(bool value) {
367
11699
  exit_info_[kExiting] = value ? 1 : 0;
368
11699
}
369
370
16266
inline ExitCode Environment::exit_code(const ExitCode default_code) const {
371
16266
  return exit_info_[kHasExitCode] == 0
372
16266
             ? default_code
373
16266
             : static_cast<ExitCode>(exit_info_[kExitCode]);
374
}
375
376
787
inline AliasedInt32Array& Environment::exit_info() {
377
787
  return exit_info_;
378
}
379
380
729
inline void Environment::set_abort_on_uncaught_exception(bool value) {
381
729
  options_->abort_on_uncaught_exception = value;
382
729
}
383
384
817
inline AliasedUint32Array& Environment::should_abort_on_uncaught_toggle() {
385
817
  return should_abort_on_uncaught_toggle_;
386
}
387
388
269877
inline AliasedInt32Array& Environment::stream_base_state() {
389
269877
  return stream_base_state_;
390
}
391
392
52717
inline uint32_t Environment::get_next_module_id() {
393
52717
  return module_id_counter_++;
394
}
395
6439
inline uint32_t Environment::get_next_script_id() {
396
6439
  return script_id_counter_++;
397
}
398
36276
inline uint32_t Environment::get_next_function_id() {
399
36276
  return function_id_counter_++;
400
}
401
402
111364
ShouldNotAbortOnUncaughtScope::ShouldNotAbortOnUncaughtScope(
403
111364
    Environment* env)
404
111364
    : env_(env) {
405
111364
  env_->PushShouldNotAbortOnUncaughtScope();
406
111364
}
407
408
222722
ShouldNotAbortOnUncaughtScope::~ShouldNotAbortOnUncaughtScope() {
409
111361
  Close();
410
111361
}
411
412
111554
void ShouldNotAbortOnUncaughtScope::Close() {
413
111554
  if (env_ != nullptr) {
414
111361
    env_->PopShouldNotAbortOnUncaughtScope();
415
111361
    env_ = nullptr;
416
  }
417
111554
}
418
419
111364
inline void Environment::PushShouldNotAbortOnUncaughtScope() {
420
111364
  should_not_abort_scope_counter_++;
421
111364
}
422
423
111361
inline void Environment::PopShouldNotAbortOnUncaughtScope() {
424
111361
  should_not_abort_scope_counter_--;
425
111361
}
426
427
1
inline bool Environment::inside_should_not_abort_on_uncaught_scope() const {
428
1
  return should_not_abort_scope_counter_ > 0;
429
}
430
431
446814
inline std::vector<double>* Environment::destroy_async_id_list() {
432
446814
  return &destroy_async_id_list_;
433
}
434
435
215556
inline double Environment::new_async_id() {
436
215556
  async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter] += 1;
437
215556
  return async_hooks()->async_id_fields()[AsyncHooks::kAsyncIdCounter];
438
}
439
440
247172
inline double Environment::execution_async_id() {
441
247172
  return async_hooks()->async_id_fields()[AsyncHooks::kExecutionAsyncId];
442
}
443
444
65476
inline double Environment::trigger_async_id() {
445
65476
  return async_hooks()->async_id_fields()[AsyncHooks::kTriggerAsyncId];
446
}
447
448
215553
inline double Environment::get_default_trigger_async_id() {
449
  double default_trigger_async_id =
450
215553
    async_hooks()->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
451
  // If defaultTriggerAsyncId isn't set, use the executionAsyncId
452
215553
  if (default_trigger_async_id < 0)
453
181617
    default_trigger_async_id = execution_async_id();
454
215553
  return default_trigger_async_id;
455
}
456
457
81774
inline std::shared_ptr<EnvironmentOptions> Environment::options() {
458
81774
  return options_;
459
}
460
461
17986
inline const std::vector<std::string>& Environment::argv() {
462
17986
  return argv_;
463
}
464
465
7023
inline const std::vector<std::string>& Environment::exec_argv() {
466
7023
  return exec_argv_;
467
}
468
469
12692
inline const std::string& Environment::exec_path() const {
470
12692
  return exec_path_;
471
}
472
473
#if HAVE_INSPECTOR
474
6341
inline void Environment::set_coverage_directory(const char* dir) {
475
6341
  coverage_directory_ = std::string(dir);
476
6341
}
477
478
6362
inline void Environment::set_coverage_connection(
479
    std::unique_ptr<profiler::V8CoverageConnection> connection) {
480
6362
  CHECK_NULL(coverage_connection_);
481
6362
  std::swap(coverage_connection_, connection);
482
6362
}
483
484
19083
inline profiler::V8CoverageConnection* Environment::coverage_connection() {
485
19083
  return coverage_connection_.get();
486
}
487
488
6317
inline const std::string& Environment::coverage_directory() const {
489
6317
  return coverage_directory_;
490
}
491
492
12
inline void Environment::set_cpu_profiler_connection(
493
    std::unique_ptr<profiler::V8CpuProfilerConnection> connection) {
494
12
  CHECK_NULL(cpu_profiler_connection_);
495
12
  std::swap(cpu_profiler_connection_, connection);
496
12
}
497
498
inline profiler::V8CpuProfilerConnection*
499
6376
Environment::cpu_profiler_connection() {
500
6376
  return cpu_profiler_connection_.get();
501
}
502
503
12
inline void Environment::set_cpu_prof_interval(uint64_t interval) {
504
12
  cpu_prof_interval_ = interval;
505
12
}
506
507
12
inline uint64_t Environment::cpu_prof_interval() const {
508
12
  return cpu_prof_interval_;
509
}
510
511
12
inline void Environment::set_cpu_prof_name(const std::string& name) {
512
12
  cpu_prof_name_ = name;
513
12
}
514
515
12
inline const std::string& Environment::cpu_prof_name() const {
516
12
  return cpu_prof_name_;
517
}
518
519
12
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
520
12
  cpu_prof_dir_ = dir;
521
12
}
522
523
12
inline const std::string& Environment::cpu_prof_dir() const {
524
12
  return cpu_prof_dir_;
525
}
526
527
12
inline void Environment::set_heap_profiler_connection(
528
    std::unique_ptr<profiler::V8HeapProfilerConnection> connection) {
529
12
  CHECK_NULL(heap_profiler_connection_);
530
12
  std::swap(heap_profiler_connection_, connection);
531
12
}
532
533
inline profiler::V8HeapProfilerConnection*
534
6364
Environment::heap_profiler_connection() {
535
6364
  return heap_profiler_connection_.get();
536
}
537
538
12
inline void Environment::set_heap_prof_name(const std::string& name) {
539
12
  heap_prof_name_ = name;
540
12
}
541
542
12
inline const std::string& Environment::heap_prof_name() const {
543
12
  return heap_prof_name_;
544
}
545
546
12
inline void Environment::set_heap_prof_dir(const std::string& dir) {
547
12
  heap_prof_dir_ = dir;
548
12
}
549
550
12
inline const std::string& Environment::heap_prof_dir() const {
551
12
  return heap_prof_dir_;
552
}
553
554
12
inline void Environment::set_heap_prof_interval(uint64_t interval) {
555
12
  heap_prof_interval_ = interval;
556
12
}
557
558
12
inline uint64_t Environment::heap_prof_interval() const {
559
12
  return heap_prof_interval_;
560
}
561
562
#endif  // HAVE_INSPECTOR
563
564
inline
565
12999
std::shared_ptr<ExclusiveAccess<HostPort>> Environment::inspector_host_port() {
566
12999
  return inspector_host_port_;
567
}
568
569
64717
inline std::shared_ptr<PerIsolateOptions> IsolateData::options() {
570
64717
  return options_;
571
}
572
573
295
inline void IsolateData::set_options(
574
    std::shared_ptr<PerIsolateOptions> options) {
575
295
  options_ = std::move(options);
576
295
}
577
578
template <typename Fn>
579
118338
void Environment::SetImmediate(Fn&& cb, CallbackFlags::Flags flags) {
580
236676
  auto callback = native_immediates_.CreateCallback(std::move(cb), flags);
581
118338
  native_immediates_.Push(std::move(callback));
582
583
118338
  if (flags & CallbackFlags::kRefed) {
584
93430
    if (immediate_info()->ref_count() == 0)
585
48090
      ToggleImmediateRef(true);
586
93430
    immediate_info()->ref_count_inc(1);
587
  }
588
118338
}
589
590
template <typename Fn>
591
2325
void Environment::SetImmediateThreadsafe(Fn&& cb, CallbackFlags::Flags flags) {
592
4650
  auto callback = native_immediates_threadsafe_.CreateCallback(
593
2325
      std::move(cb), flags);
594
  {
595
4650
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
596
2325
    native_immediates_threadsafe_.Push(std::move(callback));
597
2325
    if (task_queues_async_initialized_)
598
2316
      uv_async_send(&task_queues_async_);
599
  }
600
2325
}
601
602
template <typename Fn>
603
10599
void Environment::RequestInterrupt(Fn&& cb) {
604
21198
  auto callback = native_immediates_interrupts_.CreateCallback(
605
10599
      std::move(cb), CallbackFlags::kRefed);
606
  {
607
21198
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
608
10599
    native_immediates_interrupts_.Push(std::move(callback));
609
10599
    if (task_queues_async_initialized_)
610
4109
      uv_async_send(&task_queues_async_);
611
  }
612
10599
  RequestInterruptFromV8();
613
10599
}
614
615
3616362
inline bool Environment::can_call_into_js() const {
616

3616362
  return can_call_into_js_ && !is_stopping();
617
}
618
619
1766
inline void Environment::set_can_call_into_js(bool can_call_into_js) {
620
1766
  can_call_into_js_ = can_call_into_js;
621
1766
}
622
623
1641829
inline bool Environment::has_run_bootstrapping_code() const {
624
1641829
  return principal_realm_->has_run_bootstrapping_code();
625
}
626
627
20
inline bool Environment::has_serialized_options() const {
628
20
  return has_serialized_options_;
629
}
630
631
7004
inline void Environment::set_has_serialized_options(bool value) {
632
7004
  has_serialized_options_ = value;
633
7004
}
634
635
9562
inline bool Environment::is_main_thread() const {
636
9562
  return worker_context() == nullptr;
637
}
638
639
1162
inline bool Environment::no_native_addons() const {
640
2320
  return (flags_ & EnvironmentFlags::kNoNativeAddons) ||
641
2320
          !options_->allow_native_addons;
642
}
643
644
6331
inline bool Environment::should_not_register_esm_loader() const {
645
6331
  return flags_ & EnvironmentFlags::kNoRegisterESMLoader;
646
}
647
648
16549
inline bool Environment::owns_process_state() const {
649
16549
  return flags_ & EnvironmentFlags::kOwnsProcessState;
650
}
651
652
6374
inline bool Environment::owns_inspector() const {
653
6374
  return flags_ & EnvironmentFlags::kOwnsInspector;
654
}
655
656
17236
inline bool Environment::should_create_inspector() const {
657
34472
  return (flags_ & EnvironmentFlags::kNoCreateInspector) == 0 &&
658

17236
         !options_->test_runner && !options_->watch_mode;
659
}
660
661
137356
inline bool Environment::tracks_unmanaged_fds() const {
662
137356
  return flags_ & EnvironmentFlags::kTrackUnmanagedFds;
663
}
664
665
4035
inline bool Environment::hide_console_windows() const {
666
4035
  return flags_ & EnvironmentFlags::kHideConsoleWindows;
667
}
668
669
7300
inline bool Environment::no_global_search_paths() const {
670
14600
  return (flags_ & EnvironmentFlags::kNoGlobalSearchPaths) ||
671
14600
         !options_->global_search_paths;
672
}
673
674
1756
inline bool Environment::no_browser_globals() const {
675
  // configure --no-browser-globals
676
#ifdef NODE_NO_BROWSER_GLOBALS
677
  return true;
678
#else
679
1756
  return flags_ & EnvironmentFlags::kNoBrowserGlobals;
680
#endif
681
}
682
683
5
bool Environment::filehandle_close_warning() const {
684
5
  return emit_filehandle_warning_;
685
}
686
687
4
void Environment::set_filehandle_close_warning(bool on) {
688
4
  emit_filehandle_warning_ = on;
689
4
}
690
691
6350
void Environment::set_source_maps_enabled(bool on) {
692
6350
  source_maps_enabled_ = on;
693
6350
}
694
695
2086
bool Environment::source_maps_enabled() const {
696
2086
  return source_maps_enabled_;
697
}
698
699
7927
inline uint64_t Environment::thread_id() const {
700
7927
  return thread_id_;
701
}
702
703
16620
inline worker::Worker* Environment::worker_context() const {
704
16620
  return isolate_data()->worker_context();
705
}
706
707
966
inline void Environment::add_sub_worker_context(worker::Worker* context) {
708
966
  sub_worker_contexts_.insert(context);
709
966
}
710
711
994
inline void Environment::remove_sub_worker_context(worker::Worker* context) {
712
994
  sub_worker_contexts_.erase(context);
713
994
}
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
5281937
inline bool Environment::is_stopping() const {
721
5281937
  return is_stopping_.load();
722
}
723
724
6047
inline void Environment::set_stopping(bool value) {
725
6047
  is_stopping_.store(value);
726
6047
}
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
43133
inline performance::PerformanceState* Environment::performance_state() {
747
43133
  return performance_state_.get();
748
}
749
750
9162310
inline IsolateData* Environment::isolate_data() const {
751
9162310
  return isolate_data_;
752
}
753
754
template <typename T>
755
5319
inline void Environment::ForEachRealm(T&& iterator) const {
756
  // TODO(legendecas): iterate over more realms bound to the environment.
757
5319
  iterator(principal_realm());
758
5319
}
759
760
8
inline void Environment::ThrowError(const char* errmsg) {
761
8
  ThrowError(v8::Exception::Error, errmsg);
762
8
}
763
764
inline void Environment::ThrowTypeError(const char* errmsg) {
765
  ThrowError(v8::Exception::TypeError, errmsg);
766
}
767
768
inline void Environment::ThrowRangeError(const char* errmsg) {
769
  ThrowError(v8::Exception::RangeError, errmsg);
770
}
771
772
8
inline void Environment::ThrowError(
773
    v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
774
    const char* errmsg) {
775
16
  v8::HandleScope handle_scope(isolate());
776
8
  isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
777
8
}
778
779
11
inline void Environment::ThrowErrnoException(int errorno,
780
                                             const char* syscall,
781
                                             const char* message,
782
                                             const char* path) {
783
  isolate()->ThrowException(
784
11
      ErrnoException(isolate(), errorno, syscall, message, path));
785
11
}
786
787
15
inline void Environment::ThrowUVException(int errorno,
788
                                          const char* syscall,
789
                                          const char* message,
790
                                          const char* path,
791
                                          const char* dest) {
792
  isolate()->ThrowException(
793
15
      UVException(isolate(), errorno, syscall, message, path, dest));
794
15
}
795
796
57560
void Environment::AddCleanupHook(CleanupQueue::Callback fn, void* arg) {
797
57560
  cleanup_queue_.Add(fn, arg);
798
57560
}
799
800
37996
void Environment::RemoveCleanupHook(CleanupQueue::Callback fn, void* arg) {
801
37996
  cleanup_queue_.Remove(fn, arg);
802
37996
}
803
804
19
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
805
19
  CHECK(!main_utf16_);
806
19
  main_utf16_ = std::move(str);
807
19
}
808
809
728
void Environment::set_process_exit_handler(
810
    std::function<void(Environment*, ExitCode)>&& handler) {
811
728
  process_exit_handler_ = std::move(handler);
812
728
}
813
814
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
815
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
816
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
817
#define V(TypeName, PropertyName)                                             \
818
  inline                                                                      \
819
  v8::Local<TypeName> IsolateData::PropertyName() const {                     \
820
    return PropertyName ## _ .Get(isolate_);                                  \
821
  }
822
47610
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
823
1213498
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
824
8370444
  PER_ISOLATE_STRING_PROPERTIES(VS)
825
#undef V
826
#undef VS
827
#undef VY
828
#undef VP
829
830
#define V(PropertyName, TypeName)                                              \
831
  inline v8::Local<TypeName> IsolateData::PropertyName() const {               \
832
    return PropertyName##_.Get(isolate_);                                      \
833
  }                                                                            \
834
  inline void IsolateData::set_##PropertyName(v8::Local<TypeName> value) {     \
835
    PropertyName##_.Set(isolate_, value);                                      \
836
  }
837
1245646
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
838
#undef V
839
840
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
841
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
842
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
843
#define V(TypeName, PropertyName)                                             \
844
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
845
    return isolate_data()->PropertyName();                                    \
846
  }
847
47610
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
848
1093157
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
849
12110070
  PER_ISOLATE_STRING_PROPERTIES(VS)
850
#undef V
851
#undef VS
852
#undef VY
853
#undef VP
854
855
#define V(PropertyName, TypeName)                                              \
856
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
857
    return isolate_data()->PropertyName();                                     \
858
  }                                                                            \
859
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
860
    DCHECK(isolate_data()->PropertyName().IsEmpty());                          \
861
    isolate_data()->set_##PropertyName(value);                                 \
862
  }
863
791245
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
864
#undef V
865
866
#define V(PropertyName, TypeName)                                              \
867
  inline v8::Local<TypeName> Environment::PropertyName() const {               \
868
    DCHECK_NOT_NULL(principal_realm_);                                         \
869
    return principal_realm_->PropertyName();                                   \
870
  }                                                                            \
871
  inline void Environment::set_##PropertyName(v8::Local<TypeName> value) {     \
872
    DCHECK_NOT_NULL(principal_realm_);                                         \
873
    principal_realm_->set_##PropertyName(value);                               \
874
  }
875
9604058
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
876
#undef V
877
878
9549511
v8::Local<v8::Context> Environment::context() const {
879
9549511
  return principal_realm()->context();
880
}
881
882
11025684
Realm* Environment::principal_realm() const {
883
11025684
  return principal_realm_.get();
884
}
885
886
1
inline void Environment::set_heap_snapshot_near_heap_limit(uint32_t limit) {
887
1
  heap_snapshot_near_heap_limit_ = limit;
888
1
}
889
890
inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
891
  return is_in_heapsnapshot_heap_limit_callback_;
892
}
893
894
2
inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
895
  DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
896
2
  heapsnapshot_near_heap_limit_callback_added_ = true;
897
2
  isolate_->AddNearHeapLimitCallback(Environment::NearHeapLimitCallback, this);
898
2
}
899
900
2
inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
901
    size_t heap_limit) {
902
  DCHECK(heapsnapshot_near_heap_limit_callback_added_);
903
2
  heapsnapshot_near_heap_limit_callback_added_ = false;
904
2
  isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback,
905
                                        heap_limit);
906
2
}
907
908
}  // namespace node
909
910
// These two files depend on each other. Including base_object-inl.h after this
911
// file is the easiest way to avoid issues with that circular dependency.
912
#include "base_object-inl.h"
913
914
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
915
916
#endif  // SRC_ENV_INL_H_