GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
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 |
#include "node_contextify.h" |
||
23 |
|||
24 |
#include "memory_tracker-inl.h" |
||
25 |
#include "node_internals.h" |
||
26 |
#include "node_watchdog.h" |
||
27 |
#include "base_object-inl.h" |
||
28 |
#include "node_context_data.h" |
||
29 |
#include "node_errors.h" |
||
30 |
#include "module_wrap.h" |
||
31 |
#include "util-inl.h" |
||
32 |
|||
33 |
namespace node { |
||
34 |
namespace contextify { |
||
35 |
|||
36 |
using errors::TryCatchScope; |
||
37 |
|||
38 |
using v8::Array; |
||
39 |
using v8::ArrayBufferView; |
||
40 |
using v8::Boolean; |
||
41 |
using v8::Context; |
||
42 |
using v8::EscapableHandleScope; |
||
43 |
using v8::External; |
||
44 |
using v8::Function; |
||
45 |
using v8::FunctionCallbackInfo; |
||
46 |
using v8::FunctionTemplate; |
||
47 |
using v8::HandleScope; |
||
48 |
using v8::IndexedPropertyHandlerConfiguration; |
||
49 |
using v8::Int32; |
||
50 |
using v8::Integer; |
||
51 |
using v8::Isolate; |
||
52 |
using v8::Local; |
||
53 |
using v8::Maybe; |
||
54 |
using v8::MaybeLocal; |
||
55 |
using v8::MeasureMemoryExecution; |
||
56 |
using v8::MeasureMemoryMode; |
||
57 |
using v8::MicrotaskQueue; |
||
58 |
using v8::MicrotasksPolicy; |
||
59 |
using v8::Name; |
||
60 |
using v8::NamedPropertyHandlerConfiguration; |
||
61 |
using v8::Number; |
||
62 |
using v8::Object; |
||
63 |
using v8::ObjectTemplate; |
||
64 |
using v8::PrimitiveArray; |
||
65 |
using v8::Promise; |
||
66 |
using v8::PropertyAttribute; |
||
67 |
using v8::PropertyCallbackInfo; |
||
68 |
using v8::PropertyDescriptor; |
||
69 |
using v8::PropertyHandlerFlags; |
||
70 |
using v8::Script; |
||
71 |
using v8::ScriptCompiler; |
||
72 |
using v8::ScriptOrigin; |
||
73 |
using v8::ScriptOrModule; |
||
74 |
using v8::String; |
||
75 |
using v8::Uint32; |
||
76 |
using v8::UnboundScript; |
||
77 |
using v8::Value; |
||
78 |
using v8::WeakCallbackInfo; |
||
79 |
using v8::WeakCallbackType; |
||
80 |
|||
81 |
// The vm module executes code in a sandboxed environment with a different |
||
82 |
// global object than the rest of the code. This is achieved by applying |
||
83 |
// every call that changes or queries a property on the global `this` in the |
||
84 |
// sandboxed code, to the sandbox object. |
||
85 |
// |
||
86 |
// The implementation uses V8's interceptors for methods like `set`, `get`, |
||
87 |
// `delete`, `defineProperty`, and for any query of the property attributes. |
||
88 |
// Property handlers with interceptors are set on the object template for |
||
89 |
// the sandboxed code. Handlers for both named properties and for indexed |
||
90 |
// properties are used. Their functionality is almost identical, the indexed |
||
91 |
// interceptors mostly just call the named interceptors. |
||
92 |
// |
||
93 |
// For every `get` of a global property in the sandboxed context, the |
||
94 |
// interceptor callback checks the sandbox object for the property. |
||
95 |
// If the property is defined on the sandbox, that result is returned to |
||
96 |
// the original call instead of finishing the query on the global object. |
||
97 |
// |
||
98 |
// For every `set` of a global property, the interceptor callback defines or |
||
99 |
// changes the property both on the sandbox and the global proxy. |
||
100 |
|||
101 |
namespace { |
||
102 |
|||
103 |
// Convert an int to a V8 Name (String or Symbol). |
||
104 |
3 |
Local<Name> Uint32ToName(Local<Context> context, uint32_t index) { |
|
105 |
12 |
return Uint32::New(context->GetIsolate(), index)->ToString(context) |
|
106 |
6 |
.ToLocalChecked(); |
|
107 |
} |
||
108 |
|||
109 |
} // anonymous namespace |
||
110 |
|||
111 |
524 |
ContextifyContext::ContextifyContext( |
|
112 |
Environment* env, |
||
113 |
Local<Object> sandbox_obj, |
||
114 |
524 |
const ContextOptions& options) |
|
115 |
: env_(env), |
||
116 |
1048 |
microtask_queue_wrap_(options.microtask_queue_wrap) { |
|
117 |
524 |
MaybeLocal<Context> v8_context = CreateV8Context(env, sandbox_obj, options); |
|
118 |
|||
119 |
// Allocation failure, maximum call stack size reached, termination, etc. |
||
120 |
✗✓ | 524 |
if (v8_context.IsEmpty()) return; |
121 |
|||
122 |
1048 |
context_.Reset(env->isolate(), v8_context.ToLocalChecked()); |
|
123 |
524 |
context_.SetWeak(this, WeakCallback, WeakCallbackType::kParameter); |
|
124 |
524 |
env->AddCleanupHook(CleanupHook, this); |
|
125 |
} |
||
126 |
|||
127 |
|||
128 |
1443 |
ContextifyContext::~ContextifyContext() { |
|
129 |
481 |
env()->RemoveCleanupHook(CleanupHook, this); |
|
130 |
481 |
} |
|
131 |
|||
132 |
|||
133 |
423 |
void ContextifyContext::CleanupHook(void* arg) { |
|
134 |
423 |
ContextifyContext* self = static_cast<ContextifyContext*>(arg); |
|
135 |
423 |
self->context_.Reset(); |
|
136 |
✓✗ | 423 |
delete self; |
137 |
423 |
} |
|
138 |
|||
139 |
|||
140 |
// This is an object that just keeps an internal pointer to this |
||
141 |
// ContextifyContext. It's passed to the NamedPropertyHandler. If we |
||
142 |
// pass the main JavaScript context object we're embedded in, then the |
||
143 |
// NamedPropertyHandler will store a reference to it forever and keep it |
||
144 |
// from getting gc'd. |
||
145 |
524 |
MaybeLocal<Object> ContextifyContext::CreateDataWrapper(Environment* env) { |
|
146 |
Local<Object> wrapper; |
||
147 |
✗✓ | 1572 |
if (!env->script_data_constructor_function() |
148 |
1572 |
->NewInstance(env->context()) |
|
149 |
524 |
.ToLocal(&wrapper)) { |
|
150 |
return MaybeLocal<Object>(); |
||
151 |
} |
||
152 |
|||
153 |
524 |
wrapper->SetAlignedPointerInInternalField(ContextifyContext::kSlot, this); |
|
154 |
524 |
return wrapper; |
|
155 |
} |
||
156 |
|||
157 |
524 |
MaybeLocal<Context> ContextifyContext::CreateV8Context( |
|
158 |
Environment* env, |
||
159 |
Local<Object> sandbox_obj, |
||
160 |
const ContextOptions& options) { |
||
161 |
524 |
EscapableHandleScope scope(env->isolate()); |
|
162 |
Local<FunctionTemplate> function_template = |
||
163 |
524 |
FunctionTemplate::New(env->isolate()); |
|
164 |
|||
165 |
1048 |
function_template->SetClassName(sandbox_obj->GetConstructorName()); |
|
166 |
|||
167 |
Local<ObjectTemplate> object_template = |
||
168 |
524 |
function_template->InstanceTemplate(); |
|
169 |
|||
170 |
Local<Object> data_wrapper; |
||
171 |
✗✓ | 1048 |
if (!CreateDataWrapper(env).ToLocal(&data_wrapper)) |
172 |
return MaybeLocal<Context>(); |
||
173 |
|||
174 |
NamedPropertyHandlerConfiguration config( |
||
175 |
PropertyGetterCallback, |
||
176 |
PropertySetterCallback, |
||
177 |
PropertyDescriptorCallback, |
||
178 |
PropertyDeleterCallback, |
||
179 |
PropertyEnumeratorCallback, |
||
180 |
PropertyDefinerCallback, |
||
181 |
data_wrapper, |
||
182 |
524 |
PropertyHandlerFlags::kHasNoSideEffect); |
|
183 |
|||
184 |
IndexedPropertyHandlerConfiguration indexed_config( |
||
185 |
IndexedPropertyGetterCallback, |
||
186 |
IndexedPropertySetterCallback, |
||
187 |
IndexedPropertyDescriptorCallback, |
||
188 |
IndexedPropertyDeleterCallback, |
||
189 |
PropertyEnumeratorCallback, |
||
190 |
IndexedPropertyDefinerCallback, |
||
191 |
data_wrapper, |
||
192 |
524 |
PropertyHandlerFlags::kHasNoSideEffect); |
|
193 |
|||
194 |
524 |
object_template->SetHandler(config); |
|
195 |
524 |
object_template->SetHandler(indexed_config); |
|
196 |
Local<Context> ctx = Context::New( |
||
197 |
env->isolate(), |
||
198 |
nullptr, // extensions |
||
199 |
object_template, |
||
200 |
{}, // global object |
||
201 |
{}, // deserialization callback |
||
202 |
✓✓✓✓ |
1048 |
microtask_queue() ? microtask_queue().get() : nullptr); |
203 |
✗✓ | 524 |
if (ctx.IsEmpty()) return MaybeLocal<Context>(); |
204 |
// Only partially initialize the context - the primordials are left out |
||
205 |
// and only initialized when necessary. |
||
206 |
524 |
InitializeContextRuntime(ctx); |
|
207 |
|||
208 |
✗✓ | 524 |
if (ctx.IsEmpty()) { |
209 |
return MaybeLocal<Context>(); |
||
210 |
} |
||
211 |
|||
212 |
1572 |
ctx->SetSecurityToken(env->context()->GetSecurityToken()); |
|
213 |
|||
214 |
// We need to tie the lifetime of the sandbox object with the lifetime of |
||
215 |
// newly created context. We do this by making them hold references to each |
||
216 |
// other. The context can directly hold a reference to the sandbox as an |
||
217 |
// embedder data field. However, we cannot hold a reference to a v8::Context |
||
218 |
// directly in an Object, we instead hold onto the new context's global |
||
219 |
// object instead (which then has a reference to the context). |
||
220 |
524 |
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj); |
|
221 |
sandbox_obj->SetPrivate(env->context(), |
||
222 |
env->contextify_global_private_symbol(), |
||
223 |
1572 |
ctx->Global()); |
|
224 |
|||
225 |
1048 |
Utf8Value name_val(env->isolate(), options.name); |
|
226 |
1572 |
ctx->AllowCodeGenerationFromStrings(options.allow_code_gen_strings->IsTrue()); |
|
227 |
1048 |
ctx->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, |
|
228 |
524 |
options.allow_code_gen_wasm); |
|
229 |
|||
230 |
1048 |
ContextInfo info(*name_val); |
|
231 |
|||
232 |
✓✓ | 1048 |
if (!options.origin.IsEmpty()) { |
233 |
4 |
Utf8Value origin_val(env->isolate(), options.origin); |
|
234 |
2 |
info.origin = *origin_val; |
|
235 |
} |
||
236 |
|||
237 |
524 |
env->AssignToContext(ctx, info); |
|
238 |
|||
239 |
524 |
return scope.Escape(ctx); |
|
240 |
} |
||
241 |
|||
242 |
|||
243 |
5004 |
void ContextifyContext::Init(Environment* env, Local<Object> target) { |
|
244 |
Local<FunctionTemplate> function_template = |
||
245 |
5004 |
FunctionTemplate::New(env->isolate()); |
|
246 |
15012 |
function_template->InstanceTemplate()->SetInternalFieldCount( |
|
247 |
5004 |
ContextifyContext::kInternalFieldCount); |
|
248 |
env->set_script_data_constructor_function( |
||
249 |
15012 |
function_template->GetFunction(env->context()).ToLocalChecked()); |
|
250 |
|||
251 |
5004 |
env->SetMethod(target, "makeContext", MakeContext); |
|
252 |
5004 |
env->SetMethod(target, "isContext", IsContext); |
|
253 |
5004 |
env->SetMethod(target, "compileFunction", CompileFunction); |
|
254 |
5004 |
} |
|
255 |
|||
256 |
|||
257 |
// makeContext(sandbox, name, origin, strings, wasm); |
||
258 |
524 |
void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) { |
|
259 |
524 |
Environment* env = Environment::GetCurrent(args); |
|
260 |
|||
261 |
✗✓ | 524 |
CHECK_EQ(args.Length(), 6); |
262 |
✗✓ | 1048 |
CHECK(args[0]->IsObject()); |
263 |
1048 |
Local<Object> sandbox = args[0].As<Object>(); |
|
264 |
|||
265 |
// Don't allow contextifying a sandbox multiple times. |
||
266 |
✗✓ | 1572 |
CHECK( |
267 |
!sandbox->HasPrivate( |
||
268 |
env->context(), |
||
269 |
env->contextify_context_private_symbol()).FromJust()); |
||
270 |
|||
271 |
1048 |
ContextOptions options; |
|
272 |
|||
273 |
✗✓ | 1572 |
CHECK(args[1]->IsString()); |
274 |
1048 |
options.name = args[1].As<String>(); |
|
275 |
|||
276 |
✓✓✗✓ ✗✓ |
3138 |
CHECK(args[2]->IsString() || args[2]->IsUndefined()); |
277 |
✓✓ | 1572 |
if (args[2]->IsString()) { |
278 |
4 |
options.origin = args[2].As<String>(); |
|
279 |
} |
||
280 |
|||
281 |
✗✓ | 1048 |
CHECK(args[3]->IsBoolean()); |
282 |
1048 |
options.allow_code_gen_strings = args[3].As<Boolean>(); |
|
283 |
|||
284 |
✗✓ | 1048 |
CHECK(args[4]->IsBoolean()); |
285 |
1048 |
options.allow_code_gen_wasm = args[4].As<Boolean>(); |
|
286 |
|||
287 |
✓✗✓✓ |
2100 |
if (args[5]->IsObject() && |
288 |
✓✓✓✗ |
1056 |
!env->microtask_queue_ctor_template().IsEmpty() && |
289 |
532 |
env->microtask_queue_ctor_template()->HasInstance(args[5])) { |
|
290 |
8 |
options.microtask_queue_wrap.reset( |
|
291 |
12 |
Unwrap<MicrotaskQueueWrap>(args[5].As<Object>())); |
|
292 |
} |
||
293 |
|||
294 |
✓✗ | 1048 |
TryCatchScope try_catch(env); |
295 |
✓✗ | 1048 |
auto context_ptr = std::make_unique<ContextifyContext>(env, sandbox, options); |
296 |
|||
297 |
✗✓ | 524 |
if (try_catch.HasCaught()) { |
298 |
if (!try_catch.HasTerminated()) |
||
299 |
try_catch.ReThrow(); |
||
300 |
return; |
||
301 |
} |
||
302 |
|||
303 |
✗✓ | 1048 |
if (context_ptr->context().IsEmpty()) |
304 |
return; |
||
305 |
|||
306 |
sandbox->SetPrivate( |
||
307 |
env->context(), |
||
308 |
env->contextify_context_private_symbol(), |
||
309 |
✗✓ | 1572 |
External::New(env->isolate(), context_ptr.release())); |
310 |
} |
||
311 |
|||
312 |
|||
313 |
1609 |
void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) { |
|
314 |
1609 |
Environment* env = Environment::GetCurrent(args); |
|
315 |
|||
316 |
✗✓ | 3218 |
CHECK(args[0]->IsObject()); |
317 |
3218 |
Local<Object> sandbox = args[0].As<Object>(); |
|
318 |
|||
319 |
Maybe<bool> result = |
||
320 |
sandbox->HasPrivate(env->context(), |
||
321 |
3218 |
env->contextify_context_private_symbol()); |
|
322 |
4827 |
args.GetReturnValue().Set(result.FromJust()); |
|
323 |
1609 |
} |
|
324 |
|||
325 |
|||
326 |
58 |
void ContextifyContext::WeakCallback( |
|
327 |
const WeakCallbackInfo<ContextifyContext>& data) { |
||
328 |
58 |
ContextifyContext* context = data.GetParameter(); |
|
329 |
✓✗ | 58 |
delete context; |
330 |
58 |
} |
|
331 |
|||
332 |
// static |
||
333 |
1027 |
ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox( |
|
334 |
Environment* env, |
||
335 |
const Local<Object>& sandbox) { |
||
336 |
MaybeLocal<Value> maybe_value = |
||
337 |
sandbox->GetPrivate(env->context(), |
||
338 |
2054 |
env->contextify_context_private_symbol()); |
|
339 |
Local<Value> context_external_v; |
||
340 |
✓✗✓✗ ✓✗ |
2054 |
if (maybe_value.ToLocal(&context_external_v) && |
341 |
1027 |
context_external_v->IsExternal()) { |
|
342 |
1027 |
Local<External> context_external = context_external_v.As<External>(); |
|
343 |
1027 |
return static_cast<ContextifyContext*>(context_external->Value()); |
|
344 |
} |
||
345 |
return nullptr; |
||
346 |
} |
||
347 |
|||
348 |
// static |
||
349 |
template <typename T> |
||
350 |
1010365 |
ContextifyContext* ContextifyContext::Get(const PropertyCallbackInfo<T>& args) { |
|
351 |
1010365 |
Local<Value> data = args.Data(); |
|
352 |
return static_cast<ContextifyContext*>( |
||
353 |
3031095 |
data.As<Object>()->GetAlignedPointerFromInternalField( |
|
354 |
2020730 |
ContextifyContext::kSlot)); |
|
355 |
} |
||
356 |
|||
357 |
// static |
||
358 |
1010004 |
void ContextifyContext::PropertyGetterCallback( |
|
359 |
Local<Name> property, |
||
360 |
const PropertyCallbackInfo<Value>& args) { |
||
361 |
1010004 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
362 |
|||
363 |
// Still initializing |
||
364 |
✓✓ | 2020008 |
if (ctx->context_.IsEmpty()) |
365 |
1572 |
return; |
|
366 |
|||
367 |
1008432 |
Local<Context> context = ctx->context(); |
|
368 |
1008432 |
Local<Object> sandbox = ctx->sandbox(); |
|
369 |
MaybeLocal<Value> maybe_rv = |
||
370 |
1008432 |
sandbox->GetRealNamedProperty(context, property); |
|
371 |
✓✓ | 1008432 |
if (maybe_rv.IsEmpty()) { |
372 |
maybe_rv = |
||
373 |
16000 |
ctx->global_proxy()->GetRealNamedProperty(context, property); |
|
374 |
} |
||
375 |
|||
376 |
Local<Value> rv; |
||
377 |
✓✓ | 1008432 |
if (maybe_rv.ToLocal(&rv)) { |
378 |
✓✓ | 1008363 |
if (rv == sandbox) |
379 |
10 |
rv = ctx->global_proxy(); |
|
380 |
|||
381 |
2016726 |
args.GetReturnValue().Set(rv); |
|
382 |
} |
||
383 |
} |
||
384 |
|||
385 |
// static |
||
386 |
153 |
void ContextifyContext::PropertySetterCallback( |
|
387 |
Local<Name> property, |
||
388 |
Local<Value> value, |
||
389 |
const PropertyCallbackInfo<Value>& args) { |
||
390 |
153 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
391 |
|||
392 |
// Still initializing |
||
393 |
✗✓ | 306 |
if (ctx->context_.IsEmpty()) |
394 |
14 |
return; |
|
395 |
|||
396 |
153 |
auto attributes = PropertyAttribute::None; |
|
397 |
306 |
bool is_declared_on_global_proxy = ctx->global_proxy() |
|
398 |
459 |
->GetRealNamedPropertyAttributes(ctx->context(), property) |
|
399 |
153 |
.To(&attributes); |
|
400 |
bool read_only = |
||
401 |
153 |
static_cast<int>(attributes) & |
|
402 |
153 |
static_cast<int>(PropertyAttribute::ReadOnly); |
|
403 |
|||
404 |
306 |
bool is_declared_on_sandbox = ctx->sandbox() |
|
405 |
459 |
->GetRealNamedPropertyAttributes(ctx->context(), property) |
|
406 |
153 |
.To(&attributes); |
|
407 |
✓✓✓✓ |
302 |
read_only = read_only || |
408 |
149 |
(static_cast<int>(attributes) & |
|
409 |
static_cast<int>(PropertyAttribute::ReadOnly)); |
||
410 |
|||
411 |
✓✓ | 153 |
if (read_only) |
412 |
13 |
return; |
|
413 |
|||
414 |
// true for x = 5 |
||
415 |
// false for this.x = 5 |
||
416 |
// false for Object.defineProperty(this, 'foo', ...) |
||
417 |
// false for vmResult.x = 5 where vmResult = vm.runInContext(); |
||
418 |
280 |
bool is_contextual_store = ctx->global_proxy() != args.This(); |
|
419 |
|||
420 |
// Indicator to not return before setting (undeclared) function declarations |
||
421 |
// on the sandbox in strict mode, i.e. args.ShouldThrowOnError() = true. |
||
422 |
// True for 'function f() {}', 'this.f = function() {}', |
||
423 |
// 'var f = function()'. |
||
424 |
// In effect only for 'function f() {}' because |
||
425 |
// var f = function(), is_declared = true |
||
426 |
// this.f = function() {}, is_contextual_store = false. |
||
427 |
140 |
bool is_function = value->IsFunction(); |
|
428 |
|||
429 |
✓✓✓✓ |
140 |
bool is_declared = is_declared_on_global_proxy || is_declared_on_sandbox; |
430 |
✓✓✓✓ ✓✗✓✓ ✓✓ |
239 |
if (!is_declared && args.ShouldThrowOnError() && is_contextual_store && |
431 |
29 |
!is_function) |
|
432 |
1 |
return; |
|
433 |
|||
434 |
✓✓✓✓ ✓✓ |
381 |
if (!is_declared_on_global_proxy && is_declared_on_sandbox && |
435 |
✓✓✓✓ ✓✗ |
161 |
args.ShouldThrowOnError() && is_contextual_store && !is_function) { |
436 |
// The property exists on the sandbox but not on the global |
||
437 |
// proxy. Setting it would throw because we are in strict mode. |
||
438 |
// Don't attempt to set it by signaling that the call was |
||
439 |
// intercepted. Only change the value on the sandbox. |
||
440 |
8 |
args.GetReturnValue().Set(false); |
|
441 |
} |
||
442 |
|||
443 |
278 |
USE(ctx->sandbox()->Set(ctx->context(), property, value)); |
|
444 |
} |
||
445 |
|||
446 |
// static |
||
447 |
28 |
void ContextifyContext::PropertyDescriptorCallback( |
|
448 |
Local<Name> property, |
||
449 |
const PropertyCallbackInfo<Value>& args) { |
||
450 |
28 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
451 |
|||
452 |
// Still initializing |
||
453 |
✗✓ | 56 |
if (ctx->context_.IsEmpty()) |
454 |
return; |
||
455 |
|||
456 |
28 |
Local<Context> context = ctx->context(); |
|
457 |
|||
458 |
28 |
Local<Object> sandbox = ctx->sandbox(); |
|
459 |
|||
460 |
✓✓ | 84 |
if (sandbox->HasOwnProperty(context, property).FromMaybe(false)) { |
461 |
Local<Value> desc; |
||
462 |
✓✗ | 26 |
if (sandbox->GetOwnPropertyDescriptor(context, property).ToLocal(&desc)) { |
463 |
26 |
args.GetReturnValue().Set(desc); |
|
464 |
} |
||
465 |
} |
||
466 |
} |
||
467 |
|||
468 |
// static |
||
469 |
15 |
void ContextifyContext::PropertyDefinerCallback( |
|
470 |
Local<Name> property, |
||
471 |
const PropertyDescriptor& desc, |
||
472 |
const PropertyCallbackInfo<Value>& args) { |
||
473 |
15 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
474 |
|||
475 |
// Still initializing |
||
476 |
✗✓ | 30 |
if (ctx->context_.IsEmpty()) |
477 |
return; |
||
478 |
|||
479 |
15 |
Local<Context> context = ctx->context(); |
|
480 |
15 |
Isolate* isolate = context->GetIsolate(); |
|
481 |
|||
482 |
15 |
auto attributes = PropertyAttribute::None; |
|
483 |
bool is_declared = |
||
484 |
45 |
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(), |
|
485 |
30 |
property) |
|
486 |
15 |
.To(&attributes); |
|
487 |
bool read_only = |
||
488 |
15 |
static_cast<int>(attributes) & |
|
489 |
15 |
static_cast<int>(PropertyAttribute::ReadOnly); |
|
490 |
|||
491 |
// If the property is set on the global as read_only, don't change it on |
||
492 |
// the global or sandbox. |
||
493 |
✓✓✗✓ |
15 |
if (is_declared && read_only) |
494 |
return; |
||
495 |
|||
496 |
15 |
Local<Object> sandbox = ctx->sandbox(); |
|
497 |
|||
498 |
auto define_prop_on_sandbox = |
||
499 |
15 |
[&] (PropertyDescriptor* desc_for_sandbox) { |
|
500 |
✓✓ | 31 |
if (desc.has_enumerable()) { |
501 |
2 |
desc_for_sandbox->set_enumerable(desc.enumerable()); |
|
502 |
} |
||
503 |
✓✓ | 15 |
if (desc.has_configurable()) { |
504 |
1 |
desc_for_sandbox->set_configurable(desc.configurable()); |
|
505 |
} |
||
506 |
// Set the property on the sandbox. |
||
507 |
30 |
USE(sandbox->DefineProperty(context, property, *desc_for_sandbox)); |
|
508 |
30 |
}; |
|
509 |
|||
510 |
✓✓✗✓ ✓✓ |
15 |
if (desc.has_get() || desc.has_set()) { |
511 |
PropertyDescriptor desc_for_sandbox( |
||
512 |
10 |
desc.has_get() ? desc.get() : Undefined(isolate).As<Value>(), |
|
513 |
✓✓✓✗ |
28 |
desc.has_set() ? desc.set() : Undefined(isolate).As<Value>()); |
514 |
|||
515 |
5 |
define_prop_on_sandbox(&desc_for_sandbox); |
|
516 |
} else { |
||
517 |
Local<Value> value = |
||
518 |
✓✓ | 12 |
desc.has_value() ? desc.value() : Undefined(isolate).As<Value>(); |
519 |
|||
520 |
✗✓ | 10 |
if (desc.has_writable()) { |
521 |
PropertyDescriptor desc_for_sandbox(value, desc.writable()); |
||
522 |
define_prop_on_sandbox(&desc_for_sandbox); |
||
523 |
} else { |
||
524 |
20 |
PropertyDescriptor desc_for_sandbox(value); |
|
525 |
10 |
define_prop_on_sandbox(&desc_for_sandbox); |
|
526 |
} |
||
527 |
} |
||
528 |
} |
||
529 |
|||
530 |
// static |
||
531 |
2 |
void ContextifyContext::PropertyDeleterCallback( |
|
532 |
Local<Name> property, |
||
533 |
const PropertyCallbackInfo<Boolean>& args) { |
||
534 |
2 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
535 |
|||
536 |
// Still initializing |
||
537 |
✗✓ | 4 |
if (ctx->context_.IsEmpty()) |
538 |
1 |
return; |
|
539 |
|||
540 |
4 |
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), property); |
|
541 |
|||
542 |
✓✓ | 4 |
if (success.FromMaybe(false)) |
543 |
1 |
return; |
|
544 |
|||
545 |
// Delete failed on the sandbox, intercept and do not delete on |
||
546 |
// the global object. |
||
547 |
2 |
args.GetReturnValue().Set(false); |
|
548 |
} |
||
549 |
|||
550 |
// static |
||
551 |
160 |
void ContextifyContext::PropertyEnumeratorCallback( |
|
552 |
const PropertyCallbackInfo<Array>& args) { |
||
553 |
160 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
554 |
|||
555 |
// Still initializing |
||
556 |
✗✓ | 320 |
if (ctx->context_.IsEmpty()) |
557 |
return; |
||
558 |
|||
559 |
Local<Array> properties; |
||
560 |
|||
561 |
✗✓ | 480 |
if (!ctx->sandbox()->GetPropertyNames(ctx->context()).ToLocal(&properties)) |
562 |
return; |
||
563 |
|||
564 |
320 |
args.GetReturnValue().Set(properties); |
|
565 |
} |
||
566 |
|||
567 |
// static |
||
568 |
void ContextifyContext::IndexedPropertyGetterCallback( |
||
569 |
uint32_t index, |
||
570 |
const PropertyCallbackInfo<Value>& args) { |
||
571 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
||
572 |
|||
573 |
// Still initializing |
||
574 |
if (ctx->context_.IsEmpty()) |
||
575 |
return; |
||
576 |
|||
577 |
ContextifyContext::PropertyGetterCallback( |
||
578 |
Uint32ToName(ctx->context(), index), args); |
||
579 |
} |
||
580 |
|||
581 |
|||
582 |
1 |
void ContextifyContext::IndexedPropertySetterCallback( |
|
583 |
uint32_t index, |
||
584 |
Local<Value> value, |
||
585 |
const PropertyCallbackInfo<Value>& args) { |
||
586 |
1 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
587 |
|||
588 |
// Still initializing |
||
589 |
✗✓ | 2 |
if (ctx->context_.IsEmpty()) |
590 |
return; |
||
591 |
|||
592 |
1 |
ContextifyContext::PropertySetterCallback( |
|
593 |
1 |
Uint32ToName(ctx->context(), index), value, args); |
|
594 |
} |
||
595 |
|||
596 |
// static |
||
597 |
1 |
void ContextifyContext::IndexedPropertyDescriptorCallback( |
|
598 |
uint32_t index, |
||
599 |
const PropertyCallbackInfo<Value>& args) { |
||
600 |
1 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
601 |
|||
602 |
// Still initializing |
||
603 |
✗✓ | 2 |
if (ctx->context_.IsEmpty()) |
604 |
return; |
||
605 |
|||
606 |
1 |
ContextifyContext::PropertyDescriptorCallback( |
|
607 |
1 |
Uint32ToName(ctx->context(), index), args); |
|
608 |
} |
||
609 |
|||
610 |
|||
611 |
1 |
void ContextifyContext::IndexedPropertyDefinerCallback( |
|
612 |
uint32_t index, |
||
613 |
const PropertyDescriptor& desc, |
||
614 |
const PropertyCallbackInfo<Value>& args) { |
||
615 |
1 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
|
616 |
|||
617 |
// Still initializing |
||
618 |
✗✓ | 2 |
if (ctx->context_.IsEmpty()) |
619 |
return; |
||
620 |
|||
621 |
1 |
ContextifyContext::PropertyDefinerCallback( |
|
622 |
1 |
Uint32ToName(ctx->context(), index), desc, args); |
|
623 |
} |
||
624 |
|||
625 |
// static |
||
626 |
void ContextifyContext::IndexedPropertyDeleterCallback( |
||
627 |
uint32_t index, |
||
628 |
const PropertyCallbackInfo<Boolean>& args) { |
||
629 |
ContextifyContext* ctx = ContextifyContext::Get(args); |
||
630 |
|||
631 |
// Still initializing |
||
632 |
if (ctx->context_.IsEmpty()) |
||
633 |
return; |
||
634 |
|||
635 |
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), index); |
||
636 |
|||
637 |
if (success.FromMaybe(false)) |
||
638 |
return; |
||
639 |
|||
640 |
// Delete failed on the sandbox, intercept and do not delete on |
||
641 |
// the global object. |
||
642 |
args.GetReturnValue().Set(false); |
||
643 |
} |
||
644 |
|||
645 |
5004 |
void ContextifyScript::Init(Environment* env, Local<Object> target) { |
|
646 |
10008 |
HandleScope scope(env->isolate()); |
|
647 |
Local<String> class_name = |
||
648 |
5004 |
FIXED_ONE_BYTE_STRING(env->isolate(), "ContextifyScript"); |
|
649 |
|||
650 |
5004 |
Local<FunctionTemplate> script_tmpl = env->NewFunctionTemplate(New); |
|
651 |
15012 |
script_tmpl->InstanceTemplate()->SetInternalFieldCount( |
|
652 |
5004 |
ContextifyScript::kInternalFieldCount); |
|
653 |
5004 |
script_tmpl->SetClassName(class_name); |
|
654 |
5004 |
env->SetProtoMethod(script_tmpl, "createCachedData", CreateCachedData); |
|
655 |
5004 |
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext); |
|
656 |
5004 |
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext); |
|
657 |
|||
658 |
10008 |
target->Set(env->context(), class_name, |
|
659 |
25020 |
script_tmpl->GetFunction(env->context()).ToLocalChecked()).Check(); |
|
660 |
5004 |
env->set_script_context_constructor_template(script_tmpl); |
|
661 |
5004 |
} |
|
662 |
|||
663 |
1961 |
void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) { |
|
664 |
1961 |
Environment* env = Environment::GetCurrent(args); |
|
665 |
1961 |
Isolate* isolate = env->isolate(); |
|
666 |
1961 |
Local<Context> context = env->context(); |
|
667 |
|||
668 |
✗✓ | 1961 |
CHECK(args.IsConstructCall()); |
669 |
|||
670 |
1961 |
const int argc = args.Length(); |
|
671 |
✗✓ | 1961 |
CHECK_GE(argc, 2); |
672 |
|||
673 |
✗✓ | 5883 |
CHECK(args[0]->IsString()); |
674 |
3922 |
Local<String> code = args[0].As<String>(); |
|
675 |
|||
676 |
✗✓ | 5883 |
CHECK(args[1]->IsString()); |
677 |
3922 |
Local<String> filename = args[1].As<String>(); |
|
678 |
|||
679 |
Local<Integer> line_offset; |
||
680 |
Local<Integer> column_offset; |
||
681 |
Local<ArrayBufferView> cached_data_buf; |
||
682 |
1961 |
bool produce_cached_data = false; |
|
683 |
1961 |
Local<Context> parsing_context = context; |
|
684 |
|||
685 |
✓✗ | 1961 |
if (argc > 2) { |
686 |
// new ContextifyScript(code, filename, lineOffset, columnOffset, |
||
687 |
// cachedData, produceCachedData, parsingContext) |
||
688 |
✗✓ | 1961 |
CHECK_EQ(argc, 7); |
689 |
✗✓ | 3922 |
CHECK(args[2]->IsNumber()); |
690 |
3922 |
line_offset = args[2].As<Integer>(); |
|
691 |
✗✓ | 3922 |
CHECK(args[3]->IsNumber()); |
692 |
3922 |
column_offset = args[3].As<Integer>(); |
|
693 |
✓✓ | 5883 |
if (!args[4]->IsUndefined()) { |
694 |
✗✓ | 46 |
CHECK(args[4]->IsArrayBufferView()); |
695 |
46 |
cached_data_buf = args[4].As<ArrayBufferView>(); |
|
696 |
} |
||
697 |
✗✓ | 3922 |
CHECK(args[5]->IsBoolean()); |
698 |
3922 |
produce_cached_data = args[5]->IsTrue(); |
|
699 |
✓✓ | 5883 |
if (!args[6]->IsUndefined()) { |
700 |
✗✓ | 678 |
CHECK(args[6]->IsObject()); |
701 |
ContextifyContext* sandbox = |
||
702 |
339 |
ContextifyContext::ContextFromContextifiedSandbox( |
|
703 |
1017 |
env, args[6].As<Object>()); |
|
704 |
✗✓ | 339 |
CHECK_NOT_NULL(sandbox); |
705 |
339 |
parsing_context = sandbox->context(); |
|
706 |
} |
||
707 |
} else { |
||
708 |
line_offset = Integer::New(isolate, 0); |
||
709 |
column_offset = Integer::New(isolate, 0); |
||
710 |
} |
||
711 |
|||
712 |
ContextifyScript* contextify_script = |
||
713 |
1961 |
new ContextifyScript(env, args.This()); |
|
714 |
|||
715 |
✓✓ | 1961 |
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
716 |
TRACING_CATEGORY_NODE2(vm, script)) != 0) { |
||
717 |
14 |
Utf8Value fn(isolate, filename); |
|
718 |
✓✗ | 7 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1( |
719 |
TRACING_CATEGORY_NODE2(vm, script), |
||
720 |
"ContextifyScript::New", |
||
721 |
contextify_script, |
||
722 |
✓✗ | 14 |
"filename", TRACE_STR_COPY(*fn)); |
723 |
7 |
} |
|
724 |
7 |
||
725 |
1975 |
ScriptCompiler::CachedData* cached_data = nullptr; |
|
726 |
✓✓ | 1961 |
if (!cached_data_buf.IsEmpty()) { |
727 |
14 |
uint8_t* data = static_cast<uint8_t*>( |
|
728 |
46 |
cached_data_buf->Buffer()->GetBackingStore()->Data()); |
|
729 |
23 |
cached_data = new ScriptCompiler::CachedData( |
|
730 |
46 |
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); |
|
731 |
} |
||
732 |
|||
733 |
Local<PrimitiveArray> host_defined_options = |
||
734 |
1961 |
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); |
|
735 |
5883 |
host_defined_options->Set(isolate, loader::HostDefinedOptions::kType, |
|
736 |
1961 |
Number::New(isolate, loader::ScriptType::kScript)); |
|
737 |
5883 |
host_defined_options->Set(isolate, loader::HostDefinedOptions::kID, |
|
738 |
3922 |
Number::New(isolate, contextify_script->id())); |
|
739 |
|||
740 |
ScriptOrigin origin(filename, |
||
741 |
line_offset, // line offset |
||
742 |
column_offset, // column offset |
||
743 |
True(isolate), // is cross origin |
||
744 |
Local<Integer>(), // script id |
||
745 |
Local<Value>(), // source map URL |
||
746 |
False(isolate), // is opaque (?) |
||
747 |
False(isolate), // is WASM |
||
748 |
False(isolate), // is ES Module |
||
749 |
1961 |
host_defined_options); |
|
750 |
1777 |
ScriptCompiler::Source source(code, origin, cached_data); |
|
751 |
ScriptCompiler::CompileOptions compile_options = |
||
752 |
1961 |
ScriptCompiler::kNoCompileOptions; |
|
753 |
|||
754 |
✓✓ | 1961 |
if (source.GetCachedData() != nullptr) |
755 |
23 |
compile_options = ScriptCompiler::kConsumeCodeCache; |
|
756 |
|||
757 |
3738 |
TryCatchScope try_catch(env); |
|
758 |
✓✓ | 3738 |
ShouldNotAbortOnUncaughtScope no_abort_scope(env); |
759 |
✓✓ | 1777 |
Context::Scope scope(parsing_context); |
760 |
|||
761 |
MaybeLocal<UnboundScript> v8_script = ScriptCompiler::CompileUnboundScript( |
||
762 |
isolate, |
||
763 |
&source, |
||
764 |
1961 |
compile_options); |
|
765 |
|||
766 |
✓✓ | 1961 |
if (v8_script.IsEmpty()) { |
767 |
184 |
errors::DecorateErrorStack(env, try_catch); |
|
768 |
184 |
no_abort_scope.Close(); |
|
769 |
✓✗ | 184 |
if (!try_catch.HasTerminated()) |
770 |
184 |
try_catch.ReThrow(); |
|
771 |
TRACE_EVENT_NESTABLE_ASYNC_END0( |
||
772 |
✓✓ | 184 |
TRACING_CATEGORY_NODE2(vm, script), |
773 |
"ContextifyScript::New", |
||
774 |
28 |
contextify_script); |
|
775 |
✗✓ | 552 |
return; |
776 |
184 |
} |
|
777 |
3554 |
contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked()); |
|
778 |
|||
779 |
✓✓ | 1777 |
if (compile_options == ScriptCompiler::kConsumeCodeCache) { |
780 |
69 |
args.This()->Set( |
|
781 |
env->context(), |
||
782 |
env->cached_data_rejected_string(), |
||
783 |
115 |
Boolean::New(isolate, source.GetCachedData()->rejected)).Check(); |
|
784 |
✓✓ | 1754 |
} else if (produce_cached_data) { |
785 |
std::unique_ptr<ScriptCompiler::CachedData> cached_data { |
||
786 |
12 |
ScriptCompiler::CreateCodeCache(v8_script.ToLocalChecked()) }; |
|
787 |
6 |
bool cached_data_produced = cached_data != nullptr; |
|
788 |
✓✗ | 6 |
if (cached_data_produced) { |
789 |
MaybeLocal<Object> buf = Buffer::Copy( |
||
790 |
env, |
||
791 |
6 |
reinterpret_cast<const char*>(cached_data->data), |
|
792 |
12 |
cached_data->length); |
|
793 |
18 |
args.This()->Set(env->context(), |
|
794 |
env->cached_data_string(), |
||
795 |
24 |
buf.ToLocalChecked()).Check(); |
|
796 |
} |
||
797 |
18 |
args.This()->Set( |
|
798 |
env->context(), |
||
799 |
env->cached_data_produced_string(), |
||
800 |
30 |
Boolean::New(isolate, cached_data_produced)).Check(); |
|
801 |
} |
||
802 |
TRACE_EVENT_NESTABLE_ASYNC_END0( |
||
803 |
✓✓ | 1777 |
TRACING_CATEGORY_NODE2(vm, script), |
804 |
"ContextifyScript::New", |
||
805 |
550 |
contextify_script); |
|
806 |
✓✓ | 1777 |
} |
807 |
|||
808 |
1759 |
bool ContextifyScript::InstanceOf(Environment* env, |
|
809 |
14 |
const Local<Value>& value) { |
|
810 |
✓✗✓✗ |
3518 |
return !value.IsEmpty() && |
811 |
5256 |
env->script_context_constructor_template()->HasInstance(value); |
|
812 |
} |
||
813 |
|||
814 |
11 |
void ContextifyScript::CreateCachedData( |
|
815 |
const FunctionCallbackInfo<Value>& args) { |
||
816 |
11 |
Environment* env = Environment::GetCurrent(args); |
|
817 |
ContextifyScript* wrapped_script; |
||
818 |
✗✓ | 11 |
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); |
819 |
Local<UnboundScript> unbound_script = |
||
820 |
11 |
PersistentToLocal::Default(env->isolate(), wrapped_script->script_); |
|
821 |
std::unique_ptr<ScriptCompiler::CachedData> cached_data( |
||
822 |
22 |
ScriptCompiler::CreateCodeCache(unbound_script)); |
|
823 |
✗✓ | 11 |
if (!cached_data) { |
824 |
args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked()); |
||
825 |
} else { |
||
826 |
MaybeLocal<Object> buf = Buffer::Copy( |
||
827 |
env, |
||
828 |
11 |
reinterpret_cast<const char*>(cached_data->data), |
|
829 |
22 |
cached_data->length); |
|
830 |
22 |
args.GetReturnValue().Set(buf.ToLocalChecked()); |
|
831 |
} |
||
832 |
} |
||
833 |
|||
834 |
1080 |
void ContextifyScript::RunInThisContext( |
|
835 |
const FunctionCallbackInfo<Value>& args) { |
||
836 |
1080 |
Environment* env = Environment::GetCurrent(args); |
|
837 |
|||
838 |
ContextifyScript* wrapped_script; |
||
839 |
✗✓ | 1080 |
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); |
840 |
|||
841 |
✓✓ | 1080 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( |
842 |
TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); |
||
843 |
|||
844 |
420 |
// TODO(addaleax): Use an options object or otherwise merge this with |
|
845 |
✓✓ | 1080 |
// RunInContext(). |
846 |
✗✓ | 1080 |
CHECK_EQ(args.Length(), 4); |
847 |
6 |
||
848 |
✗✓ | 2172 |
CHECK(args[0]->IsNumber()); |
849 |
4332 |
int64_t timeout = args[0]->IntegerValue(env->context()).FromJust(); |
|
850 |
|||
851 |
✗✓ | 2160 |
CHECK(args[1]->IsBoolean()); |
852 |
2160 |
bool display_errors = args[1]->IsTrue(); |
|
853 |
|||
854 |
✗✓ | 2160 |
CHECK(args[2]->IsBoolean()); |
855 |
2160 |
bool break_on_sigint = args[2]->IsTrue(); |
|
856 |
|||
857 |
✗✓ | 2160 |
CHECK(args[3]->IsBoolean()); |
858 |
2160 |
bool break_on_first_line = args[3]->IsTrue(); |
|
859 |
|||
860 |
// Do the eval within this context |
||
861 |
2144 |
EvalMachine(env, |
|
862 |
timeout, |
||
863 |
display_errors, |
||
864 |
break_on_sigint, |
||
865 |
break_on_first_line, |
||
866 |
nullptr, // microtask_queue |
||
867 |
1080 |
args); |
|
868 |
|||
869 |
✓✓ | 1064 |
TRACE_EVENT_NESTABLE_ASYNC_END0( |
870 |
TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); |
||
871 |
} |
||
872 |
407 |
||
873 |
✓✓ | 1736 |
void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) { |
874 |
672 |
Environment* env = Environment::GetCurrent(args); |
|
875 |
5 |
||
876 |
10 |
ContextifyScript* wrapped_script; |
|
877 |
✗✓ | 682 |
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); |
878 |
|||
879 |
✗✓ | 672 |
CHECK_EQ(args.Length(), 5); |
880 |
|||
881 |
✗✓ | 1344 |
CHECK(args[0]->IsObject()); |
882 |
1344 |
Local<Object> sandbox = args[0].As<Object>(); |
|
883 |
// Get the context from the sandbox |
||
884 |
ContextifyContext* contextify_context = |
||
885 |
672 |
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox); |
|
886 |
✗✓ | 672 |
CHECK_NOT_NULL(contextify_context); |
887 |
|||
888 |
✗✓ | 1344 |
if (contextify_context->context().IsEmpty()) |
889 |
return; |
||
890 |
|||
891 |
✓✓ | 672 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( |
892 |
TRACING_CATEGORY_NODE2(vm, script), "RunInContext", wrapped_script); |
||
893 |
|||
894 |
✗✓ | 1344 |
CHECK(args[1]->IsNumber()); |
895 |
✓✓ | 3360 |
int64_t timeout = args[1]->IntegerValue(env->context()).FromJust(); |
896 |
|||
897 |
✗✓ | 1345 |
CHECK(args[2]->IsBoolean()); |
898 |
1346 |
bool display_errors = args[2]->IsTrue(); |
|
899 |
2 |
||
900 |
✗✓ | 1344 |
CHECK(args[3]->IsBoolean()); |
901 |
1344 |
bool break_on_sigint = args[3]->IsTrue(); |
|
902 |
|||
903 |
✗✓ | 1344 |
CHECK(args[4]->IsBoolean()); |
904 |
1344 |
bool break_on_first_line = args[4]->IsTrue(); |
|
905 |
|||
906 |
// Do the eval within the context |
||
907 |
672 |
Context::Scope context_scope(contextify_context->context()); |
|
908 |
672 |
EvalMachine(contextify_context->env(), |
|
909 |
timeout, |
||
910 |
display_errors, |
||
911 |
break_on_sigint, |
||
912 |
break_on_first_line, |
||
913 |
1344 |
contextify_context->microtask_queue(), |
|
914 |
672 |
args); |
|
915 |
|||
916 |
✓✓ | 672 |
TRACE_EVENT_NESTABLE_ASYNC_END0( |
917 |
TRACING_CATEGORY_NODE2(vm, script), "RunInContext", wrapped_script); |
||
918 |
} |
||
919 |
177 |
||
920 |
✓✓ | 2424 |
bool ContextifyScript::EvalMachine(Environment* env, |
921 |
const int64_t timeout, |
||
922 |
1 |
const bool display_errors, |
|
923 |
2 |
const bool break_on_sigint, |
|
924 |
2 |
const bool break_on_first_line, |
|
925 |
std::shared_ptr<MicrotaskQueue> mtask_queue, |
||
926 |
const FunctionCallbackInfo<Value>& args) { |
||
927 |
✗✓ | 1752 |
if (!env->can_call_into_js()) |
928 |
return false; |
||
929 |
✗✓ | 1752 |
if (!ContextifyScript::InstanceOf(env, args.Holder())) { |
930 |
THROW_ERR_INVALID_THIS( |
||
931 |
env, |
||
932 |
"Script methods can only be called on script instances."); |
||
933 |
return false; |
||
934 |
} |
||
935 |
3488 |
TryCatchScope try_catch(env); |
|
936 |
ContextifyScript* wrapped_script; |
||
937 |
✗✓ | 1752 |
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false); |
938 |
Local<UnboundScript> unbound_script = |
||
939 |
1752 |
PersistentToLocal::Default(env->isolate(), wrapped_script->script_); |
|
940 |
1752 |
Local<Script> script = unbound_script->BindToCurrentContext(); |
|
941 |
|||
942 |
#if HAVE_INSPECTOR |
||
943 |
✓✓ | 1752 |
if (break_on_first_line) { |
944 |
10 |
env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start"); |
|
945 |
} |
||
946 |
#endif |
||
947 |
|||
948 |
MaybeLocal<Value> result; |
||
949 |
1752 |
bool timed_out = false; |
|
950 |
1752 |
bool received_signal = false; |
|
951 |
1752 |
auto run = [&]() { |
|
952 |
3505 |
MaybeLocal<Value> result = script->Run(env->context()); |
|
953 |
✓✓✓✓ ✓✓ |
1736 |
if (!result.IsEmpty() && mtask_queue) |
954 |
2 |
mtask_queue->PerformCheckpoint(env->isolate()); |
|
955 |
1736 |
return result; |
|
956 |
1752 |
}; |
|
957 |
✓✓✗✓ |
1752 |
if (break_on_sigint && timeout != -1) { |
958 |
Watchdog wd(env->isolate(), timeout, &timed_out); |
||
959 |
SigintWatchdog swd(env->isolate(), &received_signal); |
||
960 |
result = run(); |
||
961 |
✓✓ | 1752 |
} else if (break_on_sigint) { |
962 |
289 |
SigintWatchdog swd(env->isolate(), &received_signal); |
|
963 |
146 |
result = run(); |
|
964 |
✓✓ | 1606 |
} else if (timeout != -1) { |
965 |
30 |
Watchdog wd(env->isolate(), timeout, &timed_out); |
|
966 |
15 |
result = run(); |
|
967 |
} else { |
||
968 |
1591 |
result = run(); |
|
969 |
} |
||
970 |
|||
971 |
// Convert the termination exception into a regular exception. |
||
972 |
✓✓✓✓ |
1736 |
if (timed_out || received_signal) { |
973 |
✗✓✗✗ ✗✓ |
20 |
if (!env->is_main_thread() && env->is_stopping()) |
974 |
return false; |
||
975 |
20 |
env->isolate()->CancelTerminateExecution(); |
|
976 |
// It is possible that execution was terminated by another timeout in |
||
977 |
// which this timeout is nested, so check whether one of the watchdogs |
||
978 |
// from this invocation is responsible for termination. |
||
979 |
✓✓ | 20 |
if (timed_out) { |
980 |
10 |
node::THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, timeout); |
|
981 |
✓✗ | 10 |
} else if (received_signal) { |
982 |
10 |
node::THROW_ERR_SCRIPT_EXECUTION_INTERRUPTED(env); |
|
983 |
} |
||
984 |
} |
||
985 |
|||
986 |
✓✓ | 1736 |
if (try_catch.HasCaught()) { |
987 |
✓✓✓✓ ✓✓ |
149 |
if (!timed_out && !received_signal && display_errors) { |
988 |
// We should decorate non-termination exceptions |
||
989 |
81 |
errors::DecorateErrorStack(env, try_catch); |
|
990 |
} |
||
991 |
|||
992 |
// If there was an exception thrown during script execution, re-throw it. |
||
993 |
// If one of the above checks threw, re-throw the exception instead of |
||
994 |
// letting try_catch catch it. |
||
995 |
// If execution has been terminated, but not by one of the watchdogs from |
||
996 |
// this invocation, this will re-throw a `null` value. |
||
997 |
✓✓ | 149 |
if (!try_catch.HasTerminated()) |
998 |
147 |
try_catch.ReThrow(); |
|
999 |
|||
1000 |
149 |
return false; |
|
1001 |
} |
||
1002 |
|||
1003 |
3174 |
args.GetReturnValue().Set(result.ToLocalChecked()); |
|
1004 |
1587 |
return true; |
|
1005 |
} |
||
1006 |
|||
1007 |
|||
1008 |
1961 |
ContextifyScript::ContextifyScript(Environment* env, Local<Object> object) |
|
1009 |
: BaseObject(env, object), |
||
1010 |
3922 |
id_(env->get_next_script_id()) { |
|
1011 |
1961 |
MakeWeak(); |
|
1012 |
1961 |
env->id_to_script_map.emplace(id_, this); |
|
1013 |
1961 |
} |
|
1014 |
|||
1015 |
|||
1016 |
7544 |
ContextifyScript::~ContextifyScript() { |
|
1017 |
1886 |
env()->id_to_script_map.erase(id_); |
|
1018 |
3772 |
} |
|
1019 |
|||
1020 |
|||
1021 |
30592 |
void ContextifyContext::CompileFunction( |
|
1022 |
const FunctionCallbackInfo<Value>& args) { |
||
1023 |
30592 |
Environment* env = Environment::GetCurrent(args); |
|
1024 |
30592 |
Isolate* isolate = env->isolate(); |
|
1025 |
30592 |
Local<Context> context = env->context(); |
|
1026 |
|||
1027 |
// Argument 1: source code |
||
1028 |
✗✓ | 91776 |
CHECK(args[0]->IsString()); |
1029 |
61184 |
Local<String> code = args[0].As<String>(); |
|
1030 |
|||
1031 |
// Argument 2: filename |
||
1032 |
✗✓ | 91776 |
CHECK(args[1]->IsString()); |
1033 |
61184 |
Local<String> filename = args[1].As<String>(); |
|
1034 |
|||
1035 |
// Argument 3: line offset |
||
1036 |
✗✓ | 61184 |
CHECK(args[2]->IsNumber()); |
1037 |
61184 |
Local<Integer> line_offset = args[2].As<Integer>(); |
|
1038 |
|||
1039 |
// Argument 4: column offset |
||
1040 |
✗✓ | 61184 |
CHECK(args[3]->IsNumber()); |
1041 |
61184 |
Local<Integer> column_offset = args[3].As<Integer>(); |
|
1042 |
|||
1043 |
// Argument 5: cached data (optional) |
||
1044 |
Local<ArrayBufferView> cached_data_buf; |
||
1045 |
✗✓ | 91776 |
if (!args[4]->IsUndefined()) { |
1046 |
CHECK(args[4]->IsArrayBufferView()); |
||
1047 |
cached_data_buf = args[4].As<ArrayBufferView>(); |
||
1048 |
} |
||
1049 |
|||
1050 |
// Argument 6: produce cache data |
||
1051 |
✗✓ | 61184 |
CHECK(args[5]->IsBoolean()); |
1052 |
61184 |
bool produce_cached_data = args[5]->IsTrue(); |
|
1053 |
|||
1054 |
// Argument 7: parsing context (optional) |
||
1055 |
Local<Context> parsing_context; |
||
1056 |
✓✓ | 91776 |
if (!args[6]->IsUndefined()) { |
1057 |
✗✓ | 4 |
CHECK(args[6]->IsObject()); |
1058 |
ContextifyContext* sandbox = |
||
1059 |
2 |
ContextifyContext::ContextFromContextifiedSandbox( |
|
1060 |
6 |
env, args[6].As<Object>()); |
|
1061 |
✗✓ | 2 |
CHECK_NOT_NULL(sandbox); |
1062 |
2 |
parsing_context = sandbox->context(); |
|
1063 |
} else { |
||
1064 |
30590 |
parsing_context = context; |
|
1065 |
} |
||
1066 |
|||
1067 |
// Argument 8: context extensions (optional) |
||
1068 |
Local<Array> context_extensions_buf; |
||
1069 |
✓✗ | 91776 |
if (!args[7]->IsUndefined()) { |
1070 |
✗✓ | 61184 |
CHECK(args[7]->IsArray()); |
1071 |
61184 |
context_extensions_buf = args[7].As<Array>(); |
|
1072 |
} |
||
1073 |
|||
1074 |
// Argument 9: params for the function (optional) |
||
1075 |
Local<Array> params_buf; |
||
1076 |
✓✓ | 91776 |
if (!args[8]->IsUndefined()) { |
1077 |
✗✓ | 61172 |
CHECK(args[8]->IsArray()); |
1078 |
61172 |
params_buf = args[8].As<Array>(); |
|
1079 |
} |
||
1080 |
|||
1081 |
// Read cache from cached data buffer |
||
1082 |
30592 |
ScriptCompiler::CachedData* cached_data = nullptr; |
|
1083 |
✗✓ | 30592 |
if (!cached_data_buf.IsEmpty()) { |
1084 |
uint8_t* data = static_cast<uint8_t*>( |
||
1085 |
cached_data_buf->Buffer()->GetBackingStore()->Data()); |
||
1086 |
cached_data = new ScriptCompiler::CachedData( |
||
1087 |
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); |
||
1088 |
} |
||
1089 |
|||
1090 |
// Get the function id |
||
1091 |
30592 |
uint32_t id = env->get_next_function_id(); |
|
1092 |
|||
1093 |
// Set host_defined_options |
||
1094 |
Local<PrimitiveArray> host_defined_options = |
||
1095 |
30592 |
PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); |
|
1096 |
91776 |
host_defined_options->Set( |
|
1097 |
isolate, |
||
1098 |
loader::HostDefinedOptions::kType, |
||
1099 |
30592 |
Number::New(isolate, loader::ScriptType::kFunction)); |
|
1100 |
91776 |
host_defined_options->Set( |
|
1101 |
30592 |
isolate, loader::HostDefinedOptions::kID, Number::New(isolate, id)); |
|
1102 |
|||
1103 |
ScriptOrigin origin(filename, |
||
1104 |
line_offset, // line offset |
||
1105 |
column_offset, // column offset |
||
1106 |
True(isolate), // is cross origin |
||
1107 |
Local<Integer>(), // script id |
||
1108 |
Local<Value>(), // source map URL |
||
1109 |
False(isolate), // is opaque (?) |
||
1110 |
False(isolate), // is WASM |
||
1111 |
False(isolate), // is ES Module |
||
1112 |
30592 |
host_defined_options); |
|
1113 |
|||
1114 |
30563 |
ScriptCompiler::Source source(code, origin, cached_data); |
|
1115 |
ScriptCompiler::CompileOptions options; |
||
1116 |
✓✗ | 30592 |
if (source.GetCachedData() == nullptr) { |
1117 |
30592 |
options = ScriptCompiler::kNoCompileOptions; |
|
1118 |
} else { |
||
1119 |
options = ScriptCompiler::kConsumeCodeCache; |
||
1120 |
} |
||
1121 |
|||
1122 |
61155 |
TryCatchScope try_catch(env); |
|
1123 |
✓✓ | 30563 |
Context::Scope scope(parsing_context); |
1124 |
|||
1125 |
// Read context extensions from buffer |
||
1126 |
61155 |
std::vector<Local<Object>> context_extensions; |
|
1127 |
✓✗ | 30592 |
if (!context_extensions_buf.IsEmpty()) { |
1128 |
✓✓ | 61186 |
for (uint32_t n = 0; n < context_extensions_buf->Length(); n++) { |
1129 |
Local<Value> val; |
||
1130 |
✗✓ | 2 |
if (!context_extensions_buf->Get(context, n).ToLocal(&val)) return; |
1131 |
✗✓ | 1 |
CHECK(val->IsObject()); |
1132 |
1 |
context_extensions.push_back(val.As<Object>()); |
|
1133 |
} |
||
1134 |
} |
||
1135 |
|||
1136 |
// Read params from params buffer |
||
1137 |
✓✓ | 61155 |
std::vector<Local<String>> params; |
1138 |
✓✓ | 30592 |
if (!params_buf.IsEmpty()) { |
1139 |
✓✓ | 366948 |
for (uint32_t n = 0; n < params_buf->Length(); n++) { |
1140 |
Local<Value> val; |
||
1141 |
✗✓ | 305776 |
if (!params_buf->Get(context, n).ToLocal(&val)) return; |
1142 |
✗✓ | 305776 |
CHECK(val->IsString()); |
1143 |
152888 |
params.push_back(val.As<String>()); |
|
1144 |
} |
||
1145 |
} |
||
1146 |
|||
1147 |
Local<ScriptOrModule> script; |
||
1148 |
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext( |
||
1149 |
parsing_context, &source, params.size(), params.data(), |
||
1150 |
context_extensions.size(), context_extensions.data(), options, |
||
1151 |
30592 |
v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason, &script); |
|
1152 |
|||
1153 |
Local<Function> fn; |
||
1154 |
✓✓ | 30592 |
if (!maybe_fn.ToLocal(&fn)) { |
1155 |
✓✗✓✗ ✓✗ |
29 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
1156 |
29 |
errors::DecorateErrorStack(env, try_catch); |
|
1157 |
29 |
try_catch.ReThrow(); |
|
1158 |
} |
||
1159 |
29 |
return; |
|
1160 |
} |
||
1161 |
|||
1162 |
Local<Object> cache_key; |
||
1163 |
✗✓ | 91689 |
if (!env->compiled_fn_entry_template()->NewInstance( |
1164 |
61126 |
context).ToLocal(&cache_key)) { |
|
1165 |
return; |
||
1166 |
} |
||
1167 |
30563 |
CompiledFnEntry* entry = new CompiledFnEntry(env, cache_key, id, script); |
|
1168 |
30563 |
env->id_to_function_map.emplace(id, entry); |
|
1169 |
|||
1170 |
30563 |
Local<Object> result = Object::New(isolate); |
|
1171 |
✗✓ | 91689 |
if (result->Set(parsing_context, env->function_string(), fn).IsNothing()) |
1172 |
return; |
||
1173 |
✗✓ | 91689 |
if (result->Set(parsing_context, env->cache_key_string(), cache_key) |
1174 |
.IsNothing()) |
||
1175 |
return; |
||
1176 |
|||
1177 |
✗✓ | 30563 |
if (produce_cached_data) { |
1178 |
const std::unique_ptr<ScriptCompiler::CachedData> cached_data( |
||
1179 |
ScriptCompiler::CreateCodeCacheForFunction(fn)); |
||
1180 |
bool cached_data_produced = cached_data != nullptr; |
||
1181 |
if (cached_data_produced) { |
||
1182 |
MaybeLocal<Object> buf = Buffer::Copy( |
||
1183 |
env, |
||
1184 |
reinterpret_cast<const char*>(cached_data->data), |
||
1185 |
cached_data->length); |
||
1186 |
if (result |
||
1187 |
->Set(parsing_context, |
||
1188 |
env->cached_data_string(), |
||
1189 |
buf.ToLocalChecked()) |
||
1190 |
.IsNothing()) |
||
1191 |
return; |
||
1192 |
} |
||
1193 |
if (result |
||
1194 |
->Set(parsing_context, |
||
1195 |
env->cached_data_produced_string(), |
||
1196 |
Boolean::New(isolate, cached_data_produced)) |
||
1197 |
.IsNothing()) |
||
1198 |
return; |
||
1199 |
} |
||
1200 |
|||
1201 |
✓✓ | 61126 |
args.GetReturnValue().Set(result); |
1202 |
} |
||
1203 |
|||
1204 |
53 |
void CompiledFnEntry::WeakCallback( |
|
1205 |
const WeakCallbackInfo<CompiledFnEntry>& data) { |
||
1206 |
53 |
CompiledFnEntry* entry = data.GetParameter(); |
|
1207 |
✓✗ | 53 |
delete entry; |
1208 |
53 |
} |
|
1209 |
|||
1210 |
30563 |
CompiledFnEntry::CompiledFnEntry(Environment* env, |
|
1211 |
Local<Object> object, |
||
1212 |
uint32_t id, |
||
1213 |
30563 |
Local<ScriptOrModule> script) |
|
1214 |
: BaseObject(env, object), |
||
1215 |
id_(id), |
||
1216 |
30563 |
script_(env->isolate(), script) { |
|
1217 |
30563 |
script_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter); |
|
1218 |
30563 |
} |
|
1219 |
|||
1220 |
113198 |
CompiledFnEntry::~CompiledFnEntry() { |
|
1221 |
28299 |
env()->id_to_function_map.erase(id_); |
|
1222 |
28300 |
script_.ClearWeak(); |
|
1223 |
56599 |
} |
|
1224 |
|||
1225 |
143 |
static void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) { |
|
1226 |
143 |
int ret = SigintWatchdogHelper::GetInstance()->Start(); |
|
1227 |
429 |
args.GetReturnValue().Set(ret == 0); |
|
1228 |
143 |
} |
|
1229 |
|||
1230 |
140 |
static void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) { |
|
1231 |
140 |
bool had_pending_signals = SigintWatchdogHelper::GetInstance()->Stop(); |
|
1232 |
420 |
args.GetReturnValue().Set(had_pending_signals); |
|
1233 |
140 |
} |
|
1234 |
|||
1235 |
3 |
static void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) { |
|
1236 |
3 |
bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal(); |
|
1237 |
9 |
args.GetReturnValue().Set(ret); |
|
1238 |
3 |
} |
|
1239 |
|||
1240 |
8 |
static void MeasureMemory(const FunctionCallbackInfo<Value>& args) { |
|
1241 |
✗✓ | 16 |
CHECK(args[0]->IsInt32()); |
1242 |
✗✓ | 16 |
CHECK(args[1]->IsInt32()); |
1243 |
24 |
int32_t mode = args[0].As<v8::Int32>()->Value(); |
|
1244 |
24 |
int32_t execution = args[1].As<v8::Int32>()->Value(); |
|
1245 |
8 |
Isolate* isolate = args.GetIsolate(); |
|
1246 |
|||
1247 |
8 |
Local<Context> current_context = isolate->GetCurrentContext(); |
|
1248 |
Local<Promise::Resolver> resolver; |
||
1249 |
✗✓ | 16 |
if (!Promise::Resolver::New(current_context).ToLocal(&resolver)) return; |
1250 |
std::unique_ptr<v8::MeasureMemoryDelegate> delegate = |
||
1251 |
v8::MeasureMemoryDelegate::Default( |
||
1252 |
isolate, |
||
1253 |
current_context, |
||
1254 |
resolver, |
||
1255 |
16 |
static_cast<v8::MeasureMemoryMode>(mode)); |
|
1256 |
16 |
isolate->MeasureMemory(std::move(delegate), |
|
1257 |
8 |
static_cast<v8::MeasureMemoryExecution>(execution)); |
|
1258 |
8 |
v8::Local<v8::Promise> promise = resolver->GetPromise(); |
|
1259 |
|||
1260 |
16 |
args.GetReturnValue().Set(promise); |
|
1261 |
} |
||
1262 |
|||
1263 |
4 |
MicrotaskQueueWrap::MicrotaskQueueWrap(Environment* env, Local<Object> obj) |
|
1264 |
: BaseObject(env, obj), |
||
1265 |
microtask_queue_( |
||
1266 |
4 |
MicrotaskQueue::New(env->isolate(), MicrotasksPolicy::kExplicit)) { |
|
1267 |
4 |
MakeWeak(); |
|
1268 |
4 |
} |
|
1269 |
|||
1270 |
const std::shared_ptr<MicrotaskQueue>& |
||
1271 |
12 |
MicrotaskQueueWrap::microtask_queue() const { |
|
1272 |
12 |
return microtask_queue_; |
|
1273 |
} |
||
1274 |
|||
1275 |
4 |
void MicrotaskQueueWrap::New(const FunctionCallbackInfo<Value>& args) { |
|
1276 |
✗✓ | 4 |
CHECK(args.IsConstructCall()); |
1277 |
8 |
new MicrotaskQueueWrap(Environment::GetCurrent(args), args.This()); |
|
1278 |
4 |
} |
|
1279 |
|||
1280 |
5004 |
void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) { |
|
1281 |
10008 |
HandleScope scope(env->isolate()); |
|
1282 |
Local<String> class_name = |
||
1283 |
5004 |
FIXED_ONE_BYTE_STRING(env->isolate(), "MicrotaskQueue"); |
|
1284 |
|||
1285 |
5004 |
Local<FunctionTemplate> tmpl = env->NewFunctionTemplate(New); |
|
1286 |
15012 |
tmpl->InstanceTemplate()->SetInternalFieldCount( |
|
1287 |
5004 |
ContextifyScript::kInternalFieldCount); |
|
1288 |
5004 |
tmpl->SetClassName(class_name); |
|
1289 |
|||
1290 |
✗✓ | 15012 |
if (target->Set(env->context(), |
1291 |
class_name, |
||
1292 |
25020 |
tmpl->GetFunction(env->context()).ToLocalChecked()) |
|
1293 |
.IsNothing()) { |
||
1294 |
return; |
||
1295 |
} |
||
1296 |
✓✗ | 5004 |
env->set_microtask_queue_ctor_template(tmpl); |
1297 |
} |
||
1298 |
|||
1299 |
|||
1300 |
5004 |
void Initialize(Local<Object> target, |
|
1301 |
Local<Value> unused, |
||
1302 |
Local<Context> context, |
||
1303 |
void* priv) { |
||
1304 |
5004 |
Environment* env = Environment::GetCurrent(context); |
|
1305 |
5004 |
Isolate* isolate = env->isolate(); |
|
1306 |
5004 |
ContextifyContext::Init(env, target); |
|
1307 |
5004 |
ContextifyScript::Init(env, target); |
|
1308 |
5004 |
MicrotaskQueueWrap::Init(env, target); |
|
1309 |
|||
1310 |
5004 |
env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog); |
|
1311 |
5004 |
env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog); |
|
1312 |
// Used in tests. |
||
1313 |
env->SetMethodNoSideEffect( |
||
1314 |
5004 |
target, "watchdogHasPendingSigint", WatchdogHasPendingSigint); |
|
1315 |
|||
1316 |
{ |
||
1317 |
5004 |
Local<FunctionTemplate> tpl = FunctionTemplate::New(env->isolate()); |
|
1318 |
10008 |
tpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "CompiledFnEntry")); |
|
1319 |
15012 |
tpl->InstanceTemplate()->SetInternalFieldCount( |
|
1320 |
5004 |
CompiledFnEntry::kInternalFieldCount); |
|
1321 |
|||
1322 |
5004 |
env->set_compiled_fn_entry_template(tpl->InstanceTemplate()); |
|
1323 |
} |
||
1324 |
|||
1325 |
5004 |
Local<Object> constants = Object::New(env->isolate()); |
|
1326 |
5004 |
Local<Object> measure_memory = Object::New(env->isolate()); |
|
1327 |
5004 |
Local<Object> memory_execution = Object::New(env->isolate()); |
|
1328 |
|||
1329 |
{ |
||
1330 |
5004 |
Local<Object> memory_mode = Object::New(env->isolate()); |
|
1331 |
5004 |
MeasureMemoryMode SUMMARY = MeasureMemoryMode::kSummary; |
|
1332 |
5004 |
MeasureMemoryMode DETAILED = MeasureMemoryMode::kDetailed; |
|
1333 |
20016 |
NODE_DEFINE_CONSTANT(memory_mode, SUMMARY); |
|
1334 |
5004 |
NODE_DEFINE_CONSTANT(memory_mode, DETAILED); |
|
1335 |
25020 |
READONLY_PROPERTY(measure_memory, "mode", memory_mode); |
|
1336 |
5004 |
} |
|
1337 |
25020 |
||
1338 |
10008 |
{ |
|
1339 |
5004 |
MeasureMemoryExecution DEFAULT = MeasureMemoryExecution::kDefault; |
|
1340 |
5004 |
MeasureMemoryExecution EAGER = MeasureMemoryExecution::kEager; |
|
1341 |
10008 |
NODE_DEFINE_CONSTANT(memory_execution, DEFAULT); |
|
1342 |
5004 |
NODE_DEFINE_CONSTANT(memory_execution, EAGER); |
|
1343 |
35028 |
READONLY_PROPERTY(measure_memory, "execution", memory_execution); |
|
1344 |
25020 |
} |
|
1345 |
25020 |
||
1346 |
25020 |
READONLY_PROPERTY(constants, "measureMemory", measure_memory); |
|
1347 |
|||
1348 |
15012 |
target->Set(context, env->constants_string(), constants).Check(); |
|
1349 |
|||
1350 |
5004 |
env->SetMethod(target, "measureMemory", MeasureMemory); |
|
1351 |
5004 |
} |
|
1352 |
|||
1353 |
} // namespace contextify |
||
1354 |
} // namespace node |
||
1355 |
|||
1356 |
✓✗✓✗ |
18704 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(contextify, node::contextify::Initialize) |
Generated by: GCOVR (Version 3.4) |