private void assertIsObjectOfLanguage(Object obj) throws Exception { enterTCK(); // hack to ensure entered PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); TruffleLanguage.Env env = TruffleTCKAccessor.engineAccess().getEnvForInstrument(instr, null, mimeType()); assertTrue(obj.toString(), TruffleTCKAccessor.langAccess().isObjectOfLanguage(env, obj)); }
this.instruments = createInstruments(InstrumentCache.load(SPI.allLoaders())); for (Instrument instrument : instruments.values()) { instInfos.put(instrument.getId(), LANGUAGE.createInstrument(instrument, instrument.getId(), instrument.getName(), instrument.getVersion()));
PolyglotContextImpl(PolyglotEngineImpl engine, PolyglotContextConfig config) { super(engine.impl); this.parent = null; this.engine = engine; this.config = config; this.creator = null; this.creatorArguments = Collections.emptyMap(); this.truffleContext = VMAccessor.LANGUAGE.createTruffleContext(this); this.polyglotBindings = new ConcurrentHashMap<>(); this.weakReference = new ContextWeakReference(this); this.contexts = createContextArray(); if (!config.logLevels.isEmpty()) { VMAccessor.LANGUAGE.configureLoggers(this, config.logLevels); } PolyglotLanguageContext hostContext = getContextInitialized(engine.hostLanguage, null); this.polyglotHostBindings = getAPIAccess().newValue(polyglotBindings, new PolyglotBindingsValue(hostContext)); notifyContextCreated(); PolyglotContextImpl.initializeStaticContext(this); }
synchronized (context) { if (lazy == null) { Env localEnv = LANGUAGE.createEnv(PolyglotLanguageContext.this, lang.spi, envConfig.out, envConfig.err, envConfig.in, PolyglotLanguageContext.this.env = localEnv; PolyglotLanguageContext.this.lazy = localLazy; assert VMAccessor.LANGUAGE.getLanguage(env) != null; LANGUAGE.createEnvContext(localEnv); lang.language.profile.notifyContextCreate(this, localEnv); if (eventsEnabled) {
Thread firstFailingThread = null; for (PolyglotThreadInfo threadInfo : context.getSeenThreads().values()) { if (!LANGUAGE.isThreadAccessAllowed(language.info, threadInfo.thread, singleThreaded)) { firstFailingThread = threadInfo.thread; break; initializeCaches(); try { Env createdEnv = env = LANGUAGE.createEnv(this, language.info, context.out, context.err, context.in, config, getOptionValues(), applicationArguments); LANGUAGE.createEnvContext(createdEnv); language.requireProfile().notifyContextCreate(createdEnv); } finally {
@Override public Object findMetaObjectForLanguage(Object languageVMObject, Object value) { PolyglotLanguageContext languageContext = ((PolyglotLanguageContext) languageVMObject); Env currentLanguage = languageContext.env; assert currentLanguage != null : "current language is initialized"; Env foundLanguage = null; Env hostLanguage = languageContext.context.getHostContext().env; if (VMAccessor.LANGUAGE.isObjectOfLanguage(hostLanguage, value)) { foundLanguage = hostLanguage; } else if (VMAccessor.LANGUAGE.isObjectOfLanguage(currentLanguage, value)) { foundLanguage = currentLanguage; } else { for (PolyglotLanguageContext searchContext : languageContext.context.contexts) { if (searchContext.isInitialized() && searchContext != languageContext) { Env searchEnv = searchContext.env; if (VMAccessor.LANGUAGE.isObjectOfLanguage(searchEnv, value)) { foundLanguage = searchEnv; break; } } } } if (foundLanguage != null) { return VMAccessor.LANGUAGE.findMetaObject(foundLanguage, value); } else { return null; } }
static PolyglotLanguage findObjectLanguage(PolyglotContextImpl context, PolyglotLanguageContext currentlanguageContext, Object value) { PolyglotLanguage foundLanguage = null; final PolyglotLanguageContext hostLanguageContext = context.getHostContext(); // The HostLanguage might not have context created even when JavaObjects exist // Check it separately: if (currentlanguageContext != null && isPrimitive(value)) { return currentlanguageContext.language; } else if (VMAccessor.LANGUAGE.isObjectOfLanguage(hostLanguageContext.env, value)) { foundLanguage = hostLanguageContext.language; } else if (currentlanguageContext != null && VMAccessor.LANGUAGE.isObjectOfLanguage(currentlanguageContext.env, value)) { foundLanguage = currentlanguageContext.language; } else { for (PolyglotLanguageContext searchContext : context.contexts) { if (searchContext.isInitialized() && searchContext != currentlanguageContext) { final Env searchEnv = searchContext.env; if (VMAccessor.LANGUAGE.isObjectOfLanguage(searchEnv, value)) { foundLanguage = searchContext.language; break; } } } } return foundLanguage; }
static PolyglotLanguage findObjectLanguage(PolyglotContextImpl context, Object value) { PolyglotLanguageContext[] contexts = context.contexts; for (PolyglotLanguageContext languageContext : contexts) { PolyglotLanguage language = languageContext.language; if (!language.isInitialized()) { continue; } if (language.cache.singletonLanguage instanceof HostLanguage) { // The HostLanguage might not have context created even when JavaObjects exist // Check it separately: if (((HostLanguage) language.cache.singletonLanguage).isObjectOfLanguage(value)) { return language; } else { continue; } } Env env = languageContext.env; if (env != null && LANGUAGE.isObjectOfLanguage(env, value)) { return language; } } return null; }
void leaveThread(Object prev, PolyglotThread thread) { assert isInitialized(); assert Thread.currentThread() == thread; synchronized (context) { Map<Thread, PolyglotThreadInfo> seenThreads = context.getSeenThreads(); PolyglotThreadInfo info = seenThreads.get(thread); if (info == null) { // already disposed return; } for (PolyglotLanguageContext languageContext : context.contexts) { if (languageContext.isInitialized()) { LANGUAGE.disposeThread(languageContext.env, thread); } } lazy.activePolyglotThreads.remove(thread); context.leave(prev); seenThreads.remove(thread); } VMAccessor.INSTRUMENT.notifyThreadFinished(context.engine, context.truffleContext, thread); }
boolean dispose() { assert Thread.holdsLock(context); Env localEnv = this.env; if (localEnv != null) { if (!lazy.activePolyglotThreads.isEmpty()) { throw new AssertionError("The language did not complete all polyglot threads but should have: " + lazy.activePolyglotThreads); } for (PolyglotThreadInfo threadInfo : context.getSeenThreads().values()) { assert threadInfo.thread != null; if (threadInfo.isPolyglotThread(context)) { // polyglot threads need to be cleaned up by the language continue; } LANGUAGE.disposeThread(localEnv, threadInfo.thread); } LANGUAGE.dispose(localEnv); language.freeInstance(lazy.languageInstance); return true; } return false; }
boolean dispose() { assert Thread.holdsLock(context); Env localEnv = this.env; if (localEnv != null) { if (!activePolyglotThreads.isEmpty()) { throw new AssertionError("The language did not complete all polyglot threads but should have: " + activePolyglotThreads); } for (PolyglotThreadInfo threadInfo : context.getSeenThreads().values()) { assert threadInfo.thread != null; if (threadInfo.isPolyglotThread(context)) { // polyglot threads need to be cleaned up by the language continue; } LANGUAGE.disposeThread(localEnv, threadInfo.thread); } LANGUAGE.dispose(localEnv); env = null; return true; } return false; }
private Map<String, PolyglotInstrument> initializeInstruments(Map<String, InstrumentInfo> infos) { Map<String, PolyglotInstrument> instruments = new LinkedHashMap<>(); List<InstrumentCache> cachedInstruments = InstrumentCache.load(SPI.allLoaders()); for (InstrumentCache instrumentCache : cachedInstruments) { PolyglotInstrument instrumentImpl = new PolyglotInstrument(this, instrumentCache); instrumentImpl.info = LANGUAGE.createInstrument(instrumentImpl, instrumentCache.getId(), instrumentCache.getName(), instrumentCache.getVersion()); Instrument instrument = impl.getAPIAccess().newInstrument(instrumentImpl); instrumentImpl.api = instrument; String id = instrumentImpl.cache.getId(); verifyId(id, instrumentCache.getClassName()); if (instruments.containsKey(id)) { throw failDuplicateId(id, instrumentImpl.cache.getClassName(), instruments.get(id).cache.getClassName()); } instruments.put(id, instrumentImpl); infos.put(id, instrumentImpl.info); } return instruments; }
void leaveThread(Object prev, PolyglotThread thread) { assert Thread.currentThread() == thread; synchronized (context) { Map<Thread, PolyglotThreadInfo> seenThreads = context.getSeenThreads(); PolyglotThreadInfo info = seenThreads.get(thread); if (info == null) { // already disposed return; } for (PolyglotLanguageContext languageContext : context.contexts) { if (languageContext.isInitialized()) { LANGUAGE.disposeThread(languageContext.env, thread); } } activePolyglotThreads.remove(thread); context.leave(prev); seenThreads.remove(thread); } VMAccessor.INSTRUMENT.notifyThreadFinished(context.engine, context.truffleContext, thread); }
private Map<String, PolyglotInstrument> initializeInstruments(Map<String, InstrumentInfo> infos) { Map<String, PolyglotInstrument> instruments = new LinkedHashMap<>(); List<InstrumentCache> cachedInstruments = InstrumentCache.load(VMAccessor.allLoaders()); for (InstrumentCache instrumentCache : cachedInstruments) { PolyglotInstrument instrumentImpl = new PolyglotInstrument(this, instrumentCache); instrumentImpl.info = LANGUAGE.createInstrument(instrumentImpl, instrumentCache.getId(), instrumentCache.getName(), instrumentCache.getVersion()); Instrument instrument = impl.getAPIAccess().newInstrument(instrumentImpl); instrumentImpl.api = instrument; String id = instrumentImpl.cache.getId(); verifyId(id, instrumentCache.getClassName()); if (instruments.containsKey(id)) { throw failDuplicateId(id, instrumentImpl.cache.getClassName(), instruments.get(id).cache.getClassName()); } instruments.put(id, instrumentImpl); infos.put(id, instrumentImpl.info); } return instruments; }
TruffleLanguage.Env getEnv(boolean create) { TruffleLanguage.Env localEnv = env; if ((localEnv == null && create)) { // getEnv is accessed from the instrumentation code so it needs to be // thread-safe. synchronized (this) { localEnv = env; if (localEnv == null && create) { localEnv = LANGUAGE.createEnv(this, shared.getLanguageEnsureInitialized(), engine().out, engine().err, engine().in, getArgumentsForLanguage(), new OptionValuesImpl(null, shared.options), new String[0]); this.env = localEnv; context = LANGUAGE.createEnvContext(localEnv); LANGUAGE.postInitEnv(localEnv); } } } return localEnv; }
void disposeContext() { if (env != null) { synchronized (this) { Env localEnv = this.env; assert localEnv != null; if (localEnv != null) { try { LANGUAGE.dispose(localEnv); } catch (Exception | Error ex) { ex.printStackTrace(); } this.env = null; context = UNSET_CONTEXT; } } } }
void ensureInitialized() { if (!initialized) { synchronized (engine) { if (!initialized) { try { profile = new ContextProfile(this); LoadedLanguage loadedLanguage = cache.loadLanguage(); LANGUAGE.initializeLanguage(info, loadedLanguage.getLanguage(), loadedLanguage.isSingleton()); this.options = LANGUAGE.describeOptions(loadedLanguage.getLanguage(), cache.getId()); } catch (Exception e) { throw new IllegalStateException(String.format("Error initializing language '%s' using class '%s'.", cache.getId(), cache.getClassName()), e); } initialized = true; } } } }
private PolyglotLanguageInstance ensureInitialized(PolyglotLanguageInstance instance) { if (!initialized) { synchronized (engine) { if (!initialized) { try { this.options = LANGUAGE.describeOptions(instance.spi, cache.getId()); } catch (Exception e) { throw new IllegalStateException(String.format("Error initializing language '%s' using class '%s'.", cache.getId(), cache.getClassName()), e); } initialized = true; } } } return instance; }
protected Object evalInContext(Source source, Node node, MaterializedFrame frame) { return languageSupport().evalInContext(source, node, frame); }
protected Object evalInContext(Node node, MaterializedFrame frame, String code) { return languageSupport().evalInContext(code, node, frame); }