Search in sources :

Example 16 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class TestMemberAccess method testArrayOutOfBoundsAccess.

@Test
public void testArrayOutOfBoundsAccess() throws InteropException {
    Object[] array = new Object[1];
    TruffleObject arrayObject = asTruffleObject(array);
    assertTrue(INTEROP.hasArrayElements(arrayObject));
    INTEROP.readArrayElement(arrayObject, 0);
    try {
        INTEROP.readArrayElement(arrayObject, 1);
        fail();
    } catch (InvalidArrayIndexException e) {
    }
}
Also used : InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 17 with InvalidArrayIndexException

use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.

the class HostToTypeNode method truffleObjectToArray.

private static Object truffleObjectToArray(HostContext hostContext, InteropLibrary interop, Object receiver, Class<?> arrayType, Type genericArrayType) {
    Class<?> componentType = arrayType.getComponentType();
    long size;
    try {
        size = interop.getArraySize(receiver);
    } catch (UnsupportedMessageException e1) {
        assert false : "unexpected language behavior";
        size = 0;
    }
    size = Math.min(size, Integer.MAX_VALUE);
    Object array = Array.newInstance(componentType, (int) size);
    Type genericComponentType = getGenericArrayComponentType(genericArrayType);
    for (int i = 0; i < size; i++) {
        Object guestValue;
        try {
            guestValue = interop.readArrayElement(receiver, i);
        } catch (InvalidArrayIndexException e) {
            throw HostInteropErrors.invalidArrayIndex(hostContext, receiver, componentType, i);
        } catch (UnsupportedMessageException e) {
            throw HostInteropErrors.arrayReadUnsupported(hostContext, receiver, componentType);
        }
        Object hostValue = HostToTypeNodeGen.getUncached().execute(hostContext, guestValue, componentType, genericComponentType, true);
        Array.set(array, i, hostValue);
    }
    return array;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)17 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)12 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)9 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)8 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)8 Test (org.junit.Test)4 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)3 WebAssembly (org.graalvm.wasm.api.WebAssembly)3 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 WasmModule (org.graalvm.wasm.WasmModule)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)1 StandardTags (com.oracle.truffle.api.instrumentation.StandardTags)1 Tag (com.oracle.truffle.api.instrumentation.Tag)1 NodeLibrary (com.oracle.truffle.api.interop.NodeLibrary)1 StopIterationException (com.oracle.truffle.api.interop.StopIterationException)1 ExportMessage (com.oracle.truffle.api.library.ExportMessage)1 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)1