use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ValueScopingTest method testMultiThreading.
@Test
public void testMultiThreading() throws InterruptedException, ExecutionException {
TestMultiThread o = new TestMultiThread();
HostAccess accessPolicy = HostAccess.newBuilder(HostAccess.SCOPED).allowPublicAccess(true).build();
try (Context context = Context.newBuilder().allowHostAccess(accessPolicy).build()) {
Map<String, Object> map = new ConcurrentHashMap<>();
map.put("cafe", "42");
ProxyObject proxy = ProxyObject.fromMap(map);
Value test = context.asValue(o);
// repeat a few times to cause races
for (int i = 0; i < 100; i++) {
int[] testValues = new int[] { 1, 4, 8, 16, 32, 64, 128, 256 };
for (int threads : testValues) {
o.futures.clear();
o.fails.set(0);
o.successes.set(0);
test.invokeMember("storeMultiThreaded", proxy, threads);
assertEquals(threads, o.futures.size());
for (Future<?> f : o.futures) {
f.get();
}
assertEquals(threads, o.fails.get() + o.successes.get());
}
}
o.executorService.shutdownNow();
o.executorService.awaitTermination(10, TimeUnit.SECONDS);
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ValueScopingTest method testStoreAndPin.
@Test
public void testStoreAndPin() {
HostAccess accessPolicy = HostAccess.newBuilder(HostAccess.SCOPED).allowPublicAccess(true).build();
try (Context context = Context.newBuilder().allowHostAccess(accessPolicy).build()) {
StoreAndPinTest o = new StoreAndPinTest();
Map<String, Object> map = new HashMap<>();
map.put("cafe", "42");
map.put("array", ProxyArray.fromArray());
ProxyObject proxy = ProxyObject.fromMap(map);
Value test = context.asValue(o);
// no-op
test.pin();
// value accessed out of scope
test.invokeMember("storeValue", proxy);
assertFails(() -> o.value.isString(), IllegalStateException.class, SCOPE_RELEASED);
assertFails(() -> o.value.asString(), IllegalStateException.class, SCOPE_RELEASED);
assertFails(() -> o.value.pin(), IllegalStateException.class, SCOPE_RELEASED);
test.invokeMember("storeMap", proxy);
assertFails(() -> o.map.get(""), IllegalStateException.class, SCOPE_RELEASED);
// recursive references
assertFails(() -> test.invokeMember("storeValue", o.map), IllegalStateException.class, SCOPE_RELEASED);
assertFails(() -> test.invokeMember("storeValue", o.value), IllegalStateException.class, SCOPE_RELEASED);
test.invokeMember("storeValueAndPin", proxy);
assertTrue(o.value.isProxyObject());
ValueAssert.assertValue(o.value);
// host object
test.invokeMember("storeValueAndPin", test);
assertTrue(o.value.isHostObject());
// host object
test.invokeMember("storeValueAndPin", ProxyObject.fromMap(new HashMap<>()));
assertTrue(o.value.isProxyObject());
test.invokeMember("storeMapAndPin", proxy);
// maps can be pinned too
assertEquals("42", o.map.get("cafe"));
ValueAssert.assertValue(o.value);
// if scoping is disabled
test.invokeMember("storeDisabled", proxy);
// can pin afterwards
o.value.pin();
// and access anything
ValueAssert.assertValue(o.value);
// if values are accessed the scope should propagate
test.invokeMember("storeValueElement", proxy);
assertFails(() -> o.value.hasArrayElements(), IllegalStateException.class, SCOPE_RELEASED);
assertFails(() -> o.value.pin(), IllegalStateException.class, SCOPE_RELEASED);
// if polyglot wrappers are accessed the scope should propagate
test.invokeMember("storeMapElement", proxy);
assertFails(() -> o.value.hasArrayElements(), IllegalStateException.class, SCOPE_RELEASED);
assertFails(() -> o.value.pin(), IllegalStateException.class, SCOPE_RELEASED);
// if a value is stored in guest we remove the scope
test.invokeMember("storeInGuest", proxy);
ValueAssert.assertValue(o.value);
// does not fail
o.value.pin();
// host method execution fails
try {
test.invokeMember("storeValueAndThrow", proxy);
fail("method invocation should throw an exception");
} catch (Exception ex) {
assertEquals("method failed", ex.getMessage());
}
}
}
use of org.graalvm.polyglot.HostAccess in project graal by oracle.
the class ExposeToGuestTest method explicitlyEnumeratingField.
@Test
public void explicitlyEnumeratingField() throws Exception {
HostAccess explictConfig = HostAccess.newBuilder().allowAccess(AccessibleValue.class.getField("value")).build();
Context context = Context.newBuilder().allowHostAccess(explictConfig).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 testProxyAll.
@SuppressWarnings("unchecked")
@Test
public void testProxyAll() {
HostAccess access = HostAccess.ALL;
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 HostAccessTest method onlyOneHostAccessPerEngine.
@Test
public void onlyOneHostAccessPerEngine() throws Exception {
Engine shared = Engine.create();
HostAccess config = HostAccess.newBuilder().allowAccess(OK.class.getField("value")).build();
Context c1 = Context.newBuilder().engine(shared).allowHostAccess(config).build();
Context.Builder builder = Context.newBuilder().engine(shared).allowHostAccess(HostAccess.ALL);
try {
builder.build();
fail();
} catch (IllegalStateException ex) {
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Found different host access configuration for a context with a shared engine."));
}
c1.close();
}
Aggregations