Search in sources :

Example 16 with Builder

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

the class HostAccessTest method testConverterOverloads.

@Test
public void testConverterOverloads() {
    HostAccess.Builder builder;
    Value methods;
    // test single method
    builder = HostAccess.newBuilder();
    builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
    setupEnv(builder);
    methods = context.asValue(new SingleMethod());
    assertEquals(TargetClass1.class, methods.invokeMember("m", "42").asHostObject().getClass());
    // should fail
    try {
        methods.invokeMember("m", "43");
        fail();
    } catch (IllegalArgumentException e) {
    }
    // should work
    TargetClass1 o = methods.invokeMember("m", "42").asHostObject();
    assertEquals("42", o.o);
    // test non ambiguous methods
    builder = HostAccess.newBuilder();
    builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
    builder.targetTypeMapping(String.class, TargetClass2.class, (v) -> v.equals("43"), (v) -> new TargetClass2(v));
    setupEnv(builder);
    methods = context.asValue(new MultiMethod1());
    assertEquals(TargetClass1.class, methods.invokeMember("m", "42").asHostObject().getClass());
    assertEquals(TargetClass2.class, methods.invokeMember("m", "43").asHostObject().getClass());
    // test ambiguous methods
    builder = HostAccess.newBuilder();
    builder.targetTypeMapping(String.class, TargetClass1.class, (v) -> v.equals("42"), (v) -> new TargetClass1(v));
    builder.targetTypeMapping(String.class, TargetClass2.class, (v) -> v.equals("42"), (v) -> new TargetClass2(v));
    setupEnv(builder);
    methods = context.asValue(new MultiMethod1());
    try {
        methods.invokeMember("m", "42");
        fail();
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Multiple applicable overloads found"));
    }
    builder = HostAccess.newBuilder();
    builder.targetTypeMapping(String.class, TargetClass1.class, null, (v) -> new TargetClass1(v));
    setupEnv(builder);
    methods = context.asValue(new MultiMethod2());
    assertEquals("42", methods.invokeMember("m", "42", "42").asString());
    assertEquals(42, methods.invokeMember("m", "42", 42).asInt());
}
Also used : Builder(org.graalvm.polyglot.HostAccess.Builder) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 17 with Builder

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

the class HostAccessTest method testAcceptsError.

@Test
public void testAcceptsError() {
    HostAccess.Builder builder = HostAccess.newBuilder();
    builder.allowPublicAccess(true);
    final IllegalArgumentException error = new IllegalArgumentException();
    Predicate<Integer> errorAccepts = new Predicate<>() {

        public boolean test(Integer t) {
            error.initCause(new RuntimeException());
            throw error;
        }
    };
    builder.targetTypeMapping(Integer.class, String.class, errorAccepts, (v) -> {
        throw new AssertionError();
    });
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Predicate(java.util.function.Predicate) 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