Search in sources :

Example 31 with PolyglotException

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

the class IdentityFunctionTest method createIdentitySnippet.

static Snippet createIdentitySnippet(String lang) {
    LanguageProvider tli = context.getInstalledProviders().get(lang);
    Value value = tli.createIdentityFunction(context.getContext());
    if (!value.canExecute()) {
        throw new AssertionError(String.format("Result of createIdentityFunction for tck provider %s did not return an executable value. Returned value '%s'.", lang, value));
    }
    return (Snippet.newBuilder("identity", tli.createIdentityFunction(context.getContext()), ANY).parameterTypes(ANY).resultVerifier(new ResultVerifier() {

        public void accept(SnippetRun snippetRun) throws PolyglotException {
            final PolyglotException exception = snippetRun.getException();
            if (exception != null) {
                throw exception;
            }
            Value parameter = snippetRun.getParameters().get(0);
            TypeDescriptor parameterType = TypeDescriptor.forValue(parameter);
            TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
            if (!resultType.isAssignable(parameterType) || !resultType.isAssignable(resultType)) {
                throw new AssertionError(String.format("Identity function return type must parameter type. Expected %s got %s.", parameterType, resultType));
            }
        }
    }).build());
}
Also used : LanguageProvider(org.graalvm.polyglot.tck.LanguageProvider) ResultVerifier(org.graalvm.polyglot.tck.ResultVerifier) TypeDescriptor(org.graalvm.polyglot.tck.TypeDescriptor) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 32 with PolyglotException

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

the class InlineExecutionTest method testInline.

@Test
public void testInline() throws Exception {
    Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
    boolean success = false;
    InlineSnippet inlineSnippet = testRun.getInlineSnippet();
    TestResultVerifier verifier;
    if (testRun.getInlineSnippet().getResultVerifier() != null) {
        verifier = new TestResultVerifier();
    } else {
        verifier = null;
    }
    context.getContext().initialize(testRun.getID());
    context.setInlineSnippet(testRun.getID(), inlineSnippet, verifier);
    try {
        try {
            final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
            TestUtil.validateResult(testRun, result, null);
            success = true;
        } catch (PolyglotException pe) {
            TestUtil.validateResult(testRun, null, pe);
            success = true;
        }
        if (verifier != null && verifier.exception != null) {
            success = false;
            throw verifier.exception;
        }
    } finally {
        context.setInlineSnippet(null, null, null);
        TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
    }
}
Also used : AbstractMap(java.util.AbstractMap) InlineSnippet(org.graalvm.polyglot.tck.InlineSnippet) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 33 with PolyglotException

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

the class StatementTest method testStatement.

@Test
public void testStatement() {
    Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
    boolean success = false;
    try {
        try {
            final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
            TestUtil.validateResult(testRun, result, null);
            success = true;
        } catch (PolyglotException pe) {
            TestUtil.validateResult(testRun, null, pe);
            success = true;
        }
    } finally {
        TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
    }
}
Also used : AbstractMap(java.util.AbstractMap) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 34 with PolyglotException

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

the class UnwindReenterReturnTest method testUnwindReenterOnException.

@Test
public void testUnwindReenterOnException() throws Exception {
    List<CodeAction> actionsUnwind = new ArrayList<>();
    List<CodeAction> actionsReenter = new ArrayList<>();
    // Unwind at DEFINE() and Reenter after pop from CALL(a):
    actionsUnwind.add(new CodeAction("STATEMENT(DEFINE(a, ROOT()))", TestControlFlow.ACTION.UNWIND, "3uw"));
    actionsReenter.add(new CodeAction("CALL(a)", TestControlFlow.ACTION.REENTER));
    testControlFlow.actions.put(TestControlFlow.WHERE.RETURN_EXCEPTIONAL, actionsUnwind);
    testControlFlow.actions.put(TestControlFlow.WHERE.UNWIND, actionsReenter);
    String message = null;
    try {
        run(CODE2_EXC);
        fail();
    } catch (PolyglotException pe) {
        message = pe.getMessage();
        assertTrue(message, message.contains("Identifier redefinition not supported"));
    }
    assertEquals(0, actionsUnwind.size());
    assertEquals(0, actionsReenter.size());
    assertEquals("[CALL(a), CALL(b), STATEMENT(DEFINE(a, ROOT())), CALL(a), CALL(b), STATEMENT(DEFINE(a, ROOT()))]", testControlFlow.nodesEntered.toString());
    assertEquals("[STATEMENT(DEFINE(a, ROOT())), CALL(b), CALL(a), STATEMENT(DEFINE(a, ROOT())), CALL(b), CALL(a)]", testControlFlow.nodesReturned.toString());
    String uw = "com.oracle.truffle.api.instrumentation.UnwindException";
    assertEquals("[" + message + ", " + uw + ", " + uw + ", " + message + ", " + message + ", " + message + "]", testControlFlow.returnValuesExceptions.toString());
    assertEquals("[STATEMENT(DEFINE(a, ROOT())), CALL(b), CALL(a)]", testControlFlow.nodesUnwound.toString());
    assertEquals("[3uw, 3uw, 3uw]", 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)

Example 35 with PolyglotException

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

the class AllocationReporterTest method testFailedAllocations.

@Test
public void testFailedAllocations() {
    // failed allocation
    Source source = Source.create(AllocationReporterLanguage.ID, "CanNotAllocateThisValue");
    AtomicInteger consumerCalls = new AtomicInteger(0);
    allocation.setAllocationConsumers((info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
    });
    try {
        context.eval(source);
        fail();
    } catch (PolyglotException ex) {
        // O.K.
        assertTrue(ex.getMessage(), ex.getMessage().contains("NumberFormatException"));
    }
    assertEquals(1, consumerCalls.get());
    consumerCalls.set(0);
    // too big allocation
    source = Source.create(AllocationReporterLanguage.ID, "12345678901234");
    allocation.setAllocationConsumers((info) -> {
        assertTrue(info.will);
        assertNull(info.value);
        assertEquals(0, info.oldSize);
        assertEquals(8, info.newSize);
        consumerCalls.incrementAndGet();
        throw new OutOfMemoryError("Denied allocation of 8 bytes.");
    }, (info) -> {
        assertFalse(info.will);
        assertNull(info.value);
        consumerCalls.incrementAndGet();
    });
    try {
        context.eval(source);
        fail();
    } catch (PolyglotException ex) {
        // O.K.
        assertTrue(ex.getMessage(), ex.getMessage().contains("OutOfMemoryError") && ex.getMessage().contains("Denied allocation of 8 bytes."));
    }
    assertEquals(1, consumerCalls.get());
    consumerCalls.set(0);
    // Too big reallocation
    source = Source.create(AllocationReporterLanguage.ID, "12345678901234->9876758023873465783492873465784938746502897345634897856");
    allocation.setAllocationConsumers((info) -> {
        assertTrue(info.will);
        assertEquals(12345678901234L, info.value);
        assertEquals(8, info.oldSize);
        assertEquals(AllocationReporter.SIZE_UNKNOWN, info.newSize);
        consumerCalls.incrementAndGet();
        throw new OutOfMemoryError("Denied an unknown reallocation.");
    }, (info) -> {
        consumerCalls.incrementAndGet();
    });
    try {
        context.eval(source);
        fail();
    } catch (PolyglotException ex) {
        // O.K.
        assertTrue(ex.getMessage(), ex.getMessage().contains("OutOfMemoryError") && ex.getMessage().contains("Denied an unknown reallocation."));
    }
    assertEquals(1, consumerCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) 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