Search in sources :

Example 6 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class ToJavaNode method asJavaObject.

@TruffleBoundary
private <T> T asJavaObject(TruffleObject truffleObject, Class<T> targetType, Type genericType, Object languageContext) {
    Objects.requireNonNull(truffleObject);
    Object obj;
    if (primitive.isNull(truffleObject)) {
        if (targetType.isPrimitive()) {
            throw JavaInteropErrors.nullCoercion(languageContext, truffleObject, targetType);
        }
        return null;
    } else if (JavaObject.isJavaInstance(targetType, truffleObject)) {
        obj = JavaObject.valueOf(truffleObject);
    } else if (targetType == Object.class) {
        obj = convertToObject(truffleObject, languageContext);
    } else if (languageContext == null && targetType.isInstance(truffleObject)) {
        // legacy support for cast rather than wrap
        return targetType.cast(truffleObject);
    } else if (targetType == List.class) {
        if (primitive.hasSize(truffleObject)) {
            boolean implementsFunction = shouldImplementFunction(truffleObject);
            TypeAndClass<?> elementType = getGenericParameterType(genericType, 0);
            obj = TruffleList.create(languageContext, truffleObject, implementsFunction, elementType.clazz, elementType.type);
        } else {
            throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Value must have array elements.");
        }
    } else if (targetType == Map.class) {
        Class<?> keyClazz = getGenericParameterType(genericType, 0).clazz;
        TypeAndClass<?> valueType = getGenericParameterType(genericType, 1);
        if (!isSupportedMapKeyType(keyClazz)) {
            throw newInvalidKeyTypeException(keyClazz);
        }
        boolean hasSize = (Number.class.isAssignableFrom(keyClazz)) && primitive.hasSize(truffleObject);
        boolean hasKeys = (keyClazz == Object.class || keyClazz == String.class) && primitive.hasKeys(truffleObject);
        if (hasKeys || hasSize) {
            boolean implementsFunction = shouldImplementFunction(truffleObject);
            obj = TruffleMap.create(languageContext, truffleObject, implementsFunction, keyClazz, valueType.clazz, valueType.type);
        } else {
            throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Value must have members or array elements.");
        }
    } else if (targetType == Function.class) {
        TypeAndClass<?> returnType = getGenericParameterType(genericType, 1);
        if (isExecutable(truffleObject) || isInstantiable(truffleObject)) {
            obj = TruffleFunction.create(languageContext, truffleObject, returnType.clazz, returnType.type);
        } else if (!TruffleOptions.AOT && ForeignAccess.sendHasKeys(hasKeysNode, truffleObject)) {
            obj = JavaInteropReflect.newProxyInstance(targetType, truffleObject, languageContext);
        } else {
            throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Value must be executable or instantiable.");
        }
    } else if (targetType.isArray()) {
        if (primitive.hasSize(truffleObject)) {
            obj = truffleObjectToArray(truffleObject, targetType, genericType, languageContext);
        } else {
            throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Value must have array elements.");
        }
    } else if (!TruffleOptions.AOT && targetType.isInterface()) {
        if (JavaInterop.isJavaFunctionInterface(targetType) && (isExecutable(truffleObject) || isInstantiable(truffleObject))) {
            obj = JavaInteropReflect.asJavaFunction(targetType, truffleObject, languageContext);
        } else if (ForeignAccess.sendHasKeys(hasKeysNode, truffleObject)) {
            obj = JavaInteropReflect.newProxyInstance(targetType, truffleObject, languageContext);
        } else {
            if (languageContext == null) {
                // legacy support
                obj = JavaInteropReflect.newProxyInstance(targetType, truffleObject, languageContext);
            } else {
                throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Value must have members.");
            }
        }
    } else {
        throw JavaInteropErrors.cannotConvert(languageContext, truffleObject, targetType, "Unsupported target type.");
    }
    assert targetType.isInstance(obj);
    return targetType.cast(obj);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Map(java.util.Map) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 7 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class ShapeImpl method getPropertyList.

/**
 * @since 0.17 or earlier
 */
@TruffleBoundary
@Override
public final List<Property> getPropertyList() {
    Property[] props = new Property[getPropertyCount()];
    int i = props.length;
    for (Iterator<Property> it = this.propertyMap.reverseOrderedValueIterator(); it.hasNext(); ) {
        Property currentProperty = it.next();
        if (!currentProperty.isHidden()) {
            props[--i] = currentProperty;
        }
    }
    return Arrays.asList(props);
}
Also used : Property(com.oracle.truffle.api.object.Property) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 8 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class ShapeImpl method getKeyList.

/**
 * @since 0.17 or earlier
 */
@TruffleBoundary
@Override
public final List<Object> getKeyList() {
    Object[] props = new Object[getPropertyCount()];
    int i = props.length;
    for (Iterator<Property> it = this.propertyMap.reverseOrderedValueIterator(); it.hasNext(); ) {
        Property currentProperty = it.next();
        if (!currentProperty.isHidden()) {
            props[--i] = currentProperty.getKey();
        }
    }
    return Arrays.asList(props);
}
Also used : DynamicObject(com.oracle.truffle.api.object.DynamicObject) Property(com.oracle.truffle.api.object.Property) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 9 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class ShapeImpl method getPropertyListInternal.

/**
 * Returns all (also hidden) Property objects in this shape.
 *
 * @param ascending desired order
 * @since 0.17 or earlier
 */
@TruffleBoundary
@Override
public final List<Property> getPropertyListInternal(boolean ascending) {
    Property[] props = new Property[this.propertyMap.size()];
    int i = ascending ? props.length : 0;
    for (Iterator<Property> it = this.propertyMap.reverseOrderedValueIterator(); it.hasNext(); ) {
        Property current = it.next();
        if (ascending) {
            props[--i] = current;
        } else {
            props[i++] = current;
        }
    }
    return Arrays.asList(props);
}
Also used : Property(com.oracle.truffle.api.object.Property) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 10 with TruffleBoundary

use of com.oracle.truffle.api.CompilerDirectives.TruffleBoundary in project graal by oracle.

the class ShapeImpl method changeType.

/**
 * @since 0.17 or earlier
 */
@Override
@TruffleBoundary
public final ShapeImpl changeType(ObjectType newOps) {
    ObjectTypeTransition transition = new ObjectTypeTransition(newOps);
    ShapeImpl cachedShape = queryTransition(transition);
    if (cachedShape != null) {
        return layout.getStrategy().ensureValid(cachedShape);
    }
    ShapeImpl newShape = createShape(layout, sharedData, this, newOps, propertyMap, transition, allocator(), id);
    addDirectTransition(transition, newShape);
    return newShape;
}
Also used : ObjectTypeTransition(com.oracle.truffle.object.Transition.ObjectTypeTransition) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)49 RootNode (com.oracle.truffle.api.nodes.RootNode)6 Property (com.oracle.truffle.api.object.Property)6 Specialization (com.oracle.truffle.api.dsl.Specialization)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)5 BigInteger (java.math.BigInteger)4 Node (com.oracle.truffle.api.nodes.Node)3 SourceSection (com.oracle.truffle.api.source.SourceSection)3 ByteBuffer (java.nio.ByteBuffer)3 Substitute (com.oracle.svm.core.annotate.Substitute)2 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)2 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)2 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)2 Source (com.oracle.truffle.api.source.Source)2 LLVMContext (com.oracle.truffle.llvm.runtime.LLVMContext)2 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Assumption (com.oracle.truffle.api.Assumption)1 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1