Search in sources :

Example 1 with ProxyIterable

use of org.graalvm.polyglot.proxy.ProxyIterable in project graal by oracle.

the class IteratorTest method testProxyIterable.

@Test
public void testProxyIterable() {
    Object[] values = { "a", "b" };
    setupEnv(Context.newBuilder().allowAllAccess(true).build());
    Value iterable = context.asValue(ProxyIterable.from(Arrays.asList(values)));
    verifyIterable(iterable, values, false);
    iterable = context.asValue(new ProxyIterable() {

        @Override
        public Object getIterator() {
            return Arrays.asList(values).iterator();
        }
    });
    verifyIterable(iterable, values, false);
    iterable = context.asValue(new ProxyIterable() {

        @Override
        public Object getIterator() {
            return ProxyIterator.from(Arrays.asList(values).iterator());
        }
    });
    verifyIterable(iterable, values, false);
    iterable = context.asValue(new ProxyIterable() {

        @Override
        public Object getIterator() {
            return new SimpleIterator(values);
        }
    });
    verifyIterable(iterable, values, false);
    Value invalidIterable = context.asValue(new ProxyIterable() {

        @Override
        public Object getIterator() {
            return ProxyObject.fromMap(Collections.emptyMap());
        }
    });
    assertFails(() -> invalidIterable.getIterator(), PolyglotException.class, (pe) -> assertTrue(pe.asHostException() instanceof IllegalStateException));
}
Also used : ProxyIterable(org.graalvm.polyglot.proxy.ProxyIterable) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Example 2 with ProxyIterable

use of org.graalvm.polyglot.proxy.ProxyIterable in project graal by oracle.

the class HostProxy method getIterator.

@ExportMessage
@TruffleBoundary
Object getIterator(@CachedLibrary("this") InteropLibrary library, @Shared("cache") @Cached(value = "this.context.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws UnsupportedMessageException {
    if (proxy instanceof ProxyIterable) {
        Object result = guestToHostCall(library, cache.getIterator, context, proxy);
        Object guestValue = context.toGuestValue(library, result);
        InteropLibrary interop = InteropLibrary.getFactory().getUncached();
        if (!interop.isIterator(guestValue)) {
            throw illegalProxy(context, "getIterator() returned an invalid value %s but must return an iterator.", context.asValue(library, guestValue).toString());
        }
        return guestValue;
    } else {
        throw UnsupportedMessageException.create();
    }
}
Also used : ProxyIterable(org.graalvm.polyglot.proxy.ProxyIterable) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 ProxyIterable (org.graalvm.polyglot.proxy.ProxyIterable)2 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 ExportMessage (com.oracle.truffle.api.library.ExportMessage)1 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)1 Value (org.graalvm.polyglot.Value)1 ProxyNativeObject (org.graalvm.polyglot.proxy.ProxyNativeObject)1 Test (org.junit.Test)1