Search in sources :

Example 81 with Value

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

the class SLInteropPrimitiveTest method testBoolean.

@Test
public void testBoolean() throws IOException {
    final Source src = Source.newBuilder("sl", "function testBoolean(a,b) {return a == b;} function main() {return testBoolean;}", "testBoolean.sl").build();
    final Value fnc = context.eval(src);
    Assert.assertTrue(fnc.canExecute());
    fnc.execute(true, false);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 82 with Value

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

the class SLJavaInteropConversionTest method testGR7318Object.

@Test
public void testGR7318Object() throws Exception {
    String sourceText = "function test(validator) {\n" + "  obj = new();\n" + "  obj.a = new();\n" + "  obj.b = new();\n" + "  return validator.validateObject(obj, obj);\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");
        Value res = test.execute(new Validator());
        assertTrue(res.isNumber() && res.asInt() == 42);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 83 with Value

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

the class SLJavaInteropConversionTest method testGR7318Map.

@Test
public void testGR7318Map() throws Exception {
    String sourceText = "function test(validator) {\n" + "  obj = new();\n" + "  obj.a = new();\n" + "  obj.b = new();\n" + "  return validator.validateMap(obj, obj);\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");
        Value res = test.execute(new Validator());
        assertTrue(res.isNumber() && res.asInt() == 42);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 84 with Value

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

the class SLJavaInteropExceptionTest method testFunctionProxy.

@Test
public void testFunctionProxy() throws Exception {
    String javaMethod = "validateFunction";
    String sourceText = "" + "function supplier() {\n" + "  return error();\n" + "}\n" + "function test(validator) {\n" + "  return validator." + javaMethod + "(supplier);\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) {
            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);
        }
    }
}
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 85 with Value

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

the class SLJavaInteropExceptionTest method testTruffleMap.

@Test
public void testTruffleMap() throws Exception {
    String javaMethod = "validateMap";
    String sourceText = "" + "function test(validator) {\n" + "  return validator." + javaMethod + "(new());\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");
        test.execute(new Validator());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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