org.graalvm.polyglot.PolyglotException Java Examples
The following examples show how to use
org.graalvm.polyglot.PolyglotException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
private void assertException(boolean failImmediately, String source, String... expectedFrames) { boolean initialExecute = true; try { Value value = ctx.eval("hashemi", source); initialExecute = false; if (failImmediately) { Assert.fail("Should not reach here."); } ProxyExecutable proxy = (args) -> args[0].execute(); value.execute(proxy); Assert.fail("Should not reach here."); } catch (PolyglotException e) { Assert.assertEquals(failImmediately, initialExecute); assertFrames(failImmediately, e, expectedFrames); } }
Example #2
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
private static void assertFrames(boolean isEval, PolyglotException e, String... expectedFrames) { int i = 0; boolean firstHostFrame = false; // Expected exception for (StackFrame frame : e.getPolyglotStackTrace()) { if (i < expectedFrames.length && expectedFrames[i] != null) { Assert.assertTrue(frame.isGuestFrame()); Assert.assertEquals("hashemi", frame.getLanguage().getId()); Assert.assertEquals(expectedFrames[i], frame.getRootName()); Assert.assertTrue(frame.getSourceLocation() != null); firstHostFrame = true; } else { Assert.assertTrue(frame.isHostFrame()); if (firstHostFrame) { Assert.assertEquals(isEval ? "org.graalvm.polyglot.Context.eval" : "org.graalvm.polyglot.Value.execute", frame.getRootName()); firstHostFrame = false; } } i++; } }
Example #3
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
private void assertHostException(String source, String... expectedFrames) { boolean initialExecute = true; RuntimeException[] exception = new RuntimeException[1]; try { Value value = ctx.eval("hashemi", source); initialExecute = false; ProxyExecutable proxy = (args) -> { throw exception[0] = new RuntimeException(); }; value.execute(proxy); Assert.fail("Should not reach here."); } catch (PolyglotException e) { Assert.assertFalse(initialExecute); Assert.assertTrue(e.asHostException() == exception[0]); assertFrames(false, e, expectedFrames); } }
Example #4
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
@Test public void testGuestLanguageError() { try { String source = "bebin bar() { x = 1 / \"asdf\"; }\n" + "bebin foo() { bede bar(); }\n" + "bebin azinja() { foo(); }"; ctx.eval(Source.newBuilder("hashemi", source, "script.hashemi").buildLiteral()); fail(); } catch (PolyglotException e) { assertTrue(e.isGuestException()); Iterator<StackFrame> frames = e.getPolyglotStackTrace().iterator(); assertGuestFrame(frames, "hashemi", "bar", "script.hashemi", 18, 28); assertGuestFrame(frames, "hashemi", "foo", "script.hashemi", 51, 56); assertGuestFrame(frames, "hashemi", "azinja", "script.hashemi", 77, 82); assertHostFrame(frames, Context.class.getName(), "eval"); assertHostFrame(frames, HashemExceptionTest.class.getName(), "testGuestLanguageError"); // only host frames trailing while (frames.hasNext()) { assertTrue(frames.next().isHostFrame()); } } }
Example #5
Source File: JavaScriptUserDefinedFunction.java From crate with Apache License 2.0 | 6 votes |
@Override public Object evaluate(TransactionContext txnCtx, Input<Object>[] args) { try { var function = resolvePolyglotFunctionValue(signature.getName().name(), script); var polyglotValueArgs = PolyglotValuesConverter.toPolyglotValues(args); return PolyglotValuesConverter.toCrateObject( function.execute(polyglotValueArgs), signature.getReturnType().createType()); } catch (PolyglotException | IOException e) { throw new io.crate.exceptions.ScriptException( e.getLocalizedMessage(), e, JavaScriptLanguage.NAME ); } }
Example #6
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
@Test public void testProxyGuestLanguageStack() { Value bar = ctx.eval("hashemi", "bebin foo(f) { f(); } bebin bar(f) { bede foo(f); } bebin azinja() { bede 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()); } } }
Example #7
Source File: JavaScriptUserDefinedFunction.java From crate with Apache License 2.0 | 6 votes |
@Override public Scalar<Object, Object> compile(List<Symbol> arguments) { try { return new CompiledFunction( resolvePolyglotFunctionValue( signature.getName().name(), script)); } catch (PolyglotException | IOException e) { // this should not happen if the script was validated upfront throw new io.crate.exceptions.ScriptException( "compile error", e, JavaScriptLanguage.NAME ); } }
Example #8
Source File: HashemJavaInteropExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
@Test public void testGR7284GuestHostGuestHost() throws Exception { String sourceText = "bebin test(validator) {\n" + " bede validator.validateNested();\n" + "}"; try (Context context = Context.newBuilder(HashemLanguage.ID).build()) { context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build()); Value test = context.getBindings(HashemLanguage.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); } } }
Example #9
Source File: HashemJavaInteropExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 6 votes |
@Test public void testGR7284() throws Exception { String sourceText = "bebin test(validator) {\n" + " bede validator.validateException();\n" + "}"; try (Context context = Context.newBuilder(HashemLanguage.ID).build()) { context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build()); Value test = context.getBindings(HashemLanguage.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); } } }
Example #10
Source File: DeviceArrayFreeTest.java From grcuda with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(expected = PolyglotException.class) public void testMultiDimDeviceArrayAccessAfterFreeThrows() { try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { // create DeviceArray Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); Value deviceArray = createDeviceArray.execute("int", 100, 100); deviceArray.invokeMember("free"); deviceArray.getArrayElement(0).setArrayElement(0, 42); // throws } }
Example #11
Source File: JavaScriptUserDefinedFunction.java From crate with Apache License 2.0 | 5 votes |
@Override public final Object evaluate(TransactionContext txnCtx, Input<Object>[] args) { var polyglotValueArgs = PolyglotValuesConverter.toPolyglotValues(args); try { return toCrateObject( function.execute(polyglotValueArgs), signature.getReturnType().createType()); } catch (PolyglotException e) { throw new io.crate.exceptions.ScriptException( e.getLocalizedMessage(), e, JavaScriptLanguage.NAME ); } }
Example #12
Source File: DeviceArrayFreeTest.java From grcuda with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(expected = PolyglotException.class) public void testMultiDimDeviceArrayDoubleFreeThrows() { try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { // create DeviceArray Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); Value deviceArray = createDeviceArray.execute("int", 100, 100); deviceArray.invokeMember("free"); deviceArray.invokeMember("free"); // throws } }
Example #13
Source File: GraalEngine.java From netbeans with Apache License 2.0 | 5 votes |
private Value evalImpl(ScriptContext arg1, String src) throws ScriptException { try { return ((GraalContext) arg1).ctx().eval(id(), src); } catch (PolyglotException e) { throw new ScriptException(e); } }
Example #14
Source File: GraalJavascriptEngine.java From graphviz-java with Apache License 2.0 | 5 votes |
@Override protected String execute(String js) { try { eval(js); return resultHandler.waitFor(); } catch (PolyglotException e) { throw new GraphvizException("Problem executing javascript", e); } }
Example #15
Source File: GraalScriptRunner.java From citeproc-java with Apache License 2.0 | 5 votes |
@Override public <T> T callMethod(String name, Class<T> resultType, Object... args) throws ScriptRunnerException { try { return convert(ctx.getBindings("js").getMember(name) .execute(convertArguments(args)), resultType); } catch (IOException | PolyglotException e) { throw new ScriptRunnerException("Could not call method", e); } }
Example #16
Source File: GraalScriptRunner.java From citeproc-java with Apache License 2.0 | 5 votes |
@Override public void callMethod(String name, Object... args) throws ScriptRunnerException { try { ctx.getBindings("js").getMember(name).executeVoid(convertArguments(args)); } catch (IOException | PolyglotException e) { throw new ScriptRunnerException("Could not call method", e); } }
Example #17
Source File: GraalScriptRunner.java From citeproc-java with Apache License 2.0 | 5 votes |
@Override public <T> T callMethod(Object obj, String name, Class<T> resultType, Object... args) throws ScriptRunnerException { try { return convert(((Value)obj).getMember(name) .execute(convertArguments(args)), resultType); } catch (IOException | PolyglotException e) { throw new ScriptRunnerException("Could not call method", e); } }
Example #18
Source File: GraalScriptRunner.java From citeproc-java with Apache License 2.0 | 5 votes |
@Override public void callMethod(Object obj, String name, Object... args) throws ScriptRunnerException { try { ((Value)obj).getMember(name).executeVoid(convertArguments(args)); } catch (IOException | PolyglotException e) { throw new ScriptRunnerException("Could not call method", e); } }
Example #19
Source File: JavaScriptLanguage.java From crate with Apache License 2.0 | 5 votes |
@Nullable public String validate(UserDefinedFunctionMetaData meta) { try { resolvePolyglotFunctionValue(meta.name(), meta.definition()); return null; } catch (IllegalArgumentException | IOException | PolyglotException t) { return String.format(Locale.ENGLISH, "Invalid JavaScript in function '%s.%s(%s)' AS '%s': %s", meta.schema(), meta.name(), meta.argumentTypes().stream().map(DataType::getName).collect(Collectors.joining(", ")), meta.definition(), t.getMessage() ); } }
Example #20
Source File: DeviceArrayFreeTest.java From grcuda with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(expected = PolyglotException.class) public void testDeviceArrayDoubleFreeThrows() { try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { // create DeviceArray Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); Value deviceArray = createDeviceArray.execute("int", 1000); deviceArray.invokeMember("free"); deviceArray.invokeMember("free"); // throws } }
Example #21
Source File: DeviceArrayFreeTest.java From grcuda with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(expected = PolyglotException.class) public void testDeviceArrayAccessAfterFreeThrows() { try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { // create DeviceArray Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); Value deviceArray = createDeviceArray.execute("int", 1000); deviceArray.invokeMember("free"); deviceArray.setArrayElement(0, 42); // throws } }
Example #22
Source File: HashemJavaInteropExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testFunctionProxy() throws Exception { String javaMethod = "validateFunction"; String sourceText = "" + "bebin supplier() {\n" + " bede error();\n" + "}\n" + "bebin test(validator) {\n" + " bede validator." + javaMethod + "(supplier);\n" + "}"; try (Context context = Context.newBuilder(HashemLanguage.ID).build()) { context.eval(Source.newBuilder(HashemLanguage.ID, sourceText, "Test").build()); Value test = context.getBindings(HashemLanguage.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); } } }
Example #23
Source File: HashemTestRunner.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
private static void run(Context context, Path path, PrintWriter out) throws IOException { try { /* Parse theHashemisource file. */ Source source = Source.newBuilder(HashemLanguage.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; } } }
Example #24
Source File: HashemParseErrorTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testParseErrorEOF2() { try { final Source src = Source.newBuilder("hashemi", "function\n", "testSyntaxErrorEOF2.hashem").buildLiteral(); 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()); } }
Example #25
Source File: HashemParseErrorTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testParseErrorEOF1() { try { final Source src = Source.newBuilder("hashemi", "bebin azinja", "testSyntaxErrorEOF1.hashem").buildLiteral(); 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()); } }
Example #26
Source File: HashemParseErrorTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testParseErrorEmpty() { try { final Source src = Source.newBuilder("hashemi", "", "testSyntaxErrorEmpty.hashem").buildLiteral(); 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()); } }
Example #27
Source File: HashemParseErrorTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testParseError() { try { final Source src = Source.newBuilder("hashemi", "bebin testSyntaxError(a) {break;} function main() {return testSyntaxError;}", "testSyntaxError.hashem").buildLiteral(); 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()); } }
Example #28
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
@Test public void testGuestOverHostPropagation() { Context context = Context.newBuilder("hashemi").allowAllAccess(true).build(); String code = "" + "bebin other(x) {" + " bede invalidFunction();" + "}" + "" + "bebin f(test) {" + "test.methodThatTakesFunction(other);" + "}"; context.eval("hashemi", code); try { context.getBindings("hashemi").getMember("f").execute(this); fail(); } catch (PolyglotException e) { assertFalse(e.isHostException()); assertTrue(e.isGuestException()); Iterator<StackFrame> frames = e.getPolyglotStackTrace().iterator(); assertTrue(frames.next().isGuestFrame()); assertGuestFrame(frames, "hashemi", "other", "Unnamed", 24, 41); assertHostFrame(frames, "com.oracle.truffle.polyglot.PolyglotFunction", "apply"); assertHostFrame(frames, "ninja.soroosh.hashem.lang.test.HashemExceptionTest", "methodThatTakesFunction"); assertGuestFrame(frames, "hashemi", "f", "Unnamed", 58, 93); // rest is just unit test host frames while (frames.hasNext()) { assertTrue(frames.next().isHostFrame()); } } }
Example #29
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
private static void assertProxyException(TestProxy proxy, PolyglotException e) { assertTrue(e.isHostException()); if (e.asHostException() instanceof AssertionError) { throw (AssertionError) e.asHostException(); } assertSame(proxy.thrownException, e.asHostException()); Iterator<StackFrame> frames = e.getPolyglotStackTrace().iterator(); assertHostFrame(frames, TestProxy.class.getName(), "execute"); for (int i = 0; i < 2; i++) { assertGuestFrame(frames, "hashemi", "foo", "Unnamed", 15, 18); assertGuestFrame(frames, "hashemi", "bar", "Unnamed", 42, 48); assertHostFrame(frames, Value.class.getName(), "execute"); assertHostFrame(frames, TestProxy.class.getName(), "execute"); } assertGuestFrame(frames, "hashemi", "foo", "Unnamed", 15, 18); assertGuestFrame(frames, "hashemi", "bar", "Unnamed", 42, 48); assertHostFrame(frames, Value.class.getName(), "execute"); assertHostFrame(frames, HashemExceptionTest.class.getName(), "testProxyGuestLanguageStack"); while (frames.hasNext()) { // skip unit test frames. assertTrue(frames.next().isHostFrame()); } }
Example #30
Source File: HashemExceptionTest.java From mr-hashemi with Universal Permissive License v1.0 | 5 votes |
public Object execute(Value... t) { depth--; if (depth > 0) { try { return f.execute(this); } catch (PolyglotException e) { assertProxyException(this, e); seenExceptions.add(e); throw e; } } else { thrownException = new RuntimeException("Error in proxy test."); throw thrownException; } }