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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations