GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "node.h" |
||
2 |
#include "node_context_data.h" |
||
3 |
#include "node_errors.h" |
||
4 |
#include "node_internals.h" |
||
5 |
#include "node_native_module_env.h" |
||
6 |
#include "node_platform.h" |
||
7 |
#include "node_v8_platform-inl.h" |
||
8 |
#include "uv.h" |
||
9 |
|||
10 |
#if HAVE_INSPECTOR |
||
11 |
#include "inspector/worker_inspector.h" // ParentInspectorHandle |
||
12 |
#endif |
||
13 |
|||
14 |
namespace node { |
||
15 |
using errors::TryCatchScope; |
||
16 |
using v8::Array; |
||
17 |
using v8::Context; |
||
18 |
using v8::EscapableHandleScope; |
||
19 |
using v8::Function; |
||
20 |
using v8::FunctionCallbackInfo; |
||
21 |
using v8::HandleScope; |
||
22 |
using v8::Isolate; |
||
23 |
using v8::Local; |
||
24 |
using v8::MaybeLocal; |
||
25 |
using v8::Null; |
||
26 |
using v8::Object; |
||
27 |
using v8::ObjectTemplate; |
||
28 |
using v8::Private; |
||
29 |
using v8::PropertyDescriptor; |
||
30 |
using v8::SealHandleScope; |
||
31 |
using v8::String; |
||
32 |
using v8::Value; |
||
33 |
|||
34 |
91 |
bool AllowWasmCodeGenerationCallback(Local<Context> context, |
|
35 |
Local<String>) { |
||
36 |
Local<Value> wasm_code_gen = |
||
37 |
182 |
context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration); |
|
38 |
✓✗✓✓ |
273 |
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue(); |
39 |
} |
||
40 |
|||
41 |
33 |
bool ShouldAbortOnUncaughtException(Isolate* isolate) { |
|
42 |
33 |
DebugSealHandleScope scope(isolate); |
|
43 |
33 |
Environment* env = Environment::GetCurrent(isolate); |
|
44 |
✓✓ | 33 |
return env != nullptr && |
45 |
✓✗✓✓ |
68 |
(env->is_main_thread() || !env->is_stopping()) && |
46 |
✓✓ | 63 |
env->abort_on_uncaught_exception() && |
47 |
✓✗✗✓ |
97 |
env->should_abort_on_uncaught_toggle()[0] && |
48 |
67 |
!env->inside_should_not_abort_on_uncaught_scope(); |
|
49 |
} |
||
50 |
|||
51 |
71799 |
MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context, |
|
52 |
Local<Value> exception, |
||
53 |
Local<Array> trace) { |
||
54 |
71799 |
Environment* env = Environment::GetCurrent(context); |
|
55 |
✗✓ | 71799 |
if (env == nullptr) { |
56 |
return exception->ToString(context).FromMaybe(Local<Value>()); |
||
57 |
} |
||
58 |
71799 |
Local<Function> prepare = env->prepare_stack_trace_callback(); |
|
59 |
✗✓ | 71799 |
if (prepare.IsEmpty()) { |
60 |
return exception->ToString(context).FromMaybe(Local<Value>()); |
||
61 |
} |
||
62 |
Local<Value> args[] = { |
||
63 |
context->Global(), |
||
64 |
exception, |
||
65 |
trace, |
||
66 |
287196 |
}; |
|
67 |
// This TryCatch + Rethrow is required by V8 due to details around exception |
||
68 |
// handling there. For C++ callbacks, V8 expects a scheduled exception (which |
||
69 |
// is what ReThrow gives us). Just returning the empty MaybeLocal would leave |
||
70 |
// us with a pending exception. |
||
71 |
143598 |
TryCatchScope try_catch(env); |
|
72 |
MaybeLocal<Value> result = prepare->Call( |
||
73 |
143598 |
context, Undefined(env->isolate()), arraysize(args), args); |
|
74 |
✓✓✓✗ ✓✓ |
71799 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
75 |
2 |
try_catch.ReThrow(); |
|
76 |
} |
||
77 |
71799 |
return result; |
|
78 |
} |
||
79 |
|||
80 |
414995 |
void* NodeArrayBufferAllocator::Allocate(size_t size) { |
|
81 |
void* ret; |
||
82 |
✓✓✓✓ ✓✓ |
414995 |
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) |
83 |
32353 |
ret = UncheckedCalloc(size); |
|
84 |
else |
||
85 |
382642 |
ret = UncheckedMalloc(size); |
|
86 |
✓✗ | 414995 |
if (LIKELY(ret != nullptr)) |
87 |
414995 |
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); |
|
88 |
414995 |
return ret; |
|
89 |
} |
||
90 |
|||
91 |
93377 |
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) { |
|
92 |
93377 |
void* ret = node::UncheckedMalloc(size); |
|
93 |
✓✗ | 93377 |
if (LIKELY(ret != nullptr)) |
94 |
93377 |
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); |
|
95 |
93377 |
return ret; |
|
96 |
} |
||
97 |
|||
98 |
307546 |
void* NodeArrayBufferAllocator::Reallocate( |
|
99 |
void* data, size_t old_size, size_t size) { |
||
100 |
307546 |
void* ret = UncheckedRealloc<char>(static_cast<char*>(data), size); |
|
101 |
✗✓✗✗ |
307546 |
if (LIKELY(ret != nullptr) || UNLIKELY(size == 0)) |
102 |
307546 |
total_mem_usage_.fetch_add(size - old_size, std::memory_order_relaxed); |
|
103 |
307546 |
return ret; |
|
104 |
} |
||
105 |
|||
106 |
497618 |
void NodeArrayBufferAllocator::Free(void* data, size_t size) { |
|
107 |
497618 |
total_mem_usage_.fetch_sub(size, std::memory_order_relaxed); |
|
108 |
497618 |
free(data); |
|
109 |
497618 |
} |
|
110 |
|||
111 |
9 |
DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() { |
|
112 |
✗✓ | 3 |
CHECK(allocations_.empty()); |
113 |
6 |
} |
|
114 |
|||
115 |
35 |
void* DebuggingArrayBufferAllocator::Allocate(size_t size) { |
|
116 |
70 |
Mutex::ScopedLock lock(mutex_); |
|
117 |
35 |
void* data = NodeArrayBufferAllocator::Allocate(size); |
|
118 |
35 |
RegisterPointerInternal(data, size); |
|
119 |
70 |
return data; |
|
120 |
} |
||
121 |
|||
122 |
28 |
void* DebuggingArrayBufferAllocator::AllocateUninitialized(size_t size) { |
|
123 |
56 |
Mutex::ScopedLock lock(mutex_); |
|
124 |
28 |
void* data = NodeArrayBufferAllocator::AllocateUninitialized(size); |
|
125 |
28 |
RegisterPointerInternal(data, size); |
|
126 |
56 |
return data; |
|
127 |
} |
||
128 |
|||
129 |
63 |
void DebuggingArrayBufferAllocator::Free(void* data, size_t size) { |
|
130 |
126 |
Mutex::ScopedLock lock(mutex_); |
|
131 |
63 |
UnregisterPointerInternal(data, size); |
|
132 |
63 |
NodeArrayBufferAllocator::Free(data, size); |
|
133 |
63 |
} |
|
134 |
|||
135 |
void* DebuggingArrayBufferAllocator::Reallocate(void* data, |
||
136 |
size_t old_size, |
||
137 |
size_t size) { |
||
138 |
Mutex::ScopedLock lock(mutex_); |
||
139 |
void* ret = NodeArrayBufferAllocator::Reallocate(data, old_size, size); |
||
140 |
if (ret == nullptr) { |
||
141 |
if (size == 0) // i.e. equivalent to free(). |
||
142 |
UnregisterPointerInternal(data, old_size); |
||
143 |
return nullptr; |
||
144 |
} |
||
145 |
|||
146 |
if (data != nullptr) { |
||
147 |
auto it = allocations_.find(data); |
||
148 |
CHECK_NE(it, allocations_.end()); |
||
149 |
allocations_.erase(it); |
||
150 |
} |
||
151 |
|||
152 |
RegisterPointerInternal(ret, size); |
||
153 |
return ret; |
||
154 |
} |
||
155 |
|||
156 |
void DebuggingArrayBufferAllocator::RegisterPointer(void* data, size_t size) { |
||
157 |
Mutex::ScopedLock lock(mutex_); |
||
158 |
NodeArrayBufferAllocator::RegisterPointer(data, size); |
||
159 |
RegisterPointerInternal(data, size); |
||
160 |
} |
||
161 |
|||
162 |
void DebuggingArrayBufferAllocator::UnregisterPointer(void* data, size_t size) { |
||
163 |
Mutex::ScopedLock lock(mutex_); |
||
164 |
NodeArrayBufferAllocator::UnregisterPointer(data, size); |
||
165 |
UnregisterPointerInternal(data, size); |
||
166 |
} |
||
167 |
|||
168 |
63 |
void DebuggingArrayBufferAllocator::UnregisterPointerInternal(void* data, |
|
169 |
size_t size) { |
||
170 |
✗✓ | 63 |
if (data == nullptr) return; |
171 |
63 |
auto it = allocations_.find(data); |
|
172 |
✗✓ | 63 |
CHECK_NE(it, allocations_.end()); |
173 |
✓✗ | 63 |
if (size > 0) { |
174 |
// We allow allocations with size 1 for 0-length buffers to avoid having |
||
175 |
// to deal with nullptr values. |
||
176 |
✗✓ | 63 |
CHECK_EQ(it->second, size); |
177 |
} |
||
178 |
63 |
allocations_.erase(it); |
|
179 |
} |
||
180 |
|||
181 |
63 |
void DebuggingArrayBufferAllocator::RegisterPointerInternal(void* data, |
|
182 |
size_t size) { |
||
183 |
✗✓ | 63 |
if (data == nullptr) return; |
184 |
✗✓ | 63 |
CHECK_EQ(allocations_.count(data), 0); |
185 |
63 |
allocations_[data] = size; |
|
186 |
} |
||
187 |
|||
188 |
5073 |
std::unique_ptr<ArrayBufferAllocator> ArrayBufferAllocator::Create(bool debug) { |
|
189 |
✓✗✓✓ ✓✓ |
5073 |
if (debug || per_process::cli_options->debug_arraybuffer_allocations) |
190 |
3 |
return std::make_unique<DebuggingArrayBufferAllocator>(); |
|
191 |
else |
||
192 |
5070 |
return std::make_unique<NodeArrayBufferAllocator>(); |
|
193 |
} |
||
194 |
|||
195 |
52 |
ArrayBufferAllocator* CreateArrayBufferAllocator() { |
|
196 |
52 |
return ArrayBufferAllocator::Create().release(); |
|
197 |
} |
||
198 |
|||
199 |
52 |
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) { |
|
200 |
✓✗ | 52 |
delete allocator; |
201 |
52 |
} |
|
202 |
|||
203 |
5069 |
void SetIsolateCreateParamsForNode(Isolate::CreateParams* params) { |
|
204 |
5069 |
const uint64_t constrained_memory = uv_get_constrained_memory(); |
|
205 |
✓✓ | 10122 |
const uint64_t total_memory = constrained_memory > 0 ? |
206 |
10123 |
std::min(uv_get_total_memory(), constrained_memory) : |
|
207 |
5071 |
uv_get_total_memory(); |
|
208 |
✓✗ | 5071 |
if (total_memory > 0) { |
209 |
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively. |
||
210 |
// This default is based on browser use-cases. Tell V8 to configure the |
||
211 |
// heap based on the actual physical memory. |
||
212 |
5071 |
params->constraints.ConfigureDefaults(total_memory, 0); |
|
213 |
} |
||
214 |
5071 |
params->embedder_wrapper_object_index = BaseObject::InternalFields::kSlot; |
|
215 |
5071 |
params->embedder_wrapper_type_index = std::numeric_limits<int>::max(); |
|
216 |
5071 |
} |
|
217 |
|||
218 |
5071 |
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) { |
|
219 |
✓✗ | 5071 |
if (s.flags & MESSAGE_LISTENER_WITH_ERROR_LEVEL) |
220 |
5071 |
isolate->AddMessageListenerWithErrorLevel( |
|
221 |
errors::PerIsolateMessageListener, |
||
222 |
Isolate::MessageErrorLevel::kMessageError | |
||
223 |
5071 |
Isolate::MessageErrorLevel::kMessageWarning); |
|
224 |
|||
225 |
✗✓ | 5071 |
auto* abort_callback = s.should_abort_on_uncaught_exception_callback ? |
226 |
s.should_abort_on_uncaught_exception_callback : |
||
227 |
5071 |
ShouldAbortOnUncaughtException; |
|
228 |
5071 |
isolate->SetAbortOnUncaughtExceptionCallback(abort_callback); |
|
229 |
|||
230 |
✗✓ | 5071 |
auto* fatal_error_cb = s.fatal_error_callback ? |
231 |
5071 |
s.fatal_error_callback : OnFatalError; |
|
232 |
5071 |
isolate->SetFatalErrorHandler(fatal_error_cb); |
|
233 |
|||
234 |
✓✗ | 5071 |
if ((s.flags & SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK) == 0) { |
235 |
✗✓ | 5071 |
auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ? |
236 |
5071 |
s.prepare_stack_trace_callback : PrepareStackTraceCallback; |
|
237 |
5071 |
isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb); |
|
238 |
} |
||
239 |
5071 |
} |
|
240 |
|||
241 |
5079 |
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) { |
|
242 |
5079 |
isolate->SetMicrotasksPolicy(s.policy); |
|
243 |
|||
244 |
✗✓ | 5079 |
auto* allow_wasm_codegen_cb = s.allow_wasm_code_generation_callback ? |
245 |
5079 |
s.allow_wasm_code_generation_callback : AllowWasmCodeGenerationCallback; |
|
246 |
5079 |
isolate->SetAllowWasmCodeGenerationCallback(allow_wasm_codegen_cb); |
|
247 |
|||
248 |
✓✗ | 5079 |
if ((s.flags & SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK) == 0) { |
249 |
✗✓ | 5079 |
auto* promise_reject_cb = s.promise_reject_callback ? |
250 |
5079 |
s.promise_reject_callback : PromiseRejectCallback; |
|
251 |
5079 |
isolate->SetPromiseRejectCallback(promise_reject_cb); |
|
252 |
} |
||
253 |
|||
254 |
✓✗ | 5079 |
if (s.flags & DETAILED_SOURCE_POSITIONS_FOR_PROFILING) |
255 |
5079 |
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate); |
|
256 |
5079 |
} |
|
257 |
|||
258 |
464 |
void SetIsolateUpForNode(v8::Isolate* isolate, |
|
259 |
const IsolateSettings& settings) { |
||
260 |
464 |
SetIsolateErrorHandlers(isolate, settings); |
|
261 |
464 |
SetIsolateMiscHandlers(isolate, settings); |
|
262 |
464 |
} |
|
263 |
|||
264 |
464 |
void SetIsolateUpForNode(v8::Isolate* isolate) { |
|
265 |
464 |
IsolateSettings settings; |
|
266 |
464 |
SetIsolateUpForNode(isolate, settings); |
|
267 |
464 |
} |
|
268 |
|||
269 |
// TODO(joyeecheung): we may want to expose this, but then we need to be |
||
270 |
// careful about what we override in the params. |
||
271 |
58 |
Isolate* NewIsolate(Isolate::CreateParams* params, |
|
272 |
uv_loop_t* event_loop, |
||
273 |
MultiIsolatePlatform* platform) { |
||
274 |
58 |
Isolate* isolate = Isolate::Allocate(); |
|
275 |
✗✓ | 58 |
if (isolate == nullptr) return nullptr; |
276 |
|||
277 |
// Register the isolate on the platform before the isolate gets initialized, |
||
278 |
// so that the isolate can access the platform during initialization. |
||
279 |
58 |
platform->RegisterIsolate(isolate, event_loop); |
|
280 |
|||
281 |
58 |
SetIsolateCreateParamsForNode(params); |
|
282 |
58 |
Isolate::Initialize(isolate, *params); |
|
283 |
58 |
SetIsolateUpForNode(isolate); |
|
284 |
|||
285 |
58 |
return isolate; |
|
286 |
} |
||
287 |
|||
288 |
50 |
Isolate* NewIsolate(ArrayBufferAllocator* allocator, |
|
289 |
uv_loop_t* event_loop, |
||
290 |
MultiIsolatePlatform* platform) { |
||
291 |
100 |
Isolate::CreateParams params; |
|
292 |
✓✗ | 50 |
if (allocator != nullptr) params.array_buffer_allocator = allocator; |
293 |
100 |
return NewIsolate(¶ms, event_loop, platform); |
|
294 |
} |
||
295 |
|||
296 |
8 |
Isolate* NewIsolate(std::shared_ptr<ArrayBufferAllocator> allocator, |
|
297 |
uv_loop_t* event_loop, |
||
298 |
MultiIsolatePlatform* platform) { |
||
299 |
16 |
Isolate::CreateParams params; |
|
300 |
✓✗ | 8 |
if (allocator) params.array_buffer_allocator_shared = allocator; |
301 |
16 |
return NewIsolate(¶ms, event_loop, platform); |
|
302 |
} |
||
303 |
|||
304 |
450 |
IsolateData* CreateIsolateData(Isolate* isolate, |
|
305 |
uv_loop_t* loop, |
||
306 |
MultiIsolatePlatform* platform, |
||
307 |
ArrayBufferAllocator* allocator) { |
||
308 |
450 |
return new IsolateData(isolate, loop, platform, allocator); |
|
309 |
} |
||
310 |
|||
311 |
448 |
void FreeIsolateData(IsolateData* isolate_data) { |
|
312 |
✓✗ | 448 |
delete isolate_data; |
313 |
448 |
} |
|
314 |
|||
315 |
635 |
InspectorParentHandle::~InspectorParentHandle() {} |
|
316 |
|||
317 |
// Hide the internal handle class from the public API. |
||
318 |
#if HAVE_INSPECTOR |
||
319 |
1270 |
struct InspectorParentHandleImpl : public InspectorParentHandle { |
|
320 |
std::unique_ptr<inspector::ParentInspectorHandle> impl; |
||
321 |
|||
322 |
635 |
explicit InspectorParentHandleImpl( |
|
323 |
std::unique_ptr<inspector::ParentInspectorHandle>&& impl) |
||
324 |
635 |
: impl(std::move(impl)) {} |
|
325 |
}; |
||
326 |
#endif |
||
327 |
|||
328 |
435 |
Environment* CreateEnvironment( |
|
329 |
IsolateData* isolate_data, |
||
330 |
Local<Context> context, |
||
331 |
const std::vector<std::string>& args, |
||
332 |
const std::vector<std::string>& exec_args, |
||
333 |
EnvironmentFlags::Flags flags, |
||
334 |
ThreadId thread_id, |
||
335 |
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) { |
||
336 |
435 |
Isolate* isolate = context->GetIsolate(); |
|
337 |
870 |
HandleScope handle_scope(isolate); |
|
338 |
Context::Scope context_scope(context); |
||
339 |
// TODO(addaleax): This is a much better place for parsing per-Environment |
||
340 |
// options than the global parse call. |
||
341 |
Environment* env = new Environment( |
||
342 |
435 |
isolate_data, context, args, exec_args, nullptr, flags, thread_id); |
|
343 |
#if HAVE_INSPECTOR |
||
344 |
✓✓ | 435 |
if (inspector_parent_handle) { |
345 |
783 |
env->InitializeInspector( |
|
346 |
std::move(static_cast<InspectorParentHandleImpl*>( |
||
347 |
784 |
inspector_parent_handle.get())->impl)); |
|
348 |
} else { |
||
349 |
43 |
env->InitializeInspector({}); |
|
350 |
} |
||
351 |
#endif |
||
352 |
|||
353 |
✗✓ | 869 |
if (env->RunBootstrapping().IsEmpty()) { |
354 |
FreeEnvironment(env); |
||
355 |
return nullptr; |
||
356 |
} |
||
357 |
|||
358 |
435 |
return env; |
|
359 |
} |
||
360 |
|||
361 |
4577 |
void FreeEnvironment(Environment* env) { |
|
362 |
Isolate::DisallowJavascriptExecutionScope disallow_js(env->isolate(), |
||
363 |
9155 |
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); |
|
364 |
{ |
||
365 |
9156 |
HandleScope handle_scope(env->isolate()); // For env->context(). |
|
366 |
4578 |
Context::Scope context_scope(env->context()); |
|
367 |
9156 |
SealHandleScope seal_handle_scope(env->isolate()); |
|
368 |
|||
369 |
4578 |
env->set_stopping(true); |
|
370 |
4578 |
env->stop_sub_worker_contexts(); |
|
371 |
4577 |
env->RunCleanup(); |
|
372 |
4577 |
RunAtExit(env); |
|
373 |
} |
||
374 |
|||
375 |
// This call needs to be made while the `Environment` is still alive |
||
376 |
// because we assume that it is available for async tracking in the |
||
377 |
// NodePlatform implementation. |
||
378 |
4578 |
MultiIsolatePlatform* platform = env->isolate_data()->platform(); |
|
379 |
✓✗ | 4578 |
if (platform != nullptr) |
380 |
4578 |
platform->DrainTasks(env->isolate()); |
|
381 |
|||
382 |
✓✗ | 4578 |
delete env; |
383 |
4578 |
} |
|
384 |
|||
385 |
635 |
NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( |
|
386 |
Environment* env, |
||
387 |
ThreadId thread_id, |
||
388 |
const char* url) { |
||
389 |
✗✓ | 635 |
CHECK_NOT_NULL(env); |
390 |
✗✓ | 635 |
CHECK_NE(thread_id.id, static_cast<uint64_t>(-1)); |
391 |
#if HAVE_INSPECTOR |
||
392 |
1270 |
return std::make_unique<InspectorParentHandleImpl>( |
|
393 |
1905 |
env->inspector_agent()->GetParentHandle(thread_id.id, url)); |
|
394 |
#else |
||
395 |
return {}; |
||
396 |
#endif |
||
397 |
} |
||
398 |
|||
399 |
5017 |
MaybeLocal<Value> LoadEnvironment( |
|
400 |
Environment* env, |
||
401 |
StartExecutionCallback cb) { |
||
402 |
5017 |
env->InitializeLibuv(); |
|
403 |
5017 |
env->InitializeDiagnostics(); |
|
404 |
|||
405 |
5017 |
return StartExecution(env, cb); |
|
406 |
} |
||
407 |
|||
408 |
14 |
MaybeLocal<Value> LoadEnvironment( |
|
409 |
Environment* env, |
||
410 |
const char* main_script_source_utf8) { |
||
411 |
✗✓ | 14 |
CHECK_NOT_NULL(main_script_source_utf8); |
412 |
return LoadEnvironment( |
||
413 |
env, |
||
414 |
14 |
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> { |
|
415 |
// This is a slightly hacky way to convert UTF-8 to UTF-16. |
||
416 |
Local<String> str = |
||
417 |
140 |
String::NewFromUtf8(env->isolate(), |
|
418 |
42 |
main_script_source_utf8).ToLocalChecked(); |
|
419 |
26 |
auto main_utf16 = std::make_unique<String::Value>(env->isolate(), str); |
|
420 |
|||
421 |
// TODO(addaleax): Avoid having a global table for all scripts. |
||
422 |
26 |
std::string name = "embedder_main_" + std::to_string(env->thread_id()); |
|
423 |
14 |
native_module::NativeModuleEnv::Add( |
|
424 |
name.c_str(), |
||
425 |
28 |
UnionBytes(**main_utf16, main_utf16->length())); |
|
426 |
28 |
env->set_main_utf16(std::move(main_utf16)); |
|
427 |
std::vector<Local<String>> params = { |
||
428 |
14 |
env->process_string(), |
|
429 |
40 |
env->require_string()}; |
|
430 |
std::vector<Local<Value>> args = { |
||
431 |
env->process_object(), |
||
432 |
54 |
env->native_module_require()}; |
|
433 |
40 |
return ExecuteBootstrapper(env, name.c_str(), ¶ms, &args); |
|
434 |
14 |
}); |
|
435 |
} |
||
436 |
|||
437 |
5 |
Environment* GetCurrentEnvironment(Local<Context> context) { |
|
438 |
5 |
return Environment::GetCurrent(context); |
|
439 |
} |
||
440 |
|||
441 |
4691 |
MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env) { |
|
442 |
4691 |
return GetMultiIsolatePlatform(env->isolate_data()); |
|
443 |
} |
||
444 |
|||
445 |
4691 |
MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env) { |
|
446 |
4691 |
return env->platform(); |
|
447 |
} |
||
448 |
|||
449 |
MultiIsolatePlatform* CreatePlatform( |
||
450 |
int thread_pool_size, |
||
451 |
node::tracing::TracingController* tracing_controller) { |
||
452 |
return CreatePlatform( |
||
453 |
thread_pool_size, |
||
454 |
static_cast<v8::TracingController*>(tracing_controller)); |
||
455 |
} |
||
456 |
|||
457 |
MultiIsolatePlatform* CreatePlatform( |
||
458 |
int thread_pool_size, |
||
459 |
v8::TracingController* tracing_controller) { |
||
460 |
return MultiIsolatePlatform::Create(thread_pool_size, tracing_controller) |
||
461 |
.release(); |
||
462 |
} |
||
463 |
|||
464 |
void FreePlatform(MultiIsolatePlatform* platform) { |
||
465 |
delete platform; |
||
466 |
} |
||
467 |
|||
468 |
7 |
std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create( |
|
469 |
int thread_pool_size, |
||
470 |
v8::TracingController* tracing_controller) { |
||
471 |
7 |
return std::make_unique<NodePlatform>(thread_pool_size, tracing_controller); |
|
472 |
} |
||
473 |
|||
474 |
34701 |
MaybeLocal<Object> GetPerContextExports(Local<Context> context) { |
|
475 |
34701 |
Isolate* isolate = context->GetIsolate(); |
|
476 |
34701 |
EscapableHandleScope handle_scope(isolate); |
|
477 |
|||
478 |
34701 |
Local<Object> global = context->Global(); |
|
479 |
Local<Private> key = Private::ForApi(isolate, |
||
480 |
34701 |
FIXED_ONE_BYTE_STRING(isolate, "node:per_context_binding_exports")); |
|
481 |
|||
482 |
Local<Value> existing_value; |
||
483 |
✗✓ | 69402 |
if (!global->GetPrivate(context, key).ToLocal(&existing_value)) |
484 |
return MaybeLocal<Object>(); |
||
485 |
✓✓ | 34701 |
if (existing_value->IsObject()) |
486 |
34244 |
return handle_scope.Escape(existing_value.As<Object>()); |
|
487 |
|||
488 |
457 |
Local<Object> exports = Object::New(isolate); |
|
489 |
✓✗✓✓ ✓✓ |
1828 |
if (context->Global()->SetPrivate(context, key, exports).IsNothing() || |
490 |
457 |
!InitializePrimordials(context)) |
|
491 |
4 |
return MaybeLocal<Object>(); |
|
492 |
453 |
return handle_scope.Escape(exports); |
|
493 |
} |
||
494 |
|||
495 |
// Any initialization logic should be performed in |
||
496 |
// InitializeContext, because embedders don't necessarily |
||
497 |
// call NewContext and so they will experience breakages. |
||
498 |
452 |
Local<Context> NewContext(Isolate* isolate, |
|
499 |
Local<ObjectTemplate> object_template) { |
||
500 |
904 |
auto context = Context::New(isolate, nullptr, object_template); |
|
501 |
✗✓ | 452 |
if (context.IsEmpty()) return context; |
502 |
|||
503 |
✓✓ | 452 |
if (!InitializeContext(context)) { |
504 |
4 |
return Local<Context>(); |
|
505 |
} |
||
506 |
|||
507 |
448 |
return context; |
|
508 |
} |
||
509 |
|||
510 |
8 |
void ProtoThrower(const FunctionCallbackInfo<Value>& info) { |
|
511 |
8 |
THROW_ERR_PROTO_ACCESS(info.GetIsolate()); |
|
512 |
8 |
} |
|
513 |
|||
514 |
// This runs at runtime, regardless of whether the context |
||
515 |
// is created from a snapshot. |
||
516 |
5571 |
void InitializeContextRuntime(Local<Context> context) { |
|
517 |
5571 |
Isolate* isolate = context->GetIsolate(); |
|
518 |
11142 |
HandleScope handle_scope(isolate); |
|
519 |
|||
520 |
// Delete `Intl.v8BreakIterator` |
||
521 |
// https://github.com/nodejs/node/issues/14909 |
||
522 |
5571 |
Local<String> intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl"); |
|
523 |
Local<String> break_iter_string = |
||
524 |
5571 |
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); |
|
525 |
Local<Value> intl_v; |
||
526 |
✓✗✓✗ ✓✗ |
22284 |
if (context->Global()->Get(context, intl_string).ToLocal(&intl_v) && |
527 |
5571 |
intl_v->IsObject()) { |
|
528 |
5571 |
Local<Object> intl = intl_v.As<Object>(); |
|
529 |
11142 |
intl->Delete(context, break_iter_string).Check(); |
|
530 |
} |
||
531 |
|||
532 |
// Delete `Atomics.wake` |
||
533 |
// https://github.com/nodejs/node/issues/21219 |
||
534 |
5571 |
Local<String> atomics_string = FIXED_ONE_BYTE_STRING(isolate, "Atomics"); |
|
535 |
5571 |
Local<String> wake_string = FIXED_ONE_BYTE_STRING(isolate, "wake"); |
|
536 |
Local<Value> atomics_v; |
||
537 |
✓✗✓✓ ✓✓ |
22284 |
if (context->Global()->Get(context, atomics_string).ToLocal(&atomics_v) && |
538 |
5571 |
atomics_v->IsObject()) { |
|
539 |
5563 |
Local<Object> atomics = atomics_v.As<Object>(); |
|
540 |
11126 |
atomics->Delete(context, wake_string).Check(); |
|
541 |
} |
||
542 |
|||
543 |
// Remove __proto__ |
||
544 |
// https://github.com/nodejs/node/issues/31951 |
||
545 |
5571 |
Local<String> object_string = FIXED_ONE_BYTE_STRING(isolate, "Object"); |
|
546 |
5571 |
Local<String> prototype_string = FIXED_ONE_BYTE_STRING(isolate, "prototype"); |
|
547 |
11142 |
Local<Object> prototype = context->Global() |
|
548 |
16713 |
->Get(context, object_string) |
|
549 |
11142 |
.ToLocalChecked() |
|
550 |
11142 |
.As<Object>() |
|
551 |
16713 |
->Get(context, prototype_string) |
|
552 |
11142 |
.ToLocalChecked() |
|
553 |
5571 |
.As<Object>(); |
|
554 |
5571 |
Local<String> proto_string = FIXED_ONE_BYTE_STRING(isolate, "__proto__"); |
|
555 |
✓✓ | 5571 |
if (per_process::cli_options->disable_proto == "delete") { |
556 |
8 |
prototype->Delete(context, proto_string).ToChecked(); |
|
557 |
✓✓ | 5567 |
} else if (per_process::cli_options->disable_proto == "throw") { |
558 |
Local<Value> thrower = |
||
559 |
8 |
Function::New(context, ProtoThrower).ToLocalChecked(); |
|
560 |
8 |
PropertyDescriptor descriptor(thrower, thrower); |
|
561 |
4 |
descriptor.set_enumerable(false); |
|
562 |
4 |
descriptor.set_configurable(true); |
|
563 |
8 |
prototype->DefineProperty(context, proto_string, descriptor).ToChecked(); |
|
564 |
✗✓ | 5563 |
} else if (per_process::cli_options->disable_proto != "") { |
565 |
// Validated in ProcessGlobalArgs |
||
566 |
FatalError("InitializeContextRuntime()", "invalid --disable-proto mode"); |
||
567 |
} |
||
568 |
5571 |
} |
|
569 |
|||
570 |
452 |
bool InitializeContextForSnapshot(Local<Context> context) { |
|
571 |
452 |
Isolate* isolate = context->GetIsolate(); |
|
572 |
904 |
HandleScope handle_scope(isolate); |
|
573 |
|||
574 |
904 |
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, |
|
575 |
452 |
True(isolate)); |
|
576 |
904 |
return InitializePrimordials(context); |
|
577 |
} |
||
578 |
|||
579 |
909 |
bool InitializePrimordials(Local<Context> context) { |
|
580 |
// Run per-context JS files. |
||
581 |
909 |
Isolate* isolate = context->GetIsolate(); |
|
582 |
Context::Scope context_scope(context); |
||
583 |
Local<Object> exports; |
||
584 |
|||
585 |
Local<String> primordials_string = |
||
586 |
909 |
FIXED_ONE_BYTE_STRING(isolate, "primordials"); |
|
587 |
909 |
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global"); |
|
588 |
909 |
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports"); |
|
589 |
|||
590 |
// Create primordials first and make it available to per-context scripts. |
||
591 |
909 |
Local<Object> primordials = Object::New(isolate); |
|
592 |
✓✓✓✓ |
4542 |
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() || |
593 |
✓✗✗✓ |
3630 |
!GetPerContextExports(context).ToLocal(&exports) || |
594 |
2718 |
!exports->Set(context, primordials_string, primordials).FromJust()) { |
|
595 |
4 |
return false; |
|
596 |
} |
||
597 |
|||
598 |
static const char* context_files[] = {"internal/per_context/primordials", |
||
599 |
"internal/per_context/domexception", |
||
600 |
"internal/per_context/messageport", |
||
601 |
nullptr}; |
||
602 |
|||
603 |
✓✓ | 3607 |
for (const char** module = context_files; *module != nullptr; module++) { |
604 |
std::vector<Local<String>> parameters = { |
||
605 |
5409 |
global_string, exports_string, primordials_string}; |
|
606 |
13533 |
Local<Value> arguments[] = {context->Global(), exports, primordials}; |
|
607 |
MaybeLocal<Function> maybe_fn = |
||
608 |
native_module::NativeModuleEnv::LookupAndCompile( |
||
609 |
2707 |
context, *module, ¶meters, nullptr); |
|
610 |
Local<Function> fn; |
||
611 |
✗✓ | 2707 |
if (!maybe_fn.ToLocal(&fn)) { |
612 |
return false; |
||
613 |
} |
||
614 |
MaybeLocal<Value> result = |
||
615 |
5414 |
fn->Call(context, Undefined(isolate), arraysize(arguments), arguments); |
|
616 |
// Execution failed during context creation. |
||
617 |
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal. |
||
618 |
✓✓ | 2707 |
if (result.IsEmpty()) { |
619 |
✓✓ | 4 |
return false; |
620 |
} |
||
621 |
} |
||
622 |
|||
623 |
901 |
return true; |
|
624 |
} |
||
625 |
|||
626 |
452 |
bool InitializeContext(Local<Context> context) { |
|
627 |
✓✓ | 452 |
if (!InitializeContextForSnapshot(context)) { |
628 |
4 |
return false; |
|
629 |
} |
||
630 |
|||
631 |
448 |
InitializeContextRuntime(context); |
|
632 |
448 |
return true; |
|
633 |
} |
||
634 |
|||
635 |
10 |
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { |
|
636 |
20 |
HandleScope handle_scope(isolate); |
|
637 |
10 |
Local<Context> context = isolate->GetCurrentContext(); |
|
638 |
✗✓ | 10 |
if (context.IsEmpty()) return nullptr; |
639 |
10 |
Environment* env = Environment::GetCurrent(context); |
|
640 |
✗✓ | 10 |
if (env == nullptr) return nullptr; |
641 |
10 |
return env->event_loop(); |
|
642 |
} |
||
643 |
|||
644 |
3 |
void AddLinkedBinding(Environment* env, const node_module& mod) { |
|
645 |
✗✓ | 3 |
CHECK_NOT_NULL(env); |
646 |
6 |
Mutex::ScopedLock lock(env->extra_linked_bindings_mutex()); |
|
647 |
|||
648 |
3 |
node_module* prev_head = env->extra_linked_bindings_head(); |
|
649 |
3 |
env->extra_linked_bindings()->push_back(mod); |
|
650 |
✗✓ | 3 |
if (prev_head != nullptr) |
651 |
prev_head->nm_link = &env->extra_linked_bindings()->back(); |
||
652 |
3 |
} |
|
653 |
|||
654 |
2 |
void AddLinkedBinding(Environment* env, const napi_module& mod) { |
|
655 |
2 |
AddLinkedBinding(env, napi_module_to_node_module(&mod)); |
|
656 |
2 |
} |
|
657 |
|||
658 |
1 |
void AddLinkedBinding(Environment* env, |
|
659 |
const char* name, |
||
660 |
addon_context_register_func fn, |
||
661 |
void* priv) { |
||
662 |
node_module mod = { |
||
663 |
NODE_MODULE_VERSION, |
||
664 |
NM_F_LINKED, |
||
665 |
nullptr, // nm_dso_handle |
||
666 |
nullptr, // nm_filename |
||
667 |
nullptr, // nm_register_func |
||
668 |
fn, |
||
669 |
name, |
||
670 |
priv, |
||
671 |
nullptr // nm_link |
||
672 |
1 |
}; |
|
673 |
1 |
AddLinkedBinding(env, mod); |
|
674 |
1 |
} |
|
675 |
|||
676 |
static std::atomic<uint64_t> next_thread_id{0}; |
||
677 |
|||
678 |
5293 |
ThreadId AllocateEnvironmentThreadId() { |
|
679 |
5293 |
return ThreadId { next_thread_id++ }; |
|
680 |
} |
||
681 |
|||
682 |
467 |
void DefaultProcessExitHandler(Environment* env, int exit_code) { |
|
683 |
467 |
env->set_can_call_into_js(false); |
|
684 |
467 |
env->stop_sub_worker_contexts(); |
|
685 |
467 |
DisposePlatform(); |
|
686 |
467 |
uv_library_shutdown(); |
|
687 |
467 |
exit(exit_code); |
|
688 |
} |
||
689 |
|||
690 |
|||
691 |
394 |
void SetProcessExitHandler(Environment* env, |
|
692 |
std::function<void(Environment*, int)>&& handler) { |
||
693 |
394 |
env->set_process_exit_handler(std::move(handler)); |
|
694 |
394 |
} |
|
695 |
|||
696 |
✓✗✓✗ |
14073 |
} // namespace node |
Generated by: GCOVR (Version 3.4) |