diff --git a/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java b/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java index 597d5f4dca18..79038185b3b5 100644 --- a/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java +++ b/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java @@ -487,6 +487,10 @@ public final AbstractPolyglotImpl getNext() { return next; } + public final AbstractPolyglotImpl getNextOrNull() { + return next; + } + public final void setIO(IOAccessor ioAccess) { Objects.requireNonNull(ioAccess, "IOAccess must be non null."); this.io = ioAccess; diff --git a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java index 181c9add1d9e..c6430f57e788 100644 --- a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java +++ b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java @@ -755,6 +755,8 @@ public abstract Iterator mergeHostGuestFrames(Object polyglotEngine, S public abstract TruffleFile getInternalResource(Object owner, String resourceId) throws IOException; public abstract Collection getResourceIds(String componentId); + + public abstract void setIsolatePolyglot(AbstractPolyglotImpl instance); } public abstract static class LanguageSupport extends Support { diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java index 82bd35560852..ecb4d4f4ca63 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java @@ -1988,7 +1988,12 @@ public Object installGuestToHostCodeCache(Object polyglotContextImpl, Object cac @Override public AutoCloseable createPolyglotThreadScope() { - return PolyglotImpl.findInstance().getRootImpl().createThreadScope(); + AbstractPolyglotImpl impl = PolyglotImpl.findIsolatePolyglot(); + if (impl != null) { + return impl.createThreadScope(); + } else { + return null; + } } @Override @@ -2117,6 +2122,11 @@ public Collection getResourceIds(String componentId) { } throw new IllegalArgumentException(componentId); } + + @Override + public void setIsolatePolyglot(AbstractPolyglotImpl instance) { + PolyglotImpl.setIsolatePolyglot(instance); + } } abstract static class AbstractClassLoaderSupplier implements Supplier { diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java index 35ec6b4e73c6..16c61b7e35bf 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java @@ -122,6 +122,8 @@ public final class PolyglotImpl extends AbstractPolyglotImpl { private PolyglotValueDispatch disconnectedBigIntegerHostValue; private volatile Object defaultFileSystemContext; + private static volatile AbstractPolyglotImpl isolatePolyglot; + /** * Internal method do not use. */ @@ -158,6 +160,16 @@ static PolyglotImpl findInstance() { return (PolyglotImpl) polyglot; } + static AbstractPolyglotImpl findIsolatePolyglot() { + return isolatePolyglot; + } + + static void setIsolatePolyglot(AbstractPolyglotImpl instance) { + assert instance != null; + assert isolatePolyglot == null; + isolatePolyglot = instance; + } + PolyglotEngineImpl getPreinitializedEngine() { return preInitializedEngineRef.get(); } @@ -174,13 +186,21 @@ public void initialize() { @Override public Object initializeModuleToUnnamedAccess(Lookup unnamedLookup, Object unnamedAccess, Object unnamedAPIAccess, Object unnamedIOAccess, Object unnamedManagementAccess) { ModuleToUnnamedBridge bridge = ModuleToUnnamedBridge.create(unnamedLookup, unnamedAccess, unnamedAPIAccess, unnamedIOAccess, unnamedManagementAccess); - setConstructors(Objects.requireNonNull(bridge.getAPIAccess())); - setIO(Objects.requireNonNull(bridge.getIOAccess())); - setMonitoring(Objects.requireNonNull(bridge.getManagementAccess())); - initialize(); + AbstractPolyglotImpl impl = getRootImpl(); + while (impl != null) { + initializeModuleToUnnamedBridge(impl, bridge); + impl = impl.getNextOrNull(); + } return bridge.getModuleAccess(); } + private static void initializeModuleToUnnamedBridge(AbstractPolyglotImpl impl, ModuleToUnnamedBridge bridge) { + impl.setConstructors(Objects.requireNonNull(bridge.getAPIAccess())); + impl.setIO(Objects.requireNonNull(bridge.getIOAccess())); + impl.setMonitoring(Objects.requireNonNull(bridge.getManagementAccess())); + impl.initialize(); + } + @Override public Object buildLimits(long statementLimit, Predicate statementLimitSourceFilter, Consumer onLimit) {