Merge pull request #1403 from lightpanda-io/module_async_import_self

Support a module dynamically importing itself
This commit is contained in:
Karl Seguin
2026-01-23 18:59:26 +08:00
committed by GitHub
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 // and the module must have been set after we compiled it
lp.assert(entry.module != null, "Context.module with module", .{}); lp.assert(entry.module != null, "Context.module with module", .{});
lp.assert(entry.module_promise == null, "Context.module with module_promise", .{}); 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(); entry.module_promise = try evaluated.toPromise().persist();
}
return if (comptime want_result) entry.* else {}; return if (comptime want_result) entry.* else {};
} }

View File

@@ -58,3 +58,6 @@
testing.expectEqual(true, e.toString().includes("FailedToLoad"), {script_id: 'import-404'}); testing.expectEqual(true, e.toString().includes("FailedToLoad"), {script_id: 'import-404'});
} }
</script> </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');