use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterOverloads.
@Test
public void testConverterOverloads() {
HostAccess.Builder builder;
Value methods;
// test single method
builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
setupEnv(builder);
methods = context.asValue(new SingleMethod());
assertEquals(TargetClass1.class, methods.invokeMember("m", "42").asHostObject().getClass());
// should fail
try {
methods.invokeMember("m", "43");
fail();
} catch (IllegalArgumentException e) {
}
// should work
TargetClass1 o = methods.invokeMember("m", "42").asHostObject();
assertEquals("42", o.o);
// test non ambiguous methods
builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
builder.targetTypeMapping(String.class, TargetClass2.class, (v) -> v.equals("43"), (v) -> new TargetClass2(v));
setupEnv(builder);
methods = context.asValue(new MultiMethod1());
assertEquals(TargetClass1.class, methods.invokeMember("m", "42").asHostObject().getClass());
assertEquals(TargetClass2.class, methods.invokeMember("m", "43").asHostObject().getClass());
// test ambiguous methods
builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
builder.targetTypeMapping(String.class, TargetClass2.class, (v) -> v.equals("42"), (v) -> new TargetClass2(v));
setupEnv(builder);
methods = context.asValue(new MultiMethod1());
try {
methods.invokeMember("m", "42");
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("Multiple applicable overloads found"));
}
builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, null, (v) -> new TargetClass1(v));
setupEnv(builder);
methods = context.asValue(new MultiMethod2());
assertEquals("42", methods.invokeMember("m", "42", "42").asString());
assertEquals(42, methods.invokeMember("m", "42", 42).asInt());
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testAcceptsError.
@Test
public void testAcceptsError() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowPublicAccess(true);
final IllegalArgumentException error = new IllegalArgumentException();
Predicate<Integer> errorAccepts = new Predicate<>() {
public boolean test(Integer t) {
error.initCause(new RuntimeException());
throw error;
}
};
builder.targetTypeMapping(Integer.class, String.class, errorAccepts, (v) -> {
throw new AssertionError();
});
setupEnv(builder);
Value v = context.asValue(42);
try {
v.as(String.class);
fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertSame(error, e.asHostException());
}
}
Aggregations