com.oracle.truffle.api.interop.java.ToJavaNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truffle-api Show documentation
Show all versions of truffle-api Show documentation
Truffle is a multi-language framework for executing dynamic languages
that achieves high performance when combined with Graal.
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api.interop.java;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
final class ToJavaNode extends Node {
private static final Object[] EMPTY = {};
@Child private Node isExecutable = Message.IS_EXECUTABLE.createNode();
public Object convert(VirtualFrame frame, Object value, Class> type) {
Object convertedValue;
if (value == null) {
return null;
}
if (isPrimitiveType(value.getClass())) {
convertedValue = toPrimitive(value, type);
assert convertedValue != null;
} else if (value instanceof JavaObject && type.isInstance(((JavaObject) value).obj)) {
convertedValue = ((JavaObject) value).obj;
} else if (value instanceof TruffleObject && isJavaFunctionInterface(type) && isExecutable(frame, (TruffleObject) value)) {
convertedValue = asJavaFunction(type, (TruffleObject) value);
} else if (value instanceof TruffleObject) {
convertedValue = asJavaObject(type, null, (TruffleObject) value);
} else {
assert type.isAssignableFrom(value.getClass());
convertedValue = value;
}
return convertedValue;
}
private boolean isExecutable(VirtualFrame frame, TruffleObject object) {
return ForeignAccess.sendIsExecutable(isExecutable, frame, object);
}
@TruffleBoundary
private static boolean isJavaFunctionInterface(Class> type) {
if (!type.isInterface()) {
return false;
}
for (Annotation annotation : type.getAnnotations()) {
// TODO: don't compare strings here
// fix once Truffle uses JDK8
if (annotation.toString().equals("@java.lang.FunctionalInterface()")) {
return true;
}
}
if (type.getMethods().length == 1) {
return true;
}
return false;
}
@TruffleBoundary
private static T asJavaObject(Class clazz, Type type, TruffleObject foreignObject) {
Object obj;
if (clazz.isInstance(foreignObject)) {
obj = foreignObject;
} else {
if (!clazz.isInterface()) {
throw new IllegalArgumentException();
}
if (foreignObject == null) {
return null;
}
if (clazz == List.class && Boolean.TRUE.equals(binaryMessage(Message.HAS_SIZE, foreignObject))) {
Class> elementType = Object.class;
if (type instanceof ParameterizedType) {
ParameterizedType parametrizedType = (ParameterizedType) type;
final Type[] arr = parametrizedType.getActualTypeArguments();
if (arr.length == 1 && arr[0] instanceof Class) {
elementType = (Class>) arr[0];
}
}
obj = TruffleList.create(elementType, foreignObject);
} else {
obj = Proxy.newProxyInstance(clazz.getClassLoader(), new Class>[]{clazz}, new TruffleHandler(foreignObject));
}
}
return clazz.cast(obj);
}
@TruffleBoundary
private static T asJavaFunction(Class functionalType, TruffleObject function) {
Object obj = Proxy.newProxyInstance(functionalType.getClassLoader(), new Class>[]{functionalType}, new SingleHandler(function));
return functionalType.cast(obj);
}
private static final class SingleHandler implements InvocationHandler {
private final TruffleObject symbol;
private CallTarget target;
SingleHandler(TruffleObject obj) {
this.symbol = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
CompilerAsserts.neverPartOfCompilation();
Object[] args = arguments == null ? EMPTY : arguments;
if (target == null) {
Node executeMain = Message.createExecute(args.length).createNode();
RootNode symbolNode = new TemporaryRoot(TruffleLanguage.class, executeMain, symbol);
target = Truffle.getRuntime().createCallTarget(symbolNode);
}
Object ret = target.call(args);
return toJava(ret, method);
}
}
private static final class TruffleHandler implements InvocationHandler {
private final TruffleObject obj;
TruffleHandler(TruffleObject obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
CompilerAsserts.neverPartOfCompilation();
Object[] args = arguments == null ? EMPTY : arguments;
Object val;
for (int i = 0; i < args.length; i++) {
if (args[i] == null) {
continue;
}
if (Proxy.isProxyClass(args[i].getClass())) {
InvocationHandler h = Proxy.getInvocationHandler(args[i]);
if (h instanceof TruffleHandler) {
args[i] = ((TruffleHandler) h).obj;
}
}
}
if (Object.class == method.getDeclaringClass()) {
return method.invoke(obj, args);
}
String name = method.getName();
Message message = findMessage(method.getAnnotation(MethodMessage.class));
if (message == Message.WRITE) {
if (args.length != 1) {
throw new IllegalStateException("Method needs to have a single argument to handle WRITE message " + method);
}
message(Message.WRITE, obj, name, args[0]);
return null;
}
if (message == Message.HAS_SIZE || message == Message.IS_BOXED || message == Message.IS_EXECUTABLE || message == Message.IS_NULL || message == Message.GET_SIZE) {
return message(message, obj);
}
if (message == Message.READ) {
val = message(Message.READ, obj, name);
return toJava(val, method);
}
if (message == Message.UNBOX) {
val = message(Message.UNBOX, obj);
return toJava(val, method);
}
if (Message.createExecute(0).equals(message)) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy