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