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

オープン
#354 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
44/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, javascript, wasm
領域
backend, testing

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
C
スター
297
フォーク
64
平均マージ
2日 2時間
マージ済み PR(30日)
19

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bytecodealliance/StarlingMonkey のほかの issue

bytecodealliance/StarlingMonkey の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。