Skip to content

Commit b94f63b

Browse files
joyeecheungtargos
authored andcommitted
module: handle instantiated async module jobs in require(esm)
When require(esm) encounters a cached module job that is instantiated but not yet evaluated, run the evaluation. This catches an edge case previously missed in #57187. PR-URL: #58067 Fixes: #58061 Reviewed-By: Jacob Smith <[email protected]>
1 parent 868e72e commit b94f63b

File tree

9 files changed

+59
-11
lines changed

9 files changed

+59
-11
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class ModuleLoader {
386386
throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);
387387
}
388388
const status = job.module.getStatus();
389-
debug('Module status', filename, status);
389+
debug('Module status', job, status);
390390
if (status === kEvaluated) {
391391
return { wrap: job.module, namespace: job.module.getNamespaceSync(filename, parentFilename) };
392392
} else if (status === kInstantiated) {

lib/internal/modules/esm/module_job.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
2222
debug = fn;
2323
});
2424

25-
const { ModuleWrap, kInstantiated, kEvaluationPhase } = internalBinding('module_wrap');
25+
const {
26+
ModuleWrap,
27+
kErrored,
28+
kEvaluated,
29+
kEvaluationPhase,
30+
kInstantiated,
31+
kUninstantiated,
32+
} = internalBinding('module_wrap');
2633
const {
2734
privateSymbols: {
2835
entry_point_module_private_symbol,
@@ -277,17 +284,34 @@ class ModuleJob extends ModuleJobBase {
277284
runSync(parent) {
278285
assert(this.phase === kEvaluationPhase);
279286
assert(this.module instanceof ModuleWrap);
280-
if (this.instantiated !== undefined) {
281-
return { __proto__: null, module: this.module };
287+
let status = this.module.getStatus();
288+
289+
debug('ModuleJob.runSync', this.module);
290+
// FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking
291+
// fully synchronous instead.
292+
if (status === kUninstantiated) {
293+
this.module.async = this.module.instantiateSync();
294+
status = this.module.getStatus();
282295
}
296+
if (status === kInstantiated || status === kErrored) {
297+
const filename = urlToFilename(this.url);
298+
const parentFilename = urlToFilename(parent?.filename);
299+
this.module.async ??= this.module.isGraphAsync();
283300

284-
this.module.instantiate();
285-
this.instantiated = PromiseResolve();
286-
setHasStartedUserESMExecution();
287-
const filename = urlToFilename(this.url);
288-
const parentFilename = urlToFilename(parent?.filename);
289-
const namespace = this.module.evaluateSync(filename, parentFilename);
290-
return { __proto__: null, module: this.module, namespace };
301+
if (this.module.async && !getOptionValue('--experimental-print-required-tla')) {
302+
throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);
303+
}
304+
if (status === kInstantiated) {
305+
setHasStartedUserESMExecution();
306+
const namespace = this.module.evaluateSync(filename, parentFilename);
307+
return { __proto__: null, module: this.module, namespace };
308+
}
309+
throw this.module.getError();
310+
311+
} else if (status === kEvaluated) {
312+
return { __proto__: null, module: this.module, namespace: this.module.getNamespaceSync() };
313+
}
314+
assert.fail(`Unexpected module status ${status}.`);
291315
}
292316

293317
async run(isEntryPoint = false) {

src/debug_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
5252
V(NGTCP2_DEBUG) \
5353
V(SEA) \
5454
V(WASI) \
55+
V(MODULE) \
5556
V(MKSNAPSHOT) \
5657
V(SNAPSHOT_SERDES) \
5758
V(PERMISSION_MODEL) \

src/module_wrap.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,16 @@ void ModuleWrap::GetStatus(const FunctionCallbackInfo<Value>& args) {
860860
args.GetReturnValue().Set(module->GetStatus());
861861
}
862862

863+
void ModuleWrap::IsGraphAsync(const FunctionCallbackInfo<Value>& args) {
864+
Isolate* isolate = args.GetIsolate();
865+
ModuleWrap* obj;
866+
ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
867+
868+
Local<Module> module = obj->module_.Get(isolate);
869+
870+
args.GetReturnValue().Set(module->IsGraphAsync());
871+
}
872+
863873
void ModuleWrap::GetError(const FunctionCallbackInfo<Value>& args) {
864874
Isolate* isolate = args.GetIsolate();
865875
ModuleWrap* obj;
@@ -1282,6 +1292,7 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
12821292
isolate, tpl, "createCachedData", CreateCachedData);
12831293
SetProtoMethodNoSideEffect(isolate, tpl, "getNamespace", GetNamespace);
12841294
SetProtoMethodNoSideEffect(isolate, tpl, "getStatus", GetStatus);
1295+
SetProtoMethodNoSideEffect(isolate, tpl, "isGraphAsync", IsGraphAsync);
12851296
SetProtoMethodNoSideEffect(isolate, tpl, "getError", GetError);
12861297
SetConstructorFunction(isolate, target, "ModuleWrap", tpl);
12871298
isolate_data->set_module_wrap_constructor_template(tpl);
@@ -1343,6 +1354,7 @@ void ModuleWrap::RegisterExternalReferences(
13431354
registry->Register(GetNamespace);
13441355
registry->Register(GetStatus);
13451356
registry->Register(GetError);
1357+
registry->Register(IsGraphAsync);
13461358

13471359
registry->Register(CreateRequiredModuleFacade);
13481360

src/module_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class ModuleWrap : public BaseObject {
121121
static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
122122
static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
123123
static void GetNamespace(const v8::FunctionCallbackInfo<v8::Value>& args);
124+
static void IsGraphAsync(const v8::FunctionCallbackInfo<v8::Value>& args);
124125
static void GetStatus(const v8::FunctionCallbackInfo<v8::Value>& args);
125126
static void GetError(const v8::FunctionCallbackInfo<v8::Value>& args);
126127

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import '../common/index.mjs';
2+
import assert from 'node:assert';
3+
import { b, c } from '../fixtures/es-modules/require-module-instantiated/a.mjs';
4+
assert.strictEqual(b, c);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as b } from './b.cjs';
2+
export { default as c } from './c.mjs';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./c.mjs');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const foo = 1;
2+
export default foo;
3+
export { foo as 'module.exports' };

0 commit comments

Comments
 (0)