use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLJavaInteropConversionTest method testGR7318Object.
@Test
public void testGR7318Object() throws Exception {
String sourceText = "function test(validator) {\n" + " obj = new();\n" + " obj.a = new();\n" + " obj.b = new();\n" + " return validator.validateObject(obj, obj);\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");
Value res = test.execute(new Validator());
assertTrue(res.isNumber() && res.asInt() == 42);
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLJavaInteropConversionTest method testGR7318Map.
@Test
public void testGR7318Map() throws Exception {
String sourceText = "function test(validator) {\n" + " obj = new();\n" + " obj.a = new();\n" + " obj.b = new();\n" + " return validator.validateMap(obj, obj);\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");
Value res = test.execute(new Validator());
assertTrue(res.isNumber() && res.asInt() == 42);
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLJavaInteropExceptionTest method testFunctionProxy.
@Test
public void testFunctionProxy() throws Exception {
String javaMethod = "validateFunction";
String sourceText = "" + "function supplier() {\n" + " return error();\n" + "}\n" + "function test(validator) {\n" + " return validator." + javaMethod + "(supplier);\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) {
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);
}
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLJavaInteropExceptionTest method testTruffleMap.
@Test
public void testTruffleMap() throws Exception {
String javaMethod = "validateMap";
String sourceText = "" + "function test(validator) {\n" + " return validator." + javaMethod + "(new());\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");
test.execute(new Validator());
}
}
use of org.graalvm.polyglot.Context 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);
}
}
}
Aggregations