Search in sources :

Example 6 with Builder

use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.

the class HostAccessTest method testConverterList.

@Test
public void testConverterList() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowArrayAccess(true);
    builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
    setupEnv(builder);
    List<TargetClass1> list = context.asValue(new String[] { "42", "42" }).as(TARGET_CLASS_LIST);
    assertEquals(2, list.size());
    assertEquals("42", list.get(0).o);
    assertEquals("42", list.get(1).o);
    list = context.asValue(new String[] { "42", "43" }).as(TARGET_CLASS_LIST);
    try {
        list.get(1);
        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);
    list = context.asValue(new String[] { "42", "422" }).as(TARGET_CLASS_LIST);
    assertEquals(2, list.size());
    assertEquals("42", list.get(0).o);
    assertEquals("422", list.get(1).o);
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 7 with Builder

use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.

the class HostAccessTest method testConverterClassCast.

@Test
public void testConverterClassCast() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowPublicAccess(true);
    ClassCastException cce = new ClassCastException("foo");
    builder.targetTypeMapping(Integer.class, String.class, (v) -> v.equals(42), (v) -> {
        throw cce;
    });
    setupEnv(builder);
    Value v = context.asValue(42);
    try {
        v.as(String.class);
        fail();
    } catch (ClassCastException e) {
        assertEquals("foo", e.getMessage());
        // we must not allow normal class cast exceptions to go through.
        assertNotSame(e, cce);
    }
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 8 with Builder

use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.

the class HostAccessTest method testConverterFunctional.

@Test
public void testConverterFunctional() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowPublicAccess(true);
    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);
    Value identity = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            return arguments[0];
        }
    });
    ConverterFunction f = identity.as(ConverterFunction.class);
    assertEquals("42", f.m("42").o);
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 9 with Builder

use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.

the class HostAccessTest method testConverterArray.

@Test
public void testConverterArray() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowArrayAccess(true);
    builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
    setupEnv(builder);
    TargetClass1[] array = context.asValue(new String[] { "42", "42" }).as(TargetClass1[].class);
    assertEquals(2, array.length);
    assertEquals("42", array[0].o);
    assertEquals("42", array[1].o);
    try {
        context.asValue(new String[] { "42", "43" }).as(TargetClass1[].class);
        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);
    array = context.asValue(new String[] { "42", "422" }).as(TargetClass1[].class);
    assertEquals(2, array.length);
    assertEquals("42", array[0].o);
    assertEquals("422", array[1].o);
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 10 with Builder

use of org.graalvm.polyglot.HostAccess.Builder in project graal by oracle.

the class HostAccessTest method testReturnsNull.

@Test
public void testReturnsNull() {
    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