@Override public SourceBuilder newBuilder(String language, File origin) { return Source.newBuilder(language, origin); }
/** * Builds new {@link Source source} from a provided text. One needs to specify a * {@link Builder#mimeType(java.lang.String)}, possibly a {@link Builder#name(java.lang.String)} * and other attributes and then can {@link Builder#build()} a new instance of the source. * Sample usage: * * {@link SourceSnippets#fromAString} * * @param text the text to be returned by {@link Source#getCharacters()} * @return new builder to configure additional properties * @since 0.15 */ public static Builder<RuntimeException, MissingMIMETypeException, MissingNameException> newBuilder(String text) { return newBuilder((CharSequence) text); }
/** * @since 0.15 * @deprecated use {@link #newBuilder(String, CharSequence, String)} */ @Deprecated public static Builder<RuntimeException, MissingMIMETypeException, MissingNameException> newBuilder(String text) { return newBuilder((CharSequence) text); }
@Override protected CallTarget parse(ParsingRequest request) throws Exception { Source code = request.getSource(); final RootNode root; final String txt = code.getCharacters().toString(); if (txt.startsWith("TCK42:")) { int nextColon = txt.indexOf(":", 6); String mimeType = txt.substring(6, nextColon); Source toParse = Source.newBuilder(txt.substring(nextColon + 1)).mimeType(mimeType).name("src.tck").build(); root = new MultiplyNode(this, toParse); } else { final double value = Double.parseDouble(txt); root = RootNode.createConstantNode(value); } return Truffle.getRuntime().createCallTarget(root); }
@Override @SuppressWarnings("deprecation") protected CallTarget parse(ParsingRequest request) throws Exception { Source code = request.getSource(); final RootNode root; final String txt = code.getCharacters().toString(); if (txt.startsWith("TCK42:")) { int nextColon = txt.indexOf(":", 6); String mimeType = txt.substring(6, nextColon); Source toParse = Source.newBuilder(txt.substring(nextColon + 1)).name("src.tck").mimeType(mimeType).build(); root = new MultiplyNode(this, toParse); } else { final double value = Double.parseDouble(txt); root = RootNode.createConstantNode(value); } return Truffle.getRuntime().createCallTarget(root); }
public Source getSource() { if (source == null) { String text = toString(); source = Source.newBuilder(RegexLanguage.ID, text, text).name(text).mimeType(RegexLanguage.MIME_TYPE).build(); } return source; }
boolean needsName = false; if (origin instanceof File) { builder = com.oracle.truffle.api.source.Source.newBuilder((File) origin); } else if (origin instanceof CharSequence) { builder = com.oracle.truffle.api.source.Source.newBuilder(((CharSequence) origin)); needsName = true; } else if (origin instanceof Reader) { builder = com.oracle.truffle.api.source.Source.newBuilder((Reader) origin); needsName = true; } else if (origin instanceof URL) { builder = com.oracle.truffle.api.source.Source.newBuilder((URL) origin); } else { throw new AssertionError();
return Source.newBuilder(source.getLanguage(), new InputStreamReader(connection.getInputStream()), name).uri(uri).cached(false).interactive(source.isInteractive()).internal( source.isInternal()).mimeType(source.getMimeType()).build(); } catch (IOException ex) {
@CompilerDirectives.TruffleBoundary @Override public SourceSection getSourceSection() { if (sourceSection == null) { String patternSrc = "/" + source.getPattern() + "/" + source.getFlags(); Source src = Source.newBuilder(RegexLanguage.ID, patternSrc, source.getPattern()).mimeType("application/js-regex").build(); sourceSection = src.createSection(0, patternSrc.length()); } return sourceSection; }
builder = VMAccessor.SOURCE.newBuilder(language, (File) origin); } else if (origin instanceof CharSequence) { builder = com.oracle.truffle.api.source.Source.newBuilder(language, ((CharSequence) origin), name); } else if (origin instanceof ByteSequence) { builder = com.oracle.truffle.api.source.Source.newBuilder(language, ((ByteSequence) origin), name); } else if (origin instanceof Reader) { builder = com.oracle.truffle.api.source.Source.newBuilder(language, (Reader) origin, name); } else if (origin instanceof URL) { builder = com.oracle.truffle.api.source.Source.newBuilder(language, (URL) origin); } else { throw new AssertionError();
/** @since 0.8 or earlier */ @Test public void multiplyTwoVariables() throws Exception { final String firstVar = "var" + (char) ('A' + RANDOM.nextInt(24)); final String secondVar = "var" + (char) ('0' + RANDOM.nextInt(10)); String mulCode = multiplyCode(firstVar, secondVar); // @formatter:off Source source = Source.newBuilder("TCK42:" + mimeType() + ":" + mulCode). name("evaluate " + firstVar + " * " + secondVar). mimeType("application/x-tck"). build(); // @formatter:on final PolyglotEngine.Value evalSource = vm().eval(source); final PolyglotEngine.Value invokeMul = evalSource.execute(firstVar, secondVar); Object result = invokeMul.get(); assertTrue("Expecting numeric result, was:" + result + " for " + firstVar + " and " + secondVar, result instanceof Number); assertEquals("Right value for " + firstVar + " and " + secondVar, 42, ((Number) result).intValue()); }
private static Object evalInContext(SuspendedEvent ev, Node node, MaterializedFrame frame, String code) { RootNode rootNode = node.getRootNode(); if (rootNode == null) { throw new IllegalArgumentException("Cannot evaluate in context using a node that is not yet adopated using a RootNode."); } LanguageInfo info = rootNode.getLanguageInfo(); if (info == null) { throw new IllegalArgumentException("Cannot evaluate in context using a without an associated TruffleLanguage."); } if (!info.isInteractive()) { throw new IllegalStateException("Can not evaluate in a non-interactive language."); } final Source source = Source.newBuilder(info.getId(), code, "eval in context").build(); ExecutableNode fragment = ev.getSession().getDebugger().getEnv().parseInline(source, node, frame); if (fragment != null) { ev.getInsertableNode().setParentOf(fragment); return fragment.execute(frame); } else { return Debugger.ACCESSOR.evalInContext(source, node, frame); } }
/** * Evaluate the given code in this context. * * @param code the code to evaluate * @param languageId the language to evaluate the code in * @return result of the evaluation * @since 0.30 */ public DebugValue evaluate(String code, String languageId) { assert code != null; Object prevContext = context.enter(); try { Debugger debugger = executionLifecycle.getDebugger(); CallTarget target = debugger.getEnv().parse(Source.newBuilder(languageId, code, "eval").build()); Object result = target.call(); LanguageInfo languageInfo = debugger.getEnv().getLanguages().get(languageId); return new DebugValue.HeapValue(executionLifecycle.getSession(), languageInfo, null, result); } catch (IOException ex) { throw new RuntimeException(ex); } finally { context.leave(prevContext); } }
/** * Evaluate the given code in this context. * * @param code the code to evaluate * @param languageId the language to evaluate the code in * @return result of the evaluation * @since 0.30 */ public DebugValue evaluate(String code, String languageId) { assert code != null; Object prevContext = context.enter(); try { Debugger debugger = executionLifecycle.getDebugger(); CallTarget target = debugger.getEnv().parse(Source.newBuilder(code).language(languageId).name("eval").build()); Object result = target.call(); LanguageInfo languageInfo = debugger.getEnv().getLanguages().get(languageId); return new DebugValue.HeapValue(debugger, languageInfo, null, result); } catch (IOException ex) { throw new RuntimeException(ex); } finally { context.leave(prevContext); } }
final Source source = Source.newBuilder(code).name("eval in context").language(info.getId()).mimeType("content/unknown").build(); ExecutableNode fragment = null; CallTarget target = null;
/** @since 0.8 or earlier */ @Test(expected = Exception.class) public void testInvalidTestMethod() throws Exception { String mime = mimeType(); String code = invalidCode(); // @formatter:off Source invalidCode = Source.newBuilder(code). name("Invalid code"). mimeType(mime). build(); // @formatter:on Object ret = vm().eval(invalidCode).get(); fail("Should yield IOException, but returned " + ret); }
private void initializeConditional(MaterializedFrame frame) { Node instrumentedNode = context.getInstrumentedNode(); final RootNode rootNode = instrumentedNode.getRootNode(); if (rootNode == null) { throw new IllegalStateException("Probe was disconnected from the AST."); } Source instrumentedSource = context.getInstrumentedSourceSection().getSource(); Source conditionSource; synchronized (breakpoint) { conditionSource = Source.newBuilder(instrumentedSource.getLanguage(), breakpoint.condition, "breakpoint condition").mimeType(instrumentedSource.getMimeType()).build(); if (conditionSource == null) { throw new IllegalStateException("Condition is not resolved " + rootNode); } conditionUnchanged = breakpoint.getConditionUnchanged(); } ExecutableNode snippet = breakpoint.debugger.getEnv().parseInline(conditionSource, instrumentedNode, frame); if (snippet != null) { conditionSnippet = insert(snippet); notifyInserted(snippet); } else { CallTarget callTarget = Debugger.ACCESSOR.parse(conditionSource, instrumentedNode, new String[0]); conditionCallNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget)); } } }
private void initializeConditional(MaterializedFrame frame) { Node instrumentedNode = context.getInstrumentedNode(); final RootNode rootNode = instrumentedNode.getRootNode(); if (rootNode == null) { throw new IllegalStateException("Probe was disconnected from the AST."); } Source instrumentedSource = context.getInstrumentedSourceSection().getSource(); Source conditionSource; synchronized (breakpoint) { conditionSource = Source.newBuilder(breakpoint.condition).language(instrumentedSource.getLanguage()).mimeType(instrumentedSource.getMimeType()).name( "breakpoint condition").build(); if (conditionSource == null) { throw new IllegalStateException("Condition is not resolved " + rootNode); } conditionUnchanged = breakpoint.getConditionUnchanged(); } ExecutableNode snippet = breakpoint.debugger.getEnv().parseInline(conditionSource, instrumentedNode, frame); if (snippet != null) { conditionSnippet = insert(snippet); notifyInserted(snippet); } else { CallTarget callTarget = Debugger.ACCESSOR.parse(conditionSource, instrumentedNode, new String[0]); conditionCallNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget)); } } }