Java Code Examples for com.oracle.truffle.api.interop.InteropLibrary#readArrayElement()

The following examples show how to use com.oracle.truffle.api.interop.InteropLibrary#readArrayElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop,
                @CachedLibrary(limit = "2") InteropLibrary elementInterop) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    Object value = parentInterop.execute(parent, arguments);
    try {
        return elementInterop.readArrayElement(value, index);
    } catch (UnsupportedMessageException | InvalidArrayIndexException e) {
        CompilerDirectives.transferToInterpreter();
        throw new MapException("cannot read element '" + index + "' from argument " + parent);
    }
}
 
Example 2
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 6 votes vote down vote up
@Specialization(guards = {"lib.hasMembers(object)"}, limit = "2")
protected static final ArrayObject doGetMembers(@SuppressWarnings("unused") final Object receiver, final Object object, final boolean includeInternal,
                @CachedLibrary("object") final InteropLibrary lib,
                @CachedLibrary(limit = "2") final InteropLibrary membersLib,
                @CachedLibrary(limit = "2") final InteropLibrary memberNameLib,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image) {
    // TODO: is unpacking really necessary?
    try {
        final Object members = lib.getMembers(object, includeInternal);
        final int size = (int) membersLib.getArraySize(members);
        final Object[] byteStrings = new Object[size];
        for (int i = 0; i < size; i++) {
            final Object memberName = membersLib.readArrayElement(members, i);
            byteStrings[i] = image.asByteString(memberNameLib.asString(memberName));
        }
        return image.asArrayOfObjects(byteStrings);
    } catch (final UnsupportedMessageException | InvalidArrayIndexException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example 3
Source File: HashemReadPropertyNode.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Specialization(guards = "arrays.hasArrayElements(receiver)", limit = "LIBRARY_LIMIT")
protected Object writeArray(Object receiver, Object index,
                            @CachedLibrary("receiver") InteropLibrary arrays,
                            @CachedLibrary("index") InteropLibrary numbers) {
    try {
        return arrays.readArrayElement(receiver, numbers.asLong(index));
    } catch (UnsupportedMessageException | InvalidArrayIndexException e) {
        // read was not successful. InHashemiwe only have basic support for errors.
        throw HashemUndefinedNameException.undefinedProperty(this, index);
    }
}
 
Example 4
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(limit = "1")
protected static final Object doTruffleObject(final TruffleObject object, final int index, @CachedLibrary("object") final InteropLibrary lib)
                throws UnsupportedMessageException, InvalidArrayIndexException {
    return lib.readArrayElement(object, index);
}