Java Code Examples for com.oracle.truffle.api.interop.ArityException#create()

The following examples show how to use com.oracle.truffle.api.interop.ArityException#create() . 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: AbstractSqueakObject.java    From trufflesqueak with MIT License 6 votes vote down vote up
@Specialization(rewriteOn = RespecializeException.class)
protected static final Object invokeMember(final AbstractSqueakObject receiver, final String member, final Object[] arguments,
                @Shared("lookupNode") @Cached final LookupMethodByStringNode lookupNode,
                @Shared("classNode") @Cached final SqueakObjectClassNode classNode,
                @Exclusive @Cached final WrapToSqueakNode wrapNode,
                @Exclusive @Cached final DispatchUneagerlyNode dispatchNode) throws ArityException {
    final int actualArity = arguments.length;
    final Object methodObject = lookupNode.executeLookup(classNode.executeLookup(receiver), toSelector(member, actualArity));
    if (methodObject == null) {
        CompilerDirectives.transferToInterpreterAndInvalidate();
        /* DoesNotUnderstand, rewrite this specialization. */
        throw new RespecializeException();
    }
    final CompiledMethodObject method = (CompiledMethodObject) methodObject;
    final int expectedArity = method.getNumArgs();
    if (actualArity == expectedArity) {
        return dispatchNode.executeDispatch(method, ArrayUtils.copyWithFirst(wrapNode.executeObjects(arguments), receiver), NilObject.SINGLETON);
    } else {
        throw ArityException.create(1 + expectedArity, 1 + actualArity);
    }
}
 
Example 2
Source File: DeviceArrayCopyFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary(limit = "3") InteropLibrary pointerAccess,
                @CachedLibrary(limit = "3") InteropLibrary numElementsAccess) throws UnsupportedTypeException, ArityException, IndexOutOfBoundsException {
    long numElements;
    if (arguments.length == 1) {
        numElements = deviceArray.getArraySize();
    } else if (arguments.length == 2) {
        numElements = extractNumber(arguments[1], "numElements", numElementsAccess);
    } else {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    long pointer = extractPointer(arguments[0], "fromPointer", pointerAccess);
    if (direction == CopyDirection.FROM_POINTER) {
        deviceArray.copyFrom(pointer, numElements);
    }
    if (direction == CopyDirection.TO_POINTER) {
        deviceArray.copyTo(pointer, numElements);
    }
    return deviceArray;
}
 
Example 3
Source File: MapFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Specialization(guards = "VALUE.equals(member)")
static MapFunctionBase readMemberValue(MapFunction receiver, String member) {
    return new MapFunctionBase(arguments -> {
        if (arguments.length < 1) {
            throw ArityException.create(1, arguments.length);
        }
        String name = checkString(arguments[0], "name of created value expected");
        if (arguments.length == 1) {
            return receiver.value(name);
        } else {
            Object function = arguments[1];
            Object[] args = Arrays.copyOfRange(arguments, 2, arguments.length);
            return receiver.value(name, function, args);
        }
    });
}
 
Example 4
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 5
Source File: JalangiAdapter.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@TruffleBoundary
private static boolean checkArguments(int count, Object[] arguments, String funcName) throws ArityException {
    if (arguments.length < count) {
        Logger.error("call to " + funcName + " expects " + count + " argument(s)");
        if (!GlobalConfiguration.IGNORE_JALANGI_EXCEPTION) {
            CompilerDirectives.transferToInterpreterAndInvalidate();
            throw ArityException.create(count, arguments.length);
        }
        return false;
    } else if (arguments.length > count) {
        Logger.warning("extra arguments passed to " + funcName);
    }
    return true;
}
 
Example 6
Source File: DeviceArray.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments) throws ArityException {
    if (arguments.length != 0) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(0, arguments.length);
    }
    freeMemory();
    return NoneValue.get();
}
 
Example 7
Source File: TypedDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@TruffleBoundary
public Object call(Object[] arguments) throws ArityException, UnsupportedTypeException {
    if (arguments.length < 1) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    return DeviceArrayFunction.createArray(arguments, 0, elementType, runtime);
}
 
Example 8
Source File: TypedMapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
public Object execute(Object[] arguments,
                @Cached MapArrayNode mapNode) throws ArityException {
    if (arguments.length != 1) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    return mapNode.execute(arguments[0], elementType, runtime);
}
 
