Search in sources :

Example 11 with PolyglotException

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

the class DefaultResultVerifier method accept.

@Override
public void accept(final SnippetRun snippetRun) throws PolyglotException {
    final PolyglotException exception = snippetRun.getException();
    if (exception != null) {
        throw exception;
    }
    final TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
    final TypeDescriptor snippetReturnType = snippetRun.getSnippet().getReturnType();
    if (!snippetReturnType.isAssignable(resultType)) {
        throw new AssertionError(String.format("Result is out of type bounds. Expected: %s, Got: %s.", snippetReturnType, resultType));
    }
}
Also used : PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 12 with PolyglotException

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

the class SLInstrumentTest method testEarlyReturn.

/**
 * Test that we can forcibly return early from call nodes with an arbitrary value.
 */
@Test
public void testEarlyReturn() throws Exception {
    String code = "function main() {\n" + "  a = 10;\n" + "  b = a;\n" + "  // Let fce() warm up and specialize:\n" + "  while (a == b && a < 100000) {\n" + "    a = fce(a);\n" + "    b = b + 1;\n" + "  }\n" + "  c = a;\n" + "  // Run fce() and alter it's return type in an instrument:\n" + "  c = fce(c);\n" + "  return c;\n" + "}\n" + "function fce(x) {\n" + "  return x + 1;\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
    Engine engine = Engine.newBuilder().err(engineOut).build();
    Context context = Context.newBuilder().engine(engine).build();
    // No instrument:
    Value ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(100001L, ret.asLong());
    EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
    earlyReturn.fceCode = "fce(a)";
    earlyReturn.returnValue = 200000L;
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(200001L, ret.asLong());
    earlyReturn.returnValue = "Hello!";
    ret = context.eval(source);
    assertFalse(ret.isNumber());
    assertTrue(ret.isString());
    assertEquals("Hello!1", ret.asString());
    // Specialize to long again:
    earlyReturn.fceCode = "<>";
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(100001L, ret.asLong());
    earlyReturn.fceCode = "fce(a)";
    earlyReturn.returnValue = new BigInteger("-42");
    boolean interopFailure;
    try {
        context.eval(source);
        interopFailure = false;
    } catch (PolyglotException err) {
        interopFailure = true;
    }
    assertTrue(interopFailure);
    earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(-41L, ret.asLong());
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = Boolean.TRUE;
    ret = context.eval(source);
    assertTrue(ret.isBoolean());
    assertEquals(Boolean.TRUE, ret.asBoolean());
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = -42.42;
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(-42.42, ret.asDouble(), 1e-8);
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = "Hello!";
    ret = context.eval(source);
    assertTrue(ret.isString());
    assertEquals("Hello!", ret.asString());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) SLBigNumber(com.oracle.truffle.sl.runtime.SLBigNumber) Value(org.graalvm.polyglot.Value) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 13 with PolyglotException

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

the class SLJavaInteropExceptionTest method assertNoJavaInteropStackFrames.

@SuppressWarnings("deprecation")
private static void assertNoJavaInteropStackFrames(PolyglotException ex) {
    String javaInteropPackageName = com.oracle.truffle.api.interop.java.JavaInterop.class.getName().substring(0, com.oracle.truffle.api.interop.java.JavaInterop.class.getName().lastIndexOf('.') + 1);
    assertFalse("expected no java interop stack trace elements", Arrays.stream(ex.getStackTrace()).anyMatch(ste -> ste.getClassName().startsWith(javaInteropPackageName)));
}
Also used : Arrays(java.util.Arrays) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PolyglotException(org.graalvm.polyglot.PolyglotException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Value(org.graalvm.polyglot.Value) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Supplier(java.util.function.Supplier) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) SLLanguage(com.oracle.truffle.sl.SLLanguage) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) Source(org.graalvm.polyglot.Source) Assert.fail(org.junit.Assert.fail) Context(org.graalvm.polyglot.Context) NoSuchElementException(java.util.NoSuchElementException) Assert(org.junit.Assert) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 14 with PolyglotException

use of org.graalvm.polyglot.PolyglotException 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 15 with PolyglotException

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

PolyglotException (org.graalvm.polyglot.PolyglotException)43 Value (org.graalvm.polyglot.Value)32 Test (org.junit.Test)32 Context (org.graalvm.polyglot.Context)19 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)9 Source (org.graalvm.polyglot.Source)9 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)6 StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)6 AbstractMap (java.util.AbstractMap)5 TruffleContext (com.oracle.truffle.api.TruffleContext)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 Engine (org.graalvm.polyglot.Engine)4 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)4 Env (com.oracle.truffle.api.TruffleLanguage.Env)3 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)3 IOException (java.io.IOException)3 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 CodeAction (com.oracle.truffle.api.instrumentation.test.UnwindReenterReturnTest.TestControlFlow.CodeAction)2