com.oracle.truffle.polyglot.HostObject 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) 2016, 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.polyglot;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.ForeignAccess;
import com.oracle.truffle.api.interop.KeyInfo;
import com.oracle.truffle.api.interop.Message;
import com.oracle.truffle.api.interop.MessageResolution;
import com.oracle.truffle.api.interop.Resolve;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ArrayReadNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ArrayReadNodeGen.ArrayGetNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ArrayRemoveNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ArrayWriteNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ArrayWriteNodeGen.ArraySetNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.KeyInfoCacheNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.LookupConstructorNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.LookupFieldNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.LookupFunctionalMethodNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.LookupInnerClassNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.LookupMethodNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.MapRemoveNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.ReadFieldNodeGen;
import com.oracle.truffle.polyglot.HostObjectMRFactory.WriteFieldNodeGen;
import com.oracle.truffle.polyglot.PolyglotLanguageContext.ToGuestValueNode;
final class HostObject implements TruffleObject {
static final HostObject NULL = new HostObject(null, null, false);
final Object obj;
final PolyglotLanguageContext languageContext;
private final boolean staticClass;
private HostObject(Object obj, PolyglotLanguageContext languageContext, boolean staticClass) {
this.obj = obj;
this.languageContext = languageContext;
this.staticClass = staticClass;
}
static HostObject forClass(Class> clazz, PolyglotLanguageContext languageContext) {
assert clazz != null;
return new HostObject(clazz, languageContext, false);
}
static HostObject forStaticClass(Class> clazz, PolyglotLanguageContext languageContext) {
assert clazz != null;
return new HostObject(clazz, languageContext, true);
}
static HostObject forObject(Object object, PolyglotLanguageContext languageContext) {
assert object != null && !(object instanceof Class>);
return new HostObject(object, languageContext, false);
}
static boolean isInstance(Object obj) {
return obj instanceof HostObject;
}
static boolean isInstance(TruffleObject obj) {
return obj instanceof HostObject;
}
static boolean isStaticClass(Object object) {
return object instanceof HostObject && ((HostObject) object).isStaticClass();
}
static boolean isJavaInstance(Class> targetType, Object javaObject) {
if (javaObject instanceof HostObject) {
final Object value = valueOf((HostObject) javaObject);
return targetType.isInstance(value);
} else {
return false;
}
}
boolean isPrimitive() {
return PolyglotImpl.isGuestPrimitive(obj);
}
static Object valueOf(TruffleObject value) {
final HostObject obj = (HostObject) value;
return obj.obj;
}
@Override
public ForeignAccess getForeignAccess() {
return HostObjectMRForeign.ACCESS;
}
@Override
public int hashCode() {
return System.identityHashCode(obj);
}
boolean isClass() {
return obj instanceof Class>;
}
boolean isArray() {
return obj != null && obj.getClass().isArray();
}
boolean isNull() {
return obj == null;
}
boolean isStaticClass() {
return staticClass;
}
Class> getObjectClass() {
return obj == null ? null : obj.getClass();
}
Class> asStaticClass() {
assert isStaticClass();
return (Class>) obj;
}
Class> asClass() {
assert isClass();
return (Class>) obj;
}
/**
* Gets the {@link Class} for member lookups.
*/
Class> getLookupClass() {
if (obj == null) {
return null;
} else if (isStaticClass()) {
return asStaticClass();
} else {
return obj.getClass();
}
}
@Override
public boolean equals(Object o) {
if (o instanceof HostObject) {
HostObject other = (HostObject) o;
return this.obj == other.obj && this.languageContext == other.languageContext;
}
return false;
}
@Override
public String toString() {
if (obj == null) {
return "null";
}
if (isClass()) {
return "JavaClass[" + asClass().getTypeName() + "]";
}
return "JavaObject[" + obj + " (" + getObjectClass().getTypeName() + ")" + "]";
}
}
@MessageResolution(receiverType = HostObject.class)
class HostObjectMR {
@Resolve(message = "GET_SIZE")
abstract static class ArrayGetSizeNode extends Node {
public Object access(HostObject receiver) {
Object obj = receiver.obj;
if (obj != null) {
if (obj.getClass().isArray()) {
return Array.getLength(obj);
} else if (obj instanceof List>) {
return ((List>) obj).size();
}
}
CompilerDirectives.transferToInterpreter();
throw UnsupportedMessageException.raise(Message.GET_SIZE);
}
}
@Resolve(message = "HAS_SIZE")
abstract static class ArrayHasSizeNode extends Node {
public Object access(HostObject receiver) {
Object obj = receiver.obj;
if (obj == null) {
return false;
}
return obj.getClass().isArray() || obj instanceof List>;
}
}
@Resolve(message = "INVOKE")
abstract static class InvokeNode extends Node {
private static final Message INVOKE = Message.INVOKE;
@Child private LookupMethodNode lookupMethod;
@Child private HostExecuteNode executeMethod;
@Child private LookupFieldNode lookupField;
@Child private ReadFieldNode readField;
@Child private Node sendIsExecutableNode;
@Child private Node sendExecuteNode;
public Object access(HostObject object, String name, Object[] args) {
if (TruffleOptions.AOT || object.isNull()) {
throw UnsupportedMessageException.raise(INVOKE);
}
boolean isStatic = object.isStaticClass();
Class> lookupClass = object.getLookupClass();
// (1) look for a method; if found, invoke it on obj.
HostMethodDesc foundMethod = lookupMethod().execute(lookupClass, name, isStatic);
if (foundMethod != null) {
return executeMethod().execute(foundMethod, object.obj, args, object.languageContext);
}
// (2) look for a field; if found, read its value and if that IsExecutable, Execute it.
HostFieldDesc foundField = lookupField().execute(lookupClass, name, isStatic);
if (foundField != null) {
Object fieldValue = readField().execute(foundField, object);
if (fieldValue instanceof TruffleObject) {
TruffleObject fieldObject = (TruffleObject) fieldValue;
if (sendIsExecutableNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
sendIsExecutableNode = insert(Message.IS_EXECUTABLE.createNode());
}
boolean isExecutable = ForeignAccess.sendIsExecutable(sendIsExecutableNode, fieldObject);
if (isExecutable) {
if (sendExecuteNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
sendExecuteNode = insert(Message.EXECUTE.createNode());
}
try {
return ForeignAccess.sendExecute(sendExecuteNode, fieldObject, args);
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
throw e.raise();
}
}
}
}
throw UnknownIdentifierException.raise(name);
}
private LookupMethodNode lookupMethod() {
if (lookupMethod == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupMethod = insert(LookupMethodNodeGen.create());
}
return lookupMethod;
}
private HostExecuteNode executeMethod() {
if (executeMethod == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
executeMethod = insert(HostExecuteNode.create());
}
return executeMethod;
}
private LookupFieldNode lookupField() {
if (lookupField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupField = insert(LookupFieldNodeGen.create());
}
return lookupField;
}
private ReadFieldNode readField() {
if (readField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
readField = insert(ReadFieldNodeGen.create());
}
return readField;
}
}
@Resolve(message = "IS_INSTANTIABLE")
abstract static class IsInstantiableObjectNode extends Node {
@Child private LookupConstructorNode lookupConstructor;
public Object access(HostObject receiver) {
if (TruffleOptions.AOT) {
return false;
}
return receiver.isClass() && lookupConstructor().execute(receiver.asClass()) != null;
}
private LookupConstructorNode lookupConstructor() {
if (lookupConstructor == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupConstructor = insert(LookupConstructorNodeGen.create());
}
return lookupConstructor;
}
}
@Resolve(message = "NEW")
abstract static class NewNode extends Node {
private static final Message NEW = Message.NEW;
@Child private LookupConstructorNode lookupConstructor;
@Child private HostExecuteNode executeMethod;
@Child private ToHostNode toJava;
public Object access(HostObject receiver, Object[] args) {
if (TruffleOptions.AOT) {
throw UnsupportedMessageException.raise(NEW);
}
if (receiver.isClass()) {
Class> javaClass = receiver.asClass();
if (javaClass.isArray()) {
return newArray(receiver, args);
}
HostMethodDesc constructor = lookupConstructor().execute(javaClass);
if (constructor != null) {
return executeMethod().execute(constructor, null, args, receiver.languageContext);
}
}
throw UnsupportedMessageException.raise(NEW);
}
private Object newArray(HostObject receiver, Object[] args) {
if (args.length != 1) {
throw ArityException.raise(1, args.length);
}
if (toJava == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
toJava = insert(ToHostNode.create());
}
int length;
try {
length = (int) toJava.execute(args[0], int.class, null, receiver.languageContext);
} catch (ClassCastException | NullPointerException e) {
// conversion failed by ToJavaNode
throw UnsupportedTypeException.raise(e, args);
}
Object array = Array.newInstance(receiver.asClass().getComponentType(), length);
return HostObject.forObject(array, receiver.languageContext);
}
private LookupConstructorNode lookupConstructor() {
if (lookupConstructor == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupConstructor = insert(LookupConstructorNodeGen.create());
}
return lookupConstructor;
}
private HostExecuteNode executeMethod() {
if (executeMethod == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
executeMethod = insert(HostExecuteNode.create());
}
return executeMethod;
}
}
@Resolve(message = "IS_NULL")
abstract static class NullCheckNode extends Node {
public Object access(HostObject object) {
return object.isNull();
}
}
@Resolve(message = "IS_BOXED")
abstract static class BoxedCheckNode extends Node {
@Child private ToHostPrimitiveNode primitive = ToHostPrimitiveNode.create();
public Object access(HostObject object) {
return object.isPrimitive();
}
}
@Resolve(message = "UNBOX")
abstract static class UnboxNode extends Node {
@Child private ToHostPrimitiveNode primitive = ToHostPrimitiveNode.create();
public Object access(HostObject object) {
if (object.isPrimitive()) {
return object.obj;
} else {
return UnsupportedMessageException.raise(Message.UNBOX);
}
}
}
@Resolve(message = "READ")
abstract static class ReadNode extends Node {
@Child private ArrayReadNode arrayRead;
@Child private LookupFieldNode lookupField;
@Child private ReadFieldNode readField;
@Child private LookupMethodNode lookupMethod;
@Child private LookupInnerClassNode lookupInnerClass;
public Object access(HostObject object, Number index) {
if (arrayRead == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
arrayRead = insert(ArrayReadNodeGen.create());
}
return arrayRead.executeWithTarget(object, index);
}
public Object access(HostObject object, String name) {
if (TruffleOptions.AOT || object.isNull()) {
throw UnsupportedMessageException.raise(Message.READ);
}
boolean isStatic = object.isStaticClass();
Class> lookupClass = object.getLookupClass();
HostFieldDesc foundField = lookupField().execute(lookupClass, name, isStatic);
if (foundField != null) {
return readField().execute(foundField, object);
}
HostMethodDesc foundMethod = lookupMethod().execute(lookupClass, name, isStatic);
if (foundMethod != null) {
return new HostFunction(foundMethod, object.obj, object.languageContext);
}
if (isStatic) {
LookupInnerClassNode lookupInnerClassNode = lookupInnerClass();
if ("class".equals(name)) {
return HostObject.forClass(lookupClass, object.languageContext);
}
Class> innerclass = lookupInnerClassNode.execute(lookupClass, name);
if (innerclass != null) {
return HostObject.forStaticClass(innerclass, object.languageContext);
}
}
throw UnknownIdentifierException.raise(name);
}
private ReadFieldNode readField() {
if (readField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
readField = insert(ReadFieldNodeGen.create());
}
return readField;
}
private LookupFieldNode lookupField() {
if (lookupField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupField = insert(LookupFieldNodeGen.create());
}
return lookupField;
}
private LookupMethodNode lookupMethod() {
if (lookupMethod == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupMethod = insert(LookupMethodNodeGen.create());
}
return lookupMethod;
}
private LookupInnerClassNode lookupInnerClass() {
if (lookupInnerClass == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupInnerClass = insert(LookupInnerClassNodeGen.create());
}
return lookupInnerClass;
}
}
abstract static class ArrayReadNode extends Node {
abstract static class ArrayGet extends Node {
protected abstract Object execute(Object array, int index);
@Specialization
boolean doBoolean(boolean[] array, int index) {
return array[index];
}
@Specialization
byte doByte(byte[] array, int index) {
return array[index];
}
@Specialization
short doShort(short[] array, int index) {
return array[index];
}
@Specialization
char doChar(char[] array, int index) {
return array[index];
}
@Specialization
int doInt(int[] array, int index) {
return array[index];
}
@Specialization
long doLong(long[] array, int index) {
return array[index];
}
@Specialization
float doFloat(float[] array, int index) {
return array[index];
}
@Specialization
double doDouble(double[] array, int index) {
return array[index];
}
@Specialization
Object doObject(Object[] array, int index) {
return array[index];
}
}
@Child private ArrayGet arrayGet = ArrayGetNodeGen.create();
private final ToGuestValueNode toGuest = ToGuestValueNode.create();
protected abstract Object executeWithTarget(HostObject receiver, Object index);
@Specialization(guards = {"receiver.isArray()"})
protected Object doArrayIntIndex(HostObject receiver, int index) {
return doArrayAccess(receiver, index);
}
@Specialization(guards = {"receiver.isArray()", "index.getClass() == clazz"}, replaces = "doArrayIntIndex")
protected Object doArrayCached(HostObject receiver, Number index,
@Cached("index.getClass()") Class extends Number> clazz) {
return doArrayAccess(receiver, clazz.cast(index).intValue());
}
@Specialization(guards = {"receiver.isArray()"}, replaces = "doArrayCached")
protected Object doArrayGeneric(HostObject receiver, Number index) {
return doArrayAccess(receiver, index.intValue());
}
@TruffleBoundary
@Specialization(guards = {"isList(receiver)"})
protected Object doListIntIndex(HostObject receiver, int index) {
try {
return toGuest.apply(receiver.languageContext, ((List>) receiver.obj).get(index));
} catch (IndexOutOfBoundsException e) {
CompilerDirectives.transferToInterpreter();
throw UnknownIdentifierException.raise(String.valueOf(index));
}
}
@TruffleBoundary
@Specialization(guards = {"isList(receiver)"}, replaces = "doListIntIndex")
protected Object doListGeneric(HostObject receiver, Number index) {
return doListIntIndex(receiver, index.intValue());
}
@SuppressWarnings("unused")
@TruffleBoundary
@Specialization(guards = {"!receiver.isArray()", "!isList(receiver)"})
protected static Object notArray(HostObject receiver, Number index) {
throw UnsupportedMessageException.raise(Message.READ);
}
private Object doArrayAccess(HostObject object, int index) {
Object obj = object.obj;
assert object.isArray();
Object val = null;
try {
val = arrayGet.execute(obj, index);
} catch (ArrayIndexOutOfBoundsException outOfBounds) {
CompilerDirectives.transferToInterpreter();
throw UnknownIdentifierException.raise(String.valueOf(index));
}
return toGuest.apply(object.languageContext, val);
}
static boolean isList(HostObject receiver) {
return receiver.obj instanceof List;
}
}
@Resolve(message = "WRITE")
abstract static class WriteNode extends Node {
@Child private ArrayWriteNode arrayWrite;
@Child private LookupFieldNode lookupField;
@Child private WriteFieldNode writeField;
public Object access(HostObject receiver, Number index, Object value) {
if (arrayWrite == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
arrayWrite = insert(ArrayWriteNode.create());
}
try {
return arrayWrite.executeWithTarget(receiver, index, value);
} catch (ClassCastException | NullPointerException e) {
// conversion failed by ToJavaNode
throw UnsupportedTypeException.raise(e, new Object[]{value});
}
}
public Object access(HostObject receiver, String name, Object value) {
if (TruffleOptions.AOT || receiver.isNull()) {
throw UnsupportedMessageException.raise(Message.WRITE);
}
HostFieldDesc f = lookupField().execute(receiver.getLookupClass(), name, receiver.isStaticClass());
if (f == null) {
throw UnknownIdentifierException.raise(name);
}
try {
writeField().execute(f, receiver, value);
} catch (ClassCastException | NullPointerException e) {
// conversion failed by ToJavaNode
throw UnsupportedTypeException.raise(e, new Object[]{value});
}
return HostObject.NULL;
}
private LookupFieldNode lookupField() {
if (lookupField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
lookupField = insert(LookupFieldNodeGen.create());
}
return lookupField;
}
private WriteFieldNode writeField() {
if (writeField == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
writeField = insert(WriteFieldNodeGen.create());
}
return writeField;
}
}
abstract static class ArrayWriteNode extends Node {
abstract static class ArraySet extends Node {
protected abstract void execute(Object array, int index, Object value);
@Specialization
void doBoolean(boolean[] array, int index, boolean value) {
array[index] = value;
}
@Specialization
void doByte(byte[] array, int index, byte value) {
array[index] = value;
}
@Specialization
void doShort(short[] array, int index, short value) {
array[index] = value;
}
@Specialization
void doChar(char[] array, int index, char value) {
array[index] = value;
}
@Specialization
void doInt(int[] array, int index, int value) {
array[index] = value;
}
@Specialization
void doLong(long[] array, int index, long value) {
array[index] = value;
}
@Specialization
void doFloat(float[] array, int index, float value) {
array[index] = value;
}
@Specialization
void doDouble(double[] array, int index, double value) {
array[index] = value;
}
@Specialization
void doObject(Object[] array, int index, Object value) {
array[index] = value;
}
}
@Child private ToHostNode toJavaNode = ToHostNode.create();
@Child private ArraySet arraySet = ArraySetNodeGen.create();
protected abstract Object executeWithTarget(HostObject receiver, Object index, Object value);
@Specialization(guards = {"receiver.isArray()"})
protected final Object doArrayIntIndex(HostObject receiver, int index, Object value) {
return doArrayAccess(receiver, index, value);
}
@Specialization(guards = {"receiver.isArray()", "index.getClass() == clazz"})
protected final Object doArrayCached(HostObject receiver, Number index, Object value,
@Cached("index.getClass()") Class extends Number> clazz) {
return doArrayAccess(receiver, clazz.cast(index).intValue(), value);
}
@Specialization(guards = {"receiver.isArray()"}, replaces = "doArrayCached")
protected final Object doArrayGeneric(HostObject receiver, Number index, Object value) {
return doArrayAccess(receiver, index.intValue(), value);
}
@SuppressWarnings("unchecked")
@TruffleBoundary
@Specialization(guards = {"isList(receiver)"})
protected Object doListIntIndex(HostObject receiver, int index, Object value) {
final Object javaValue = toJavaNode.execute(value, Object.class, null, receiver.languageContext);
try {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy