Search in sources :

Example 21 with HostAccess

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());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Proxy(org.graalvm.polyglot.proxy.Proxy) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 22 with HostAccess

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);
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Before(org.junit.Before)

Example 23 with HostAccess

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));
    }
}
Also used : Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 24 with HostAccess

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());
    }
}
Also used : ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) List(java.util.List) HostAccess(org.graalvm.polyglot.HostAccess) TypeLiteral(org.graalvm.polyglot.TypeLiteral) Value(org.graalvm.polyglot.Value) Map(java.util.Map) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Test(org.junit.Test) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Function(java.util.function.Function) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Map(java.util.Map) Test(org.junit.Test)

Example 25 with HostAccess

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());
}
Also used : HostAccess(org.graalvm.polyglot.HostAccess) ValueAssert.assertValue(com.oracle.truffle.tck.tests.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

HostAccess (org.graalvm.polyglot.HostAccess)35 Test (org.junit.Test)30 Context (org.graalvm.polyglot.Context)25 Value (org.graalvm.polyglot.Value)25 LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)13 PolyglotException (org.graalvm.polyglot.PolyglotException)6 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)6 Export (org.graalvm.polyglot.HostAccess.Export)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 ValueAssert.assertValue (com.oracle.truffle.tck.tests.ValueAssert.assertValue)2 Field (java.lang.reflect.Field)2 URLClassLoader (java.net.URLClassLoader)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Function (java.util.function.Function)2 TypeLiteral (org.graalvm.polyglot.TypeLiteral)2 ProxyArray (org.graalvm.polyglot.proxy.ProxyArray)2 Assert (org.junit.Assert)2 Before (org.junit.Before)2