Search in sources :

Example 76 with Source

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

the class DebuggerSessionSnippets method example.

@SuppressFBWarnings("")
public void example() {
    // @formatter:off
    TruffleInstrument.Env instrumentEnv = null;
    // BEGIN: DebuggerSessionSnippets#example
    try (DebuggerSession session = Debugger.find(instrumentEnv).startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent event) {
            // step into the next event
            event.prepareStepInto(1);
        }
    })) {
        Source someCode = Source.newBuilder("...").mimeType("...").name("example").build();
        // install line breakpoint
        session.install(Breakpoint.newBuilder(someCode).lineIs(3).build());
    }
// END: DebuggerSessionSnippets#example
// @formatter:on
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(com.oracle.truffle.api.source.Source)

Example 77 with Source

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

the class ContextLookupTest method assertExpectedContext.

private static void assertExpectedContext(PolyglotEngine vm, LanguageLookup language, LanguageLookupContext expectedContext) {
    Source s1 = Source.newBuilder("assertContext").name("").mimeType(LanguageLookup.MIME_TYPE).build();
    PolyglotEngine.Value result = vm.getLanguages().get(LanguageLookup.MIME_TYPE).eval(s1);
    LanguageLookupContext prevContext = language.expectedContext;
    language.expectedContext = expectedContext;
    // trying to exercise all TruffleLanguage API
    result.execute();
    vm.eval(s1);
    result.getMetaObject();
    result.getSourceLocation();
    assertEquals("something", result.as(String.class));
    Source s2 = Source.newBuilder("").name("").mimeType(LanguageLookup.MIME_TYPE).interactive().build();
    vm.eval(s2);
    vm.findGlobalSymbol("");
    vm.getLanguages().get(LanguageLookup.MIME_TYPE).getGlobalObject();
    language.expectedContext = prevContext;
}
Also used : Source(com.oracle.truffle.api.source.Source)

Example 78 with Source

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

the class EngineTest method testCaching.

@Test
public void testCaching() {
    CachingLanguageChannel channel = new CachingLanguageChannel();
    PolyglotEngine vm = register(createBuilder().config(CachingLanguage.MIME_TYPE, "channel", channel).build());
    final Source source1 = Source.newBuilder("unboxed").name("something").mimeType(CachingLanguage.MIME_TYPE).build();
    final Source source2 = Source.newBuilder("unboxed").name("something").mimeType(CachingLanguage.MIME_TYPE).build();
    int cachedTargetsSize = -1;
    int interopTargetsSize = -1;
    // from now on we should not create any new targets
    for (int i = 0; i < 10; i++) {
        PolyglotEngine.Value value1 = vm.eval(source1);
        PolyglotEngine.Value value2 = vm.eval(source2);
        value1 = value1.execute().execute().execute().execute();
        value2 = value2.execute().execute().execute().execute();
        value1.get();
        value2.get();
        assertNotNull(value1.as(CachingTruffleObject.class));
        assertNotNull(value2.as(CachingTruffleObject.class));
        if (i == 0) {
            cachedTargetsSize = channel.parseTargets.size();
            interopTargetsSize = channel.interopTargets.size();
            // its fair to assume some call targets need to get created
            assertNotEquals(0, cachedTargetsSize);
            assertNotEquals(0, interopTargetsSize);
        } else {
            // we need to have stable call targets after the first run.
            assertEquals(cachedTargetsSize, channel.parseTargets.size());
            assertEquals(interopTargetsSize, channel.interopTargets.size());
        }
    }
}
Also used : Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 79 with Source

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

the class EngineTest method testCachingFailing.

@Test
public void testCachingFailing() {
    CachingLanguageChannel channel = new CachingLanguageChannel();
    PolyglotEngine vm = register(createBuilder().config(CachingLanguage.MIME_TYPE, "channel", channel).build());
    final Source source1 = Source.newBuilder("boxed").name("something").mimeType(CachingLanguage.MIME_TYPE).build();
    final Source source2 = Source.newBuilder("boxed").name("something").mimeType(CachingLanguage.MIME_TYPE).build();
    int cachedTargetsSize = -1;
    int interopTargetsSize = -1;
    for (int i = 0; i < 10; i++) {
        PolyglotEngine.Value value1 = vm.eval(source1);
        PolyglotEngine.Value value2 = vm.eval(source2);
        TestInterface testInterface1 = value1.as(TestInterface.class);
        testInterface1.foobar();
        value1.as(Byte.class);
        value1.as(Short.class);
        value1.as(Integer.class);
        value1.as(Long.class);
        value1.as(Float.class);
        value1.as(Double.class);
        Map<?, ?> m1 = value1.as(Map.class);
        assertTrue(m1.isEmpty());
        List<?> l1 = value1.as(List.class);
        assertEquals(0, l1.size());
        ArrayLike a1 = value1.as(ArrayLike.class);
        assertEquals(0, a1.size());
        assertTrue(a1.isArray());
        TestInterface testInterface2 = value2.as(TestInterface.class);
        testInterface2.foobar();
        value2.as(Byte.class);
        value2.as(Short.class);
        value2.as(Integer.class);
        value2.as(Long.class);
        value2.as(Float.class);
        value2.as(Double.class);
        value2.as(Map.class);
        Map<?, ?> m2 = value2.as(Map.class);
        assertTrue(m2.isEmpty());
        List<?> l2 = value2.as(List.class);
        assertEquals(0, l2.size());
        ArrayLike a2 = value1.as(ArrayLike.class);
        assertEquals(0, a2.size());
        assertTrue(a2.isArray());
        if (i == 0) {
            // warmup
            cachedTargetsSize = channel.parseTargets.size();
            interopTargetsSize = channel.interopTargets.size();
            assertNotEquals(0, cachedTargetsSize);
            assertNotEquals(0, interopTargetsSize);
            channel.frozen = true;
        } else {
            // we need to have stable call targets after the first run.
            assertEquals(cachedTargetsSize, channel.parseTargets.size());
            assertEquals(interopTargetsSize, channel.interopTargets.size());
        }
    }
}
Also used : Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 80 with Source

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

the class AsyncExecutorTest method testFailingAsyncLanguageAccess.

@Test
public void testFailingAsyncLanguageAccess() {
    ExecutorService service = Executors.newFixedThreadPool(10);
    PolyglotEngine engine = PolyglotEngine.newBuilder().executor(service).build();
    Source s = Source.newBuilder("").mimeType("application/x-test-async").name("").build();
    List<PolyglotEngine.Value> values = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        values.add(engine.eval(s));
    }
    boolean atLeastOneIsOK = false;
    boolean atLeastOneIsBad = false;
    for (int i = 0; i < 100; i++) {
        // this way you can crash arbitrary languages.
        try {
            final int result = values.get(i).as(Integer.class);
            Assert.assertTrue("We obtained a result", result >= 0);
            atLeastOneIsOK = true;
        } catch (IllegalStateException ex) {
            assertTrue(ex.getMessage(), ex.getMessage().contains("Currently executing in Thread"));
            atLeastOneIsBad = true;
        }
    }
    assertTrue("1st execution has to be OK", atLeastOneIsOK);
    assertTrue("Executions in other threads have to be rejected", atLeastOneIsBad);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) 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