Search in sources :

Example 86 with Value

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

the class SLJavaInteropExceptionTest method testGR7284.

@Test
public void testGR7284() throws Exception {
    String sourceText = "function test(validator) {\n" + "  return validator.validateException();\n" + "}";
    try (Context context = Context.newBuilder(SLLanguage.ID).build()) {
        context.eval(Source.newBuilder(SLLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(SLLanguage.ID).getMember("test");
        try {
            test.execute(new Validator());
            fail("expected a PolyglotException but did not throw");
        } catch (PolyglotException ex) {
            assertTrue("expected HostException", ex.isHostException());
            assertThat(ex.asHostException(), instanceOf(NoSuchElementException.class));
            assertNoJavaInteropStackFrames(ex);
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 87 with Value

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

the class SLJavaInteropTest method sumPairsInArrayOfArray.

@Test
public void sumPairsInArrayOfArray() {
    String scriptText = // 
    "function values(sum, arr) {\n" + // 
    "  sum.sumArrayArray(arr);\n" + // 
    "}\n";
    context.eval("sl", 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);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 88 with Value

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

the class SLJavaInteropTest method asFunctionWithVarArgs.

@Test
public void asFunctionWithVarArgs() throws Exception {
    String scriptText = // 
    "function values(a, b) {\n" + // 
    "  println(\"Called with \" + a + \" and \" + b);\n" + // 
    "}\n";
    context.eval("sl", scriptText);
    Value fn = lookup("values");
    PassInVarArg valuesIn = fn.as(PassInVarArg.class);
    valuesIn.call("OK", "Fine");
    assertEquals("Called with OK and Fine\n", os.toString("UTF-8"));
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 89 with Value

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

the class SLJavaInteropTest method sumPairs.

@Test
public void sumPairs() {
    String scriptText = // 
    "function values(sum, k, v) {\n" + // 
    "  obj = new();\n" + // 
    "  obj.key = k;\n" + // 
    "  obj.value = v;\n" + // 
    "  return sum.sum(obj);\n" + // 
    "}\n";
    context.eval("sl", scriptText);
    Value fn = lookup("values");
    Sum javaSum = new Sum();
    Object sum = javaSum;
    Object ret1 = fn.execute(sum, "one", 1).asHostObject();
    Object ret2 = fn.execute(sum, "two", 2).as(Object.class);
    Sum ret3 = fn.execute(sum, "three", 3).as(Sum.class);
    assertEquals(6, javaSum.sum);
    assertSame(ret1, ret2);
    assertSame(ret3, ret2);
    assertSame(sum, ret2);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 90 with Value

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

the class SLJavaInteropTest method asFunction.

@Test
public void asFunction() throws Exception {
    String scriptText = "function test() {\n" + "    println(\"Called!\");\n" + "}\n";
    context.eval("sl", scriptText);
    Value main = lookup("test");
    Runnable runnable = main.as(Runnable.class);
    runnable.run();
    assertEquals("Called!\n", os.toString("UTF-8"));
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9