@Override public Env createEnv(Object vmObject, TruffleLanguage<?> language, OutputStream stdOut, OutputStream stdErr, InputStream stdIn, Map<String, Object> config, OptionValues options, String[] applicationArguments, FileSystem fileSystem) { Env env = new Env(vmObject, language, stdOut, stdErr, stdIn, config, options, applicationArguments, fileSystem); LinkedHashSet<Object> collectedServices = new LinkedHashSet<>(); LanguageInfo info = language.languageInfo; AccessAPI.instrumentAccess().collectEnvServices(collectedServices, API.nodes().getEngineObject(info), language); env.services = new ArrayList<>(collectedServices); return env; }
/** * Internal method do not use. * * @since 0.27 */ @Override public Engine buildEngine(OutputStream out, OutputStream err, InputStream in, Map<String, String> arguments, long timeout, TimeUnit timeoutUnit, boolean sandbox, long maximumAllowedAllocationBytes, boolean useSystemProperties, boolean boundEngine) { ensureInitialized(); OutputStream resolvedOut = out == null ? System.out : out; OutputStream resolvedErr = err == null ? System.err : err; InputStream resolvedIn = in == null ? System.in : in; DispatchOutputStream dispatchOut = INSTRUMENT.createDispatchOutput(resolvedOut); DispatchOutputStream dispatchErr = INSTRUMENT.createDispatchOutput(resolvedErr); ClassLoader contextClassLoader = TruffleOptions.AOT ? null : Thread.currentThread().getContextClassLoader(); PolyglotEngineImpl impl = preInitializedEngineRef.getAndSet(null); if (impl != null) { if (!impl.patch(dispatchOut, dispatchErr, resolvedIn, arguments, timeout, timeoutUnit, sandbox, useSystemProperties, contextClassLoader, boundEngine)) { impl.ensureClosed(false, true); impl = null; } } if (impl == null) { impl = new PolyglotEngineImpl(this, dispatchOut, dispatchErr, resolvedIn, arguments, timeout, timeoutUnit, sandbox, useSystemProperties, contextClassLoader, boundEngine); } Engine engine = getAPIAccess().newEngine(impl); impl.api = engine; return engine; }
void ensureCreated() { if (!created) { synchronized (engine) { if (!created) { if (!initialized) { ensureInitialized(); } try { INSTRUMENT.createInstrument(engine.instrumentationHandler, this, cache.services(), getOptionValues()); } catch (Exception e) { throw new IllegalStateException(String.format("Error initializing instrument '%s' using class '%s'.", cache.getId(), cache.getClassName()), e); } created = true; } } } }
Predicate<Source> sourceFilter, Predicate<String> rootFilter, boolean collectInputValues, boolean collectReturnValues, boolean collectExceptions) { PolyglotEngineImpl engine = (PolyglotEngineImpl) engineImpl.getAPIAccess().getImpl(engineAPI); Instrumenter instrumenter = (Instrumenter) VMAccessor.INSTRUMENT.getEngineInstrumenter(engine.instrumentationHandler);
private PolyglotEngineImpl(PolyglotImpl impl, DispatchOutputStream out, DispatchOutputStream err, InputStream in, Map<String, String> options, long timeout, TimeUnit timeoutUnit, boolean sandbox, boolean useSystemProperties, ClassLoader contextClassLoader, boolean boundEngine, boolean preInitialization) { super(impl); this.instrumentationHandler = INSTRUMENT.createInstrumentationHandler(this, out, err, in); this.impl = impl; this.out = out;
private PolyglotEngineImpl(PolyglotImpl impl, DispatchOutputStream out, DispatchOutputStream err, InputStream in, Map<String, String> options, boolean useSystemProperties, ClassLoader contextClassLoader, boolean boundEngine, boolean preInitialization, MessageTransport messageInterceptor, Handler logHandler) { super(impl); this.instrumentationHandler = INSTRUMENT.createInstrumentationHandler(this, out, err, in, messageInterceptor); this.impl = impl; this.out = out;
if (configOut == null || configOut == INSTRUMENT.getOut(this.out)) { useOut = this.out; } else { useOut = INSTRUMENT.createDelegatingOutput(configOut, this.out); if (configErr == null || configErr == INSTRUMENT.getOut(this.err)) { useErr = this.err; } else { useErr = INSTRUMENT.createDelegatingOutput(configErr, this.err);
PolyglotRuntime(DispatchOutputStream out, DispatchOutputStream err, InputStream in, boolean automaticDispose) { PolyglotEngine.ensureInitialized(); this.engineProfile = PolyglotEngine.GLOBAL_PROFILE; this.instrumentationHandler = INSTRUMENT.createInstrumentationHandler(this, out, err, in);
realOut = realRuntime.out; } else { realOut = INSTRUMENT.createDispatchOutput(out); engine().attachOutputConsumer(realOut, realRuntime.out); realErr = realRuntime.err; } else { realErr = INSTRUMENT.createDispatchOutput(err); engine().attachOutputConsumer(realErr, realRuntime.err);
Handler logHandler = PolyglotLogHandler.asHandler(logHandlerOrStream); DispatchOutputStream dispatchOut = INSTRUMENT.createDispatchOutput(resolvedOut); DispatchOutputStream dispatchErr = INSTRUMENT.createDispatchOutput(resolvedErr); ClassLoader contextClassLoader = TruffleOptions.AOT ? null : Thread.currentThread().getContextClassLoader();
/** * Returns an additional service provided by this instrument, specified by type. * <p> * Here is an example for locating a hypothetical <code>DebuggerController</code>: * * {@codesnippet DebuggerExampleTest} * * @param <T> the type of the service * @param type class of the service that is being requested * @return instance of requested type, <code>null</code> if no such service is available * @since 0.9 */ public <T> T lookup(Class<T> type) { if (PolyglotRuntime.this.disposed) { return null; } if (!isEnabled() && cache.supportsService(type)) { setEnabled(true); } return INSTRUMENT.getInstrumentationHandlerService(PolyglotRuntime.this.instrumentationHandler, this, type); }
private void patchInstance(OutputStream newOut, OutputStream newErr, InputStream newIn, boolean newHostAccessAllowed, boolean newCreateThreadAllowed, Predicate<String> newClassFilter, Map<String, String[]> newApplicationArguments, Set<String> newAllowedPublicLanguages) { this.hostAccessAllowed = newHostAccessAllowed; this.createThreadAllowed = newCreateThreadAllowed; this.applicationArguments = newApplicationArguments; this.classFilter = newClassFilter; if (newOut == null || newOut == INSTRUMENT.getOut(engine.out)) { this.out = engine.out; } else { this.out = INSTRUMENT.createDelegatingOutput(newOut, engine.out); } if (newErr == null || newErr == INSTRUMENT.getOut(engine.err)) { this.err = engine.err; } else { this.err = INSTRUMENT.createDelegatingOutput(newErr, engine.err); } this.in = newIn == null ? engine.in : newIn; this.allowedPublicLanguages = newAllowedPublicLanguages; }
/** * Pre-initializes a polyglot engine instance. */ @Override public void preInitializeEngine() { final Handler logHandler = PolyglotLogHandler.createStreamHandler(System.err, false, true); try { final PolyglotEngineImpl preInitializedEngine = PolyglotEngineImpl.preInitialize( this, INSTRUMENT.createDispatchOutput(System.out), INSTRUMENT.createDispatchOutput(System.err), System.in, TruffleOptions.AOT ? null : Thread.currentThread().getContextClassLoader(), logHandler); preInitializedEngineRef.set(preInitializedEngine); } finally { logHandler.flush(); } }
void setEnabledImpl(final boolean enabled, boolean cleanup) { synchronized (instrumentLock) { if (this.enabled != enabled) { if (enabled) { if (PolyglotRuntime.this.disposed) { return; } INSTRUMENT.initializeInstrument(PolyglotRuntime.this.instrumentationHandler, this, getCache().getInstrumentationClass()); OptionDescriptors descriptors = INSTRUMENT.describeOptions(getRuntime().instrumentationHandler, this, this.getId()); OptionValuesImpl values = new OptionValuesImpl(null, descriptors); INSTRUMENT.createInstrument(PolyglotRuntime.this.instrumentationHandler, this, cache.services(), values); } else { INSTRUMENT.disposeInstrument(PolyglotRuntime.this.instrumentationHandler, this, cleanup); } this.enabled = enabled; } } }
<T> T lookup(Class<T> serviceClass, boolean wrapExceptions) { engine.checkState(); if (cache.supportsService(serviceClass)) { try { ensureCreated(); } catch (Throwable t) { if (wrapExceptions) { throw PolyglotImpl.wrapGuestException(engine, t); } else { throw t; } } return INSTRUMENT.getInstrumentationHandlerService(engine.instrumentationHandler, this, serviceClass); } else { return null; } }
void ensureInitialized() { if (!initialized) { synchronized (engine) { if (!initialized) { try { Class<?> loadedInstrument = cache.getInstrumentationClass(); INSTRUMENT.initializeInstrument(engine.instrumentationHandler, this, loadedInstrument); this.options = INSTRUMENT.describeOptions(engine.instrumentationHandler, this, cache.getId()); } catch (Exception e) { throw new IllegalStateException(String.format("Error initializing instrument '%s' using class '%s'.", cache.getId(), cache.getClassName()), e); } initialized = true; } } } }
void ensureInitialized() { if (!initialized) { synchronized (instrumentLock) { if (!initialized) { try { Class<?> loadedInstrument = cache.getInstrumentationClass(); INSTRUMENT.initializeInstrument(engine.instrumentationHandler, this, loadedInstrument); this.options = INSTRUMENT.describeOptions(engine.instrumentationHandler, this, cache.getId()); } catch (Exception e) { throw new IllegalStateException(String.format("Error initializing instrument '%s' using class '%s'.", cache.getId(), cache.getClassName()), e); } initialized = true; } } } }
/** * Pre-initializes a polyglot engine instance. * * @since 0.31 */ @Override public void preInitializeEngine() { ensureInitialized(); final PolyglotEngineImpl preInitializedEngine = PolyglotEngineImpl.preInitialize( this, INSTRUMENT.createDispatchOutput(System.out), INSTRUMENT.createDispatchOutput(System.err), System.in, TruffleOptions.AOT ? null : Thread.currentThread().getContextClassLoader()); preInitializedEngineRef.set(preInitializedEngine); }
/** * Creates new instance of a runtime. Uses data stored in this builder to configure it. Once * the instance is obtained, pass it to * {@link PolyglotEngine.Builder#runtime(com.oracle.truffle.api.vm.PolyglotRuntime)} method. * * @return new instances of the {@link PolyglotRuntime} * @since 0.25 */ public PolyglotRuntime build() { DispatchOutputStream realOut = INSTRUMENT.createDispatchOutput(out == null ? System.out : out); DispatchOutputStream realErr = INSTRUMENT.createDispatchOutput(err == null ? System.err : err); InputStream realIn = in == null ? System.in : in; return new PolyglotRuntime(realOut, realErr, realIn, false); }
CancelHandler() { this.instrumenter = (Instrumenter) INSTRUMENT.getEngineInstrumenter(instrumentationHandler); }