use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterList.
@Test
public void testConverterList() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowArrayAccess(true);
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
setupEnv(builder);
List<TargetClass1> list = context.asValue(new String[] { "42", "42" }).as(TARGET_CLASS_LIST);
assertEquals(2, list.size());
assertEquals("42", list.get(0).o);
assertEquals("42", list.get(1).o);
list = context.asValue(new String[] { "42", "43" }).as(TARGET_CLASS_LIST);
try {
list.get(1);
fail();
} catch (ClassCastException e) {
assertTrue(e.getMessage(), e.getMessage().startsWith("Cannot convert '43'"));
}
builder = HostAccess.newBuilder();
builder.allowArrayAccess(true);
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);
list = context.asValue(new String[] { "42", "422" }).as(TARGET_CLASS_LIST);
assertEquals(2, list.size());
assertEquals("42", list.get(0).o);
assertEquals("422", list.get(1).o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterClassCast.
@Test
public void testConverterClassCast() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowPublicAccess(true);
ClassCastException cce = new ClassCastException("foo");
builder.targetTypeMapping(Integer.class, String.class, (v) -> v.equals(42), (v) -> {
throw cce;
});
setupEnv(builder);
Value v = context.asValue(42);
try {
v.as(String.class);
fail();
} catch (ClassCastException e) {
assertEquals("foo", e.getMessage());
// we must not allow normal class cast exceptions to go through.
assertNotSame(e, cce);
}
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterFunctional.
@Test
public void testConverterFunctional() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowPublicAccess(true);
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);
Value identity = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return arguments[0];
}
});
ConverterFunction f = identity.as(ConverterFunction.class);
assertEquals("42", f.m("42").o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterArray.
@Test
public void testConverterArray() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowArrayAccess(true);
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
setupEnv(builder);
TargetClass1[] array = context.asValue(new String[] { "42", "42" }).as(TargetClass1[].class);
assertEquals(2, array.length);
assertEquals("42", array[0].o);
assertEquals("42", array[1].o);
try {
context.asValue(new String[] { "42", "43" }).as(TargetClass1[].class);
fail();
} catch (ClassCastException e) {
assertTrue(e.getMessage(), e.getMessage().startsWith("Cannot convert '43'"));
}
builder = HostAccess.newBuilder();
builder.allowArrayAccess(true);
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);
array = context.asValue(new String[] { "42", "422" }).as(TargetClass1[].class);
assertEquals(2, array.length);
assertEquals("42", array[0].o);
assertEquals("422", array[1].o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testReturnsNull.
@Test
public void testReturnsNull() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.targetTypeMapping(Integer.class, Integer.class, (v) -> v.equals(42), (v) -> {
return null;
});
setupEnv(builder);
try {
context.asValue(new PassPrimitive()).invokeMember("f0", 42);
fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof NullPointerException);
}
assertNull(context.asValue(42).as(int.class));
assertEquals(43, context.asValue(43).asInt());
}
Aggregations