use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testBuilderCannotChangeMembersAndTargetMappingsOfHostAccess.
/*
* Test for GR-32346.
*/
@Test
public void testBuilderCannotChangeMembersAndTargetMappingsOfHostAccess() throws Exception {
// Set up hostAccess
Builder builder = HostAccess.newBuilder();
builder.allowAccess(OK.class.getField("value"));
builder.targetTypeMapping(Value.class, String.class, (v) -> v.isString(), (v) -> "foo");
HostAccess hostAccess = builder.build();
// Try to change members or targetMappings through child builder
Builder childBuilder = HostAccess.newBuilder(hostAccess);
childBuilder.allowAccess(Ban.class.getField("value"));
childBuilder.targetTypeMapping(Value.class, Integer.class, null, (v) -> 42);
// Ensure hostAccess has not been altered by child builder
try (Context c = Context.newBuilder().allowHostAccess(hostAccess).build()) {
assertAccess(c);
assertEquals("foo", c.asValue("a string").as(String.class));
assertEquals(123, (int) c.asValue(123).as(Integer.class));
}
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testPassNullObject.
@Test
public void testPassNullObject() {
HostAccess.Builder builder = HostAccess.newBuilder();
AtomicInteger invoked = new AtomicInteger();
builder.targetTypeMapping(Integer.class, Integer.class, (v) -> {
assertNull(v);
invoked.incrementAndGet();
return true;
}, (v) -> {
return null;
});
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 testConverterMapString.
@Test
public void testConverterMapString() {
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");
Map<String, TargetClass1> map = context.asValue(testObj).as(TARGET_CLASS_MAP_STRING);
assertEquals(2, map.size());
assertEquals("42", map.get("f0").o);
assertEquals("42", map.get("f1").o);
try {
map.put("f0", new TargetClass1("43"));
fail();
} catch (ClassCastException e) {
}
testObj.f0 = null;
map.put("f0", new TargetClass1("42"));
assertEquals("42", testObj.f0);
map = context.asValue(new ConverterMapTestObject("42", "43")).as(TARGET_CLASS_MAP_STRING);
assertEquals("42", map.get("f0").o);
try {
map.get("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(TARGET_CLASS_MAP_STRING);
assertEquals(2, map.size());
assertEquals("42", map.get("f0").o);
assertEquals("422", map.get("f1").o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterMapLong.
@Test
public void testConverterMapLong() {
HostAccess.Builder builder = HostAccess.newBuilder();
builder.allowArrayAccess(true);
builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
setupEnv(builder);
Map<Long, TargetClass1> map = context.asValue(new String[] { "42", "42" }).as(TARGET_CLASS_MAP_LONG);
assertEquals(2, map.size());
assertEquals("42", map.get(0L).o);
assertEquals("42", map.get(1L).o);
map = context.asValue(new String[] { "42", "43" }).as(TARGET_CLASS_MAP_LONG);
try {
map.get(1L);
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);
map = context.asValue(new String[] { "42", "422" }).as(TARGET_CLASS_MAP_LONG);
assertEquals(2, map.size());
assertEquals("42", map.get(0L).o);
assertEquals("422", map.get(1L).o);
}
use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.
the class HostAccessTest method testConverterReturnsNull.
@Test
public void testConverterReturnsNull() {
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