Search in sources :

Example 1 with HostAccess

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

the class ExposeToGuestTest method testProxyExplicit.

@SuppressWarnings("unchecked")
@Test
public void testProxyExplicit() {
    HostAccess access = HostAccess.EXPLICIT;
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Impl());
        Value f = v.getMember("noArg");
        assertEquals("42", v.as(MarkedInterface.class).exported("42"));
        try {
            v.as(EmptyInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            v.as(UnmarkedInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        assertEquals(42, f.as(MarkedFunctional.class).f());
        try {
            f.as(UnmarkedFunctional.class);
            fail();
        } catch (ClassCastException e) {
        }
        assertEquals(42, f.as(Function.class).apply(null));
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 2 with HostAccess

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

the class ExposeToGuestTest method testProxyManualAll.

@SuppressWarnings("unchecked")
@Test
public void testProxyManualAll() {
    HostAccess access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowAllImplementations(true).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Impl());
        Value f = v.getMember("noArg");
        assertEquals("42", v.as(MarkedInterface.class).exported("42"));
        assertEquals("42", v.as(UnmarkedInterface.class).exported("42"));
        assertNotNull(v.as(EmptyInterface.class));
        assertEquals(42, f.as(MarkedFunctional.class).f());
        assertEquals(42, f.as(UnmarkedFunctional.class).f());
        assertEquals(42, f.as(Function.class).apply(null));
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 3 with HostAccess

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

the class ExposeToGuestTest method customExportedAnnotation.

@Test
public void customExportedAnnotation() {
    HostAccess accessMeConfig = HostAccess.newBuilder().allowAccessAnnotatedBy(AccessMe.class).build();
    Context context = Context.newBuilder().allowHostAccess(accessMeConfig).build();
    Value readValue = context.eval("sl", "" + "function readValue(x) {\n" + "  return x.value;\n" + "}\n" + "function main() {\n" + "  return readValue;\n" + "}\n");
    Assert.assertEquals(42, readValue.execute(new AccessibleValue()).asInt());
    assertPropertyUndefined("Default annotation isn't enough", readValue, new ExportedValue());
    assertPropertyUndefined("Public isn't enough by default", readValue, new PublicValue());
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 4 with HostAccess

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

the class ExposeToGuestTest method testAdapterClassImplementationsNotAllowed.

@Test
public void testAdapterClassImplementationsNotAllowed() {
    HostAccess access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Impl());
        try {
            v.as(MarkedClass.class);
            fail();
        } catch (ClassCastException e) {
            assertThat(e.getMessage(), containsString("Unsupported target type"));
        }
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Export(org.graalvm.polyglot.HostAccess.Export) Test(org.junit.Test)

Example 5 with HostAccess

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

the class ExposeToGuestTest method testProxyOverloads.

@SuppressWarnings("unchecked")
@Test
public void testProxyOverloads() {
    HostAccess access = HostAccess.EXPLICIT;
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Overloaded());
        Value arg = c.asValue(new Impl());
        try {
            v.invokeMember("overloaded", arg);
            fail();
        } catch (IllegalArgumentException e) {
            // multiple overloads
            assertTrue(e.getMessage(), e.getMessage().contains("Multiple applicable overloads"));
        }
        assertEquals("42", v.invokeMember("overloaded", "42").asString());
    }
    // disable interface proxies.
    access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Overloaded());
        Value arg = c.asValue(new Impl());
        try {
            v.invokeMember("overloaded", arg);
            fail();
        } catch (IllegalArgumentException e) {
            // multiple overloads
            assertTrue(e.getMessage(), e.getMessage().contains("no applicable overload found"));
        }
        assertEquals("42", v.invokeMember("overloaded", "42").asString());
    }
    // disable only object proxies
    access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowImplementationsAnnotatedBy(FunctionalInterface.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Overloaded());
        Value arg = c.asValue(new Impl());
        assertEquals("MarkedFunctional", v.invokeMember("overloaded", arg.getMember("noArg")).asString());
        assertEquals("42", v.invokeMember("overloaded", "42").asString());
    }
    // disable only functional proxies
    access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowImplementations(MarkedInterface.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Overloaded());
        Value arg = c.asValue(new Impl());
        assertEquals("MarkedInterface", v.invokeMember("overloaded", arg).asString());
        assertEquals("42", v.invokeMember("overloaded", "42").asString());
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Export(org.graalvm.polyglot.HostAccess.Export) 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