com.oracle.truffle.api.metadata.ScopeProvider.AbstractScope ascope; if (scopeProvider != null) { TruffleLanguage.Env env = ScopeAccessor.engine().getEnvForInstrument(languageInfo); Object context = ScopeAccessor.langs().getContext(env); ascope = scopeProvider.findScope(context, node, frame);
/** * Unwraps a host exception thrown by a Java method invocation. * * Host exceptions may be thrown by {@linkplain Message messages} sent to Java objects that * involve the invocation of a Java method or constructor ({@code EXECUTE}, {@code INVOKE}, * {@code NEW}). Host exceptions can be identified using {@link #isHostException(Throwable)}. * * @param exception the host exception to unwrap * @return the original Java exception * @throws IllegalArgumentException if the {@code exception} is not a host exception * @see #isHostException(Throwable) * @since 0.31 */ public static Throwable asHostException(Throwable exception) { EngineSupport engine = ACCESSOR.engine(); if (engine != null && engine.isHostException(exception)) { return engine.asHostException(exception); } throw new IllegalArgumentException("Not a HostException"); }
<T extends OutputStream> EventBinding<T> addOutputBinding(EventBinding<T> binding, boolean errorOutput) { if (TRACE) { String kind = (errorOutput) ? "error" : "standard"; trace("BEGIN: Adding " + kind + " output binding %s%n", binding.getElement()); } if (errorOutput) { this.outputErrBindings.add(binding); AccessorInstrumentHandler.engineAccess().attachOutputConsumer(this.err, binding.getElement()); } else { this.outputStdBindings.add(binding); AccessorInstrumentHandler.engineAccess().attachOutputConsumer(this.out, binding.getElement()); } if (TRACE) { String kind = (errorOutput) ? "error" : "standard"; trace("END: Added " + kind + " output binding %s%n", binding.getElement()); } return binding; }
return AccessAPI.engineAccess().createDefaultTopScope(global);
return AccessAPI.engineAccess().createDefaultTopScope(global);
throw new IllegalArgumentException("The root node " + rootNode + " does not have a language associated."); final TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo); Iterable<Scope> langScopes = AccessorInstrumentHandler.langAccess().findLocalScopes(env, node, frame); assert langScopes != null : languageInfo.getId();
if (elm instanceof OutputStream) { if (outputErrBindings.contains(binding)) { AccessorInstrumentHandler.engineAccess().detachOutputConsumer(err, (OutputStream) elm); } else if (outputStdBindings.contains(binding)) { AccessorInstrumentHandler.engineAccess().detachOutputConsumer(out, (OutputStream) elm);
throw new IllegalArgumentException("The root node " + rootNode + " does not have a language associated."); final TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo); Iterable<Scope> langScopes = AccessorInstrumentHandler.langAccess().findLocalScopes(env, node, frame); assert langScopes != null : languageInfo.getId();
} else { realOut = INSTRUMENT.createDispatchOutput(out); engine().attachOutputConsumer(realOut, realRuntime.out); } else { realErr = INSTRUMENT.createDispatchOutput(err); engine().attachOutputConsumer(realErr, realRuntime.err);
return AccessAPI.engineAccess().createThread(vmObject, runnable, context != null ? context.impl : null, group, stackSize);
return AccessAPI.engineAccess().createThread(vmObject, runnable, context != null ? context.impl : null);
if (elm instanceof OutputStream) { if (outputErrBindings.contains(binding)) { AccessorInstrumentHandler.engineAccess().detachOutputConsumer(err, (OutputStream) elm); } else if (outputStdBindings.contains(binding)) { AccessorInstrumentHandler.engineAccess().detachOutputConsumer(out, (OutputStream) elm);
/** * Find a hierarchy of local scopes enclosing the given {@link Node node}. Unless the node is in * a global scope, it is expected that there is at least one scope provided, that corresponds to * the enclosing function. The language might provide additional block scopes, closure scopes, * etc. Global top scopes are provided by {@link #findTopScopes(java.lang.Object)}. The scope * hierarchy should correspond with the scope nesting, from the inner-most to the outer-most. * The scopes are expected to contain variables valid at the given node. * <p> * Scopes may depend on the information provided by the frame. <br/> * Lexical scopes are returned when <code>frame</code> argument is <code>null</code>. * <p> * When not overridden, the enclosing {@link RootNode}'s scope with variables read from its * {@link FrameDescriptor}'s {@link FrameSlot}s is provided by default. * <p> * The * {@link com.oracle.truffle.api.instrumentation.TruffleInstrument.Env#findLocalScopes(com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.Frame)} * provides result of this method to instruments. * * @param context the current context of the language * @param node a node to find the enclosing scopes for. The node, is inside a {@link RootNode} * associated with this language. * @param frame The current frame the node is in, or <code>null</code> for lexical access when * the program is not running, or is not suspended at the node's location. * @return an iterable with scopes in their nesting order from the inner-most to the outer-most. * @since 0.30 */ protected Iterable<Scope> findLocalScopes(C context, Node node, Frame frame) { assert node != null; return AccessAPI.engineAccess().createDefaultLexicalScope(node, frame); }
/** * Find a hierarchy of local scopes enclosing the given {@link Node node}. Unless the node is in * a global scope, it is expected that there is at least one scope provided, that corresponds to * the enclosing function. The language might provide additional block scopes, closure scopes, * etc. Global top scopes are provided by {@link #findTopScopes(java.lang.Object)}. The scope * hierarchy should correspond with the scope nesting, from the inner-most to the outer-most. * The scopes are expected to contain variables valid at the given node. * <p> * Scopes may depend on the information provided by the frame. <br/> * Lexical scopes are returned when <code>frame</code> argument is <code>null</code>. * <p> * When not overridden, the enclosing {@link RootNode}'s scope with variables read from its * {@link FrameDescriptor}'s {@link FrameSlot}s is provided by default. * <p> * The * {@link com.oracle.truffle.api.instrumentation.TruffleInstrument.Env#findLocalScopes(com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.Frame)} * provides result of this method to instruments. * * @param context the current context of the language * @param node a node to find the enclosing scopes for. The node, is inside a {@link RootNode} * associated with this language. * @param frame The current frame the node is in, or <code>null</code> for lexical access when * the program is not running, or is not suspended at the node's location. * @return an iterable with scopes in their nesting order from the inner-most to the outer-most. * @since 0.30 */ protected Iterable<Scope> findLocalScopes(C context, Node node, Frame frame) { assert node != null; return AccessAPI.engineAccess().createDefaultLexicalScope(node, frame); }
/** * Parses source snippet of the node's language at the provided node location. The result is * an AST fragment represented by {@link ExecutableNode} that accepts frames valid at the * provided node location, or <code>null</code> when inline parsing is not supported by the * language. * * @param source a source snippet to parse at the provided node location * @param node a context location where the source is parsed at, must not be * <code>null</code> * @param frame a frame location where the source is parsed at, can be <code>null</code> * @return the executable fragment representing the parsed result, or <code>null</code> * @since 0.31 */ public ExecutableNode parseInline(Source source, Node node, MaterializedFrame frame) { if (node == null) { throw new IllegalArgumentException("Node must not be null."); } TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); // Assert that the languages match: assert AccessorInstrumentHandler.langAccess().getLanguageInfo(env) == node.getRootNode().getLanguageInfo(); ExecutableNode fragment = AccessorInstrumentHandler.langAccess().parseInline(env, source, node, frame); if (fragment != null) { TruffleLanguage<?> languageSPI = AccessorInstrumentHandler.langAccess().getSPI(env); fragment = new GuardedExecutableNode(languageSPI, fragment, frame); } return fragment; }
/** * Evaluates source of (potentially different) language using the current context. 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 * @throws IOException if the parsing or evaluation fails for some reason * @since 0.12 * @deprecated Use * {@link TruffleInstrument.Env#parseInline(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.MaterializedFrame)} * with {@link #getInstrumentedNode()} instead. */ @Deprecated public CallTarget parseInContext(Source source, String... argumentNames) throws IOException { Node instrumentedNode = getInstrumentedNode(); LanguageInfo languageInfo = instrumentedNode.getRootNode().getLanguageInfo(); if (languageInfo == null) { throw new IllegalArgumentException("No language available for given node."); } Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo); return AccessorInstrumentHandler.langAccess().parse(env, source, instrumentedNode, argumentNames); }
/** * Evaluates source of (potentially different) language using the current context. 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 * @throws IOException if the parsing or evaluation fails for some reason * @since 0.12 * @deprecated Use * {@link TruffleInstrument.Env#parseInline(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.MaterializedFrame)} * with {@link #getInstrumentedNode()} instead. */ @Deprecated public CallTarget parseInContext(Source source, String... argumentNames) throws IOException { Node instrumentedNode = getInstrumentedNode(); LanguageInfo languageInfo = instrumentedNode.getRootNode().getLanguageInfo(); if (languageInfo == null) { throw new IllegalArgumentException("No language available for given node."); } Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo); return AccessorInstrumentHandler.langAccess().parse(env, source, instrumentedNode, argumentNames); }
/** * Parses source snippet of the node's language at the provided node location. The result is * an AST fragment represented by {@link ExecutableNode} that accepts frames valid at the * provided node location, or <code>null</code> when inline parsing is not supported by the * language. * * @param source a source snippet to parse at the provided node location * @param node a context location where the source is parsed at, must not be * <code>null</code> * @param frame a frame location where the source is parsed at, can be <code>null</code> * @return the executable fragment representing the parsed result, or <code>null</code> * @since 0.31 */ public ExecutableNode parseInline(Source source, Node node, MaterializedFrame frame) { if (node == null) { throw new IllegalArgumentException("Node must not be null."); } TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); // Assert that the languages match: assert AccessorInstrumentHandler.langAccess().getLanguageInfo(env) == node.getRootNode().getLanguageInfo(); ExecutableNode fragment = AccessorInstrumentHandler.langAccess().parseInline(env, source, node, frame); if (fragment != null) { TruffleLanguage<?> languageSPI = AccessorInstrumentHandler.langAccess().getSPI(env); fragment = new GuardedExecutableNode(languageSPI, fragment, frame); } return fragment; }
/** * Uses the provided language to find a meta-object of a value, if any. The meta-object * represents a description of the object, reveals it's kind and it's features. Some * information that a meta-object might define includes the base object's type, interface, * class, methods, attributes, etc. When no meta-object is known, <code>null</code> is * returned. For the best results, use the {@link #findLanguage(java.lang.Object) value's * language}, if any. * * @param language a language * @param value a value to find the meta-object of, must be an interop type (i.e. either * implementing TruffleObject or be a primitive value) * @return the meta-object, or <code>null</code> * @see #findLanguage(java.lang.Object) * @since 0.27 */ public Object findMetaObject(LanguageInfo language, Object value) { AccessorInstrumentHandler.interopAccess().checkInteropType(value); final TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(language); Object metaObject = AccessorInstrumentHandler.langAccess().findMetaObject(env, value); assert checkNullOrInterop(metaObject); return metaObject; }
/** * Uses the provided language to find a meta-object of a value, if any. The meta-object * represents a description of the object, reveals it's kind and it's features. Some * information that a meta-object might define includes the base object's type, interface, * class, methods, attributes, etc. When no meta-object is known, <code>null</code> is * returned. For the best results, use the {@link #findLanguage(java.lang.Object) value's * language}, if any. * * @param language a language * @param value a value to find the meta-object of, must be an interop type (i.e. either * implementing TruffleObject or be a primitive value) * @return the meta-object, or <code>null</code> * @see #findLanguage(java.lang.Object) * @since 0.27 */ public Object findMetaObject(LanguageInfo language, Object value) { AccessorInstrumentHandler.interopAccess().checkInteropType(value); final TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(language); return AccessorInstrumentHandler.langAccess().findMetaObject(env, value); }