use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class HostAccessTest method banAccessToEquals.
@Test
public void banAccessToEquals() throws Exception {
// @formatter:off
HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Object.class, false).build();
// @formatter:on
setupEnv(config);
Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + " return x.equals(y);\n" + "}\n" + "function main() {\n" + " return readValue;\n" + "}\n");
MyEquals myEquals = new MyEquals();
assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
Value res;
try {
res = readValue.execute(new Object());
} catch (PolyglotException ex) {
return;
}
fail("expecting no result: " + res);
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class JavaStringCoercionTest method before.
@Before
public void before() {
HostAccess access = TargetMappings.enableStringCoercions(HostAccess.newBuilder().allowPublicAccess(true)).build();
setupEnv(Context.newBuilder().allowHostAccess(access).allowAllAccess(true).build(), ProxyLanguage.setDelegate(new ProxyLanguage() {
}));
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class HostAccessExamplesTest method banAccessToReflection.
@Test
public void banAccessToReflection() throws Exception {
// @formatter:off
HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Class.class).denyAccess(Method.class).denyAccess(Field.class).denyAccess(Proxy.class).denyAccess(Object.class, false).build();
try (Context context = Context.newBuilder().allowHostAccess(config).build()) {
Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + " return x.equals(y);\n" + "}\n" + "function main() {\n" + " return readValue;\n" + "}\n");
MyEquals myEquals = new MyEquals();
assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
Value res;
try {
res = readValue.execute(new Object());
} catch (PolyglotException ex) {
return;
}
fail("expecting no result: " + res);
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ArrayToObjectMappingTest method testArrayAsMapLong.
@Test
public void testArrayAsMapLong() {
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL).targetTypeMapping(Value.class, Object.class, v -> v.hasMembers() && v.hasArrayElements(), v -> v.as(new TypeLiteral<Map<Long, Object>>() {
})).build();
try (Context c = Context.newBuilder("js").allowHostAccess(hostAccess).build()) {
Value fn = c.asValue((Function<Object, Object>) f -> {
if (f instanceof Map) {
Assert.assertEquals(4, ((Map<?, ?>) f).size());
return "Map";
}
return f.getClass().getName();
});
Value result = fn.execute(new ArrayAndMembers("a", "b", "c", "d"));
Assert.assertEquals("Map", result.asString());
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ValueHostConversionTest method testHostObjectTargetTypeMappingLowest.
@Test
public void testHostObjectTargetTypeMappingLowest() {
HostAccess.Builder b = HostAccess.newBuilder();
b.allowPublicAccess(true);
b.targetTypeMapping(Number.class, String.class, null, Number::toString, TargetMappingPrecedence.LOWEST);
b.targetTypeMapping(Number.class, Boolean.class, null, n -> n.doubleValue() != 0.0, TargetMappingPrecedence.LOWEST);
b.targetTypeMapping(Number.class, Integer.class, null, n -> n.intValue(), TargetMappingPrecedence.LOWEST);
b.targetTypeMapping(Number.class, Long.class, null, n -> n.longValue(), TargetMappingPrecedence.LOWEST);
b.targetTypeMapping(Number.class, Double.class, null, n -> n.doubleValue(), TargetMappingPrecedence.LOWEST);
HostAccess hostAccess = b.build();
setupEnv(Context.newBuilder().allowHostAccess(hostAccess).build());
assertEquals("42", context.asValue(new BigDecimal(42)).as(String.class));
assertEquals(false, context.asValue(new BigDecimal(0)).as(Boolean.class));
assertEquals(true, context.asValue(new BigDecimal(1)).as(Boolean.class));
assertEquals(Integer.valueOf(42), context.asValue(new BigDecimal(42)).as(Integer.class));
assertEquals(Integer.valueOf(42), context.asValue(new BigDecimal(42.5)).as(Integer.class));
assertEquals(Long.valueOf(42), context.asValue(new BigDecimal(42)).as(Long.class));
assertEquals(Long.valueOf(42), context.asValue(new BigDecimal(42.5)).as(Long.class));
assertEquals(Double.valueOf(42.5), context.asValue(new BigDecimal(42.5)).as(Double.class));
assertEquals(Double.valueOf(Double.POSITIVE_INFINITY), context.asValue(new BigDecimal(2).pow(9001)).as(Double.class));
// sanity check: default mappings take precedence over lowest in overload selection.
assertEquals("object", context.asValue(new StringHierarchy()).invokeMember("hierarchy", new BigDecimal(42)).asString());
// the only applicable method is one that requires the lowest precedence target mapping.
Value onlyString = context.asValue(new OnlyString());
assertEquals("42", onlyString.invokeMember("accept", new BigDecimal(42)).asString());
Value onlyInt = context.asValue(new OnlyInt());
assertEquals("42", onlyInt.invokeMember("accept", new BigDecimal(42)).asString());
}
Aggregations