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());
}
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));
}
}
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));
}
}
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());
}
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());
}
Aggregations