Search in sources :

Example 76 with Context

use of org.graalvm.polyglot.Context 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 77 with Context

use of org.graalvm.polyglot.Context 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 78 with Context

use of org.graalvm.polyglot.Context 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 79 with Context

use of org.graalvm.polyglot.Context 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)

Example 80 with Context

use of org.graalvm.polyglot.Context 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)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12