Skip to content

Commit 1a8adb1

Browse files
committed
deps: V8: backport e7785934cd73
Original commit message: [api][module] Return a Promise from Module::Evaluate() Module evaluation always produces a Promise, but the public API exposed the result as MaybeLocal<Value>, and for synthetic modules it could even hand back the raw, non-Promise value returned by the embedder's evaluation steps. Embedders therefore had to defensively cast the result, and the actual contract was invisible in the type system. This CL makes the contract explicit: - SyntheticModule::Evaluate() now returns the top-level capability Promise instead of the raw value produced by the evaluation steps, so the completion value is always a Promise, matching source text modules and the documented behavior. - SyntheticModule::Evaluate() nows CHECK if callback result from EvaluationSteps is a Promise. The reasoning behind it is that the behavior for an embedder that relies on non-promise result was already inconsistent, given the first call would return the result form callback (an arbitrary Local<Value>), but subsequent calls for module->Evaluate() would return the capability with `undefined` as result. - Module::Evaluate() and their variant now returns `MaybeDirectHandle<JSPromise>` to be more explicit by the return type. Bug: 531396274 Change-Id: Ifbfed4f849bb47d8db1e022bfb750d9ad2d6308b Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8131138 Reviewed-by: Camillo Bruni <cbruni@chromium.org> Commit-Queue: Caio Lima <caiolima@igalia.com> Reviewed-by: Olivier Flückiger <olivf@chromium.org> Cr-Commit-Position: refs/heads/main@{#109010} Refs: v8/v8@e778593 Co-authored-by: Caio Lima <caiolima@igalia.com>
1 parent 3781ebc commit 1a8adb1

10 files changed

Lines changed: 39 additions & 39 deletions

File tree

‎common.gypi‎

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

4343
# Reset this number to 0 on major V8 upgrades.
4444
# Increment by one for each non-official patch applied to deps/v8.
45-
'v8_embedder_string': '-node.28',
45+
'v8_embedder_string': '-node.29',
4646

4747
##### V8 defaults for Node.js #####
4848

