LibJS: Do not attempt to link modules which have failed to load

Linking a module has assertions about the module's state, namely that
the state is not "new". The state remains "new" if loading the module
has failed. See: https://tc39.es/ecma262/#figure-module-graph-missing

    In any case, this exception causes a loading failure, which results
    in A's [[Status]] remaining new.

So we must propagate that failure, instead of blindly moving on to the
linking steps.
This commit is contained in:
Timothy Flynn 2025-01-20 11:10:07 -05:00 committed by Andreas Kling
parent 7d420bbd3d
commit 049109452e
Notes: github-actions[bot] 2025-01-21 13:59:50 +00:00
2 changed files with 8 additions and 1 deletions

View file

@ -551,7 +551,10 @@ ThrowCompletionOr<void> VM::link_and_eval_module(Badge<Bytecode::Interpreter>, S
ThrowCompletionOr<void> VM::link_and_eval_module(CyclicModule& module)
{
auto filename = module.filename();
module.load_requested_modules(nullptr);
auto& promise_capability = module.load_requested_modules(nullptr);
if (auto const& promise = verify_cast<Promise>(*promise_capability.promise()); promise.state() == Promise::State::Rejected)
return JS::throw_completion(promise.result());
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] Linking module {}", filename);
auto linked_or_error = module.link(*this);

View file

@ -219,6 +219,10 @@ describe("in- and exports", () => {
expect(result.default).toBeInstanceOf(RegExp);
expect(result.default.toString()).toBe(/foo/.toString());
});
test("importing a non-existent file results in a SyntaxError", () => {
expectedModuleToThrowSyntaxError("./i-do-no-exist.mjs", "Cannot find/open module");
});
});
describe("loops", () => {