Dynamic import() is broken: not-yet-loaded modules reject with "module record has unexpected status: New", and import() from a classic script rejects with undefined

Aperta
#354 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
44/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, javascript, wasm
Ambito
backend, testing

Direzione di ricerca

Start with runtime/script_loader.cpp and trace module_load_hook, especially how static and dynamic loads handle the payload and referrer. Reproduce with the supplied module and classic-script examples, then run the listed test262 dynamic-import tests. Done means dynamic imports resolve for unloaded and evaluating modules, work from classic scripts, and reject missing modules with an error value.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

import() only works if the target module was already linked and evaluated by a static import:

  • From a module, importing a module that is not loaded yet rejects with InternalError: module record has unexpected status: New.
  • A module that imports itself while evaluating (the normal cycle case) rejects with … unexpected status: Evaluating.
  • From a classic script (--legacy-script), the promise rejects with undefined.

Environment

  • StarlingMonkey v0.4.0 release starling.wasm, wasmtime 50.0.1 (wasmtime run -S http --dir . starling.wasm main.js).
  • runtime/script_loader.cpp is unchanged on main (54b3329b).
  • Checked on 2026-11-03. Reference: SpiderMonkey jsshell 147.0.4 (the SpiderMonkey version StarlingMonkey v0.4.0 embeds), js -m.

Reproduction

b.js:

export const y = 2;

main.js:

import("./b.js").then(
  ns => console.log("b ok", ns.y),
  e => console.log("b rejected", e && e.name, e && e.message));
case StarlingMonkey jsshell 147.0.4
main.js as above b rejected InternalError module record has unexpected status: New b ok 2
same with top-level await import("./b.js") rejects with the same InternalError resolves
import { y } from "./b.js" first, then await import("./b.js") works works
self.js: export const v = 1; import("./self.js").then(...) rejects: InternalError: module record has unexpected status: Evaluating self import ok 1
import("./nope.js") (missing file) rejects with ReferenceError: Error loading module … (fine) rejects
main.js run with --legacy-script rejection value is undefined b ok 2

test262: language/module-code/top-level-await/dynamic-import-resolution.js fails with the "unexpected status: New"
error; other language/expressions/dynamic-import/ tests are affected the same way.

Expected

Per ECMA-262 EvaluateImportCall →
HostLoadImportedModule →
FinishLoadingImportedModule →
ContinueDynamicImport, the loaded module is linked and evaluated
and the promise resolves with its namespace. import() is valid in classic scripts (the referrer is the Script
Record, or the Realm when there is no active script). Importing a module that is still evaluating is legal and
resolves after evaluation. A failure must reject with an error value, not undefined.

Likely cause (from reading the code, not verified with a debug build)

module_load_hook in runtime/script_loader.cpp handles static and dynamic loads the same way:

  1. It calls FinishLoadingImportedModule(cx, referrer, module_request, payload, result, false) synchronously from inside
    the load hook, also when payload is a dynamic-import promise. jsshell's ModuleLoader::loadImportedModule treats
    that case differently: it detects the promise payload and finishes the load later (from doDynamicImport, with a
    null referrer). The synchronous re-entry is the likely reason SpiderMonkey reaches evaluation of a module that was
    never linked (New) or is still evaluating.

  2. For a classic-script referrer, JS::GetScriptPrivate(referrer) is undefined (only modules get the info object).
    The code then does

    MOZ_ASSERT(ref_priv.isObject(), "Module referrer must have an object private value");
    RootedObject info(cx, &ref_priv.toObject());
    

    In a release build the assert is compiled out and toObject() is called on undefined. In practice the hook then
    returns false with no pending exception, which rejects the promise with undefined.

Suggested fix (sketch)

  • For a dynamic import (promise payload), defer: queue a job that resolves and compiles the module and calls
    FinishLoadingImportedModule(cx, nullptr, moduleRequest, payload, module, false), or
    FinishLoadingImportedModuleFailedWithPendingException(cx, payload) on error, as jsshell's ModuleLoader does.
  • When the referrer has no object private (classic script, eval, Function), resolve relative to the main script
    path or the working directory instead of dereferencing it.
  • Add integration tests for import() from a module, from a script, of a module that is evaluating, and of a missing file.
Lingua principale
C
Stelle
297
Fork
64
Merge medio
2g 2h
PR unite (30g)
19

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bytecodealliance/StarlingMonkey

Tutte le issue di bytecodealliance/StarlingMonkey

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.