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

Abierto
#354 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
44/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c, javascript, wasm
Área
backend, testing

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
C
Estrellas
297
Forks
64
Merge medio
2 d 2 h
PR fusionados (30 d)
19

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de bytecodealliance/StarlingMonkey

Todos los issues de bytecodealliance/StarlingMonkey

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.