use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testMappingInvocations.
@Test
public void testMappingInvocations() {
HostAccess.Builder builder = HostAccess.newBuilder();
CountingPredicate accepts0 = new CountingPredicate("42");
CountingConverter converter0 = new CountingConverter();
builder.targetTypeMapping(Value.class, String.class, accepts0, converter0);
CountingPredicate accepts1 = new CountingPredicate("43");
CountingConverter converter1 = new CountingConverter();
builder.targetTypeMapping(Value.class, String.class, accepts1, converter1);
setupEnv(builder);
context.asValue("42").as(String.class);
assertEquals(1, accepts0.count);
assertEquals(1, converter0.count);
assertEquals(0, accepts1.count);
assertEquals(0, converter1.count);
context.asValue("43").as(String.class);
assertEquals(2, accepts0.count);
assertEquals(1, converter0.count);
assertEquals(1, accepts1.count);
assertEquals(1, converter1.count);
context.asValue("44").as(String.class);
assertEquals(3, accepts0.count);
assertEquals(1, converter0.count);
assertEquals(2, accepts1.count);
assertEquals(1, converter1.count);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testPassNullValue.
@Test
public void testPassNullValue() {
HostAccess.Builder builder = HostAccess.newBuilder();
AtomicInteger invoked = new AtomicInteger();
builder.targetTypeMapping(Value.class, Integer.class, (v) -> {
assertTrue(v.isNull());
invoked.incrementAndGet();
return v.fitsInInt();
}, (v) -> {
throw new AssertionError();
});
setupEnv(builder);
assertNull(context.asValue(null).as(Integer.class));
assertEquals(1, invoked.get());
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterProxy.
@Test
public void testConverterProxy() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v != null && v.equals("42"), (v) -> new TargetClass1(v));
builder.targetTypeMapping(TargetClass1.class, String.class, (v) -> v != null && v.o.equals("42"), (v) -> (String) v.o);
setupEnv(builder);
ConverterMapTestObject testObj = new ConverterMapTestObject("42", "42");
ConverterProxy map = context.asValue(testObj).as(ConverterProxy.class);
assertEquals("42", map.f0().o);
assertEquals("42", map.f1().o);
assertEquals("42", testObj.f0);
map = context.asValue(new ConverterMapTestObject("42", "43")).as(ConverterProxy.class);
assertEquals("42", map.f0().o);
try {
map.f1();
fail();
} catch (ClassCastException e) {
assertTrue(e.getMessage(), e.getMessage().startsWith("Cannot convert '43'"));
}
builder = HostAccess.newBuilder();
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.startsWith("42"), (v) -> new TargetClass1(v));
setupEnv(builder);
map = context.asValue(new ConverterMapTestObject("42", "422")).as(ConverterProxy.class);
assertEquals("42", map.f0().o);
assertEquals("422", map.f1().o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testRecursion.
/*
* Test for GR-15593.
*/
@Test
@Ignore
public void testRecursion() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.targetTypeMapping(Value.class, Integer.class, (v) -> {
return true;
}, (v) -> {
return v.as(Integer.class);
});
setupEnv(builder);
try {
assertNull(context.asValue(42).as(Integer.class));
fail();
} catch (PolyglotException e) {
// which type of exception ends up here depends on where the stack overflow happens
if (e.isGuestException()) {
assertTrue(e.isInternalError());
} else {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof StackOverflowError);
}
}
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterError.
@Test
public void testConverterError() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowPublicAccess(true);
final IllegalArgumentException error = new IllegalArgumentException();
builder.targetTypeMapping(Integer.class, String.class, (v) -> v.equals(42), (v) -> {
throw error;
});
setupEnv(builder);
Value v = context.asValue(42);
try {
v.as(String.class);
fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertSame(error, e.asHostException());
}
}
Aggregations