Skip to content

Commit 674d33c

Browse files
devsnektargos
authored andcommitted
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@{#62830} Refs: v8/v8@b33af60 Backport-PR-URL: #28779 PR-URL: #28671 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent 8586294 commit 674d33c

File tree

4 files changed

+93
-58
lines changed

4 files changed

+93
-58
lines changed

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,13 @@ class V8_EXPORT ScriptCompiler {
17031703
CompileOptions options = kNoCompileOptions,
17041704
NoCacheReason no_cache_reason = kNoCacheNoReason);
17051705

1706+
static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
1707+
Local<Context> context, Source* source, size_t arguments_count,
1708+
Local<String> arguments[], size_t context_extension_count,
1709+
Local<Object> context_extensions[], CompileOptions options,
1710+
NoCacheReason no_cache_reason,
1711+
Local<ScriptOrModule>* script_or_module_out);
1712+
17061713
/**
17071714
* Creates and returns code cache for the specified unbound_script.
17081715
* This will return nullptr if the script cannot be serialized. The

deps/v8/src/api.cc

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,69 +2500,92 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
25002500
Local<String> arguments[], size_t context_extension_count,
25012501
Local<Object> context_extensions[], CompileOptions options,
25022502
NoCacheReason no_cache_reason) {
2503-
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2504-
Function);
2505-
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2503+
return ScriptCompiler::CompileFunctionInContext(
2504+
v8_context, source, arguments_count, arguments, context_extension_count,
2505+
context_extensions, options, no_cache_reason, nullptr);
2506+
}
25062507

2507-
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2508-
options == CompileOptions::kEagerCompile ||
2509-
options == CompileOptions::kNoCompileOptions);
2508+
MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
2509+
Local<Context> v8_context, Source* source, size_t arguments_count,
2510+
Local<String> arguments[], size_t context_extension_count,
2511+
Local<Object> context_extensions[], CompileOptions options,
2512+
NoCacheReason no_cache_reason,
2513+
Local<ScriptOrModule>* script_or_module_out) {
2514+
Local<Function> result;
25102515

2511-
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2516+
{
2517+
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2518+
Function);
2519+
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
25122520

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-
}
2521+
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2522+
options == CompileOptions::kEagerCompile ||
2523+
options == CompileOptions::kNoCompileOptions);
25392524

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);
2525+
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
25442526

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

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();
2579+
if (script_or_module_out != nullptr) {
2580+
i::Handle<i::JSFunction> function =
2581+
i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*result));
2582+
i::Isolate* isolate = function->GetIsolate();
2583+
i::Handle<i::SharedFunctionInfo> shared(function->shared(), isolate);
2584+
i::Handle<i::Script> script(i::Script::cast(shared->script()), isolate);
2585+
*script_or_module_out = v8::Utils::ScriptOrModuleToLocal(script);
25622586
}
2563-
delete script_data;
2564-
RETURN_ON_FAILED_EXECUTION(Function);
2565-
RETURN_ESCAPED(Utils::CallableToLocal(result));
2587+
2588+
return result;
25662589
}
25672590

25682591
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)