Search in sources :

Example 1 with InsightObjectFactory.createConfig

use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.

the class InsightObjectTest method onEnterCallback.

@Test
public void onEnterCallback() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        boolean[] program = { false };
        String[] functionName = { null };
        final InsightAPI.OnEventHandler listener = (ctx, frame) -> {
            if (ctx.name().length() == 0) {
                assertFalse("Program root is entered just once", program[0]);
                program[0] = true;
                return;
            }
            assertNull("No function entered yet", functionName[0]);
            functionName[0] = ctx.name();
        };
        agentAPI.on("enter", listener, InsightObjectFactory.createConfig(false, false, true, null, null));
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(foo,\n" + "    LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + "  ),\n" + "  CALL(foo)\n" + ")", "sample.px").build();
        // @formatter:on
        c.eval(sampleScript);
        assertTrue("Program started", program[0]);
        assertEquals("Function foo has been called", "foo", functionName[0]);
        agentAPI.off("enter", listener);
        program[0] = false;
        functionName[0] = null;
        // @formatter:off
        Source sampleScript2 = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(foo,\n" + "    LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + "  ),\n" + "  CALL(foo)\n" + ")", "sample.px").build();
        // @formatter:on
        c.eval(sampleScript2);
        assertFalse("No listener notified", program[0]);
        assertNull("No function entered", functionName[0]);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with InsightObjectFactory.createConfig

use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.

the class InsightObjectTest method internalScriptsAreIgnored.

@Test
public void internalScriptsAreIgnored() throws Exception {
    int[] closeCounter = { 0 };
    try (Context c = InsightObjectFactory.newContext()) {
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(foo,\n" + "    LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + "  ),\n" + "  CALL(foo)\n" + ")", "sample.px").internal(true).build();
        // @formatter:on
        final InsightAPI.OnSourceLoadedHandler listener = (ev) -> {
            if (ev.name().equals(sampleScript.getName())) {
                Assert.fail("Don't load internal scripts: " + ev.uri());
            }
        };
        agentAPI.on("source", listener);
        int[] expressionCounter = { 0 };
        agentAPI.on("enter", (ev, frame) -> {
            expressionCounter[0]++;
        }, InsightObjectFactory.createConfig(true, false, false, null, null));
        agentAPI.on("return", (ev, frame) -> {
            expressionCounter[0]++;
        }, InsightObjectFactory.createConfig(true, false, false, null, null));
        agentAPI.on("close", () -> {
            closeCounter[0]++;
        });
        c.eval(sampleScript);
        assertEquals("No expressions entered & exited", 0, expressionCounter[0]);
    }
    assertEquals("Close is reported", 1, closeCounter[0]);
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 3 with InsightObjectFactory.createConfig

use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.

the class InsightObjectTest method evalFirstAndThenOnEnterCallbackImpl.

private static void evalFirstAndThenOnEnterCallbackImpl(Executor registerIn) throws Throwable {
    try (Context c = InsightObjectFactory.newContext()) {
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(foo,\n" + "    LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))\n" + "  ),\n" + "  CALL(foo)\n" + ")", "sample.px").build();
        // @formatter:on
        c.eval(sampleScript);
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        String[] functionName = { null };
        final InsightAPI.OnEventHandler listener = (ctx, frame) -> {
            if (ctx.name().length() == 0) {
                return;
            }
            assertNull("No function entered yet", functionName[0]);
            functionName[0] = ctx.name();
            assertNotEquals("Subset of the source found", -1, ctx.characters().indexOf("LOOP(10, STATEMENT(EXPRESSION,EXPRESSION))"));
            assertEquals(14, ctx.column());
            assertEquals(14, ctx.startColumn());
            assertEquals(2, ctx.endColumn());
            assertEquals(2, ctx.line());
            assertEquals(2, ctx.startLine());
            assertEquals(4, ctx.endLine());
            String fullText = ctx.source().characters();
            final int charIndex = ctx.charIndex();
            final int charEndIndex = ctx.charEndIndex();
            final int charLength = ctx.charLength();
            final int begOffset = indexOfLine(fullText, 2) + 13;
            final int endOffset = indexOfLine(fullText, 4) + 2;
            assertEquals(begOffset, charIndex);
            assertEquals(endOffset, charEndIndex);
            assertEquals(endOffset - begOffset, charLength);
        };
        CountDownLatch await = new CountDownLatch(1);
        Throwable[] err = { null };
        registerIn.execute(() -> {
            try {
                agentAPI.on("enter", listener, InsightObjectFactory.createConfig(false, false, true, null, null));
            } catch (Throwable t) {
                err[0] = t;
            } finally {
                await.countDown();
            }
        });
        await.await(10, TimeUnit.SECONDS);
        if (err[0] != null) {
            throw err[0];
        }
        // @formatter:off
        Source runScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  CALL(foo)\n" + ")", "run.px").build();
        // @formatter:on
        c.eval(runScript);
        assertEquals("Function foo has been called", "foo", functionName[0]);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) CountDownLatch(java.util.concurrent.CountDownLatch) Source(org.graalvm.polyglot.Source)

Example 4 with InsightObjectFactory.createConfig

use of org.graalvm.tools.insight.test.InsightObjectFactory.createConfig in project graal by oracle.

the class InsightObjectTest method accessFrameVariables.

@Test
public void accessFrameVariables() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(do_mul,\n" + "    ARGUMENT(a),\n" + "    ARGUMENT(b),\n" + "    EXPRESSION\n" + "  ),\n" + "  DEFINE(mul,\n" + "    ARGUMENT(a),\n" + "    ARGUMENT(b),\n" + "    CALL_WITH(do_mul, 1, READ_VAR(a), READ_VAR(b))\n" + "  ),\n" + "  CALL(mul, CONSTANT(6), CONSTANT(7))\n" + ")", "sample.px").build();
        // @formatter:on
        Set<String> names = new TreeSet<>();
        final InsightAPI.OnEventHandler captureNames = (ctx, frame) -> {
            assertTrue(names.isEmpty());
            names.addAll(frame.keySet());
        };
        agentAPI.on("enter", captureNames, createConfig(true, false, false, "do_mul.*", null));
        c.eval(sampleScript);
        agentAPI.off("enter", captureNames);
        Assert.assertArrayEquals("THIS, a and b found", new Object[] { "THIS", "a", "b" }, names.toArray());
        Object[] values = { 0, 0, 0 };
        agentAPI.on("enter", (ctx, frame) -> {
            values[0] = frame.get("a");
            values[1] = frame.get("b");
            frame.put("a", 33);
            assertEquals(Integer.valueOf(33), frame.get("a"));
            frame.put("a", "ahoj");
            assertEquals("ahoj", frame.get("a"));
            try {
                frame.put("c", 42);
                fail("Expecting an exception when setting unknown variable c");
            } catch (IllegalArgumentException t) {
                if (!t.getMessage().contains("identifier 'c'")) {
                    fail(t.getMessage());
                }
            }
            values[2] = frame.get("THIS");
            try {
                frame.put("THIS", 42);
                fail("Expecting an exception when setting THIS");
            } catch (IllegalArgumentException t) {
                if (!t.getMessage().contains("identifier 'THIS'")) {
                    fail(t.getMessage());
                }
            }
        }, InsightObjectFactory.createConfig(true, false, false, "do_mul", null));
        Value mul = c.getBindings(InstrumentationTestLanguage.ID).getMember("mul");
        assertNotNull("mul function found", mul);
        assertTrue("mul function found", mul.canExecute());
        Random r = new Random();
        for (int i = 1; i <= 100000; i++) {
            int a = r.nextInt();
            int b = r.nextInt();
            mul.execute(a, b);
            assertEquals(i + "th: a has been read", a, values[0]);
            assertEquals(i + "th: b has been read", b, values[1]);
            assertEquals(i + "th: THIS has been read", 1, values[2]);
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Source(org.graalvm.polyglot.Source) Random(java.util.Random) TreeSet(java.util.TreeSet) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)4 InstrumentationTestLanguage (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage)4 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)4 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Map (java.util.Map)4 Random (java.util.Random)4 Set (java.util.Set)4 TreeSet (java.util.TreeSet)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Executor (java.util.concurrent.Executor)4 Executors (java.util.concurrent.Executors)4 TimeUnit (java.util.concurrent.TimeUnit)4 Function (java.util.function.Function)4