Skip to content

Commit 5131bbe

Browse files
targosMylesBorins
authored andcommitted
deps: V8: cherry-pick 777fa98
Original commit message: Make SetSyntheticModuleExport throw instead of crash for nonexistent export name Per spec, Module::SetSyntheticModuleExport should throw a ReferenceError when called with an export name that was not supplied when constructing that SyntheticModule. Instead, the current implementation crashes with a failed CHECK(). Add a new Module::SyntheticModuleSetExport that throws (without an ensuing crash) for this case, and deprecate the old Module::SetSyntheticModuleExport. Bug: v8:9828 Change-Id: I3b3d353064c3851882781818099bd8f6ee74c809 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1860996 Reviewed-by: Adam Klein <[email protected]> Reviewed-by: Georg Neis <[email protected]> Commit-Queue: Dan Clark <[email protected]> Cr-Commit-Position: refs/heads/master@{#64438} Refs: v8/v8@777fa98 Backport-PR-URL: #30513 PR-URL: #30020 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 824e8b6 commit 5131bbe

File tree

7 files changed

+111
-16
lines changed

7 files changed

+111
-16
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
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.14',
42+
'v8_embedder_string': '-node.15',
4343

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

deps/v8/include/v8.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,9 +1560,17 @@ class V8_EXPORT Module : public Data {
15601560
/**
15611561
* Set this module's exported value for the name export_name to the specified
15621562
* export_value. This method must be called only on Modules created via
1563-
* CreateSyntheticModule. export_name must be one of the export_names that
1564-
* were passed in that CreateSyntheticModule call.
1563+
* CreateSyntheticModule. An error will be thrown if export_name is not one
1564+
* of the export_names that were passed in that CreateSyntheticModule call.
1565+
* Returns Just(true) on success, Nothing<bool>() if an error was thrown.
15651566
*/
1567+
V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport(
1568+
Isolate* isolate, Local<String> export_name, Local<Value> export_value);
1569+
V8_DEPRECATE_SOON(
1570+
"Use the preceding SetSyntheticModuleExport with an Isolate parameter, "
1571+
"instead of the one that follows. The former will throw a runtime "
1572+
"error if called for an export that doesn't exist (as per spec); "
1573+
"the latter will crash with a failed CHECK().")
15661574
void SetSyntheticModuleExport(Local<String> export_name,
15671575
Local<Value> export_value);
15681576
};

deps/v8/src/api/api.cc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,28 @@ Local<Module> Module::CreateSyntheticModule(
23622362
i_module_name, i_export_names, evaluation_steps)));
23632363
}
23642364

2365+
Maybe<bool> Module::SetSyntheticModuleExport(Isolate* isolate,
2366+
Local<String> export_name,
2367+
Local<v8::Value> export_value) {
2368+
auto i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2369+
i::Handle<i::String> i_export_name = Utils::OpenHandle(*export_name);
2370+
i::Handle<i::Object> i_export_value = Utils::OpenHandle(*export_value);
2371+
i::Handle<i::Module> self = Utils::OpenHandle(this);
2372+
Utils::ApiCheck(self->IsSyntheticModule(),
2373+
"v8::Module::SyntheticModuleSetExport",
2374+
"v8::Module::SyntheticModuleSetExport must only be called on "
2375+
"a SyntheticModule");
2376+
ENTER_V8_NO_SCRIPT(i_isolate, isolate->GetCurrentContext(), Module,
2377+
SetSyntheticModuleExport, Nothing<bool>(), i::HandleScope);
2378+
has_pending_exception =
2379+
i::SyntheticModule::SetExport(i_isolate,
2380+
i::Handle<i::SyntheticModule>::cast(self),
2381+
i_export_name, i_export_value)
2382+
.IsNothing();
2383+
RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
2384+
return Just(true);
2385+
}
2386+
23652387
void Module::SetSyntheticModuleExport(Local<String> export_name,
23662388
Local<v8::Value> export_value) {
23672389
i::Handle<i::String> i_export_name = Utils::OpenHandle(*export_name);
@@ -2371,9 +2393,9 @@ void Module::SetSyntheticModuleExport(Local<String> export_name,
23712393
"v8::Module::SetSyntheticModuleExport",
23722394
"v8::Module::SetSyntheticModuleExport must only be called on "
23732395
"a SyntheticModule");
2374-
i::SyntheticModule::SetExport(self->GetIsolate(),
2375-
i::Handle<i::SyntheticModule>::cast(self),
2376-
i_export_name, i_export_value);
2396+
i::SyntheticModule::SetExportStrict(self->GetIsolate(),
2397+
i::Handle<i::SyntheticModule>::cast(self),
2398+
i_export_name, i_export_value);
23772399
}
23782400

