Skip to content

Commit 72493af

Browse files
targosaduh95
authored andcommitted
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 <adamk@chromium.org> Reviewed-by: Georg Neis <neis@chromium.org> Commit-Queue: Dan Clark <daniec@microsoft.com> Cr-Commit-Position: refs/heads/master@{#64438} Refs: v8/v8@777fa98 PR-URL: nodejs#30062 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4442359 commit 72493af

7 files changed

Lines changed: 114 additions & 19 deletions

File tree

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
# Reset this number to 0 on major V8 upgrades.
4040
# Increment by one for each non-official patch applied to deps/v8.
41-
'v8_embedder_string': '-node.16',
41+
'v8_embedder_string': '-node.17',
4242

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

deps/v8/include/v8.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,11 +1385,19 @@ class V8_EXPORT Module {
13851385
/**
13861386
* Set this module's exported value for the name export_name to the specified
13871387
* export_value. This method must be called only on Modules created via
1388-
* CreateSyntheticModule. export_name must be one of the export_names that
1389-
* were passed in that CreateSyntheticModule call.
1390-
*/
1391-
void SetSyntheticModuleExport(Local<String> export_name,
1392-
Local<Value> export_value);
1388+
* CreateSyntheticModule. An error will be thrown if export_name is not one
1389+
* of the export_names that were passed in that CreateSyntheticModule call.
1390+
* Returns Just(true) on success, Nothing<bool>() if an error was thrown.
1391+
*/
1392+
V8_WARN_UNUSED_RESULT Maybe<bool> SetSyntheticModuleExport(
1393+
Isolate* isolate, Local<String> export_name, Local<Value> export_value);
1394+
V8_DEPRECATE_SOON(
1395+
"Use the preceding SetSyntheticModuleExport with an Isolate parameter, "
1396+
"instead of the one that follows. The former will throw a runtime "
1397+
"error if called for an export that doesn't exist (as per spec); "
1398+
"the latter will crash with a failed CHECK().",
1399+
void SetSyntheticModuleExport(Local<String> export_name,
1400+
Local<Value> export_value));
13931401
};
13941402

13951403
/**

deps/v8/src/api/api.cc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,28 @@ Local<Module> Module::CreateSyntheticModule(
23332333
i_module_name, i_export_names, evaluation_steps)));
23342334
}
23352335

2336+
Maybe<bool> Module::SetSyntheticModuleExport(Isolate* isolate,
2337+
Local<String> export_name,
2338+
Local<v8::Value> export_value) {
2339+
auto i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2340+
i::Handle<i::String> i_export_name = Utils::OpenHandle(*export_name);
2341+
i::Handle<i::Object> i_export_value = Utils::OpenHandle(*export_value);
2342+
i::Handle<i::Module> self = Utils::OpenHandle(this);
2343+
Utils::ApiCheck(self->IsSyntheticModule(),
2344+
"v8::Module::SyntheticModuleSetExport",
2345+
"v8::Module::SyntheticModuleSetExport must only be called on "
2346+
"a SyntheticModule");
2347+
ENTER_V8_NO_SCRIPT(i_isolate, isolate->GetCurrentContext(), Module,
2348+
SetSyntheticModuleExport, Nothing<bool>(), i::HandleScope);
2349+
has_pending_exception =
2350+
i::SyntheticModule::SetExport(i_isolate,
2351+
i::Handle<i::SyntheticModule>::cast(self),
2352+
i_export_name, i_export_value)
2353+
.IsNothing();
2354+
RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
2355+
return Just(true);
2356+
}
2357+
23362358
void Module::SetSyntheticModuleExport(Local<String> export_name,
23372359
Local<v8::Value> export_value) {
23382360
i::Handle<i::String> i_export_name = Utils::OpenHandle(*export_name);
@@ -2342,9 +2364,9 @@ void Module::SetSyntheticModuleExport(Local<String> export_name,
23422364
"v8::Module::SetSyntheticModuleExport",
23432365
"v8::Module::SetSyntheticModuleExport must only be called on "
23442366
"a SyntheticModule");
2345-
i::SyntheticModule::SetExport(self->GetIsolate(),
2346-
i::Handle<i::SyntheticModule>::cast(self),
2347-
i_export_name, i_export_value);
2367+
i::SyntheticModule::SetExportStrict(self->GetIsolate(),
2368+
i::Handle<i::SyntheticModule>::cast(self),
2369+
i_export_name, i_export_value);
23482370
}
23492371

23502372
namespace {

deps/v8/src/logging/counters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ class RuntimeCallTimer final {
779779
V(Message_GetStartColumn) \
780780
V(Module_Evaluate) \
781781
V(Module_InstantiateModule) \
782+
V(Module_SetSyntheticModuleExport) \
782783
V(NumberObject_New) \
783784
V(NumberObject_NumberValue) \
784785
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
@@ -29,9 +29,21 @@ class SyntheticModule : public Module {
2929
DECL_ACCESSORS(export_names, FixedArray)
3030
DECL_ACCESSORS(evaluation_steps, Foreign)
3131

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

3648
// Layout description.
3749
DEFINE_FIELD_OFFSET_CONSTANTS(Module::kHeaderSize,

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

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

2362923629
v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackSetExport(
2363023630
Local<Context> context, Local<Module> module) {
23631-
module->SetSyntheticModuleExport(v8_str("test_export"), v8_num(42));
23631+
Maybe<bool> set_export_result = module->SetSyntheticModuleExport(
23632+
context->GetIsolate(), v8_str("test_export"), v8_num(42));
23633+
CHECK(set_export_result.FromJust());
2363223634
return v8::Undefined(reinterpret_cast<v8::Isolate*>(context->GetIsolate()));
2363323635
}
2363423636

@@ -23835,7 +23837,9 @@ TEST(SyntheticModuleSetExports) {
2383523837
// undefined.
2383623838
CHECK(foo_cell->value().IsUndefined());
2383723839

23838-
module->SetSyntheticModuleExport(foo_string, bar_string);
23840+
Maybe<bool> set_export_result =
23841+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23842+
CHECK(set_export_result.FromJust());
2383923843

2384023844
// After setting the export the Cell should still have the same idenitity.
2384123845
CHECK_EQ(exports->Lookup(v8::Utils::OpenHandle(*foo_string)), *foo_cell);
@@ -23846,6 +23850,34 @@ TEST(SyntheticModuleSetExports) {
2384623850
->Equals(*v8::Utils::OpenHandle(*bar_string)));
2384723851
}
2384823852

23853+
TEST(SyntheticModuleSetMissingExport) {
23854+
LocalContext env;
23855+
v8::Isolate* isolate = env->GetIsolate();
23856+
auto i_isolate = reinterpret_cast<i::Isolate*>(isolate);
23857+
v8::Isolate::Scope iscope(isolate);
23858+
v8::HandleScope scope(isolate);
23859+
v8::Local<v8::Context> context = v8::Context::New(isolate);
23860+
v8::Context::Scope cscope(context);
23861+
23862+
Local<String> foo_string = v8_str("foo");
23863+
Local<String> bar_string = v8_str("bar");
23864+
23865+
Local<Module> module = CreateAndInstantiateSyntheticModule(
23866+
isolate, v8_str("SyntheticModuleSetExports-TestSyntheticModule"), context,
23867+
std::vector<v8::Local<v8::String>>(),
23868+
UnexpectedSyntheticModuleEvaluationStepsCallback);
23869+
23870+
i::Handle<i::SyntheticModule> i_module =
23871+
i::Handle<i::SyntheticModule>::cast(v8::Utils::OpenHandle(*module));
23872+
i::Handle<i::ObjectHashTable> exports(i_module->exports(), i_isolate);
23873+
23874+
TryCatch try_catch(isolate);
23875+
Maybe<bool> set_export_result =
23876+
module->SetSyntheticModuleExport(isolate, foo_string, bar_string);
23877+
CHECK(set_export_result.IsNothing());
23878+
CHECK(try_catch.HasCaught());
23879+
}
23880+
2384923881
TEST(SyntheticModuleEvaluationStepsNoThrow) {
2385023882
synthetic_module_callback_count = 0;
2385123883
LocalContext env;

0 commit comments

Comments
 (0)