Search in sources :

Example 6 with HostAccess

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

the class ExposeToGuestTest method testProxyMarked.

@SuppressWarnings("unchecked")
@Test
public void testProxyMarked() {
    HostAccess access = // 
    HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowImplementations(UnmarkedInterface.class).allowImplementations(UnmarkedFunctional.class).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(UnmarkedInterface.class).exported("42"));
        try {
            v.as(EmptyInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            v.as(MarkedInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        assertEquals(42, f.as(UnmarkedFunctional.class).f());
        try {
            f.as(MarkedFunctional.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) Export(org.graalvm.polyglot.HostAccess.Export) Test(org.junit.Test)

Example 7 with HostAccess

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

the class ExposeToGuestTest method doAccessForbiddenInManual.

private static void doAccessForbiddenInManual(boolean asList) throws Exception {
    HostAccess config = HostAccess.newBuilder().allowAccess(FooInterface.class.getMethod("foo", Number.class)).build();
    Context context = Context.newBuilder().allowHostAccess(config).build();
    Value readValue = context.eval("sl", "" + "function callFoo(x) {\n" + "  return x.foo(1)[0];\n" + "}\n" + "function main() {\n" + "  return callFoo;\n" + "}\n");
    boolean[] gotIn = { false };
    FooInterface<Number> foo = returnAsArrayOrList(gotIn, asList);
    final Value arrayRead;
    try {
        arrayRead = readValue.execute(foo);
    } catch (Exception ex) {
        assertEquals("Expecting an exception", PolyglotException.class, ex.getClass());
        Assert.assertTrue("Foo lamda called", gotIn[0]);
        return;
    }
    fail("The read shouldn't succeed: " + arrayRead);
}
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) PolyglotException(org.graalvm.polyglot.PolyglotException) PolyglotException(org.graalvm.polyglot.PolyglotException) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException)

Example 8 with HostAccess

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

the class ExposeToGuestTest method testAdapterNoDefaultConstructor.

@Test
public void testAdapterNoDefaultConstructor() {
    HostAccess access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowImplementations(NoDefaultConstructor.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Impl());
        try {
            v.as(NoDefaultConstructor.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) Test(org.junit.Test)

Example 9 with HostAccess

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

the class ExposeToGuestTest method testProxyNone.

@SuppressWarnings("unchecked")
@Test
public void testProxyNone() {
    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());
        Value f = v.getMember("exported");
        try {
            v.as(MarkedInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            v.as(EmptyInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            v.as(UnmarkedInterface.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            f.as(MarkedFunctional.class);
            fail();
        } catch (ClassCastException e) {
        }
        try {
            f.as(UnmarkedFunctional.class);
            fail();
        } catch (ClassCastException e) {
        }
        assertEquals("42", f.as(Function.class).apply("42"));
    }
}
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 10 with HostAccess

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

the class SLHostClassLoadingTest method testSLHostClassLoading.

@Test
public void testSLHostClassLoading() throws IOException {
    HostAccess accessPolicy = HostAccess.newBuilder(HostAccess.ALL).build();
    try (Context context = Context.newBuilder().allowIO(true).allowHostAccess(accessPolicy).allowHostClassLookup(cls -> true).allowHostClassLoading(true).build()) {
        final Class<?> hostClass = HostClassLoadingTestClass1.class;
        Path tempDir = renameHostClass(hostClass, TEST_REPLACE_CLASS_NAME);
        Path jar = createJar(tempDir);
        assertHostClassPath(context, hostClass, jar);
        Files.deleteIfExists(jar);
        deleteDir(tempDir);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) HostAccess(org.graalvm.polyglot.HostAccess) 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