use of org.graalvm.polyglot.Value 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);
}
}
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLJavaInteropTest method sumPairsInArrayOfArray.
@Test
public void sumPairsInArrayOfArray() {
String scriptText = //
"function values(sum, arr) {\n" + //
" sum.sumArrayArray(arr);\n" + //
"}\n";
context.eval("sl", scriptText);
Value fn = lookup("values");
Sum javaSum = new Sum();
PairImpl[][] arr = { new PairImpl[] { new PairImpl("one", 1) }, new PairImpl[] { new PairImpl("two", 2), new PairImpl("three", 3) } };
fn.execute(javaSum, arr);
assertEquals(6, javaSum.sum);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLJavaInteropTest method asFunctionWithVarArgs.
@Test
public void asFunctionWithVarArgs() throws Exception {
String scriptText = //
"function values(a, b) {\n" + //
" println(\"Called with \" + a + \" and \" + b);\n" + //
"}\n";
context.eval("sl", scriptText);
Value fn = lookup("values");
PassInVarArg valuesIn = fn.as(PassInVarArg.class);
valuesIn.call("OK", "Fine");
assertEquals("Called with OK and Fine\n", os.toString("UTF-8"));
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLJavaInteropTest method sumPairs.
@Test
public void sumPairs() {
String scriptText = //
"function values(sum, k, v) {\n" + //
" obj = new();\n" + //
" obj.key = k;\n" + //
" obj.value = v;\n" + //
" return sum.sum(obj);\n" + //
"}\n";
context.eval("sl", scriptText);
Value fn = lookup("values");
Sum javaSum = new Sum();
Object sum = javaSum;
Object ret1 = fn.execute(sum, "one", 1).asHostObject();
Object ret2 = fn.execute(sum, "two", 2).as(Object.class);
Sum ret3 = fn.execute(sum, "three", 3).as(Sum.class);
assertEquals(6, javaSum.sum);
assertSame(ret1, ret2);
assertSame(ret3, ret2);
assertSame(sum, ret2);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLJavaInteropTest method asFunction.
@Test
public void asFunction() throws Exception {
String scriptText = "function test() {\n" + " println(\"Called!\");\n" + "}\n";
context.eval("sl", scriptText);
Value main = lookup("test");
Runnable runnable = main.as(Runnable.class);
runnable.run();
assertEquals("Called!\n", os.toString("UTF-8"));
}
Aggregations