Search in sources :

Example 86 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class TruffleLanguageSnippets method parseWithParams.

// BEGIN: TruffleLanguageSnippets#parseWithParams
public void parseWithParams(Env env) {
    Source multiply = Source.newBuilder("a * b").mimeType("text/javascript").name("mul.js").build();
    CallTarget method = env.parse(multiply, "a", "b");
    Number fortyTwo = (Number) method.call(6, 7);
    assert 42 == fortyTwo.intValue();
    Number ten = (Number) method.call(2, 5);
    assert 10 == ten.intValue();
}
Also used : Source(com.oracle.truffle.api.source.Source)

Example 87 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class LanguageSPITest method testExceptionGetSourceLocation.

@Test
public void testExceptionGetSourceLocation() {
    try (final Context context = Context.create(LanguageSPITestLanguage.ID)) {
        final String text = "0123456789";
        LanguageSPITestLanguage.runinside = (env) -> {
            Source src = Source.newBuilder(text).mimeType(LanguageSPITestLanguage.ID).name("test.txt").build();
            throw new ParseException(src, 1, 2);
        };
        try {
            context.eval(LanguageSPITestLanguage.ID, text);
            Assert.fail("PolyglotException expected.");
        } catch (PolyglotException pe) {
            Assert.assertTrue(pe.isSyntaxError());
            Assert.assertEquals("12", pe.getSourceLocation().getCharacters().toString());
        } finally {
            LanguageSPITestLanguage.runinside = null;
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 88 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class LanguageSPITest method testTruffleContext.

@Test
public void testTruffleContext() {
    Context context = Context.create();
    Function<Env, Object> f = new Function<Env, Object>() {

        public Object apply(Env env) {
            boolean assertions = false;
            assert (assertions = true) == true;
            if (!assertions) {
                fail("Tests must be run with assertions on");
            }
            // No more recursive runs inside
            LanguageSPITestLanguage.runinside = null;
            Throwable[] error = new Throwable[1];
            Thread thread = new Thread(() -> {
                try {
                    Source source = Source.newBuilder("").language(LanguageSPITestLanguage.ID).name("s").build();
                    boolean parsingFailed = false;
                    try {
                        // execute Truffle code in a fresh thread fails
                        env.parse(source).call();
                    } catch (AssertionError e) {
                        // No current context available.
                        parsingFailed = true;
                    }
                    if (!parsingFailed) {
                        fail("no assertion error \"No current context available.\"");
                    }
                    TruffleContext truffleContext = env.getContext();
                    // attach the Thread
                    Object prev = truffleContext.enter();
                    try {
                        // execute Truffle code
                        env.parse(source).call();
                    } finally {
                        // detach the Thread
                        truffleContext.leave(prev);
                    }
                } catch (Throwable t) {
                    error[0] = t;
                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            if (error[0] != null) {
                throw new AssertionError(error[0]);
            }
            boolean leaveFailed = false;
            try {
                TruffleContext truffleContext = env.getContext();
                truffleContext.leave(null);
            } catch (AssertionError e) {
                leaveFailed = true;
            }
            if (!leaveFailed) {
                fail("no assertion error for leaving without enter");
            }
            return null;
        }
    };
    eval(context, f);
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) Source(com.oracle.truffle.api.source.Source) Function(java.util.function.Function) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 89 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderDocumentationTest method readerSample.

@Test
public void readerSample() throws Exception {
    if (!loadedOK) {
        return;
    }
    Source source = (Source) invokeStatic(SOURCE_SNIPPETS, "fromReader", SourceBuilderDocumentationTest.class);
    assertNotNull("Every source must have URI", source.getURI());
}
Also used : Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 90 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderDocumentationTest method relativeFile.

@Test
public void relativeFile() throws Exception {
    if (!loadedOK) {
        return;
    }
    File relative = null;
    for (File f : new File(".").listFiles()) {
        if (f.isFile() && f.canRead()) {
            relative = f;
            break;
        }
    }
    if (relative == null) {
        // skip the test
        return;
    }
    Source direct = Source.newBuilder(relative).name(relative.getPath()).build();
    Source fromBuilder = (Source) invokeStatic(SOURCE_SNIPPETS, "likeFileName", relative.getPath());
    assertEquals("Both sources are equal", direct, fromBuilder);
}
Also used : File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Aggregations

Source (com.oracle.truffle.api.source.Source)113 Test (org.junit.Test)65 RootNode (com.oracle.truffle.api.nodes.RootNode)23 File (java.io.File)20 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)16 Node (com.oracle.truffle.api.nodes.Node)16 SourceSection (com.oracle.truffle.api.source.SourceSection)16 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)15 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)11 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CallTarget (com.oracle.truffle.api.CallTarget)5 FileWriter (java.io.FileWriter)5 RootCallTarget (com.oracle.truffle.api.RootCallTarget)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3