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

8258216: Allow Nashorn to operate when not loaded as a JPMS module #9

Merged
merged 3 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions make/nashorn/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@
<attribute name="Implementation-Version" value="${nashorn.version}"/>
</section>
</manifest>
<service type="javax.script.ScriptEngineFactory" provider="org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory"/>
<service type="jdk.dynalink.linker.GuardingDynamicLinkerExporter" provider="org.openjdk.nashorn.api.linker.NashornLinkerExporter"/>
</jar>
</target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ final void addModuleExport(final Module to) {
}
}

static boolean isInNamedModule() {
// True if Nashorn is loaded as a JPMS module (typically, added to
// --module-path); false if it is loaded through classpath into an
// unnamed module. There are modular execution aspects that need to
// be taken care of when Nashorn is used as a JPMS module.
return NASHORN_MODULE.isNamed();
}

protected static void checkPackageAccess(final String name) {
final int i = name.lastIndexOf('.');
if (i != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,28 @@ final class ScriptLoader extends NashornLoader {
super(context.getStructLoader());
this.context = context;

// new scripts module, it's specific exports and read-edges
scriptModule = createModule("org.openjdk.nashorn.scripts");

// specific exports from nashorn to new scripts module
NASHORN_MODULE.addExports(OBJECTS_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_ARRAYS_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_LINKER_PKG, scriptModule);
NASHORN_MODULE.addExports(SCRIPTS_PKG, scriptModule);

// nashorn needs to read scripts module methods,fields
NASHORN_MODULE.addReads(scriptModule);
if (isInNamedModule()) {
// new scripts module, it's specific exports and read-edges
scriptModule = createModule();

// specific exports from nashorn to new scripts module
NASHORN_MODULE.addExports(OBJECTS_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_ARRAYS_PKG, scriptModule);
NASHORN_MODULE.addExports(RUNTIME_LINKER_PKG, scriptModule);
NASHORN_MODULE.addExports(SCRIPTS_PKG, scriptModule);

// nashorn needs to read scripts module methods,fields
NASHORN_MODULE.addReads(scriptModule);
} else {
scriptModule = null;
}
}

private Module createModule(final String moduleName) {
private Module createModule() {
final Module structMod = context.getStructLoader().getModule();
final ModuleDescriptor.Builder builder =
ModuleDescriptor.newModule(moduleName, Set.of(Modifier.SYNTHETIC))
ModuleDescriptor.newModule("org.openjdk.nashorn.scripts", Set.of(Modifier.SYNTHETIC))
.requires("java.logging")
.requires(NASHORN_MODULE.getName())
.requires(structMod.getName())
Expand All @@ -95,7 +99,7 @@ private Module createModule(final String moduleName) {
protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
checkPackageAccess(name);
final Class<?> cl = super.loadClass(name, resolve);
if (!structureAccessAdded) {
if (!structureAccessAdded && isInNamedModule()) {
final StructureLoader structLoader = context.getStructLoader();
if (cl.getClassLoader() == structLoader) {
structureAccessAdded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,24 @@ final class StructureLoader extends NashornLoader {
StructureLoader(final ClassLoader parent) {
super(parent);

// new structures module, it's exports, read edges
structuresModule = createModule("org.openjdk.nashorn.structures");

// specific exports from nashorn to the structures module
NASHORN_MODULE.addExports(SCRIPTS_PKG, structuresModule);
NASHORN_MODULE.addExports(RUNTIME_PKG, structuresModule);

// nashorn has to read fields from classes of the new module
NASHORN_MODULE.addReads(structuresModule);
if (isInNamedModule()) {
// new structures module, it's exports, read edges
structuresModule = createModule();

// specific exports from nashorn to the structures module
NASHORN_MODULE.addExports(SCRIPTS_PKG, structuresModule);
NASHORN_MODULE.addExports(RUNTIME_PKG, structuresModule);

// nashorn has to read fields from classes of the new module
NASHORN_MODULE.addReads(structuresModule);
} else {
structuresModule = null;
}
}

private Module createModule(final String moduleName) {
private Module createModule() {
final ModuleDescriptor descriptor =
ModuleDescriptor.newModule(moduleName, Set.of(Modifier.SYNTHETIC))
ModuleDescriptor.newModule("org.openjdk.nashorn.structures", Set.of(Modifier.SYNTHETIC))
.requires(NASHORN_MODULE.getName())
.packages(Set.of(SCRIPTS_PKG))
.build();
Expand Down