Skip to content

Commit

Permalink
8258216: Allow Nashorn to operate when not loaded as a JPMS module
Browse files Browse the repository at this point in the history
  • Loading branch information
szegedi authored Dec 23, 2020
1 parent e782364 commit 24f9901
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
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 @@ -116,6 +116,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

0 comments on commit 24f9901

Please sign in to comment.