Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: migrate to script context based host defined options #44923

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions deps/v8/include/v8-message.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ScriptOriginOptions {
*/
class V8_EXPORT ScriptOrigin {
public:
V8_DEPRECATE_SOON("Use constructor without the isolate.")
V8_INLINE ScriptOrigin(Isolate* isolate, Local<Value> resource_name,
int resource_line_offset = 0,
int resource_column_offset = 0,
Expand All @@ -70,17 +71,30 @@ class V8_EXPORT ScriptOrigin {
bool resource_is_opaque = false, bool is_wasm = false,
bool is_module = false,
Local<Data> host_defined_options = Local<Data>())
: v8_isolate_(isolate),
resource_name_(resource_name),
: resource_name_(resource_name),
resource_line_offset_(resource_line_offset),
resource_column_offset_(resource_column_offset),
options_(resource_is_shared_cross_origin, resource_is_opaque, is_wasm,
is_module),
script_id_(script_id),
source_map_url_(source_map_url),
host_defined_options_(host_defined_options) {
VerifyHostDefinedOptions();
}
host_defined_options_(host_defined_options) {}

V8_INLINE explicit ScriptOrigin(
Local<Value> resource_name, int resource_line_offset = 0,
int resource_column_offset = 0,
bool resource_is_shared_cross_origin = false, int script_id = -1,
Local<Value> source_map_url = Local<Value>(),
bool resource_is_opaque = false, bool is_wasm = false,
bool is_module = false, Local<Data> host_defined_options = Local<Data>())
: resource_name_(resource_name),
resource_line_offset_(resource_line_offset),
resource_column_offset_(resource_column_offset),
options_(resource_is_shared_cross_origin, resource_is_opaque, is_wasm,
is_module),
script_id_(script_id),
source_map_url_(source_map_url),
host_defined_options_(host_defined_options) {}

V8_INLINE Local<Value> ResourceName() const;
V8_INLINE int LineOffset() const;
Expand All @@ -91,8 +105,6 @@ class V8_EXPORT ScriptOrigin {
V8_INLINE ScriptOriginOptions Options() const { return options_; }

private:
void VerifyHostDefinedOptions() const;
Isolate* v8_isolate_;
Local<Value> resource_name_;
int resource_line_offset_;
int resource_column_offset_;
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/samples/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
bool report_exceptions) {
v8::HandleScope handle_scope(isolate);
v8::TryCatch try_catch(isolate);
v8::ScriptOrigin origin(isolate, name);
v8::ScriptOrigin origin(name);
v8::Local<v8::Context> context(isolate->GetCurrentContext());
v8::Local<v8::Script> script;
if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
Expand Down
92 changes: 41 additions & 51 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,15 @@ static ScriptOrigin GetScriptOriginForScript(i::Isolate* i_isolate,
i::Handle<i::Script> script) {
i::Handle<i::Object> scriptName(script->GetNameOrSourceURL(), i_isolate);
i::Handle<i::Object> source_map_url(script->source_mapping_url(), i_isolate);
i::Handle<i::Object> host_defined_options(script->host_defined_options(),
i_isolate);
i::Handle<i::Object> host_defined_options;
ScriptOriginOptions options(script->origin_options());
bool is_wasm = false;
#if V8_ENABLE_WEBASSEMBLY
is_wasm = script->type() == i::Script::TYPE_WASM;
#endif // V8_ENABLE_WEBASSEMBLY
v8::ScriptOrigin origin(
reinterpret_cast<v8::Isolate*>(i_isolate), Utils::ToLocal(scriptName),
script->line_offset(), script->column_offset(),
options.IsSharedCrossOrigin(), script->id(),
Utils::ToLocal(scriptName), script->line_offset(),
script->column_offset(), options.IsSharedCrossOrigin(), script->id(),
Utils::ToLocal(source_map_url), options.IsOpaque(), is_wasm,
options.IsModule(), Utils::ToLocal(host_defined_options));
return origin;
Expand Down Expand Up @@ -2142,11 +2140,10 @@ MaybeLocal<Value> Script::Run(Local<Context> context,
}

i::Handle<i::Object> receiver = i_isolate->global_proxy();
// TODO(cbruni, chromium:1244145): Remove once migrated to the context.
i::Handle<i::Object> options(
i::Script::cast(fun->shared().script()).host_defined_options(),
i_isolate);
Local<Value> result;
i::Handle<i::Object> options = host_defined_options.IsEmpty()
? i_isolate->factory()->empty_fixed_array()
: Utils::OpenHandle(*host_defined_options);
has_pending_exception = !ToLocal<Value>(
i::Execution::CallScript(i_isolate, fun, receiver, options), &result);

Expand Down Expand Up @@ -2500,18 +2497,16 @@ Module::GetStalledTopLevelAwaitMessage(Isolate* isolate) {

namespace {

i::ScriptDetails GetScriptDetails(
i::Isolate* i_isolate, Local<Value> resource_name, int resource_line_offset,
int resource_column_offset, Local<Value> source_map_url,
Local<Data> host_defined_options, ScriptOriginOptions origin_options) {
i::ScriptDetails GetScriptDetails(i::Isolate* i_isolate,
Local<Value> resource_name,
int resource_line_offset,
int resource_column_offset,
Local<Value> source_map_url,
ScriptOriginOptions origin_options) {
i::ScriptDetails script_details(Utils::OpenHandle(*(resource_name), true),
origin_options);
script_details.line_offset = resource_line_offset;
script_details.column_offset = resource_column_offset;
script_details.host_defined_options =
host_defined_options.IsEmpty()
? i_isolate->factory()->empty_fixed_array()
: Utils::OpenHandle(*(host_defined_options));
if (!source_map_url.IsEmpty()) {
script_details.source_map_url = Utils::OpenHandle(*(source_map_url));
}
Expand All @@ -2536,7 +2531,7 @@ MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
i::ScriptDetails script_details = GetScriptDetails(
i_isolate, source->resource_name, source->resource_line_offset,
source->resource_column_offset, source->source_map_url,
source->host_defined_options, source->resource_options);
source->resource_options);

i::MaybeHandle<i::SharedFunctionInfo> maybe_function_info;
if (options == kConsumeCodeCache) {
Expand Down Expand Up @@ -2615,7 +2610,12 @@ MaybeLocal<Module> ScriptCompiler::CompileModule(
if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>();
i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
return ToApiHandle<Module>(i_isolate->factory()->NewSourceTextModule(shared));
i::Handle<i::Object> host_defined_options =
source->host_defined_options.IsEmpty()
? i_isolate->factory()->empty_fixed_array()
: Utils::OpenHandle(*source->host_defined_options);
return ToApiHandle<Module>(
i_isolate->factory()->NewSourceTextModule(shared, host_defined_options));
}

// static
Expand Down Expand Up @@ -2690,7 +2690,7 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInternal(
i::ScriptDetails script_details = GetScriptDetails(
i_isolate, source->resource_name, source->resource_line_offset,
source->resource_column_offset, source->source_map_url,
source->host_defined_options, source->resource_options);
source->resource_options);

std::unique_ptr<i::AlignedCachedData> cached_data;
if (options == kConsumeCodeCache) {
Expand All @@ -2714,16 +2714,19 @@ MaybeLocal<Function> ScriptCompiler::CompileFunctionInternal(
}
// TODO(cbruni): remove script_or_module_out paramater
if (script_or_module_out != nullptr) {
i::Handle<i::JSFunction> function =
i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*result));
auto function = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*result));
i::Isolate* i_isolate = function->GetIsolate();
i::Handle<i::SharedFunctionInfo> shared(function->shared(), i_isolate);
i::Handle<i::Script> script(i::Script::cast(shared->script()), i_isolate);
i::Handle<i::Object> host_defined_options =
source->host_defined_options.IsEmpty()
? i_isolate->factory()->empty_fixed_array()
: Utils::OpenHandle(*source->host_defined_options);
// TODO(cbruni, v8:12302): Avoid creating tempory ScriptOrModule objects.
auto script_or_module = i::Handle<i::ScriptOrModule>::cast(
i_isolate->factory()->NewStruct(i::SCRIPT_OR_MODULE_TYPE));
script_or_module->set_resource_name(script->name());
script_or_module->set_host_defined_options(script->host_defined_options());
script_or_module->set_host_defined_options(*host_defined_options);
#ifdef V8_SCRIPTORMODULE_LEGACY_LIFETIME
i::Handle<i::ArrayList> list =
i::handle(script->script_or_modules(), i_isolate);
Expand Down Expand Up @@ -2767,10 +2770,9 @@ void ScriptCompiler::ConsumeCodeCacheTask::SourceTextAvailable(
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
DCHECK_NO_SCRIPT_NO_EXCEPTION(i_isolate);
i::Handle<i::String> str = Utils::OpenHandle(*(source_text));
i::ScriptDetails script_details =
GetScriptDetails(i_isolate, origin.ResourceName(), origin.LineOffset(),
origin.ColumnOffset(), origin.SourceMapUrl(),
origin.GetHostDefinedOptions(), origin.Options());
i::ScriptDetails script_details = GetScriptDetails(
i_isolate, origin.ResourceName(), origin.LineOffset(),
origin.ColumnOffset(), origin.SourceMapUrl(), origin.Options());
impl_->SourceTextAvailable(i_isolate, str, script_details);
}

Expand Down Expand Up @@ -2804,10 +2806,9 @@ i::MaybeHandle<i::SharedFunctionInfo> CompileStreamedSource(
i::Isolate* i_isolate, ScriptCompiler::StreamedSource* v8_source,
Local<String> full_source_string, const ScriptOrigin& origin) {
i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
i::ScriptDetails script_details =
GetScriptDetails(i_isolate, origin.ResourceName(), origin.LineOffset(),
origin.ColumnOffset(), origin.SourceMapUrl(),
origin.GetHostDefinedOptions(), origin.Options());
i::ScriptDetails script_details = GetScriptDetails(
i_isolate, origin.ResourceName(), origin.LineOffset(),
origin.ColumnOffset(), origin.SourceMapUrl(), origin.Options());
i::ScriptStreamingData* data = v8_source->impl();
return i::Compiler::GetSharedFunctionInfoForStreamedScript(
i_isolate, str, script_details, data);
Expand Down Expand Up @@ -2849,8 +2850,13 @@ MaybeLocal<Module> ScriptCompiler::CompileModule(
has_pending_exception = !maybe_sfi.ToHandle(&sfi);
if (has_pending_exception) i_isolate->ReportPendingMessages();
RETURN_ON_FAILED_EXECUTION(Module);
RETURN_ESCAPED(
ToApiHandle<Module>(i_isolate->factory()->NewSourceTextModule(sfi)));
// TODO(cleanup)
i::Handle<i::Object> options =
origin.GetHostDefinedOptions().IsEmpty()
? i_isolate->factory()->empty_fixed_array()
: Utils::OpenHandle(*origin.GetHostDefinedOptions());
RETURN_ESCAPED(ToApiHandle<Module>(
i_isolate->factory()->NewSourceTextModule(sfi, options)));
}

uint32_t ScriptCompiler::CachedDataVersionTag() {
Expand Down Expand Up @@ -3061,21 +3067,6 @@ ScriptOrigin Message::GetScriptOrigin() const {
return GetScriptOriginForScript(i_isolate, script);
}

void ScriptOrigin::VerifyHostDefinedOptions() const {
// TODO(cbruni, chromium:1244145): Remove checks once we allow arbitrary
// host-defined options.
USE(v8_isolate_);
if (host_defined_options_.IsEmpty()) return;
Utils::ApiCheck(host_defined_options_->IsFixedArray(), "ScriptOrigin()",
"Host-defined options has to be a PrimitiveArray");
i::Handle<i::FixedArray> options =
Utils::OpenHandle(*host_defined_options_.As<FixedArray>());
for (int i = 0; i < options->length(); i++) {
Utils::ApiCheck(options->get(i).IsPrimitive(), "ScriptOrigin()",
"PrimitiveArray can only contain primtive values");
}
}

v8::Local<Value> Message::GetScriptResourceName() const {
DCHECK_NO_SCRIPT_NO_EXCEPTION(Utils::OpenHandle(this)->GetIsolate());
return GetScriptOrigin().ResourceName();
Expand Down Expand Up @@ -5372,15 +5363,14 @@ Local<Value> Function::GetDebugName() const {

ScriptOrigin Function::GetScriptOrigin() const {
auto self = Utils::OpenHandle(this);
auto i_isolate = reinterpret_cast<v8::Isolate*>(self->GetIsolate());
if (!self->IsJSFunction()) return v8::ScriptOrigin(i_isolate, Local<Value>());
if (!self->IsJSFunction()) return v8::ScriptOrigin(Local<Value>());
auto func = i::Handle<i::JSFunction>::cast(self);
if (func->shared().script().IsScript()) {
i::Handle<i::Script> script(i::Script::cast(func->shared().script()),
func->GetIsolate());
return GetScriptOriginForScript(func->GetIsolate(), script);
}
return v8::ScriptOrigin(i_isolate, Local<Value>());
return v8::ScriptOrigin(Local<Value>());
}

const int Function::kLineOffsetNotFound = -1;
Expand Down
35 changes: 30 additions & 5 deletions deps/v8/src/ast/scopes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ Scope::Scope(Zone* zone, ScopeType scope_type,
already_resolved_ = true;
#endif
set_language_mode(scope_info->language_mode());
DCHECK_EQ(ContextHeaderLength(), num_heap_slots_);
private_name_lookup_skips_outer_class_ =
scope_info->PrivateNameLookupSkipsOuterClass();
// We don't really need to use the preparsed scope data; this is just to
Expand Down Expand Up @@ -276,6 +275,8 @@ DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type,
if (scope_info->SloppyEvalCanExtendVars()) {
DCHECK(!is_eval_scope());
sloppy_eval_can_extend_vars_ = true;
has_context_extension_slot_ = HasContextExtensionSlot();
num_heap_slots_ = ContextHeaderLength();
}
if (scope_info->ClassScopeHasPrivateBrand()) {
DCHECK(IsClassConstructor(function_kind()));
Expand Down Expand Up @@ -358,8 +359,11 @@ void Scope::SetDefaults() {
is_debug_evaluate_scope_ = false;

inner_scope_calls_eval_ = false;
inner_scope_calls_dynamic_import_ = false;
force_context_allocation_for_parameters_ = false;

has_context_extension_slot_ = HasContextExtensionSlot();

is_declaration_scope_ = false;

private_name_lookup_skips_outer_class_ = false;
Expand Down Expand Up @@ -860,6 +864,9 @@ Scope* Scope::FinalizeBlockScope() {
}

if (inner_scope_calls_eval_) outer_scope()->inner_scope_calls_eval_ = true;
if (inner_scope_calls_dynamic_import_) {
outer_scope()->set_inner_scope_calls_dynamic_import();
}

// No need to propagate sloppy_eval_can_extend_vars_, since if it was relevant
// to this scope we would have had to bail out at the top.
Expand Down Expand Up @@ -899,12 +906,18 @@ void Scope::Snapshot::Reparent(DeclarationScope* new_parent) {
if (inner_scope->inner_scope_calls_eval_) {
new_parent->inner_scope_calls_eval_ = true;
}
if (inner_scope->inner_scope_calls_dynamic_import_) {
new_parent->set_inner_scope_calls_dynamic_import();
}
DCHECK_NE(inner_scope, new_parent);
}
inner_scope->outer_scope_ = new_parent;
if (inner_scope->inner_scope_calls_eval_) {
new_parent->inner_scope_calls_eval_ = true;
}
if (inner_scope->inner_scope_calls_dynamic_import_) {
new_parent->set_inner_scope_calls_dynamic_import();
}
new_parent->inner_scope_ = new_parent->sibling_;
inner_scope->sibling_ = nullptr;
// Reset the sibling rather than the inner_scope_ since we
Expand Down Expand Up @@ -1949,6 +1962,12 @@ void Scope::Print(int n) {
Indent(n1, "// scope skips outer class for #-names\n");
}
if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
if (inner_scope_calls_dynamic_import_) {
Indent(n1, "// inner scope calls 'import'\n");
}
if (has_context_extension_slot_) {
Indent(n1, "// has context extension slot\n");
}
if (is_declaration_scope()) {
DeclarationScope* scope = AsDeclarationScope();
if (scope->was_lazily_parsed()) Indent(n1, "// lazily parsed\n");
Expand Down Expand Up @@ -2596,6 +2615,7 @@ void Scope::AllocateVariablesRecursively() {
// scope.
bool must_have_context =
scope->is_with_scope() || scope->is_module_scope() ||
scope->NeedsHostDefinedOptions() ||
#if V8_ENABLE_WEBASSEMBLY
scope->IsAsmModule() ||
#endif // V8_ENABLE_WEBASSEMBLY
Expand All @@ -2607,12 +2627,17 @@ void Scope::AllocateVariablesRecursively() {

// If we didn't allocate any locals in the local context, then we only
// need the minimal number of slots if we must have a context.
if (scope->num_heap_slots_ == scope->ContextHeaderLength() &&
!must_have_context) {
if (!must_have_context &&
scope->num_heap_slots() == scope->ContextHeaderLength()) {
scope->num_heap_slots_ = 0;
}

// Allocation done.
DCHECK_EQ(scope->has_context_extension_slot_,
scope->HasContextExtensionSlot());
DCHECK_IMPLIES(scope->has_context_extension_slot_, must_have_context);
DCHECK_IMPLIES(
scope->has_context_extension_slot_,
scope->num_heap_slots() >= Context::MIN_CONTEXT_EXTENDED_SLOTS);
DCHECK_IMPLIES(must_have_context, scope->num_heap_slots() > 0);
DCHECK(scope->num_heap_slots_ == 0 ||
scope->num_heap_slots_ >= scope->ContextHeaderLength());
return Iteration::kDescend;
Expand Down
Loading