Skip to content

Commit 84d3243

Browse files
committed
deps: V8: cherry-pick 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 PR-URL: #28016 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann (רפאל פלחי) <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 3b7c952 commit 84d3243

File tree

4 files changed

+79
-60
lines changed

4 files changed

+79
-60
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
# Reset this number to 0 on major V8 upgrades.
4141
# Increment by one for each non-official patch applied to deps/v8.
42-
'v8_embedder_string': '-node.13',
42+
'v8_embedder_string': '-node.14',
4343

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

deps/v8/include/v8.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,8 @@ class V8_EXPORT ScriptCompiler {
16681668
Local<String> arguments[], size_t context_extension_count,
16691669
Local<Object> context_extensions[],
16701670
CompileOptions options = kNoCompileOptions,
1671-
NoCacheReason no_cache_reason = kNoCacheNoReason);
1671+
NoCacheReason no_cache_reason = kNoCacheNoReason,
1672+
Local<ScriptOrModule>* script_or_module_out = nullptr);
16721673

16731674
/**
16741675
* Creates and returns code cache for the specified unbound_script.

deps/v8/src/api/api.cc

+69-56
Original file line numberDiff line numberDiff line change
@@ -2441,70 +2441,83 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
24412441
Local<Context> v8_context, Source* source, size_t arguments_count,
24422442
Local<String> arguments[], size_t context_extension_count,
24432443
Local<Object> context_extensions[], CompileOptions options,
2444-
NoCacheReason no_cache_reason) {
2445-
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2446-
Function);
2447-
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2444+
NoCacheReason no_cache_reason,
2445+
Local<ScriptOrModule>* script_or_module_out) {
2446+
Local<Function> result;
24482447

2449-
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2450-
options == CompileOptions::kEagerCompile ||
2451-
options == CompileOptions::kNoCompileOptions);
2448+
{
2449+
PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2450+
Function);
2451+
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
24522452

2453-
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2453+
DCHECK(options == CompileOptions::kConsumeCodeCache ||
2454+
options == CompileOptions::kEagerCompile ||
2455+
options == CompileOptions::kNoCompileOptions);
24542456

2455-
DCHECK(context->IsNativeContext());
2456-
i::Handle<i::SharedFunctionInfo> outer_info(
2457-
context->empty_function().shared(), isolate);
2458-
2459-
i::Handle<i::JSFunction> fun;
2460-
i::Handle<i::FixedArray> arguments_list =
2461-
isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
2462-
for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2463-
i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2464-
if (!IsIdentifier(isolate, argument)) return Local<Function>();
2465-
arguments_list->set(i, *argument);
2466-
}
2467-
2468-
for (size_t i = 0; i < context_extension_count; ++i) {
2469-
i::Handle<i::JSReceiver> extension =
2470-
Utils::OpenHandle(*context_extensions[i]);
2471-
if (!extension->IsJSObject()) return Local<Function>();
2472-
context = isolate->factory()->NewWithContext(
2473-
context,
2474-
i::ScopeInfo::CreateForWithScope(
2475-
isolate,
2476-
context->IsNativeContext()
2477-
? i::Handle<i::ScopeInfo>::null()
2478-
: i::Handle<i::ScopeInfo>(context->scope_info(), isolate)),
2479-
extension);
2480-
}
2457+
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
24812458

2482-
i::Compiler::ScriptDetails script_details = GetScriptDetails(
2483-
isolate, source->resource_name, source->resource_line_offset,
2484-
source->resource_column_offset, source->source_map_url,
2485-
source->host_defined_options);
2459+
DCHECK(context->IsNativeContext());
24862460

2487-
i::ScriptData* script_data = nullptr;
2488-
if (options == kConsumeCodeCache) {
2489-
DCHECK(source->cached_data);
2490-
// ScriptData takes care of pointer-aligning the data.
2491-
script_data = new i::ScriptData(source->cached_data->data,
2492-
source->cached_data->length);
2461+
i::Handle<i::FixedArray> arguments_list =
2462+
isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
2463+
for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2464+
i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2465+
if (!IsIdentifier(isolate, argument)) return Local<Function>();
2466+
arguments_list->set(i, *argument);
2467+
}
2468+
2469+
for (size_t i = 0; i < context_extension_count; ++i) {
2470+
i::Handle<i::JSReceiver> extension =
2471+
Utils::OpenHandle(*context_extensions[i]);
2472+
if (!extension->IsJSObject()) return Local<Function>();
2473+
context = isolate->factory()->NewWithContext(
2474+
context,
2475+
i::ScopeInfo::CreateForWithScope(
2476+
isolate,
2477+
context->IsNativeContext()
2478+
? i::Handle<i::ScopeInfo>::null()
2479+
: i::Handle<i::ScopeInfo>(context->scope_info(), isolate)),
2480+
extension);
2481+
}
2482+
2483+
i::Compiler::ScriptDetails script_details = GetScriptDetails(
2484+
isolate, source->resource_name, source->resource_line_offset,
2485+
source->resource_column_offset, source->source_map_url,
2486+
source->host_defined_options);
2487+
2488+
i::ScriptData* script_data = nullptr;
2489+
if (options == kConsumeCodeCache) {
2490+
DCHECK(source->cached_data);
2491+
// ScriptData takes care of pointer-aligning the data.
2492+
script_data = new i::ScriptData(source->cached_data->data,
2493+
source->cached_data->length);
2494+
}
2495+
2496+
i::Handle<i::JSFunction> scoped_result;
2497+
has_pending_exception =
2498+
!i::Compiler::GetWrappedFunction(
2499+
Utils::OpenHandle(*source->source_string), arguments_list, context,
2500+
script_details, source->resource_options, script_data, options,
2501+
no_cache_reason)
2502+
.ToHandle(&scoped_result);
2503+
if (options == kConsumeCodeCache) {
2504+
source->cached_data->rejected = script_data->rejected();
2505+
}
2506+
delete script_data;
2507+
RETURN_ON_FAILED_EXECUTION(Function);
2508+
result = handle_scope.Escape(Utils::CallableToLocal(scoped_result));
24932509
}
24942510

2495-
i::Handle<i::JSFunction> result;
2496-
has_pending_exception =
2497-
!i::Compiler::GetWrappedFunction(
2498-
Utils::OpenHandle(*source->source_string), arguments_list, context,
2499-
script_details, source->resource_options, script_data, options,
2500-
no_cache_reason)
2501-
.ToHandle(&result);
2502-
if (options == kConsumeCodeCache) {
2503-
source->cached_data->rejected = script_data->rejected();
2511+
if (script_or_module_out != nullptr) {
2512+
i::Handle<i::JSFunction> function =
2513+
i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*result));
2514+
i::Isolate* isolate = function->GetIsolate();
2515+
i::Handle<i::SharedFunctionInfo> shared(function->shared(), isolate);
2516+
i::Handle<i::Script> script(i::Script::cast(shared->script()), isolate);
2517+
*script_or_module_out = v8::Utils::ScriptOrModuleToLocal(script);
25042518
}
2505-
delete script_data;
2506-
RETURN_ON_FAILED_EXECUTION(Function);
2507-
RETURN_ESCAPED(Utils::CallableToLocal(result));
2519+
2520+
return result;
25082521
}
25092522

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

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,16 @@ TEST(CompileFunctionInContextScriptOrigin) {
650650
v8::Integer::New(CcTest::isolate(), 22),
651651
v8::Integer::New(CcTest::isolate(), 41));
652652
v8::ScriptCompiler::Source script_source(v8_str("throw new Error()"), origin);
653+
Local<ScriptOrModule> script;
653654
v8::Local<v8::Function> fun =
654-
v8::ScriptCompiler::CompileFunctionInContext(env.local(), &script_source,
655-
0, nullptr, 0, nullptr)
655+
v8::ScriptCompiler::CompileFunctionInContext(
656+
env.local(), &script_source, 0, nullptr, 0, nullptr,
657+
v8::ScriptCompiler::CompileOptions::kNoCompileOptions,
658+
v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason, &script)
656659
.ToLocalChecked();
657660
CHECK(!fun.IsEmpty());
661+
CHECK(!script.IsEmpty());
662+
CHECK(script->GetResourceName()->StrictEquals(v8_str("test")));
658663
v8::TryCatch try_catch(CcTest::isolate());
659664
CcTest::isolate()->SetCaptureStackTraceForUncaughtExceptions(true);
660665
CHECK(fun->Call(env.local(), env->Global(), 0, nullptr).IsEmpty());

0 commit comments

Comments
 (0)