23792401
namespace {

deps/v8/src/logging/counters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ class RuntimeCallTimer final {
783783
V(Message_GetStartColumn) \
784784
V(Module_Evaluate) \
785785
V(Module_InstantiateModule) \
786+
V(Module_SetSyntheticModuleExport) \
786787
V(NumberObject_New) \
787788
V(NumberObject_NumberValue) \
788789
V(Object_CallAsConstructor) \

deps/v8/src/objects/synthetic-module.cc

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,36 @@ namespace internal {
1717

1818
// Implements SetSyntheticModuleBinding:
1919
// https://heycam.github.io/webidl/#setsyntheticmoduleexport
20-
void SyntheticModule::SetExport(Isolate* isolate,
21-
Handle<SyntheticModule> module,
22-
Handle<String> export_name,
23-
Handle<Object> export_value) {
20+
Maybe<bool> SyntheticModule::SetExport(Isolate* isolate,
21+
Handle<SyntheticModule> module,
22+
Handle<String> export_name,
23+
Handle<Object> export_value) {
2424
Handle<ObjectHashTable> exports(module->exports(), isolate);
2525
Handle<Object> export_object(exports->Lookup(export_name), isolate);
26-
CHECK(export_object->IsCell());
26+
27+
if (!export_object->IsCell()) {
28+
isolate->Throw(*isolate->factory()->NewReferenceError(
29+
MessageTemplate::kModuleExportUndefined, export_name));
30+
return Nothing<bool>();
31+
}
32+
2733
Handle<Cell> export_cell(Handle<Cell>::cast(export_object));
2834
// Spec step 2: Set the mutable binding of export_name to export_value
2935
export_cell->set_value(*export_value);
36+
37+
return Just(true);
38+
}
39+
40+
void SyntheticModule::SetExportStrict(Isolate* isolate,
41+
Handle<SyntheticModule> module,
42+
Handle<String> export_name,
43+
Handle<Object> export_value) {
44+
Handle<ObjectHashTable> exports(module->exports(), isolate);
45+
Handle<Object> export_object(exports->Lookup(export_name), isolate);
46+
CHECK(export_object->IsCell());
47+
Maybe<bool> set_export_result =
48+
SetExport(isolate, module, export_name, export_value);
49+
CHECK(set_export_result.FromJust());
3050
}
3151

3252
// Implements Synthetic Module Record's ResolveExport concrete method:

deps/v8/src/objects/synthetic-module.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ class SyntheticModule
2424
DECL_VERIFIER(SyntheticModule)
2525
DECL_PRINTER(SyntheticModule)
2626

27-
static void SetExport(Isolate* isolate, Handle<SyntheticModule> module,
28-
Handle<String> export_name,
29-
Handle<Object> export_value);
27+
// Set module's exported value for the specified export_name to the specified
28+
// export_value. An error will be thrown if export_name is not one
29+
// of the export_names that were supplied during module construction.
30+
// Returns Just(true) on success, Nothing<bool>() if an error was thrown.
31+
static Maybe<bool> SetExport(Isolate* isolate, Handle<SyntheticModule> module,
32+
Handle<String> export_name,
33+
Handle<Object> export_value);
34+
// The following redundant method should be deleted when the deprecated
35+
// version of v8::SetSyntheticModuleExport is removed. It differs from
36+
// SetExport in that it crashes rather than throwing an error if the caller
37+
// attempts to set an export_name that was not present during construction of
38+
// the module.
39+
static void SetExportStrict(Isolate* isolate, Handle<SyntheticModule> module,
40+
Handle<String> export_name,
41+
Handle<Object> export_value);
3042

3143
using BodyDescriptor = SubclassBodyDescriptor<
3244
Module::BodyDescriptor,

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23721,7 +23721,9 @@ v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackFail(
2372123721

2372223722
v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackSetExport(
2372323723
Local<Context> context, Local<Module> module) {
23724-
module->SetSyntheticModuleExport(v8_str("test_export"), v8_num(42));
23724+
Maybe<bool> set_export_result = module->SetSyntheticModuleExport(
23725+
context->GetIsolate(), v8_str("test_export"), v8_num(42));
23726+
CHECK(set_export_result.FromJust());
2372523727
return v8::Undefined(reinterpret_cast<v8::Isolate*>(context->GetIsolate()));
2372623728
}
2372723729

@@ -23940,7 +23942,9 @@ TEST(SyntheticModuleSetExports) {
2394023942
// undefined.
2394123943
CHECK(foo_cell->value().IsUndefined());
2394223944

23943-
module->SetSyntheticModuleExport(foo_string, bar_string);
23945+
Maybe<bool> set_export_result =
23946+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23947+
CHECK(set_export_result.FromJust());
2394423948

2394523949
// After setting the export the Cell should still have the same idenitity.
2394623950
CHECK_EQ(exports->Lookup(v8::Utils::OpenHandle(*foo_string)), *foo_cell);
@@ -23951,6 +23955,34 @@ TEST(SyntheticModuleSetExports) {
2395123955
->Equals(*v8::Utils::OpenHandle(*bar_string)));
2395223956
}
2395323957

23958+
TEST(SyntheticModuleSetMissingExport) {
23959+
LocalContext env;
23960+
v8::Isolate* isolate = env->GetIsolate();
23961+
auto i_isolate = reinterpret_cast<i::Isolate*>(isolate);
23962+
v8::Isolate::Scope iscope(isolate);
23963+
v8::HandleScope scope(isolate);
23964+
v8::Local<v8::Context> context = v8::Context::New(isolate);
23965+
v8::Context::Scope cscope(context);
23966+
23967+
Local<String> foo_string = v8_str("foo");
23968+
Local<String> bar_string = v8_str("bar");
23969+
23970+
Local<Module> module = CreateAndInstantiateSyntheticModule(
23971+
isolate, v8_str("SyntheticModuleSetExports-TestSyntheticModule"), context,
23972+
std::vector<v8::Local<v8::String>>(),
23973+
UnexpectedSyntheticModuleEvaluationStepsCallback);
23974+
23975+
i::Handle<i::SyntheticModule> i_module =
23976+
i::Handle<i::SyntheticModule>::cast(v8::Utils::OpenHandle(*module));
23977+
i::Handle<i::ObjectHashTable> exports(i_module->exports(), i_isolate);
23978+
23979+
TryCatch try_catch(isolate);
23980+
Maybe<bool> set_export_result =
23981+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23982+
CHECK(set_export_result.IsNothing());
23983+
CHECK(try_catch.HasCaught());
23984+
}
23985+
2395423986
TEST(SyntheticModuleEvaluationStepsNoThrow) {
2395523987
synthetic_module_callback_count = 0;
2395623988
LocalContext env;

0 commit comments

Comments
 (0)