Search in sources :

Example 36 with ExportMessage

use of com.oracle.truffle.api.library.ExportMessage in project graal by oracle.

the class MapInterop method getHashEntriesIterator.

@SuppressWarnings("static-method")
@ExportMessage
public static Object getHashEntriesIterator(StaticObject receiver, @CachedLibrary(limit = "1") InteropLibrary setLibrary, @Cached.Exclusive @Cached InvokeEspressoNode invoke) throws UnsupportedMessageException {
    if (!hasHashEntries(receiver)) {
        throw UnsupportedMessageException.create();
    }
    Meta meta = receiver.getKlass().getMeta();
    Method entrySet = getInteropKlass(receiver).itableLookup(meta.java_util_Map, meta.java_util_Map_entrySet.getITableIndex());
    Object set = null;
    try {
        set = invoke.execute(entrySet, receiver, EMPTY_ARRAY);
    } catch (ArityException | UnsupportedTypeException e) {
        throw EspressoError.shouldNotReachHere(e);
    }
    assert set != null;
    assert setLibrary.hasIterator(set);
    return setLibrary.getIterator(set);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) ArityException(com.oracle.truffle.api.interop.ArityException) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 37 with ExportMessage

use of com.oracle.truffle.api.library.ExportMessage in project graal by oracle.

the class EspressoInstrumentableNode method getScope.

@ExportMessage
@TruffleBoundary
@SuppressWarnings("static-method")
public final Object getScope(Frame frame, @SuppressWarnings("unused") boolean nodeEnter) {
    // construct the current scope with valid local variables information
    Method method = getMethod();
    Local[] liveLocals = method.getLocalVariableTable().getLocalsAt(getBci(frame));
    if (liveLocals.length == 0) {
        // class was compiled without a local variable table
        // include "this" in method arguments throughout the method
        boolean hasReceiver = !method.isStatic();
        int localCount = hasReceiver ? 1 : 0;
        localCount += method.getParameterCount();
        liveLocals = new Local[localCount];
        Klass[] parameters = (Klass[]) method.getParameters();
        Utf8ConstantTable utf8Constants = getContext().getLanguage().getUtf8ConstantTable();
        int startslot = 0;
        if (hasReceiver) {
            // include 'this' and method arguments
            liveLocals[0] = new Local(utf8Constants.getOrCreate(Symbol.Name.thiz), utf8Constants.getOrCreate(method.getDeclaringKlass().getType()), 0, 65536, 0);
            startslot++;
        }
        // include method parameters
        for (int i = startslot; i < localCount; i++) {
            Klass param = hasReceiver ? parameters[i - 1] : parameters[i];
            liveLocals[i] = new Local(utf8Constants.getOrCreate(ByteSequence.create("param_" + (i))), utf8Constants.getOrCreate(param.getType()), 0, 65536, i);
        }
    }
    return EspressoScope.createVariables(liveLocals, frame, method.getName());
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) Local(com.oracle.truffle.espresso.classfile.attributes.Local) Method(com.oracle.truffle.espresso.impl.Method) Utf8ConstantTable(com.oracle.truffle.espresso.descriptors.Utf8ConstantTable) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 38 with ExportMessage

use of com.oracle.truffle.api.library.ExportMessage in project graal by oracle.

the class Klass method writeMember.

@ExportMessage
final void writeMember(String member, Object value, @Shared("lookupField") @Cached LookupFieldNode lookupFieldNode, @Shared("error") @Cached BranchProfile error, @Exclusive @Cached ToEspressoNode toEspressoNode) throws UnknownIdentifierException, UnsupportedTypeException {
    Field field = lookupFieldNode.execute(this, member, true);
    // Can only write to non-final fields.
    if (field != null && !field.isFinalFlagSet()) {
        Object espressoValue = toEspressoNode.execute(value, field.resolveTypeKlass());
        field.set(tryInitializeAndGetStatics(), espressoValue);
    } else {
        error.enter();
        throw UnknownIdentifierException.create(member);
    }
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 39 with ExportMessage

use of com.oracle.truffle.api.library.ExportMessage in project graal by oracle.

the class BaseInterop method toDisplayString.

@ExportMessage
@TruffleBoundary
public static Object toDisplayString(StaticObject object, boolean allowSideEffects) {
    if (object.isForeignObject()) {
        InteropLibrary interopLibrary = InteropLibrary.getUncached();
        try {
            return "Foreign object: " + interopLibrary.asString(interopLibrary.toDisplayString(object.rawForeignObject(), allowSideEffects));
        } catch (UnsupportedMessageException e) {
            throw EspressoError.shouldNotReachHere("Interop library failed to convert display string to string");
        }
    }
    if (StaticObject.isNull(object)) {
        return "NULL";
    }
    Klass thisKlass = object.getKlass();
    Meta meta = thisKlass.getMeta();
    if (allowSideEffects) {
        // Call guest toString.
        int toStringIndex = meta.java_lang_Object_toString.getVTableIndex();
        Method toString = thisKlass.vtableLookup(toStringIndex);
        return meta.toHostString((StaticObject) toString.invokeDirect(object));
    }
    // Handle some special instances without side effects.
    if (thisKlass == meta.java_lang_Class) {
        return "class " + thisKlass.getTypeAsString();
    }
    if (thisKlass == meta.java_lang_String) {
        return meta.toHostString(object);
    }
    return thisKlass.getTypeAsString() + "@" + Integer.toHexString(System.identityHashCode(object));
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 40 with ExportMessage

use of com.oracle.truffle.api.library.ExportMessage in project graal by oracle.

the class EspressoInterop method fitsInShort.

@ExportMessage
static boolean fitsInShort(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Klass klass = receiver.getKlass();
    if (isAtMostShort(klass)) {
        return true;
    }
    Meta meta = klass.getMeta();
    if (klass == meta.java_lang_Integer) {
        int content = meta.java_lang_Integer_value.getInt(receiver);
        return (short) content == content;
    }
    if (klass == meta.java_lang_Long) {
        long content = meta.java_lang_Long_value.getLong(receiver);
        return (short) content == content;
    }
    if (klass == meta.java_lang_Float) {
        float content = meta.java_lang_Float_value.getFloat(receiver);
        return (short) content == content && !isNegativeZero(content);
    }
    if (klass == meta.java_lang_Double) {
        double content = meta.java_lang_Double_value.getDouble(receiver);
        return (short) content == content && !isNegativeZero(content);
    }
    return false;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Aggregations

ExportMessage (com.oracle.truffle.api.library.ExportMessage)68 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)24 Meta (com.oracle.truffle.espresso.meta.Meta)24 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)16 Method (com.oracle.truffle.espresso.impl.Method)13 ByteBuffer (java.nio.ByteBuffer)10 ByteOrder (java.nio.ByteOrder)10 Klass (com.oracle.truffle.espresso.impl.Klass)8 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)8 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)8 Value (org.graalvm.polyglot.Value)8 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)8 ArityException (com.oracle.truffle.api.interop.ArityException)7 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)7 ProxyNativeObject (org.graalvm.polyglot.proxy.ProxyNativeObject)7 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)6 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)5 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)4 Field (com.oracle.truffle.espresso.impl.Field)4 ProxyHashMap (org.graalvm.polyglot.proxy.ProxyHashMap)4