Search in sources :

Example 71 with TruffleObject

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

the class ToJavaNode method convertImpl.

private Object convertImpl(Object value, Class<?> targetType, Type genericType, Object languageContext) {
    Object convertedValue;
    if (isAssignableFromTrufflePrimitiveType(targetType)) {
        convertedValue = primitive.toPrimitive(value, targetType);
        if (convertedValue != null) {
            return convertedValue;
        } else if (targetType == char.class || targetType == Character.class) {
            Integer safeChar = primitive.toInteger(value);
            if (safeChar != null) {
                int v = safeChar;
                if (v >= 0 && v < 65536) {
                    return (char) v;
                }
            }
        }
    }
    if (targetType == Value.class && languageContext != null) {
        convertedValue = value instanceof Value ? value : JavaInterop.toHostValue(value, languageContext);
    } else if (value instanceof TruffleObject) {
        convertedValue = asJavaObject((TruffleObject) value, targetType, genericType, languageContext);
    } else if (targetType.isAssignableFrom(value.getClass())) {
        convertedValue = value;
    } else {
        CompilerDirectives.transferToInterpreter();
        String reason;
        if (isAssignableFromTrufflePrimitiveType(targetType)) {
            reason = "Invalid or lossy primitive coercion.";
        } else {
            reason = "Unsupported target type.";
        }
        throw JavaInteropErrors.cannotConvert(languageContext, value, targetType, reason);
    }
    return convertedValue;
}
Also used : Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 72 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject 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 73 with TruffleObject

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

the class ObjectProxyHandler method asJavaFunction.

@CompilerDirectives.TruffleBoundary
static <T> T asJavaFunction(Class<T> functionalType, TruffleObject function, Object languageContext) {
    assert JavaInterop.isJavaFunctionInterface(functionalType);
    Method functionalInterfaceMethod = functionalInterfaceMethod(functionalType);
    final FunctionProxyHandler handler = new FunctionProxyHandler(function, functionalInterfaceMethod, languageContext);
    Object obj = Proxy.newProxyInstance(functionalType.getClassLoader(), new Class<?>[] { functionalType }, handler);
    return functionalType.cast(obj);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Method(java.lang.reflect.Method) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 74 with TruffleObject

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

the class SLInstrumentTest method verifyLexicalScopes.

@CompilerDirectives.TruffleBoundary
private static void verifyLexicalScopes(Iterable<Scope> lexicalScopes, Iterable<Scope> dynamicScopes, int line, MaterializedFrame frame) {
    int depth = 0;
    switch(line) {
        case 1:
            break;
        case 2:
            for (Scope ls : lexicalScopes) {
                // Test that ls.getNode() returns the current root node:
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", null);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", null);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                // Test that ls.getNode() returns the current root node:
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", "n_n");
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", "n_n");
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        case 3:
        case 7:
        case 19:
        case 20:
            for (Scope ls : lexicalScopes) {
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", null);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables, "n", null, "a", null);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                checkRootNode(ls, "test", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments, "n", "n_n");
                TruffleObject variables = (TruffleObject) ls.getVariables();
                long aVal = (line < 19) ? 1L : 4L;
                checkVars(variables, "n", "n_n", "a", aVal);
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        case 4:
        case 8:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", "n_n", "a", 1L);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", "n_n");
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 5:
        case 9:
        case 10:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long bVal = (line == 5) ? 10L : 20L;
                    checkVars(variables, "b", bVal);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long aVal = (line == 10) ? 0L : 1L;
                    checkVars(variables, "n", "n_n", "a", aVal);
                    TruffleObject arguments = (TruffleObject) ls.getArguments();
                    checkVars(arguments, "n", "n_n");
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 11:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 2, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", 20L, "c", 1L);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 2, depth);
            break;
        case 12:
        case 13:
        case 14:
        case 15:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 3, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long bVal = (line < 14) ? 20L : 5L;
                    long cVal = (line < 15) ? 1L : 6L;
                    checkVars(variables, "b", bVal, "c", cVal);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    long aVal = (line == 12) ? 0L : 4L;
                    checkVars(variables, "n", "n_n", "a", aVal);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 3, depth);
            break;
        case 16:
            for (Scope ls : lexicalScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "d", null);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", null, "c", null);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", null, "a", null);
                }
                depth++;
            }
            assertEquals("LexicalScope depth", 3, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                if (depth == 0) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "d", 7L);
                    assertNull(ls.getArguments());
                } else if (depth == 1) {
                    checkBlock(ls);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "b", 5L, "c", 6L);
                    assertNull(ls.getArguments());
                } else {
                    checkRootNode(ls, "test", frame);
                    TruffleObject variables = (TruffleObject) ls.getVariables();
                    checkVars(variables, "n", "n_n", "a", 4L);
                }
                depth++;
            }
            assertEquals("DynamicScope depth", 3, depth);
            break;
        case 22:
        case 23:
            for (Scope ls : lexicalScopes) {
                checkRootNode(ls, "main", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables);
                depth++;
            }
            assertEquals("LexicalScope depth", 1, depth);
            depth = 0;
            for (Scope ls : dynamicScopes) {
                checkRootNode(ls, "main", frame);
                TruffleObject arguments = (TruffleObject) ls.getArguments();
                checkVars(arguments);
                TruffleObject variables = (TruffleObject) ls.getVariables();
                checkVars(variables);
                depth++;
            }
            assertEquals("DynamicScope depth", 1, depth);
            break;
        default:
            fail("Untested line: " + line);
            break;
    }
}
Also used : Scope(com.oracle.truffle.api.Scope) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 75 with TruffleObject

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

the class SLInstrumentTest method checkVars.

private static void checkVars(TruffleObject vars, Object... expected) {
    for (int i = 0; i < expected.length; i += 2) {
        String name = (String) expected[i];
        Object value = expected[i + 1];
        assertTrue(name, contains(vars, name));
        if (value != null) {
            assertEquals(name, value, read(vars, name));
        } else {
            assertTrue(isNull((TruffleObject) read(vars, name)));
        }
    }
    assertEquals(expected.length / 2, keySize(vars));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)201 Test (org.junit.Test)135 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)34 InteropException (com.oracle.truffle.api.interop.InteropException)18 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)17 Specialization (com.oracle.truffle.api.dsl.Specialization)16 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)14 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)12 ArrayTruffleObject (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest.ArrayTruffleObject)10 Node (com.oracle.truffle.api.nodes.Node)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 CallTarget (com.oracle.truffle.api.CallTarget)7 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)7 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)7 LinkedHashMap (java.util.LinkedHashMap)7 Source (com.oracle.truffle.api.source.Source)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5