Support a module dynamically importing itself

This commit is contained in:
Karl Seguin
2026-01-23 10:45:41 +08:00
parent 065ca39d60
commit 7522b71c86
3 changed files with 15 additions and 3 deletions

View File

@@ -301,9 +301,17 @@ pub fn module(self: *Context, comptime want_result: bool, local: *const js.Local
// and the module must have been set after we compiled it
lp.assert(entry.module != null, "Context.module with module", .{});
lp.assert(entry.module_promise == null, "Context.module with module_promise", .{});
entry.module_promise = try evaluated.toPromise().persist();
if (entry.module_promise != null) {
// While loading this script, it's possible that it was dynamically
// included (either the module dynamically loaded itself (unlikely) or
// it included a script which dynamically imported it). If it was, then
// the module_promise would already be setup, and we don't need to do
// anything
} else {
// The *much* more likely case where the module we're trying to load
// didn't [directly or indirectly] dynamically load itself.
entry.module_promise = try evaluated.toPromise().persist();
}
return if (comptime want_result) entry.* else {};
}

View File

@@ -58,3 +58,6 @@
testing.expectEqual(true, e.toString().includes("FailedToLoad"), {script_id: 'import-404'});
}
</script>
<!-- this used to crash -->
<script type=module src=modules/self_async.js></script>

View File

@@ -0,0 +1 @@
const c = await import('./self_async.js');