Search in sources :

Example 16 with PolyglotException

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

the class SLParseErrorTest method testParseError.

@Test
public void testParseError() throws IOException {
    try {
        final Source src = Source.newBuilder("sl", "function testSyntaxError(a) {break;} function main() {return testSyntaxError;}", "testSyntaxError.sl").build();
        context.eval(src);
        Assert.assertTrue("Should not reach here.", false);
    } catch (PolyglotException e) {
        Assert.assertTrue("Should be a syntax error.", e.isSyntaxError());
        Assert.assertNotNull("Should have source section.", e.getSourceLocation());
    }
}
Also used : PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 17 with PolyglotException

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

the class SLTCKLanguageProvider method createExpressions.

@Override
public Collection<? extends Snippet> createExpressions(Context context) {
    final Collection<Snippet> res = new ArrayList<>();
    final Value fnc = eval(context, String.format(PATTERN_BIN_OP_FNC, "add", "+"), "add");
    Snippet.Builder opb = Snippet.newBuilder("+", fnc, TypeDescriptor.NUMBER).parameterTypes(TypeDescriptor.NUMBER, TypeDescriptor.NUMBER);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.STRING, TypeDescriptor.ANY);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, TypeDescriptor.STRING);
    res.add(opb.build());
    res.add(createBinaryOperator(context, "-", "sub", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "*", "mul", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "/", "div", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).resultVerifier((snippetRun) -> {
        final Value dividend = snippetRun.getParameters().get(0);
        final Value divider = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (dividend.isNumber() && divider.fitsInDouble() && divider.asDouble() == 0) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "==", "eq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "!=", "neq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "<=", "le", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">=", "ge", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "<", "l", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">", "g", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "||", "or", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && !firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "&&", "land", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createPostfixOperator(context, "()", "callNoArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY)).build());
    res.add(createPostfixOperator(context, "(1)", "callOneArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER)).build());
    res.add(createPostfixOperator(context, "(1, \"\")", "callTwoArgs", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER, TypeDescriptor.STRING)).build());
    return Collections.unmodifiableCollection(res);
}
Also used : ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) Snippet(org.graalvm.polyglot.tck.Snippet) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 18 with PolyglotException

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

the class SLExceptionTest method testProxyGuestLanguageStack.

@Test
public void testProxyGuestLanguageStack() {
    Value bar = ctx.eval("sl", "function foo(f) { f(); } function bar(f) { return foo(f); } function main() { return bar; }");
    TestProxy proxy = new TestProxy(3, bar);
    try {
        bar.execute(proxy);
        fail();
    } catch (PolyglotException e) {
        assertProxyException(proxy, e);
        for (PolyglotException seenException : proxy.seenExceptions) {
            // exceptions are unwrapped and wrapped again
            assertNotSame(e, seenException);
            assertSame(e.asHostException(), seenException.asHostException());
        }
    }
}
Also used : Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 19 with PolyglotException

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

the class SLTestRunner method run.

private static void run(Context context, Path path, PrintWriter out) throws IOException {
    try {
        /* Parse the SL source file. */
        Source source = Source.newBuilder(SLLanguage.ID, path.toFile()).interactive(true).build();
        /* Call the main entry point, without any arguments. */
        context.eval(source);
    } catch (PolyglotException ex) {
        if (!ex.isInternalError()) {
            out.println(ex.getMessage());
        } else {
            throw ex;
        }
    }
}
Also used : PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source)

Example 20 with PolyglotException

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

the class UnwindReenterReturnTest method testReenterSimpleOnException.

@Test
public void testReenterSimpleOnException() throws Exception {
    List<CodeAction> actionsExc = new ArrayList<>();
    List<CodeAction> actionsUnwind = new ArrayList<>();
    // Throw unwind from return exception and do reenter:
    actionsExc.add(new CodeAction("CALL(a)", TestControlFlow.ACTION.UNWIND));
    testControlFlow.actions.put(TestControlFlow.WHERE.RETURN_EXCEPTIONAL, actionsExc);
    actionsUnwind.add(new CodeAction("CALL(a)", TestControlFlow.ACTION.REENTER));
    testControlFlow.actions.put(TestControlFlow.WHERE.UNWIND, actionsUnwind);
    String message = null;
    try {
        run(CODE1_EXC);
        fail();
    } catch (PolyglotException pe) {
        message = pe.getMessage();
        assertTrue(message, message.contains("Identifier redefinition not supported"));
    }
    assertEquals(0, actionsExc.size());
    assertEquals(0, actionsUnwind.size());
    assertEquals("[CALL(a), CALL(a)]", testControlFlow.nodesEntered.toString());
    assertEquals("[CALL(a), CALL(a)]", testControlFlow.nodesReturned.toString());
    assertEquals("[" + message + ", " + message + "]", testControlFlow.returnValuesExceptions.toString());
    assertEquals("[CALL(a)]", testControlFlow.nodesUnwound.toString());
    assertEquals("[null]", testControlFlow.unwoundInfos.toString());
}
Also used : CodeAction(com.oracle.truffle.api.instrumentation.test.UnwindReenterReturnTest.TestControlFlow.CodeAction) ArrayList(java.util.ArrayList) 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