Search in sources :

Example 1 with Builder

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

the class HostAccessTest method testMappingInvocations.

@Test
public void testMappingInvocations() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    CountingPredicate accepts0 = new CountingPredicate("42");
    CountingConverter converter0 = new CountingConverter();
    builder.targetTypeMapping(Value.class, String.class, accepts0, converter0);
    CountingPredicate accepts1 = new CountingPredicate("43");
    CountingConverter converter1 = new CountingConverter();
    builder.targetTypeMapping(Value.class, String.class, accepts1, converter1);
    setupEnv(builder);
    context.asValue("42").as(String.class);
    assertEquals(1, accepts0.count);
    assertEquals(1, converter0.count);
    assertEquals(0, accepts1.count);
    assertEquals(0, converter1.count);
    context.asValue("43").as(String.class);
    assertEquals(2, accepts0.count);
    assertEquals(1, converter0.count);
    assertEquals(1, accepts1.count);
    assertEquals(1, converter1.count);
    context.asValue("44").as(String.class);
    assertEquals(3, accepts0.count);
    assertEquals(1, converter0.count);
    assertEquals(2, accepts1.count);
    assertEquals(1, converter1.count);
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 2 with Builder

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

the class HostAccessTest method testPassNullValue.

@Test
public void testPassNullValue() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    AtomicInteger invoked = new AtomicInteger();
    builder.targetTypeMapping(Value.class, Integer.class, (v) -> {
        assertTrue(v.isNull());
        invoked.incrementAndGet();
        return v.fitsInInt();
    }, (v) -> {
        throw new AssertionError();
    });
    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 3 with Builder

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

the class HostAccessTest method testConverterProxy.

@Test
public void testConverterProxy() {
    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");
    ConverterProxy map = context.asValue(testObj).as(ConverterProxy.class);
    assertEquals("42", map.f0().o);
    assertEquals("42", map.f1().o);
    assertEquals("42", testObj.f0);
    map = context.asValue(new ConverterMapTestObject("42", "43")).as(ConverterProxy.class);
    assertEquals("42", map.f0().o);
    try {
        map.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(ConverterProxy.class);
    assertEquals("42", map.f0().o);
    assertEquals("422", map.f1().o);
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Test(org.junit.Test)

Example 4 with Builder

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

the class HostAccessTest method testRecursion.

/*
     * Test for GR-15593.
     */
@Test
@Ignore
public void testRecursion() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.targetTypeMapping(Value.class, Integer.class, (v) -> {
        return true;
    }, (v) -> {
        return v.as(Integer.class);
    });
    setupEnv(builder);
    try {
        assertNull(context.asValue(42).as(Integer.class));
        fail();
    } catch (PolyglotException e) {
        // which type of exception ends up here depends on where the stack overflow happens
        if (e.isGuestException()) {
            assertTrue(e.isInternalError());
        } else {
            assertTrue(e.isHostException());
            assertTrue(e.asHostException() instanceof StackOverflowError);
        }
    }
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HostAccess(org.graalvm.polyglot.HostAccess) PolyglotException(org.graalvm.polyglot.PolyglotException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Builder

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

the class HostAccessTest method testConverterError.

@Test
public void testConverterError() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowPublicAccess(true);
    final IllegalArgumentException error = new IllegalArgumentException();
    builder.targetTypeMapping(Integer.class, String.class, (v) -> v.equals(42), (v) -> {
        throw error;
    });
    setupEnv(builder);
    Value v = context.asValue(42);
    try {
        v.as(String.class);
        fail();
    } catch (PolyglotException e) {
        assertTrue(e.isHostException());
        assertSame(error, e.asHostException());
    }
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) 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