Skip to content

Commit f3d00c5

Browse files
committed
deps: V8: backport 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 PR-URL: #30062 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9a71091 commit f3d00c5

File tree

7 files changed

+114
-19
lines changed

7 files changed

+114
-19
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.16',
42+
'v8_embedder_string': '-node.17',
4343

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

deps/v8/include/v8.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -1440,11 +1440,19 @@ class V8_EXPORT Module {
14401440
/**
14411441
* Set this module's exported value for the name export_name to the specified
14421442
* export_value. This method must be called only on Modules created via
1443-
* CreateSyntheticModule. export_name must be one of the export_names that
1444-
* were passed in that CreateSyntheticModule call.
1445-
*/
1446-
void SetSyntheticModuleExport(Local<String> export_name,
1447-
Local<Value> export_value);
1443+
* CreateSyntheticModule. An error will be thrown if export_name is not one
1444+
* of the export_names that were passed in that CreateSyntheticModule call.
1445+
* Returns Just(true) on success, Nothing<bool>() if an error was thrown.
1446+
*/
1447+
V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport(
1448+
Isolate* isolate, Local<String> export_name, Local<Value> export_value);
1449+
V8_DEPRECATE_SOON(
1450+
"Use the preceding SetSyntheticModuleExport with an Isolate parameter, "
1451+
"instead of the one that follows. The former will throw a runtime "
1452+
"error if called for an export that doesn't exist (as per spec); "
1453+
"the latter will crash with a failed CHECK().",
1454+
void SetSyntheticModuleExport(Local<String> export_name,
1455+
Local<Value> export_value));
14481456
};
14491457

14501458
/**

deps/v8/src/api/api.cc

+25-3
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

+1
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ class RuntimeCallTimer final {
780780
V(Message_GetStartColumn) \
781781
V(Module_Evaluate) \
782782
V(Module_InstantiateModule) \
783+
V(Module_SetSyntheticModuleExport) \
783784
V(NumberObject_New) \
784785
V(NumberObject_NumberValue) \
785786
V(Object_CallAsConstructor) \

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

+25-5
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

+15-3
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

+34-2
Original file line numberDiff line numberDiff line change
@@ -23654,7 +23654,9 @@ v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackFail(
2365423654

2365523655
v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackSetExport(
2365623656
Local<Context> context, Local<Module> module) {
23657-
module->SetSyntheticModuleExport(v8_str("test_export"), v8_num(42));
23657+
Maybe<bool> set_export_result = module->SetSyntheticModuleExport(
23658+
context->GetIsolate(), v8_str("test_export"), v8_num(42));
23659+
CHECK(set_export_result.FromJust());
2365823660
return v8::Undefined(reinterpret_cast<v8::Isolate*>(context->GetIsolate()));
2365923661
}
2366023662

@@ -23861,7 +23863,9 @@ TEST(SyntheticModuleSetExports) {
2386123863
// undefined.
2386223864
CHECK(foo_cell->value().IsUndefined());
2386323865

23864-
module->SetSyntheticModuleExport(foo_string, bar_string);
23866+
Maybe<bool> set_export_result =
23867+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23868+
CHECK(set_export_result.FromJust());
2386523869

2386623870
// After setting the export the Cell should still have the same idenitity.
2386723871
CHECK_EQ(exports->Lookup(v8::Utils::OpenHandle(*foo_string)), *foo_cell);
@@ -23872,6 +23876,34 @@ TEST(SyntheticModuleSetExports) {
2387223876
->Equals(*v8::Utils::OpenHandle(*bar_string)));
2387323877
}
2387423878

23879+
TEST(SyntheticModuleSetMissingExport) {
23880+
LocalContext env;
23881+
v8::Isolate* isolate = env->GetIsolate();
23882+
auto i_isolate = reinterpret_cast<i::Isolate*>(isolate);
23883+
v8::Isolate::Scope iscope(isolate);
23884+
v8::HandleScope scope(isolate);
23885+
v8::Local<v8::Context> context = v8::Context::New(isolate);
23886+
v8::Context::Scope cscope(context);
23887+
23888+
Local<String> foo_string = v8_str("foo");
23889+
Local<String> bar_string = v8_str("bar");
23890+
23891+
Local<Module> module = CreateAndInstantiateSyntheticModule(
23892+
isolate, v8_str("SyntheticModuleSetExports-TestSyntheticModule"), context,
23893+
std::vector<v8::Local<v8::String>>(),
23894+
UnexpectedSyntheticModuleEvaluationStepsCallback);
23895+
23896+
i::Handle<i::SyntheticModule> i_module =
23897+
i::Handle<i::SyntheticModule>::cast(v8::Utils::OpenHandle(*module));
23898+
i::Handle<i::ObjectHashTable> exports(i_module->exports(), i_isolate);
23899+
23900+
TryCatch try_catch(isolate);
23901+
Maybe<bool> set_export_result =
23902+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23903+
CHECK(set_export_result.IsNothing());
23904+
CHECK(try_catch.HasCaught());
23905+
}
23906+
2387523907
TEST(SyntheticModuleEvaluationStepsNoThrow) {
2387623908
synthetic_module_callback_count = 0;
2387723909
LocalContext env;

0 commit comments

Comments
 (0)