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));
}
}
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);
}
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"));
}
}
}
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"));
}
}
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);
}
}
Aggregations