Search in sources :

Example 11 with Builder

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

Example 12 with Builder

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());
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 13 with Builder

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

Example 14 with Builder

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

Example 15 with Builder

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

Aggregations

HostAccess (org.graalvm.polyglot.HostAccess)17 Builder (org.graalvm.polyglot.HostAccess.Builder)17 Test (org.junit.Test)17 PolyglotException (org.graalvm.polyglot.PolyglotException)5 Value (org.graalvm.polyglot.Value)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Predicate (java.util.function.Predicate)1 Context (org.graalvm.polyglot.Context)1 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)1 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)1 Ignore (org.junit.Ignore)1