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

The following examples show how to use com.oracle.truffle.api.interop.InteropLibrary#getArraySize() . 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: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Specialization(limit = "3")
Object doMap(Object source, Type elementType, CUDARuntime runtime,
                @CachedLibrary("source") InteropLibrary interop,
                @CachedContext(GrCUDALanguage.class) @SuppressWarnings("unused") GrCUDAContext context,
                @Cached(value = "createLoop(source)", uncached = "createUncachedLoop(source, context)") CallTarget loop) {

    if (source instanceof DeviceArray && ((DeviceArray) source).getElementType() == elementType) {
        return source;
    }

    if (!interop.hasArrayElements(source)) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot map from non-array to DeviceArray");
    }

    long size;
    try {
        size = interop.getArraySize(source);
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot read array size");
    }
    DeviceArray result = new DeviceArray(runtime, size, elementType);
    loop.call(size, source, result);
    return result;
}
 
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: HashemGetSizeBuiltin.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Specialization(limit = "3")
public Object getSize(Object obj, @CachedLibrary("obj") InteropLibrary arrays) {
    try {
        return arrays.getArraySize(obj);
    } catch (UnsupportedMessageException e) {
        throw new HashemException("Element is not a valid array.", this);
    }
}
 
Example 4
Source File: ShreddedObject.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
public long getArraySize(@CachedLibrary("this.delegate") InteropLibrary interop) {
    try {
        return interop.getArraySize(delegate);
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new MapException("cannot get size from 'hasArrayElements' object:" + e.getMessage());
    }
}
 
Example 5
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.hasArrayElements(object)", limit = "2")
protected static final long doGetArraySize(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        return lib.getArraySize(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example 6
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.hasMembers(object)"}, limit = "2")
protected static final Object doGetMembers(@SuppressWarnings("unused") final Object receiver, final Object object, final boolean includeInternal,
                @CachedLibrary("object") final InteropLibrary lib,
                @CachedLibrary(limit = "2") final InteropLibrary sizeLib) {
    try {
        return sizeLib.getArraySize(lib.getMembers(object, includeInternal));
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example 7
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(limit = "1")
protected static final int doTruffleObject(final TruffleObject object, @CachedLibrary("object") final InteropLibrary lib) throws UnsupportedMessageException {
    return (int) lib.getArraySize(object);
}