Skip to content

Commit

Permalink
[source-phase-imports] Add �stractModuleSource%
Browse files Browse the repository at this point in the history
Make `WebAssembly.Module` a subclass of �stractModuleSource%.
At the moment, `HostGetModuleSourceNameCallback` is only applied
to `WebAssembly.Module`.

A runtime native method `%GetAbstractModuleSource` is added for testing.

Bug: 42204365
Change-Id: I0150f3d75e1f63e3695fc6f0a0f5c29fcc59536e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/ /5690180
Reviewed-by: Shu-yu Guo <[email protected]>
Commit-Queue: Chengzhong Wu (legendecas) <[email protected]>
Reviewed-by: Adam Klein <[email protected]>
Cr-Commit-Position: refs/heads/main@{#95568}
  • Loading branch information
legendecas authored and V8 LUCI CQ committed Aug 9, 2024
1 parent b5012b8 commit 3ac0c46
Show file tree
Hide file tree
Showing 19 changed files with 399 additions and 233 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 1317,7 @@ filegroup(
"src/builtins/builtins.cc",
"src/builtins/builtins-inl.h",
"src/builtins/builtins.h",
"src/builtins/builtins-abstract-module-source.cc",
"src/builtins/builtins-api.cc",
"src/builtins/builtins-array.cc",
"src/builtins/builtins-arraybuffer.cc",
Expand Down
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5343,6 5343,7 @@ v8_source_set("v8_base_without_compiler") {
"src/baseline/baseline.cc",
"src/baseline/bytecode-offset-iterator.cc",
"src/builtins/accessors.cc",
"src/builtins/builtins-abstract-module-source.cc",
"src/builtins/builtins-api.cc",
"src/builtins/builtins-array.cc",
"src/builtins/builtins-arraybuffer.cc",
Expand Down
40 changes: 40 additions & 0 deletions src/builtins/builtins-abstract-module-source.cc
Original file line number Diff line number Diff line change
@@ -0,0 1,40 @@
// Copyright 2024 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/builtins/builtins-utils-inl.h"
#include "src/objects/objects-inl.h"

namespace v8 {
namespace internal {

// https://tc39.es/proposal-source-phase-imports/#sec-get-�stractmodulesource%.prototype.@@tostringtag
BUILTIN(AbstractModuleSourceToStringTag) {
HandleScope scope(isolate);
// 1. Let O be the this value.
Handle<Object> receiver = args.receiver();

// 2. If O is not an Object, return undefined.
if (!IsJSReceiver(*receiver)) {
return *isolate->factory()->undefined_value();
}
// 3. Let sourceNameResult be Completion(HostGetModuleSourceName(O)).
// 4. If sourceNameResult is an abrupt completion, return undefined.
// 5. Let name be ! sourceNameResult.
// 6. Assert: name is a String.
// 7. Return name.

#if V8_ENABLE_WEBASSEMBLY
// https://webassembly.github.io/esm-integration/js-api/index.html#hostgetmodulesourcename
// Whenever a WebAssembly Module object is provided with a [[Module]] internal
// slot, the string "WebAssembly.Module" is always returned.
if (IsWasmModuleObject(*receiver)) {
return *isolate->factory()->WebAssemblyModule_string();
}
#endif
// TODO(42204365): Implement host hook.
return *isolate->factory()->undefined_value();
}

} // namespace internal
} // namespace v8
4 changes: 4 additions & 0 deletions src/builtins/builtins-definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 359,14 @@ namespace internal {
/* Special internal builtins */ \
CPP(EmptyFunction) \
CPP(Illegal) \
CPP(IllegalInvocationThrower) \
CPP(StrictPoisonPillThrower) \
CPP(UnsupportedThrower) \
TFJ(ReturnReceiver, kJSArgcReceiverSlots, kReceiver) \
\
/* AbstractModuleSource */ \
CPP(AbstractModuleSourceToStringTag) \
\
/* Array */ \
TFC(ArrayConstructor, JSTrampoline) \
TFC(ArrayConstructorImpl, ArrayConstructor) \
Expand Down
6 changes: 6 additions & 0 deletions src/builtins/builtins-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,12 @@ BUILTIN(Illegal) {
UNREACHABLE();
}

BUILTIN(IllegalInvocationThrower) {
HandleScope scope(isolate);
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kIllegalInvocation));
}

BUILTIN(EmptyFunction) { return ReadOnlyRoots(isolate).undefined_value(); }

BUILTIN(UnsupportedThrower) {
Expand Down
25 changes: 24 additions & 1 deletion src/init/bootstrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5815,7 5815,30 @@ void Genesis::InitializeGlobal_js_float16array() {
}

void Genesis::InitializeGlobal_js_source_phase_imports() {
// TODO(42204365): Initialize AbstractModuleSource.
if (!v8_flags.js_source_phase_imports) return;
Factory* factory = isolate()->factory();
// -- �stractModuleSource%
// #sec-�stractmodulesource%
// https://tc39.es/proposal-source-phase-imports/#sec-�stractmodulesource%
Handle<JSFunction> abstract_module_source_fun = CreateFunction(
isolate_, "AbstractModuleSource", JS_OBJECT_TYPE, JSObject::kHeaderSize,
0, factory->the_hole_value(), Builtin::kIllegalInvocationThrower);
abstract_module_source_fun->shared()->set_length(0);
abstract_module_source_fun->shared()->DontAdaptArguments();

native_context()->set_abstract_module_source_function(
*abstract_module_source_fun);

// Setup �stractModuleSourcePrototype%.
Handle<JSObject> abstract_module_source_prototype(
Cast<JSObject>(abstract_module_source_fun->instance_prototype()),
isolate());
native_context()->set_abstract_module_source_prototype(
*abstract_module_source_prototype);

SimpleInstallGetter(isolate(), abstract_module_source_prototype,
isolate()->factory()->to_string_tag_symbol(),
Builtin::kAbstractModuleSourceToStringTag, true);
}

void Genesis::InitializeGlobal_regexp_linear_flag() {
Expand Down
1 change: 1 addition & 0 deletions src/init/heap-symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 403,7 @@
V(_, roundingIncrement_string, "roundingIncrement") \
V(_, RuntimeError_string, "RuntimeError") \
V(_, WebAssemblyException_string, "WebAssembly.Exception") \
V(_, WebAssemblyModule_string, "WebAssembly.Module") \
V(_, Script_string, "Script") \
V(_, script_string, "script") \
V(_, second_string, "second") \
Expand Down
4 changes: 4 additions & 0 deletions src/objects/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 84,10 @@ enum ContextLookupFlags {
V(RAB_GSAB_BIGINT64_ARRAY_MAP_INDEX, Map, rab_gsab_bigint64_array_map) \
V(RAB_GSAB_FLOAT16_ARRAY_MAP_INDEX, Map, rab_gsab_float16_array_map) \
/* Below is alpha-sorted */ \
V(ABSTRACT_MODULE_SOURCE_FUNCTION_INDEX, JSFunction, \
abstract_module_source_function) \
V(ABSTRACT_MODULE_SOURCE_PROTOTYPE_INDEX, JSObject, \
abstract_module_source_prototype) \
V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, \
accessor_property_descriptor_map) \
V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Expand Down
Loading

0 comments on commit 3ac0c46

Please sign in to comment.