Skip to content

feat: add java: modules, ES modules and the #!support directive to the JavaScript binding - #27

Merged
NenkaLab merged 1 commit into
masterfrom
feat/java-modules-esm
Nov 15, 2026
Merged

NenkaLab merged 1 commit into
masterfrom
feat/java-modules-esm

Conversation

@NenkaLab

Copy link
Copy Markdown
Member

What changed

The shared JavaScript module loader (modules.js, JavaScriptBinding.MODULE_LOADER) now gives every JavaScript engine the same module rules (docs/bindings/javascript.md, "모듈"):

  • java: modules. require('java:java.io') gives the package, require('java:java.io.File') (last name capitalised) the class. Engines pass a java(name, isClass) hook that goes through their own Java lookup, so class access and the file guard apply as before. Null means the project may not use Java, undefined that there is no such class; both give an error naming the specifier.
  • CommonJS and ES modules. Node's rules: .mjs is ESM, .cjs CommonJS, .js ESM when it has top-level import/export. An engine that runs ES modules itself creates the loader with esm = true and compiles what makeRequire.esm.entry/moduleFor hand it, one module per nameOf name. Project files, CommonJS files, the kit, runtime modules and java: all become module sources (a value module exports its keys, a java: package the names the project's imports ask for). require of an ES module, a missing file or an extension-less import fail with clear errors. An engine whose own import() cannot reach the project passes an importer (setImporter); import( is then rewritten to it.
  • #!support directive on the first line, replaced in place so line numbers stay right: esm converts static import/export (and import(), import.meta) to CommonJS on engines that only run CommonJS, with live getters for exports; on ES module engines it forces ESM. cjs gives an ES module a require of its own. Unknown values fail; other #! lines are ignored.
  • makeRequire.script(entry) gives the entry file as a global script (directive line dropped, converted when needed). JavaScriptBinding.MODULE_EXTENSIONS lists mjs/cjs for hosts that collect sources.
  • The tokenizer that finds import/export statements skips strings, comments, template literals and regular expressions. Java objects that throw on unknown members (Rhino) are never taken for converted modules.
  • TCK: JavaScriptEngineConformance checks #!support esm imports, unknown directives and plain shebang lines on every engine; new JavaScriptModuleConformance (11 cases) for engines that run ES modules; JavaGuardConformance checks that java: modules give the same guarded classes and fail when Java is off.

Testing

  • :contract:test :tck:test.
  • The loader on its own with node: 20 checks (conversion, live exports, dynamic import, java hook errors, directives, line numbers, tricky strings/regex/templates, ESM helpers).
  • With this revision: Rhino RhinoConformanceTest 30/30 and new Java module tests; NodeJSPlugin JVM 67/67 (module conformance 11/11) and device 14/14; GraalJS JVM 48/48 (module conformance 11/11) and device 14/14, on an API 36 x86_64 emulator.
  • End to end in the app on the emulator: Node.js and GraalJS projects with ESM entry, .mjs/.cjs mixed, java: named and default imports, #!support cjs, top-level await and dynamic import; a Rhino project with #!support esm and the same imports.

@NenkaLab
NenkaLab merged commit 9ee091b into master Nov 15, 2026
1 check passed
@NenkaLab
NenkaLab deleted the feat/java-modules-esm branch November 15, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant