Search in sources :

Example 91 with Source

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

the class DebuggerExampleTest method testStepOver.

@Test
@SuppressWarnings("hiding")
public void testStepOver() throws IOException {
    Source source = lines(// 1
    "ROOT(", // 2
    "DEFINE(foo,STATEMENT),", // 3
    "DEFINE(bar", // 4
    ",STATEMENT", // 5
    ",STATEMENT(CALL(foo))", // 6
    ",STATEMENT),", /**/
    "STATEMENT(CALL(bar)))");
    // 7
    final AtomicBoolean allStepped = new AtomicBoolean();
    debugger.installBreakpoint(4, new Callback() {

        public void halted(DebuggerController debugger, EventContext haltedAt) {
            assertLineAt(haltedAt, 4);
            debugger.stepOver(new Callback() {

                public void halted(DebuggerController debugger, EventContext haltedAt) {
                    assertLineAt(haltedAt, 5);
                    debugger.stepOver(new Callback() {

                        public void halted(DebuggerController debugger, EventContext haltedAt) {
                            assertLineAt(haltedAt, 6);
                            allStepped.set(true);
                            debugger.stepOver(new Callback() {

                                public void halted(DebuggerController debugger, EventContext haltedAt) {
                                    throw new AssertionError();
                                }
                            });
                        }
                    });
                }
            });
        }
    });
    run(source);
    Assert.assertTrue(allStepped.get());
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(com.oracle.truffle.api.instrumentation.test.examples.DebuggerController.Callback) Source(org.graalvm.polyglot.Source) Test(org.junit.Test) AbstractInstrumentationTest(com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)

Example 92 with Source

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

the class AllocationReporterTest method testNestedAllocations.

@Test
public void testNestedAllocations() {
    Source source = Source.create(AllocationReporterLanguage.ID, "NEW { NEW }\n" + "10 { 20 30 { 1234567890123456789 } }\n" + "12345678901234->897654123210445621235489 { 10->NEW { 20->NEW } 30->NEW }\n");
    AtomicInteger consumerCalls = new AtomicInteger(0);
    allocation.setAllocationConsumers(// NEW { NEW }
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("NewObject", info.value.toString());
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("NewObject", info.value.toString());
        consumerCalls.incrementAndGet();
    }, // | 10 { 20 ...
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { | 20 ...
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { 20 | 30 ...
    (info) -> {
        assertFalse(info.will);
        assertEquals(20, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { 20 30 { | 1234567890123456789 } }
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(8, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { 20 30 { 1234567890123456789 | } }
    (info) -> {
        assertFalse(info.will);
        assertEquals(1234567890123456789L, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(8, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { 20 30 { 1234567890123456789 } | }
    (info) -> {
        assertFalse(info.will);
        assertEquals(30, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10 { 20 30 { 1234567890123456789 } } |
    (info) -> {
        assertFalse(info.will);
        assertEquals(10, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(4, info.newSize);
        consumerCalls.incrementAndGet();
    }, // }
    (info) -> {
        assertTrue(info.will);
        assertEquals(12345678901234L, info.value);
        assertEquals(8, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertTrue(info.will);
        assertEquals(10, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertTrue(info.will);
        assertEquals(20, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(20, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(10, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertTrue(info.will);
        assertEquals(30, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(30, info.value);
        assertEquals(4, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(12345678901234L, info.value);
        assertEquals(8, info.oldSize);
        assertEquals(5, info.newSize);
        consumerCalls.incrementAndGet();
    });
    context.eval(source);
    assertEquals(20, consumerCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 93 with Source

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

the class AllocationReporterTest method doTestAllocationReport.

private void doTestAllocationReport(long[] estimatedSizes, long[] computedSizes) {
    // Items to allocate:
    Source source = Source.create(AllocationReporterLanguage.ID, "NEW\n" + "10\n" + "12345678901234\n" + "-1000\n" + "8767584273645748301282734657402983457843901293874657867582034875\n");
    AtomicInteger consumerCalls = new AtomicInteger(0);
    allocation.setAllocationConsumers(// NEW
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(estimatedSizes[0], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("NewObject", info.value.toString());
        assertEquals(0, info.oldSize);
        assertEquals(computedSizes[0], info.newSize);
        consumerCalls.incrementAndGet();
    }, // 10
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(estimatedSizes[1], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("java.lang.Integer", info.value.getClass().getName());
        assertEquals(10, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(computedSizes[1], info.newSize);
        consumerCalls.incrementAndGet();
    }, // 12345678901234
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(estimatedSizes[2], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("java.lang.Long", info.value.getClass().getName());
        assertEquals(12345678901234L, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(computedSizes[2], info.newSize);
        consumerCalls.incrementAndGet();
    }, // -1000
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(estimatedSizes[3], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("java.lang.Integer", info.value.getClass().getName());
        assertEquals(-1000, info.value);
        assertEquals(0, info.oldSize);
        assertEquals(computedSizes[3], info.newSize);
        consumerCalls.incrementAndGet();
    }, // 8767584273645748301282734657402983457843901293874657867582034875
    (info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(estimatedSizes[4], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(BigNumber.class, info.value.getClass());
        assertEquals(0, info.oldSize);
        assertEquals(computedSizes[4], info.newSize);
        consumerCalls.incrementAndGet();
    });
    context.eval(source);
    assertEquals(10, consumerCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Source(org.graalvm.polyglot.Source)

Example 94 with Source

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

the class AllocationReporterTest method doTestReallocationReport.

private void doTestReallocationReport(long[] estimatedSizes, long[] computedSizes) {
    Source source = Source.create(AllocationReporterLanguage.ID, "NEW->10\n" + "8767584273645748301282734657402983457843901293874657867582034875->987364758928736457840187265789\n");
    AtomicInteger consumerCalls = new AtomicInteger(0);
    allocation.setAllocationConsumers(// NEW -> 10
    (info) -> {
        assertTrue(info.will);
        assertEquals("NewObject", info.value.toString());
        assertEquals(estimatedSizes[0], info.oldSize);
        assertEquals(estimatedSizes[1], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals("NewObject", info.value.toString());
        assertEquals(computedSizes[0], info.oldSize);
        assertEquals(computedSizes[1], info.newSize);
        consumerCalls.incrementAndGet();
    }, // BigNumber -> BigNumber
    (info) -> {
        assertTrue(info.will);
        assertEquals(BigNumber.class, info.value.getClass());
        assertEquals(estimatedSizes[2], info.oldSize);
        assertEquals(estimatedSizes[3], info.newSize);
        consumerCalls.incrementAndGet();
    }, (info) -> {
        assertFalse(info.will);
        assertEquals(BigNumber.class, info.value.getClass());
        assertEquals(computedSizes[2], info.oldSize);
        assertEquals(computedSizes[3], info.newSize);
        consumerCalls.incrementAndGet();
    });
    context.eval(source);
    assertEquals(4, consumerCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Source(org.graalvm.polyglot.Source)

Example 95 with Source

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

the class AllocationReporterTest method testWrongAllocations.

@Test
@Ignore
public void testWrongAllocations() {
    // Test of wrong allocation reports
    // A call to allocated() without a prior notifyWill... fails:
    Source source = Source.create(AllocationReporterLanguage.ID, "WRONG");
    AtomicInteger consumerCalls = new AtomicInteger(0);
    allocation.setAllocationConsumers((info) -> consumerCalls.incrementAndGet());
    try {
        context.eval(source);
        fail();
    } catch (AssertionError err) {
        assertEquals("onEnter() was not called", err.getMessage());
    }
    assertEquals(0, consumerCalls.get());
    // Have one notifyWillReallocate() call caused by NEW,
    // but denied to suppress the notifyAllocated().
    // Then call notifyAllocated() alone:
    source = Source.create(AllocationReporterLanguage.ID, "10->10");
    allocation.setAllocationConsumers((info) -> {
        consumerCalls.incrementAndGet();
        throw new OutOfMemoryError("Denied one allocation.");
    });
    try {
        context.eval(source);
        fail();
    } catch (OutOfMemoryError err) {
    // O.K.
    }
    assertEquals(1, consumerCalls.get());
    source = Source.create(AllocationReporterLanguage.ID, "WRONG");
    allocation.setAllocationConsumers((info) -> consumerCalls.incrementAndGet(), (info) -> consumerCalls.incrementAndGet());
    try {
        context.eval(source);
        fail();
    } catch (AssertionError err) {
        assertEquals("A different reallocated value. Was: 10 now is: NewObject", err.getMessage());
    }
    assertEquals(1, consumerCalls.get());
    consumerCalls.set(0);
    // Exposal of internal values is not allowed
    source = Source.create(AllocationReporterLanguage.ID, "INTERNAL");
    allocation.setAllocationConsumers((info) -> consumerCalls.incrementAndGet(), (info) -> consumerCalls.incrementAndGet());
    try {
        context.eval(source);
        fail();
    } catch (AssertionError err) {
        assertEquals("Wrong value class, TruffleObject is required. Was: " + AllocationReporterLanguage.AllocValue.class.getName(), err.getMessage());
    }
    assertEquals(1, consumerCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Source(org.graalvm.polyglot.Source) Ignore(org.junit.Ignore) Test(org.junit.Test)

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