‎deps/v8/include/v8-script.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class V8_EXPORT Module : public Data {
280280
*
281281
* If IsGraphAsync() is false, the returned Promise is settled.
282282
*/
283-
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Evaluate(Local<Context> context);
283+
V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Evaluate(Local<Context> context);
284284

285285
/**
286286
* Returns the namespace object of this module.
@@ -336,6 +336,8 @@ class V8_EXPORT Module : public Data {
336336
* exception was thrown) and return an empy MaybeLocal to indicate falure
337337
* (where an exception was thrown).
338338
*/
339+
// TODO(caiolima): Change this to `MaybeLocal<Promise>` given it's expected a
340+
// Promise as return.
339341
using SyntheticModuleEvaluationSteps =
340342
MaybeLocal<Value> (*)(Local<Context> context, Local<Module> module);
341343

‎deps/v8/src/api/api.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,7 @@ Maybe<bool> Module::InstantiateModule(Local<Context> context,
23982398
return Just(true);
23992399
}
24002400

2401-
MaybeLocal<Value> Module::Evaluate(Local<Context> context) {
2401+
MaybeLocal<Promise> Module::Evaluate(Local<Context> context) {
24022402
auto i_isolate = i::Isolate::Current();
24032403
TRACE_EVENT_CALL_STATS_SCOPED(i_isolate, "v8", "V8.Execute");
24042404
EnterV8Scope<InternalEscapableScope> api_scope{i_isolate, context,

‎deps/v8/src/objects/module.cc‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ bool Module::FinishInstantiate(Isolate* isolate, Handle<Module> module,
265265
}
266266
}
267267

268-
MaybeDirectHandle<Object> Module::Evaluate(Isolate* isolate,
269-
Handle<Module> module) {
268+
MaybeDirectHandle<JSPromise> Module::Evaluate(Isolate* isolate,
269+
Handle<Module> module) {
270270
#ifdef DEBUG
271271
PrintStatusMessage(*module, "Evaluating module ");
272272
#endif // DEBUG
@@ -492,16 +492,13 @@ void JSDeferredModuleNamespace::EvaluateModuleSync(
492492
return;
493493
}
494494

495-
MaybeDirectHandle<Object> maybe_result = Module::Evaluate(isolate, module);
496-
DirectHandle<Object> result;
497-
if (!maybe_result.ToHandle(&result)) {
495+
MaybeDirectHandle<JSPromise> maybe_result = Module::Evaluate(isolate, module);
496+
DirectHandle<JSPromise> promise;
497+
if (!maybe_result.ToHandle(&promise)) {
498498
return;
499499
}
500500

501-
// If there's a result, it needs to be a promise with either Reject or
502-
// Fulfilled status.
503-
DCHECK(IsJSPromise(*result));
504-
DirectHandle<JSPromise> promise = Cast<JSPromise>(result);
501+
// The result is always the module's top-level capability promise.
505502
// 5. If promise.[[PromiseState]] is rejected, then
506503
if (promise->status() == Promise::kRejected) {
507504
// a. If promise.[[PromiseIsHandled]] is false, perform

‎deps/v8/src/objects/module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Module : public TorqueGeneratedModule<Module, HeapObject> {
7474
const UserResolveCallbacks& callbacks);
7575

7676
// Implementation of spec operation ModuleEvaluation.
77-
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> Evaluate(
77+
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSPromise> Evaluate(
7878
Isolate* isolate, Handle<Module> module);
7979

8080
// Get the namespace object for [module]. If it doesn't exist yet, it is

‎deps/v8/src/objects/source-text-module.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,8 @@ bool SourceTextModule::MaybeHandleEvaluationException(
886886
return false;
887887
}
888888

889-
// ES#sec-moduleevaluation
890-
MaybeDirectHandle<Object> SourceTextModule::Evaluate(
889+
// https://tc39.es/ecma262/#sec-moduleevaluation
890+
MaybeDirectHandle<JSPromise> SourceTextModule::Evaluate(
891891
Isolate* isolate, Handle<SourceTextModule> module) {
892892
CHECK(module->status() == kLinked || module->status() == kEvaluatingAsync ||
893893
module->status() == kEvaluated);

‎deps/v8/src/objects/source-text-module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class SourceTextModule
192192
AvailableAncestorsSet* exec_list);
193193

194194
// Implementation of spec concrete method Evaluate.
195-
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> Evaluate(
195+
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSPromise> Evaluate(
196196
Isolate* isolate, Handle<SourceTextModule> module);
197197

198198
// Implementation of spec abstract operation InnerModuleEvaluation.

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool SyntheticModule::FinishInstantiate(Isolate* isolate,
105105

106106
// Implements Synthetic Module Record's Evaluate concrete method:
107107
// https://heycam.github.io/webidl/#smr-evaluate
108-
MaybeDirectHandle<Object> SyntheticModule::Evaluate(
108+
MaybeDirectHandle<JSPromise> SyntheticModule::Evaluate(
109109
Isolate* isolate, DirectHandle<SyntheticModule> module) {
110110
module->SetStatus(kEvaluating);
111111

@@ -117,29 +117,16 @@ MaybeDirectHandle<Object> SyntheticModule::Evaluate(
117117
Utils::ToLocal(Cast<Module>(module)))
118118
.ToLocal(&result)) {
119119
module->RecordError(isolate, isolate->exception());
120-
return MaybeDirectHandle<Object>();
120+
return MaybeDirectHandle<JSPromise>();
121121
}
122122

123123
module->SetStatus(kEvaluated);
124124

125125
DirectHandle<Object> result_from_callback = Utils::OpenDirectHandle(*result);
126-
127-
DirectHandle<JSPromise> capability;
128-
if (IsJSPromise(*result_from_callback)) {
129-
capability = Cast<JSPromise>(result_from_callback);
130-
} else {
131-
// The host's evaluation steps should have returned a resolved Promise,
132-
// but as an allowance to hosts that have not yet finished the migration
133-
// to top-level await, create a Promise if the callback result didn't give
134-
// us one.
135-
capability = isolate->factory()->NewJSPromise();
136-
JSPromise::Resolve(capability, isolate->factory()->undefined_value())
137-
.ToHandleChecked();
138-
}
139-
126+
CHECK(IsJSPromise(*result_from_callback));
127+
DirectHandle<JSPromise> capability = Cast<JSPromise>(result_from_callback);
140128
module->set_top_level_capability(*capability);
141-
142-
return result_from_callback;
129+
return capability;
143130
}
144131

145132
} // namespace internal

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SyntheticModule
6060
static V8_WARN_UNUSED_RESULT bool FinishInstantiate(
6161
Isolate* isolate, DirectHandle<SyntheticModule> module);
6262

63-
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> Evaluate(
63+
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSPromise> Evaluate(
6464
Isolate* isolate, DirectHandle<SyntheticModule> module);
6565

6666
TQ_OBJECT_CONSTRUCTORS(SyntheticModule)

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24711,7 +24711,11 @@ static int synthetic_module_callback_count;
2471124711
v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallback(
2471224712
Local<Context> context, Local<Module> module) {
2471324713
synthetic_module_callback_count++;
24714-
return v8::Undefined(reinterpret_cast<v8::Isolate*>(CcTest::isolate()));
24714+
// Synthetic module evaluation steps must return a Promise.
24715+
Local<v8::Promise::Resolver> resolver =
24716+
v8::Promise::Resolver::New(context).ToLocalChecked();
24717+
resolver->Resolve(context, v8::Undefined(CcTest::isolate())).Check();
24718+
return resolver->GetPromise();
2471524719
}
2471624720

2471724721
v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackFail(
@@ -24727,7 +24731,11 @@ v8::MaybeLocal<Value> SyntheticModuleEvaluationStepsCallbackSetExport(
2472724731
Maybe<bool> set_export_result = module->SetSyntheticModuleExport(
2472824732
CcTest::isolate(), v8_str("test_export"), v8_num(42));
2472924733
CHECK(set_export_result.FromJust());
24730-
return v8::Undefined(reinterpret_cast<v8::Isolate*>(CcTest::isolate()));
24734+
// Synthetic module evaluation steps must return a Promise.
24735+
Local<v8::Promise::Resolver> resolver =
24736+
v8::Promise::Resolver::New(context).ToLocalChecked();
24737+
resolver->Resolve(context, v8::Undefined(CcTest::isolate())).Check();
24738+
return resolver->GetPromise();
2473124739
}
2473224740

2473324741
namespace {
@@ -25058,7 +25066,10 @@ TEST(SyntheticModuleEvaluationStepsNoThrow) {
2505825066
context, export_names, SyntheticModuleEvaluationStepsCallback);
2505925067
CHECK_EQ(synthetic_module_callback_count, 0);
2506025068
Local<Value> completion_value = module->Evaluate(context).ToLocalChecked();
25061-
CHECK(completion_value->IsUndefined());
25069+
CHECK(completion_value->IsPromise());
25070+
Local<v8::Promise> promise(Local<v8::Promise>::Cast(completion_value));
25071+
CHECK_EQ(promise->State(), v8::Promise::kFulfilled);
25072+
CHECK(promise->Result()->IsUndefined());
2506225073
CHECK_EQ(synthetic_module_callback_count, 1);
2506325074
CHECK_EQ(module->GetStatus(), Module::kEvaluated);
2506425075
}
@@ -25116,7 +25127,10 @@ TEST(SyntheticModuleEvaluationStepsSetExport) {
2511625127
CHECK(IsUndefined(test_export_cell->value()));
2511725128

2511825129
Local<Value> completion_value = module->Evaluate(context).ToLocalChecked();
25119-
CHECK(completion_value->IsUndefined());
25130+
CHECK(completion_value->IsPromise());
25131+
Local<v8::Promise> promise(Local<v8::Promise>::Cast(completion_value));
25132+
CHECK_EQ(promise->State(), v8::Promise::kFulfilled);
25133+
CHECK(promise->Result()->IsUndefined());
2512025134
CHECK_EQ(42, i::Object::NumberValue(test_export_cell->value()));
2512125135
CHECK_EQ(module->GetStatus(), Module::kEvaluated);
2512225136
}

0 commit comments

Comments
 (0)