Repository navigation
feat: add java: modules, ES modules and the #!support directive to the JavaScript binding - #27
Merged
Merged
Conversation
…e JavaScript binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ajava(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..mjsis ESM,.cjsCommonJS,.jsESM when it has top-levelimport/export. An engine that runs ES modules itself creates the loader withesm = trueand compiles whatmakeRequire.esm.entry/moduleForhand it, one module pernameOfname. Project files, CommonJS files, the kit, runtime modules andjava:all become module sources (a value module exports its keys, ajava:package the names the project's imports ask for).requireof an ES module, a missing file or an extension-less import fail with clear errors. An engine whose ownimport()cannot reach the project passes an importer (setImporter);import(is then rewritten to it.#!supportdirective on the first line, replaced in place so line numbers stay right:esmconverts staticimport/export(andimport(),import.meta) to CommonJS on engines that only run CommonJS, with live getters for exports; on ES module engines it forces ESM.cjsgives an ES module arequireof 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_EXTENSIONSlistsmjs/cjsfor hosts that collect sources.JavaScriptEngineConformancechecks#!support esmimports, unknown directives and plain shebang lines on every engine; newJavaScriptModuleConformance(11 cases) for engines that run ES modules;JavaGuardConformancechecks thatjava:modules give the same guarded classes and fail when Java is off.Testing
:contract:test :tck:test.RhinoConformanceTest30/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..mjs/.cjsmixed,java:named and default imports,#!support cjs, top-level await and dynamic import; a Rhino project with#!support esmand the same imports.