Skip to content

Commit b7bcfc9

Browse files
committed
deps: V8: backport b33af60
Original commit message: [api] Get ScriptOrModule from CompileFunctionInContext Adds a new out param which allows accessing the ScriptOrModule of a function, which allows an embedder such as Node.js to use the function's i::Script lifetime. Refs: nodejs/node-v8#111 Change-Id: I34346d94d76e8f9b8377c97d948673f4b95eb9d5 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1699698 Reviewed-by: Yang Guo <[email protected]> Commit-Queue: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{nodejs#62830} Refs: v8/v8@b33af60 PR-URL: nodejs#28671 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent 15d44bf commit b7bcfc9

4 files changed

Lines changed: 79 additions & 60 deletions

File tree

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
# Reset this number to 0 on major V8 upgrades.
4040
# Increment by one for each non-official patch applied to deps/v8.
41-
'v8_embedder_string': '-node.15',
41+
'v8_embedder_string': '-node.16',
4242

4343
##### V8 defaults for Node.js #####
4444

deps/v8/include/v8.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,8 @@ class V8_EXPORT ScriptCompiler {
17011701
Local<String> arguments[], size_t context_extension_count,
17021702
Local<Object> context_extensions[],
17031703
CompileOptions options = kNoCompileOptions,
1704-
NoCacheReason no_cache_reason = kNoCacheNoReason);
1704+
NoCacheReason no_cache_reason = kNoCacheNoReason,
1705+
Local<ScriptOrModule>* script_or_module_out = nullptr);
17051706

17061707
/**
17071708
* Creates and returns code cache for the specified unbound_script.

deps/v8/src/api.cc

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,70 +2499,83 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
24992499
Local<Context> v8_context, Source* source, size_t arguments_count,
25002500
Local<String> arguments[], size_t context_extension_count,
25012501
Local<Object> context_extensions[], CompileOptions options,
2502-
NoCacheReason no_cache_reason) {
2503-
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2504-
Function);
2505-
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2502+
NoCacheReason no_cache_reason,
2503+
Local<ScriptOrModule>* script_or_module_out) {
2504+
Local<Function> result;
25062505

2507-
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2508-
options == CompileOptions::kEagerCompile ||
2509-
options == CompileOptions::kNoCompileOptions);
2506+
{
2507+
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2508+
Function);
2509+
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
25102510

2511-
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2511+
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2512+
options == CompileOptions::kEagerCompile ||
2513+
options == CompileOptions::kNoCompileOptions);
25122514

2513-
DCHECK(context->IsNativeContext());
2514-
i::Handle<i::SharedFunctionInfo> outer_info(
2515-
context->empty_function()->shared(), isolate);
2516-
2517-
i::Handle<i::JSFunction> fun;
2518-
i::Handle<i::FixedArray> arguments_list =
2519-
isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
2520-
for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2521-
i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2522-
if (!IsIdentifier(isolate, argument)) return Local<Function>();
2523-
arguments_list->set(i, *argument);
2524-
}
2525-
2526-
for (size_t i = 0; i < context_extension_count; ++i) {
2527-
i::Handle<i::JSReceiver> extension =
2528-
Utils::OpenHandle(*context_extensions[i]);
2529-
if (!extension->IsJSObject()) return Local<Function>();
2530-
context = isolate->factory()->NewWithContext(
2531-
context,
2532-
i::ScopeInfo::CreateForWithScope(
2533-
isolate,
2534-
context->IsNativeContext()
2535-
? i::Handle<i::ScopeInfo>::null()
2536-
: i::Handle<i::ScopeInfo>(context->scope_info(), isolate)),
2537-
extension);
2538-
}
2515+
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
25392516

2540-
i::Compiler::ScriptDetails script_details = GetScriptDetails(
2541-
isolate, source->resource_name, source->resource_line_offset,
2542-
source->resource_column_offset, source->source_map_url,
2543-
source->host_defined_options);
2517+
DCHECK(context->IsNativeContext());
25442518

2545-
i::ScriptData* script_data = nullptr;
2546-
if (options == kConsumeCodeCache) {
2547-
DCHECK(source->cached_data);
2548-
// ScriptData takes care of pointer-aligning the data.
2549-
script_data = new i::ScriptData(source->cached_data->data,
2550-
source->cached_data->length);
2519+
i::Handle<i::FixedArray> arguments_list =
2520+
isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
2521+
for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2522+
i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2523+
if (!IsIdentifier(isolate, argument)) return Local<Function>();
2524+
arguments_list->set(i, *argument);
2525+
}
2526+
2527+
for (size_t i = 0; i < context_extension_count; ++i) {
2528+
i::Handle<i::JSReceiver> extension =
2529+
Utils::OpenHandle(*context_extensions[i]);
2530+
if (!extension->IsJSObject()) return Local<Function>();
2531+
context = isolate->factory()->NewWithContext(
2532+
context,
2533+
i::ScopeInfo::CreateForWithScope(
2534+
isolate,
2535+
context->IsNativeContext()
2536+
? i::Handle<i::ScopeInfo>::null()
2537+
: i::Handle<i::ScopeInfo>(context->scope_info(), isolate)),
2538+
extension);
2539+
}
2540+
2541+
i::Compiler::ScriptDetails script_details = GetScriptDetails(
2542+
isolate, source->resource_name, source->resource_line_offset,
2543+
source->resource_column_offset, source->source_map_url,
2544+
source->host_defined_options);
2545+
2546+
i::ScriptData* script_data = nullptr;
2547+
if (options == kConsumeCodeCache) {
2548+
DCHECK(source->cached_data);
2549+
// ScriptData takes care of pointer-aligning the data.
2550+
script_data = new i::ScriptData(source->cached_data->data,
2551+
source->cached_data->length);
2552+
}
2553+
2554+
i::Handle<i::JSFunction> scoped_result;
2555+
has_pending_exception =
2556+
!i::Compiler::GetWrappedFunction(
2557+
Utils::OpenHandle(*source->source_string), arguments_list, context,
2558+
script_details, source->resource_options, script_data, options,
2559+
no_cache_reason)
2560+
.ToHandle(&scoped_result);
2561+
if (options == kConsumeCodeCache) {
2562+
source->cached_data->rejected = script_data->rejected();
2563+
}
2564+
delete script_data;
2565+
RETURN_ON_FAILED_EXECUTION(Function);
2566+
result = handle_scope.Escape(Utils::CallableToLocal(scoped_result));
25512567
}
25522568

2553-
i::Handle<i::JSFunction> result;
2554-
has_pending_exception =
2555-
!i::Compiler::GetWrappedFunction(
2556-
Utils::OpenHandle(*source->source_string), arguments_list, context,
2557-
script_details, source->resource_options, script_data, options,
2558-
no_cache_reason)
2559-
.ToHandle(&result);
2560-
if (options == kConsumeCodeCache) {
2561-
source->cached_data->rejected = script_data->rejected();
2569+
if (script_or_module_out != nullptr) {
2570+
i::Handle<i::JSFunction> function =
2571+
i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*result));
2572+
i::Isolate* isolate = function->GetIsolate();
2573+
i::Handle<i::SharedFunctionInfo> shared(function->shared(), isolate);
2574+
i::Handle<i::Script> script(i::Script::cast(shared->script()), isolate);
2575+
*script_or_module_out = v8::Utils::ScriptOrModuleToLocal(script);
25622576
}
2563-
delete script_data;
2564-
RETURN_ON_FAILED_EXECUTION(Function);
2565-
RETURN_ESCAPED(Utils::CallableToLocal(result));
2577+
2578+
return result;
25662579
}
25672580

25682581
void ScriptCompiler::ScriptStreamingTask::Run() { data_->task->Run(); }

deps/v8/test/cctest/test-compiler.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,16 @@ TEST(CompileFunctionInContextScriptOrigin) {
647647
v8::Integer::New(CcTest::isolate(), 22),
648648
v8::Integer::New(CcTest::isolate(), 41));
649649
v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
650+
Local<ScriptOrModule> script;
650651
v8::Local<v8::Function> fun =
651-
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
652-
0, nullptr, 0, nullptr)
652+
v8::ScriptCompiler::CompileFunctionInContext(
653+
env.local(), &script_source, 0, nullptr, 0, nullptr,
654+
v8::ScriptCompiler::CompileOptions::kNoCompileOptions,
655+
v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason, &script)
653656
.ToLocalChecked();
654657
CHECK(!fun.IsEmpty());
658+
CHECK(!script.IsEmpty());
659+
CHECK(script->GetResourceName()->StrictEquals(v8_str("test")));
655660
v8::TryCatch try_catch(CcTest::isolate());
656661
CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
657662
CHECK(fun->Call(env.local(), env->Global(), 0, nullptr).IsEmpty());

0 commit comments

Comments
 (0)