use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ValueScopingTest method testHostInterop.
@Test
public void testHostInterop() {
HostAccess accessPolicy = HostAccess.newBuilder(HostAccess.EXPLICIT).allowPublicAccess(true).build();
try (Context context = Context.newBuilder().allowHostAccess(accessPolicy).build()) {
HostInteropTest o = new HostInteropTest();
Value test = context.asValue(o);
HostInteropTestClass c = new HostInteropTestClass();
assertSame(c, test.invokeMember("valueCast", c).asHostObject());
assertSame(c, test.invokeMember("valueCast", c).asHostObject());
Proxy p = ProxyObject.fromMap(new HashMap<>());
assertSame(p, test.invokeMember("proxyCast", p).asProxyObject());
assertSame(p, test.invokeMember("proxyCast", p).asProxyObject());
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method setup.
@Before
public void setup() {
final HostAccess hostAccess = HostAccess.newBuilder().allowBufferAccess(true).build();
final Context context = Context.newBuilder().allowExperimentalOptions(true).option("engine.CompileImmediately", "true").allowHostAccess(hostAccess).build();
setupContext(context);
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class HostAccessExamplesTest method testSampleMappings.
@Test
public void testSampleMappings() {
HostAccess.Builder builder = HostAccess.newBuilder();
// character coercion
TargetMappings.enableStringCoercions(builder);
HostAccess config = builder.build();
try (Context context = Context.newBuilder().allowHostAccess(config).build()) {
assertEquals((byte) 42, (byte) context.asValue("42").as(byte.class));
assertEquals(Byte.valueOf((byte) 42), context.asValue("42").as(Byte.class));
assertEquals((short) 42, (short) context.asValue("42").as(short.class));
assertEquals(Short.valueOf((short) 42), context.asValue("42").as(Short.class));
assertEquals(42, (int) context.asValue("42").as(int.class));
assertEquals(Integer.valueOf(42), context.asValue("42").as(Integer.class));
assertEquals(42L, (long) context.asValue("42").as(long.class));
assertEquals(Long.valueOf(42L), context.asValue("42").as(Long.class));
assertEquals(42f, context.asValue("42").as(float.class), 0.001f);
assertEquals(Float.valueOf(42f), context.asValue("42").as(Float.class));
assertEquals(42d, context.asValue("42").as(double.class), 0.001d);
assertEquals(Double.valueOf(42d), context.asValue("42").as(Double.class));
assertEquals(true, context.asValue("true").as(boolean.class));
assertEquals(false, context.asValue("false").as(boolean.class));
assertEquals(true, context.asValue("true").as(Boolean.class));
assertEquals("42", context.asValue((byte) 42).as(String.class));
assertEquals("42", context.asValue((short) 42).as(String.class));
assertEquals("42", context.asValue(42).as(String.class));
assertEquals("42", context.asValue(42L).as(String.class));
assertEquals("42.0", context.asValue((float) 42).as(String.class));
assertEquals("42.0", context.asValue((double) 42).as(String.class));
assertEquals("true", context.asValue(true).as(String.class));
assertEquals((Character) (char) 42, context.asValue(42).as(char.class));
assertEquals((Character) (char) 42, context.asValue(42).as(Character.class));
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ArrayToObjectMappingTest method testArrayAsMapGeneric.
@Test
public void testArrayAsMapGeneric() {
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL).targetTypeMapping(Value.class, Object.class, v -> v.hasMembers() && v.hasArrayElements(), v -> v.as(Map.class)).build();
try (Context c = Context.newBuilder("js").allowHostAccess(hostAccess).build()) {
Value fn = c.asValue((Function<Object, Object>) f -> {
if (f instanceof Map) {
Assert.assertEquals(0, ((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 testStringToPrimitive.
@Test
public void testStringToPrimitive() {
HostAccess hostAccess = TargetMappings.enableStringCoercions(HostAccess.newBuilder().allowPublicAccess(true)).build();
setupEnv(Context.newBuilder().allowAllAccess(true).allowHostAccess(hostAccess).build());
Value hierarchy = context.asValue(new PrimitiveHierarchy()).getMember("hierarchy");
assertEquals("int", hierarchy.execute(String.valueOf(Integer.MIN_VALUE)).asString());
assertEquals("int", hierarchy.execute(String.valueOf(Integer.MAX_VALUE)).asString());
assertEquals("int", hierarchy.execute(String.valueOf((long) Integer.MIN_VALUE)).asString());
assertEquals("int", hierarchy.execute(String.valueOf((long) Integer.MAX_VALUE)).asString());
assertEquals("byte", hierarchy.execute(String.valueOf(Byte.MIN_VALUE)).asString());
assertEquals("byte", hierarchy.execute(String.valueOf(Byte.MAX_VALUE)).asString());
assertEquals("short", hierarchy.execute(String.valueOf(Short.MIN_VALUE)).asString());
assertEquals("short", hierarchy.execute(String.valueOf(Short.MAX_VALUE)).asString());
assertEquals("long", hierarchy.execute(String.valueOf(Integer.MIN_VALUE - 1L)).asString());
assertEquals("long", hierarchy.execute(String.valueOf(Integer.MAX_VALUE + 1L)).asString());
assertEquals("long", hierarchy.execute(String.valueOf(Long.MIN_VALUE)).asString());
assertEquals("long", hierarchy.execute(String.valueOf(Long.MAX_VALUE)).asString());
assertEquals("float", hierarchy.execute(String.valueOf((float) -(Math.pow(2, 24) - 1))).asString());
assertEquals("float", hierarchy.execute(String.valueOf((float) +(Math.pow(2, 24) - 1))).asString());
assertEquals("float", hierarchy.execute(String.valueOf(Float.MIN_VALUE)).asString());
assertEquals("float", hierarchy.execute(String.valueOf(Float.MAX_VALUE)).asString());
// lossy
assertEquals("float", hierarchy.execute(String.valueOf((float) Integer.MIN_VALUE)).asString());
assertEquals("float", hierarchy.execute(String.valueOf((float) Integer.MAX_VALUE)).asString());
assertEquals("float", hierarchy.execute(String.valueOf((double) Integer.MIN_VALUE)).asString());
assertEquals("float", hierarchy.execute(String.valueOf((double) Integer.MAX_VALUE)).asString());
assertEquals("double", hierarchy.execute(String.valueOf(Double.MIN_VALUE)).asString());
assertEquals("double", hierarchy.execute(String.valueOf(Double.MAX_VALUE)).asString());
assertEquals("boolean", hierarchy.execute(String.valueOf(false)).asString());
assertEquals("boolean", hierarchy.execute(String.valueOf(true)).asString());
}
Aggregations