@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; }
/** * Looks additional service up. An environment for a particular {@link TruffleLanguage * language} may also be associated with additional services. One can request * implementations of such services by calling this method with the type identifying the * requested service and its API. * * Services that can be obtained via this method include * {@link com.oracle.truffle.api.instrumentation.Instrumenter} and others. * * @param <T> type of requested service * @param type class of requested service * @return instance of T or <code>null</code> if there is no such service available * @since 0.12 */ @TruffleBoundary public <T> T lookup(Class<T> type) { checkDisposed(); for (Object obj : services) { if (type.isInstance(obj)) { return type.cast(obj); } } return null; }
@Override public void uncaughtException(Thread t, Throwable e) { Env currentEnv = env; if (currentEnv != null) { try { e.printStackTrace(new PrintStream(currentEnv.err())); } catch (Throwable exc) { // Still show the original error if printing on Env.err() fails for some // reason e.printStackTrace(); } } else { e.printStackTrace(); } } }
/** * Creates a new thread that has access to the current language context. A thread is * {@link TruffleLanguage#initializeThread(Object, Thread) initialized} when it is * {@link Thread#start() started} and {@link TruffleLanguage#disposeThread(Object, Thread) * disposed} as soon as the thread finished the execution. In order to start threads the * language needs to {@link TruffleLanguage#isThreadAccessAllowed(Thread, boolean) allow} * access from multiple threads at the same time. * <p> * It is recommended to set an * {@link Thread#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) * uncaught exception handler} for the created thread. For example the thread can throw an * uncaught exception if one of the initialized language contexts don't support execution on * this thread. * <p> * The language that created and started the thread is responsible to complete all running * or waiting threads when the context is {@link TruffleLanguage#disposeContext(Object) * disposed}. * * @param runnable the runnable to run on this thread. * @throws IllegalStateException if thread creation is not {@link #isCreateThreadAllowed() * allowed}. * @since 0.28 */ @TruffleBoundary public Thread createThread(Runnable runnable) { return createThread(runnable, null); }
/** * Looks additional service up. An environment for a particular {@link TruffleLanguage * language} may also be associated with additional services. One can request * implementations of such services by calling this method with the type identifying the * requested service and its API. * * Services that can be obtained via this method include * {@link com.oracle.truffle.api.instrumentation.Instrumenter} and others. * * @param <T> type of requested service * @param type class of requested service * @return instance of T or <code>null</code> if there is no such service available * @since 0.12 */ @TruffleBoundary public <T> T lookup(Class<T> type) { checkDisposed(); for (Object obj : services) { if (type.isInstance(obj)) { return type.cast(obj); } } return null; }
/** * Evaluates source of (potentially different) language. The {@link Source#getMimeType() * MIME type} is used to identify the {@link TruffleLanguage} to use to perform the * {@link #parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest)} . The names of * arguments are parameters for the resulting {#link CallTarget} that allow the * <code>source</code> to reference the actual parameters passed to * {@link CallTarget#call(java.lang.Object...)}. * * @param source the source to evaluate * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments * that can be referenced from the source * @return the call target representing the parsed result * @since 0.8 or earlier */ @TruffleBoundary public CallTarget parse(Source source, String... argumentNames) { CompilerAsserts.neverPartOfCompilation(); checkDisposed(); return AccessAPI.engineAccess().parseForLanguage(vmObject, source, argumentNames); }
/** * Evaluates source of (potentially different) language. The {@link Source#getMimeType() * MIME type} is used to identify the {@link TruffleLanguage} to use to perform the * {@link #parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest)} . The names of * arguments are parameters for the resulting {#link CallTarget} that allow the * <code>source</code> to reference the actual parameters passed to * {@link CallTarget#call(java.lang.Object...)}. * * @param source the source to evaluate * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments * that can be referenced from the source * @return the call target representing the parsed result * @since 0.8 or earlier */ @TruffleBoundary public CallTarget parse(Source source, String... argumentNames) { CompilerAsserts.neverPartOfCompilation(); checkDisposed(); return AccessAPI.engineAccess().getEnvForLanguage(vmObject, source.getLanguage(), source.getMimeType()).spi.parse(source, null, null, argumentNames); }
/** * Returns a {@link TruffleFile} for given {@link URI}. * * @param uri the {@link URI} to create {@link TruffleFile} for * @return {@link TruffleFile} * @since 1.0 */ @TruffleBoundary public TruffleFile getTruffleFile(URI uri) { checkDisposed(); try { return new TruffleFile(fileSystem, fileSystem.parsePath(uri)); } catch (UnsupportedOperationException e) { throw new FileSystemNotFoundException("FileSystem for: " + uri.getScheme() + " scheme is not supported."); } }
/** * Allows it to be determined if this {@link org.graalvm.polyglot.Context} can execute code * written in a language with a given MIME type. * * @see Source#getMimeType() * @see #parse(Source, String...) * * @return a boolean that indicates if the MIME type is supported * @since 0.11 */ @TruffleBoundary public boolean isMimeTypeSupported(String mimeType) { checkDisposed(); return AccessAPI.engineAccess().isMimeTypeSupported(vmObject, mimeType); }
/** * Allows it to be determined if this {@link org.graalvm.polyglot.Context} can execute code * written in a language with a given MIME type. * * @see Source#getMimeType() * @see #parse(Source, String...) * * @return a boolean that indicates if the MIME type is supported * @since 0.11 */ @TruffleBoundary public boolean isMimeTypeSupported(String mimeType) { checkDisposed(); return AccessAPI.engineAccess().isMimeTypeSupported(vmObject, mimeType); }
@Override public void uncaughtException(Thread t, Throwable e) { Env currentEnv = env; if (currentEnv != null) { try { e.printStackTrace(new PrintStream(currentEnv.err())); } catch (Throwable exc) { // Still show the original error if printing on Env.err() fails for some reason e.printStackTrace(); } } else { e.printStackTrace(); } } }
/** * Standard error writer provided by * {@link org.graalvm.polyglot.Context.Builder#err(OutputStream)} this language is being * executed in. * * @return writer, never <code>null</code> * @since 0.8 or earlier */ @TruffleBoundary public OutputStream err() { checkDisposed(); return err; }
/** * Creates a new thread that has access to the given context. See * {@link #createThread(Runnable, TruffleContext, ThreadGroup, long)} for a detailed * description of the parameters. The <code>stackSize</code> set to 0. * * @see #getContext() * @see #newContextBuilder() * @since 0.28 */ @TruffleBoundary public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context, ThreadGroup group) { return createThread(runnable, context, group, 0); }
@Override public Iterable<Scope> findTopScopes(Env env) { return env.findTopScopes(); }
@Override public SourceSection findSourceLocation(Env env, Object obj) { return env.findSourceLocation(obj); }
/** * Configuration arguments passed from an outer language context to an inner language * context. Inner language contexts can be created using {@link #newContextBuilder()}. * * @see TruffleContext to create inner contexts. * @see TruffleContext.Builder#config(String, Object) to pass configuration objects to the * inner context. * @since 0.11 */ @TruffleBoundary public Map<String, Object> getConfig() { checkDisposed(); return config; }
/** * Configuration arguments passed from an outer language context to an inner language * context. Inner language contexts can be created using {@link #newContextBuilder()}. * * @see TruffleContext to create inner contexts. * @see TruffleContext.Builder#config(String, Object) to pass configuration objects to the * inner context. * @since 0.11 */ @TruffleBoundary public Map<String, Object> getConfig() { checkDisposed(); return config; }
/** * Creates a new thread that has access to the given context. See * {@link #createThread(Runnable, TruffleContext, ThreadGroup, long)} for a detailed * description of the parameters. The <code>group</code> is null and <code>stackSize</code> * set to 0. * * @see #getContext() * @see #newContextBuilder() * @since 0.28 */ @TruffleBoundary public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context) { return createThread(runnable, context, null, 0); }
@Override protected void initializeContext(Env context) throws Exception { context.exportSymbol("env", JavaInterop.asTruffleValue(context)); }
/** * Standard output writer provided by * {@link org.graalvm.polyglot.Context.Builder#out(OutputStream)} this language is being * executed in. * * @return writer, never <code>null</code> * @since 0.8 or earlier */ @TruffleBoundary public OutputStream out() { checkDisposed(); return out; }