Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to install custom JS module loaders. #893

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sgammon
Copy link

@sgammon sgammon commented Feb 27, 2025

Summary

Adds hooks for Java embedders to inject module implementations for ESM and CJS.

oracle/graal#9177

Changelog

  • Adds JSModuleLoaderFactory interface for ESM loader hook.
  • Adds CommonJSResolverHook interface for CJS resolver hook.
  • Adds js.module-loader-factory=handler setting to enable.
  • Adjusts JSEngine to retain the installed factory and/or resolver.
  • Adjusts JSRealm to use JSEngine to create the module loader.
  • Adjusts NpmCompatibleESModuleLoader to be extensible.
  • Adjusts CommonJSResolution to use the resolver hook if present.

- Adds `JSModuleLoaderFactory` interface for ESM loader hook.
- Adds `CommonJSResolverHook` interface for CJS resolver hook.
- Adds `js.module-loader-factory=handler` setting to enable.
- Adjusts `JSEngine` to retain the installed factory and/or resolver.
- Adjusts `JSRealm` to use `JSEngine` to create the module loader.
- Adjusts `NpmCompatibleESModuleLoader` to be extensible.
- Adjusts `CommonJSResolution` to use the resolver hook if present.

Relates to oracle/graal#9177

Signed-off-by: Sam Gammon <sam@elide.dev>
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Feb 27, 2025
@@ -169,6 +171,21 @@ protected static Object fallback(@SuppressWarnings("unused") Object function, Ob
@TruffleBoundary
private Object requireImpl(String moduleIdentifier, TruffleFile entryPath, JSRealm realm) {
log("required module '", moduleIdentifier, "' from path ", entryPath);
// 1.1 (Non-spec): If a module resolver hook has been installed, give it a chance to resolve the module, but
// only if `handler` mode is enabled for JS module resolution.
if (realm.getContextOptions().getModuleLoaderFactoryMode().equals(HANDLER)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be HANDLER.equals(realm.getContextOptions().getModuleLoaderFactoryMode()) to avoid npe

@woess woess self-requested a review February 27, 2025 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant