Java Code Examples for org.graalvm.polyglot.Value#execute()

The following examples show how to use org.graalvm.polyglot.Value#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void sumPairsInArrayOfArray() {
    String scriptText = "bebin values(sum, arr) {\n" + //
                    "  sum.sumArrayArray(arr);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");

    Sum javaSum = new Sum();

    PairImpl[][] arr = {
                    new PairImpl[]{
                                    new PairImpl("one", 1),
                    },
                    new PairImpl[]{
                                    new PairImpl("two", 2),
                                    new PairImpl("three", 3),
                    }
    };
    fn.execute(javaSum, arr);
    assertEquals(6, javaSum.sum);
}
 
Example 2
Source File: HashemJavaInteropTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void sumPairInMapOfArray() {
    String scriptText = "bebin values(sum, arr) {\n" + //
                    "  sum.sumMapArray(arr);\n" + //
                    "}\n"; //
    context.eval("hashemi", scriptText);
    Value fn = lookup("values");

    Sum javaSum = new Sum();

    TwoPairsImpl groups = new TwoPairsImpl(
                    new PairImpl[]{
                                    new PairImpl("one", 1),
                    },
                    new PairImpl[]{
                                    new PairImpl("two", 2),
                                    new PairImpl("three", 3),
                    });
    fn.execute(javaSum, groups);
    assertEquals(6, javaSum.sum);
}
 
Example 3
Source File: DeviceArrayCopyFunctionTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDeviceArrayCopyToDeviceArray() {
    final int numElements = 1000;
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        // create device array initialize its elements.
        Value sourceDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            sourceDeviceArray.setArrayElement(i, i + 1);
        }
        // create destination device array initialize its elements to zero.
        Value destinationDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            destinationDeviceArray.setArrayElement(i, 0);
        }
        sourceDeviceArray.invokeMember("copyTo", destinationDeviceArray, numElements);
        // Verify content of device array
        for (int i = 0; i < numElements; ++i) {
            assertEquals(i + 1, destinationDeviceArray.getArrayElement(i).asInt());
        }
    }
}
 
Example 4
Source File: HashemExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
private void assertHostException(String source, String... expectedFrames) {
    boolean initialExecute = true;
    RuntimeException[] exception = new RuntimeException[1];
    try {
        Value value = ctx.eval("hashemi", source);
        initialExecute = false;
        ProxyExecutable proxy = (args) -> {
            throw exception[0] = new RuntimeException();
        };
        value.execute(proxy);
        Assert.fail("Should not reach here.");
    } catch (PolyglotException e) {
        Assert.assertFalse(initialExecute);
        Assert.assertTrue(e.asHostException() == exception[0]);
        assertFrames(false, e, expectedFrames);
    }
}
 
Example 5
Source File: GraalEngine.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
    if (!(thiz instanceof Value)) {
        throw new IllegalArgumentException();
    }
    final Value thisValue = (Value) thiz;
    Value fn = thisValue.getMember(name);
    if (!fn.canExecute()) {
        throw new NoSuchMethodException(name);
    }
    Value result = fn.execute(args);
    return unbox(result);
}
 
Example 6
Source File: HashemJavaInteropConversionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testGR7318List() throws Exception {
    String sourceText = "bebin test(validator, array) {\n" +
            "  array[0] = jadid();\n" +
            "  array[1] = jadid();\n" +
            "  bede validator.validateList(array, array);\n" +
            "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).allowHostAccess(HostAccess.ALL).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        Value res = test.execute(new Validator(), new Object[2]);
        assertTrue(res.isNumber() && res.asInt() == 42);
    }
}
 
Example 7
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testTruffleMap() throws Exception {
    String javaMethod = "validateMap";
    String sourceText = "" +
                    "bebin test(validator) {\n" +
                    "  bede validator." + javaMethod + "(jadid());\n" +
                    "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        test.execute(new Validator());
    }
}
 
Example 8
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testFunctionProxy() throws Exception {
    String javaMethod = "validateFunction";
    String sourceText = "" +
                    "bebin supplier() {\n" +
                    "  bede error();\n" +
                    "}\n" +
                    "bebin test(validator) {\n" +
                    "  bede validator." + javaMethod + "(supplier);\n" +
                    "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        try {
            test.execute(new Validator());
            fail("expected a PolyglotException but did not throw");
        } catch (PolyglotException ex) {
            StackTraceElement last = null;
            boolean found = false;
            for (StackTraceElement curr : ex.getStackTrace()) {
                if (curr.getMethodName().contains(javaMethod)) {
                    assertNotNull(last);
                    assertThat("expected Proxy stack frame", last.getClassName(), containsString("Proxy"));
                    found = true;
                    break;
                }
                last = curr;
            }
            assertTrue(javaMethod + " not found in stack trace", found);
        }
    }
}
 
Example 9
Source File: MultiDimArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void test2DimArrayOutOfBoundsOnWriteAccess() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        final int numDim1 = 19;
        final int numDim2 = 53;
        Value matrix = deviceArrayConstructor.execute("int", numDim1, numDim2);
        assertEquals(numDim1, matrix.getArraySize());
        assertEquals(numDim2, matrix.getArrayElement(0).getArraySize());
        // out-of-bounds write access
        matrix.getArrayElement(0).setArrayElement(53, 42);
    }
}
 
Example 10
Source File: DeviceArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDeviceArrayCreationFromDeviceArrayConstructor() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArrayFunc = context.eval("grcuda", "DeviceArray");
        Value deviceArray = deviceArrayFunc.execute(dataTypeString, arrayLength);
        assertTrue(deviceArray.hasArrayElements());
        assertEquals(arrayLength, deviceArray.getArraySize());
    }
}
 
Example 11
Source File: HashemJavaInteropExceptionTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@HostAccess.Export
public void validateNested() throws Exception {
    String sourceText = "bebin test(validator) {\n" +
                    "  bede validator.validateException();\n" +
                    "}";
    try (Context context = Context.newBuilder(HashemLanguage.ID).build()) {
        context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(HashemLanguage.ID).getMember("test");
        test.execute(Validator.this);
    }
}
 
Example 12
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = PolyglotException.class)
public void testDeviceArrayAccessAfterFreeThrows() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 1000);
        deviceArray.invokeMember("free");
        deviceArray.setArrayElement(0, 42); // throws
    }
}
 
Example 13
Source File: BuildKernelTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testBuildKernelwithNIDLSignature() {
    // See if inc_kernel can be built
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value buildkernel = context.eval("grcuda", "buildkernel");
        Value incrKernel = buildkernel.execute(INCREMENT_KERNEL_SOURCE, INCREMENT_KERNEL_NIDL_SIGNATURE);
        assertNotNull(incrKernel);
        assertTrue(incrKernel.canExecute());
        assertEquals(0, incrKernel.getMember("launchCount").asInt());
        assertNotNull(incrKernel.getMember("ptx").asString());
    }
}
 
Example 14
Source File: MultiDimArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test3DimArrayRowMajorFromConstructor() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        // 3-dimensional array through DeviceArray constructor (row-major)
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        final int numDim1 = 5;
        final int numDim2 = 3;
        final int numDim3 = 2;
        Value matrix = deviceArrayConstructor.execute("int", numDim1, numDim2, numDim3);
        assertEquals(numDim1, matrix.getArraySize());
        assertEquals(numDim2, matrix.getArrayElement(0).getArraySize());
        assertEquals(numDim3, matrix.getArrayElement(0).getArrayElement(0).getArraySize());
        for (int i = 0; i < numDim1; i++) {
            for (int j = 0; j < numDim2; j++) {
                for (int k = 0; k < numDim3; k++) {
                    matrix.getArrayElement(i).getArrayElement(j).setArrayElement(k, i * numDim3 * numDim2 + j * numDim3 + k);
                }
            }
        }
        for (int i = 0; i < numDim1; i++) {
            for (int j = 0; j < numDim2; j++) {
                for (int k = 0; k < numDim3; k++) {
                    assertEquals(i * numDim3 * numDim2 + j * numDim3 + k,
                                    matrix.getArrayElement(i).getArrayElement(j).getArrayElement(k).asInt());
                }
            }
        }
    }
}
 
Example 15
Source File: HashemInteropPrimitiveTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testChar() {
    final Source src = Source.newBuilder("hashemi", "bebin testChar(a,b) {bede a == b;} bebin azinja() {bede testChar;}", "testChar.hashem").buildLiteral();
    final Value fnc = context.eval(src);
    Assert.assertTrue(fnc.canExecute());
    fnc.execute('a', 'b');
}
 
Example 16
Source File: HashemInteropPrimitiveTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Test
public void testBoolean() {
    final Source src = Source.newBuilder("hashemi", "bebin testBoolean(a,b) {bede a == b;} bebin azinja() {bede testBoolean;}", "testBoolean.hashem").buildLiteral();
    final Value fnc = context.eval(src);
    Assert.assertTrue(fnc.canExecute());
    fnc.execute(true, false);
}
 
Example 17
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = PolyglotException.class)
public void testDeviceArrayDoubleFreeThrows() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 1000);
        deviceArray.invokeMember("free");
        deviceArray.invokeMember("free"); // throws
    }
}
 
Example 18
Source File: MultiDimArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test3DimArrayColMajorFromConstructor() {
    // 3-dimensional array through DeviceArray constructor (column-major)
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        final int numDim1 = 5;
        final int numDim2 = 3;
        final int numDim3 = 2;
        Value matrix = deviceArrayConstructor.execute("int", numDim1, numDim2, numDim3, "F");
        assertEquals(numDim1, matrix.getArraySize());
        assertEquals(numDim2, matrix.getArrayElement(0).getArraySize());
        assertEquals(numDim3, matrix.getArrayElement(0).getArrayElement(0).getArraySize());
        for (int i = 0; i < numDim1; i++) {
            for (int j = 0; j < numDim2; j++) {
                for (int k = 0; k < numDim3; k++) {
                    matrix.getArrayElement(i).getArrayElement(j).setArrayElement(k, i + j * numDim1 + k * numDim1 * numDim2);
                }
            }
        }
        for (int i = 0; i < numDim1; i++) {
            for (int j = 0; j < numDim2; j++) {
                for (int k = 0; k < numDim3; k++) {
                    assertEquals(i + j * numDim1 + k * numDim1 * numDim2,
                                    matrix.getArrayElement(i).getArrayElement(j).getArrayElement(k).asInt());
                }
            }
        }
    }
}
 
Example 19
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCanInvokeFreeMultiDimDeviceArray() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 100, 100);
        assertTrue(deviceArray.canInvokeMember("free"));
        deviceArray.invokeMember("free");
        // check that freed flag set
        assertTrue(deviceArray.hasMember("isMemoryFreed"));
        assertTrue(deviceArray.getMember("isMemoryFreed").asBoolean());
    }
}
 
Example 20
Source File: HashemDebugDirectTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Test
public void stepInStepOver() throws Throwable {
    final Source factorial = createFactorial();
    context.eval(factorial);

    session.suspendNextExecution();

    assertLocation("test", 2, true, "res = fac(2)", "res", UNASSIGNED);
    stepInto(1);
    assertLocation("fac", 7, true,
                    "n <= 1", "n",
                    "2", "nMinusOne",
                    UNASSIGNED, "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 10, true,
                    "nMinusOne = n - 1", "n",
                    "2", "nMinusOne",
                    UNASSIGNED, "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 11, true,
                    "nMOFact = fac(nMinusOne)", "n",
                    "2", "nMinusOne",
                    "1", "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 12, true,
                    "res = n * nMOFact", "n", "2", "nMinusOne",
                    "1", "nMOFact",
                    "1", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 13, true,
                    "bede res", "n",
                    "2", "nMinusOne",
                    "1", "nMOFact",
                    "1", "res", "2");
    stepOver(1);
    assertLocation("test", 2, false, "fac(2)", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("test", 3, true, "bechap(res)", "res", "2");
    stepOut();

    Value value = context.getBindings("hashemi").getMember("test");
    assertTrue(value.canExecute());
    Value resultValue = value.execute();
    String resultStr = resultValue.toString();
    Number result = resultValue.asInt();
    assertExecutedOK();

    assertNotNull(result);
    assertEquals("Factorial computed OK", 2, result.intValue());
    assertEquals("Factorial computed OK", "2", resultStr);
}