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