Search in sources :

Example 71 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLTestRunner method run.

private static void run(Context context, Path path, PrintWriter out) throws IOException {
    try {
        /* Parse the SL source file. */
        Source source = Source.newBuilder(SLLanguage.ID, path.toFile()).interactive(true).build();
        /* Call the main entry point, without any arguments. */
        context.eval(source);
    } catch (PolyglotException ex) {
        if (!ex.isInternalError()) {
            out.println(ex.getMessage());
        } else {
            throw ex;
        }
    }
}
Also used : PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source)

Example 72 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class PolyglotExceptionFrame method createGuest.

static PolyglotExceptionFrame createGuest(PolyglotExceptionImpl exception, TruffleStackTraceElement frame, boolean first) {
    if (frame == null) {
        return null;
    }
    RootNode targetRoot = frame.getTarget().getRootNode();
    if (targetRoot.isInternal()) {
        return null;
    }
    LanguageInfo info = targetRoot.getLanguageInfo();
    if (info == null) {
        return null;
    }
    PolyglotEngineImpl engine = exception.context.getEngine();
    PolyglotLanguage language = engine.idToLanguage.get(info.getId());
    String rootName = targetRoot.getName();
    SourceSection location;
    Node callNode = frame.getLocation();
    if (callNode != null) {
        com.oracle.truffle.api.source.SourceSection section = callNode.getEncapsulatingSourceSection();
        if (section != null) {
            Source source = engine.getAPIAccess().newSource(language.getId(), section.getSource());
            location = engine.getAPIAccess().newSourceSection(source, section);
        } else {
            location = null;
        }
    } else {
        location = first ? exception.getSourceLocation() : null;
    }
    return new PolyglotExceptionFrame(exception, language, location, rootName, false, null);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) Source(org.graalvm.polyglot.Source) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) SourceSection(org.graalvm.polyglot.SourceSection)

Example 73 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class PolyglotExceptionFrame method formatSource.

private static String formatSource(SourceSection sourceSection) {
    if (sourceSection == null) {
        return "Unknown";
    }
    Source source = sourceSection.getSource();
    if (source == null) {
        // safety check. likely not necsssary
        return "Unknown";
    }
    StringBuilder b = new StringBuilder();
    String path = source.getPath();
    if (path == null) {
        b.append(source.getName());
    } else {
        Path pathAbsolute = Paths.get(path);
        Path pathBase = new File("").getAbsoluteFile().toPath();
        try {
            Path pathRelative = pathBase.relativize(pathAbsolute);
            b.append(pathRelative.toFile());
        } catch (IllegalArgumentException e) {
            b.append(source.getName());
        }
    }
    b.append(":").append(formatIndices(sourceSection, true));
    return b.toString();
}
Also used : Path(java.nio.file.Path) File(java.io.File) Source(org.graalvm.polyglot.Source)

Example 74 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class PolyglotEngineWithJavaScript method createJavaScriptFactoryForJavaClass.

public void createJavaScriptFactoryForJavaClass() {
    Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + "  return function(h, m, s) {\n" + "     return new Moment(h, m, s);\n" + "  };\n" + "})\n", "ConstructMoment.js").buildLiteral();
    // Evaluate the JavaScript function definition
    final Value jsFunction = context.eval(src);
    // Create a JavaScript factory for the provided Java class
    final Value jsFactory = jsFunction.execute(Moment.class);
    // Convert the JavaScript factory to a Java foreign function
    MomentFactory momentFactory = jsFactory.as(MomentFactory.class);
    final Moment javaMoment = momentFactory.create(6, 30, 10);
    assertEquals("Hours", 6, javaMoment.hours);
    assertEquals("Minutes", 30, javaMoment.minutes);
    assertEquals("Seconds", 10, javaMoment.seconds);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source)

Example 75 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class PolyglotEngineWithJavaScript method accessFieldsOfJavaObject.

public void accessFieldsOfJavaObject() {
    Source src = Source.newBuilder("js", "" + "(function(t) {\n" + "  return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral();
    final Moment javaMoment = new Moment(6, 30, 10);
    // Evaluate the JavaScript function definition
    Value jsFunction = context.eval(src);
    // Execute the JavaScript function, passing a Java object argument
    Value jsSeconds = jsFunction.execute(javaMoment);
    // Convert foreign object result to desired Java type
    int seconds = jsSeconds.as(Number.class).intValue();
    assertEquals(3600 * 6 + 30 * 60 + 10, seconds);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source)

Aggregations

Source (org.graalvm.polyglot.Source)196 Test (org.junit.Test)165 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)103 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)95 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)51 Value (org.graalvm.polyglot.Value)31 Context (org.graalvm.polyglot.Context)25 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)21 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)14 Instrument (org.graalvm.polyglot.Instrument)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 SourceSection (com.oracle.truffle.api.source.SourceSection)12 EventContext (com.oracle.truffle.api.instrumentation.EventContext)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Engine (org.graalvm.polyglot.Engine)9 ArrayList (java.util.ArrayList)8 PolyglotException (org.graalvm.polyglot.PolyglotException)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)7