Example 9
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
public Object execute(Object[] arguments,
                @CachedLibrary(limit = "2") InteropLibrary stringInterop,
                @Cached("createIdentityProfile()") ValueProfile elementTypeStringProfile,
                @Cached("createIdentityProfile()") ValueProfile elementTypeProfile,
                @Cached MapArrayNode mapNode) throws ArityException, UnsupportedTypeException {
    if (arguments.length < 1) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    String typeName;
    try {
        typeName = elementTypeStringProfile.profile(stringInterop.asString(arguments[0]));
    } catch (UnsupportedMessageException e1) {
        throw UnsupportedTypeException.create(arguments, "first argument of MapDeviceArray must be string (type name)");
    }
    Type elementType;
    try {
        elementType = elementTypeProfile.profile(Type.fromGrCUDATypeString(typeName));
    } catch (TypeException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAInternalException(e.getMessage());
    }
    if (arguments.length == 1) {
        return new TypedMapDeviceArrayFunction(runtime, elementType);
    } else {
        if (arguments.length != 2) {
            CompilerDirectives.transferToInterpreter();
            throw ArityException.create(2, arguments.length);
        }
        return mapNode.execute(arguments[1], elementType, runtime);
    }
}
 
Example 10
Source File: AbstractSqueakObject.java    From trufflesqueak with MIT License 5 votes vote down vote up
/**
 * The same as {@link #toSelector(String)}, but may return alternative selector depending on
 * actualArity.
 *
 * @param identifier for interop
 * @param actualArity of selector
 * @return String for Smalltalk selector
 */
@TruffleBoundary
private static String toSelector(final String identifier, final int actualArity) throws ArityException {
    final String selector = identifier.replace('_', ':');
    final int expectedArity = (int) selector.chars().filter(ch -> ch == ':').count();
    if (expectedArity == actualArity) {
        return selector;
    } else {
        if (expectedArity + 1 == actualArity && !selector.endsWith(":")) {
            return selector + ":";
        } else {
            throw ArityException.create(1 + expectedArity, 1 + actualArity);
        }
    }
}
 
Example 11
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop) throws UnsupportedTypeException, ArityException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    return new ShreddedObject(parentInterop.execute(parent, arguments));
}
 
Example 12
Source File: MapFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@ExportMessage
@TruffleBoundary
public MappedFunction execute(Object[] arguments) throws ArityException, UnsupportedTypeException {
    if (arguments.length == 0) {
        throw ArityException.create(1, 0);
    }
    Object function = arguments[0];
    if (!INTEROP.isExecutable(function)) {
        throw UnsupportedTypeException.create(arguments, "expecting executable function as first argument");
    }
    String description = null;
    if (arguments.length > 1) {
        Object last = arguments[arguments.length - 1];
        if (INTEROP.isString(last)) {
            try {
                description = INTEROP.asString(last);
            } catch (UnsupportedMessageException e) {
                throw new GrCUDAInternalException("mismatch between isString and asString");
            }
        }
    }
    Object[] values = new Object[arguments.length - 1 - (description == null ? 0 : 1)];
    ArgumentSet argSet = new ArgumentSet();
    ArgumentSet shreddedArgSet = new ArgumentSet();
    ArgumentSet valueSet = new ArgumentSet();

    for (int i = 0; i < values.length; i++) {
        values[i] = bindArgument(arguments[i + 1], argSet, shreddedArgSet, valueSet);
    }
    Object boundReturn = bindArgument(returnValue, argSet, shreddedArgSet, valueSet);
    int[] shreddedIndexes = new int[shreddedArgSet.nameList.size()];
    for (String name : shreddedArgSet.nameList.getKeys()) {
        shreddedIndexes[shreddedArgSet.nameList.get(name)] = argSet.readMember(name);
    }
    Integer returnValueIndex = valueSet.nameList.get("return");
    return new MappedFunction(function, values, shreddedIndexes, valueSet.nameList.size(), boundReturn, returnValueIndex, description);
}
 
Example 13
Source File: MapFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Specialization(guards = "SIZE.equals(member)")
static MapFunctionBase readMemberSize(MapFunction receiver, String member) {
    return new MapFunctionBase(arguments -> {
        if (arguments.length == 0) {
            throw ArityException.create(1, 0);
        }
        try {
            return receiver.size((MapArgObject) arguments[0], Arrays.copyOfRange(arguments, 1, arguments.length, MapArgObject[].class));
        } catch (ClassCastException | ArrayStoreException e) {
            throw UnsupportedTypeException.create(arguments, "expected argument objects");
        }
    });
}
 
