Search in sources :

Example 26 with HostAccess

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);
    }
}
Also used : Context(org.graalvm.polyglot.Context) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 27 with HostAccess

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());
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 28 with HostAccess

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());
}
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 29 with HostAccess

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));
    }
}
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 30 with HostAccess

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();
}
Also used : Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Engine(org.graalvm.polyglot.Engine) 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