Example 14
Source File: Device.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("static-method")
@ExportMessage
public Object execute(Object[] arguments) throws ArityException {
    if (arguments.length != 0) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(0, arguments.length);
    }
    runtime.cudaSetDevice(deviceId);
    return NoneValue.get();
}
 
Example 15
Source File: BlockClosureObject.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
public Object execute(final Object[] arguments,
                @Exclusive @Cached final WrapToSqueakNode wrapNode) throws ArityException {
    if (getNumArgs() == arguments.length) {
        final Object[] frameArguments = FrameAccess.newClosureArgumentsTemplate(this, NilObject.SINGLETON, arguments.length);
        for (int i = 0; i < arguments.length; i++) {
            frameArguments[FrameAccess.getArgumentStartIndex() + i] = wrapNode.executeWrap(arguments[i]);
        }
        return getCompiledBlock().getCallTarget().call(frameArguments);
    } else {
        throw ArityException.create((int) getNumArgs(), arguments.length);
    }
}
 
Example 16
Source File: MultiDimDeviceArray.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments) throws ArityException {
    if (arguments.length != 0) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(0, arguments.length);
    }
    freeMemory();
    return NoneValue.get();
}
 
Example 17
Source File: AbstractSqueakObject.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(replaces = "invokeMember")
protected static final Object invokeMemberWithDNU(final AbstractSqueakObject receiver, final String member, final Object[] arguments,
                @Shared("lookupNode") @Cached final LookupMethodByStringNode lookupNode,
                @Shared("classNode") @Cached final SqueakObjectClassNode classNode,
                @Exclusive @Cached final WrapToSqueakNode wrapNode,
                @Exclusive @Cached final DispatchUneagerlyNode dispatchNode,
                @Exclusive @Cached final AbstractPointersObjectWriteNode writeNode,
                @Exclusive @Cached("createBinaryProfile()") final ConditionProfile hasMethodProfile,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image) throws UnsupportedMessageException, ArityException {
    final int actualArity = arguments.length;
    final String selector = toSelector(member, actualArity);
    final ClassObject classObject = classNode.executeLookup(receiver);
    final Object methodObject = lookupNode.executeLookup(classObject, selector);
    if (hasMethodProfile.profile(methodObject instanceof CompiledMethodObject)) {
        final CompiledMethodObject method = (CompiledMethodObject) methodObject;
        final int expectedArity = method.getNumArgs();
        if (actualArity == expectedArity) {
            return dispatchNode.executeDispatch(method, ArrayUtils.copyWithFirst(wrapNode.executeObjects(arguments), receiver), NilObject.SINGLETON);
        } else {
            throw ArityException.create(1 + expectedArity, 1 + actualArity);
        }
    } else {
        final CompiledMethodObject doesNotUnderstandMethodObject = (CompiledMethodObject) lookupNode.executeLookup(classObject, "doesNotUnderstand:");
        final NativeObject symbol = (NativeObject) image.asByteString(selector).send("asSymbol");
        final PointersObject message = image.newMessage(writeNode, symbol, classObject, arguments);
        try {
            return dispatchNode.executeDispatch(doesNotUnderstandMethodObject, new Object[]{receiver, message}, NilObject.SINGLETON);
        } catch (final ProcessSwitch ps) {
            CompilerDirectives.transferToInterpreter();
            /*
             * A ProcessSwitch exception is thrown in case Squeak/Smalltalk wants to open
             * the debugger. This needs to be avoided in headless mode.
             */
            if (image.isHeadless()) {
                throw UnsupportedMessageException.create();
            } else {
                throw ps;
            }
        }
    }
}
 
Example 18
Source File: ClassObject.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(guards = "arguments.length > 1")
protected static final Object doMultipleArguments(@SuppressWarnings("unused") final ClassObject receiver, final Object[] arguments) throws ArityException {
    throw ArityException.create(1, arguments.length);
}
 
Example 19
Source File: MapFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static void checkArity(Object[] arguments, int expectedArity) throws ArityException {
    if (arguments.length != expectedArity) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(expectedArity, arguments.length);
    }
}
 
Example 20
Source File: Function.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void checkArgumentLength(Object[] arguments, int expected) throws ArityException {
    if (arguments.length != expected) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(expected, arguments.length);
    }
}