Search in sources :

Example 46 with ExportMessage

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

the class EspressoInterop method asTime.

@ExportMessage
@TruffleBoundary
static LocalTime asTime(StaticObject receiver, @Shared("error") @Cached BranchProfile error) throws UnsupportedMessageException {
    receiver.checkNotForeign();
    if (isTime(receiver)) {
        Meta meta = receiver.getKlass().getMeta();
        if (instanceOf(receiver, meta.java_time_LocalTime)) {
            byte hour = (byte) meta.java_time_LocalTime_hour.get(receiver);
            byte minute = (byte) meta.java_time_LocalTime_minute.get(receiver);
            byte second = (byte) meta.java_time_LocalTime_second.get(receiver);
            int nano = (int) meta.java_time_LocalTime_nano.get(receiver);
            return LocalTime.of(hour, minute, second, nano);
        } else if (instanceOf(receiver, meta.java_time_LocalDateTime)) {
            StaticObject localTime = (StaticObject) meta.java_time_LocalDateTime_toLocalTime.invokeDirect(receiver);
            return asTime(localTime, error);
        } else if (instanceOf(receiver, meta.java_time_ZonedDateTime)) {
            StaticObject localTime = (StaticObject) meta.java_time_ZonedDateTime_toLocalTime.invokeDirect(receiver);
            return asTime(localTime, error);
        } else if (instanceOf(receiver, meta.java_time_Instant)) {
            // return ((Instant) obj).atZone(UTC).toLocalTime();
            StaticObject zoneIdUTC = (StaticObject) meta.java_time_ZoneId_of.invokeDirect(null, meta.toGuestString("UTC"));
            assert instanceOf(zoneIdUTC, meta.java_time_ZoneId);
            StaticObject zonedDateTime = (StaticObject) meta.java_time_Instant_atZone.invokeDirect(receiver, zoneIdUTC);
            assert instanceOf(zonedDateTime, meta.java_time_ZonedDateTime);
            StaticObject localTime = (StaticObject) meta.java_time_ZonedDateTime_toLocalTime.invokeDirect(zonedDateTime);
            assert instanceOf(localTime, meta.java_time_LocalTime);
            return asTime(localTime, error);
        } else if (instanceOf(receiver, meta.java_util_Date)) {
            // return ((Date) obj).toInstant().atZone(UTC).toLocalTime();
            int index = meta.java_util_Date_toInstant.getVTableIndex();
            Method virtualToInstant = receiver.getKlass().vtableLookup(index);
            StaticObject instant = (StaticObject) virtualToInstant.invokeDirect(receiver);
            return asTime(instant, error);
        }
    }
    error.enter();
    throw UnsupportedMessageException.create();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 47 with ExportMessage

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

the class EspressoInterop method fitsInFloat.

@ExportMessage
static boolean fitsInFloat(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Klass klass = receiver.getKlass();
    if (isAtMostFloat(klass)) {
        return true;
    }
    Meta meta = klass.getMeta();
    /*
         * We might lose precision when we convert an int or a long to a float, however, we still
         * perform the conversion. This is consistent with Truffle interop, see GR-22718 for more
         * details.
         */
    if (klass == meta.java_lang_Integer) {
        int content = meta.java_lang_Integer_value.getInt(receiver);
        float floatContent = content;
        return (int) floatContent == content;
    }
    if (klass == meta.java_lang_Long) {
        long content = meta.java_lang_Long_value.getLong(receiver);
        float floatContent = content;
        return (long) floatContent == content;
    }
    if (klass == meta.java_lang_Double) {
        double content = meta.java_lang_Double_value.getDouble(receiver);
        return !Double.isFinite(content) || (float) content == 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)

Example 48 with ExportMessage

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

the class EspressoInterop method getMembers.

@ExportMessage
@TruffleBoundary
static Object getMembers(StaticObject receiver, @SuppressWarnings("unused") boolean includeInternal) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return EmptyKeysArray.INSTANCE;
    }
    ArrayList<String> members = new ArrayList<>();
    if (receiver.getKlass() == receiver.getKlass().getMeta().java_lang_Class) {
        // SVM does not like ArrayList.addAll(). Do manual copy.
        for (String s : CLASS_KEYS) {
            members.add(s);
        }
    }
    ObjectKlass k = getInteropKlass(receiver);
    for (Field f : k.getFieldTable()) {
        if (f.isPublic() && !f.isRemoved()) {
            members.add(f.getNameAsString());
        }
    }
    for (Method.MethodVersion m : k.getVTable()) {
        if (LookupVirtualMethodNode.isCandidate(m.getMethod())) {
            // Note: If there are overloading, the same key may appear twice.
            // TODO: Cache the keys array in the Klass.
            members.add(m.getMethod().getInteropString());
        }
    }
    // SVM does not like ArrayList.toArray(). Do manual copy.
    String[] array = new String[members.size()];
    int pos = 0;
    for (String str : members) {
        array[pos++] = str;
    }
    return new KeysArray(array);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) ArrayList(java.util.ArrayList) EmptyKeysArray(com.oracle.truffle.espresso.impl.EmptyKeysArray) KeysArray(com.oracle.truffle.espresso.impl.KeysArray) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 49 with ExportMessage

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

the class EspressoInterop method isDate.

// endregion ### Members
// region ### Date/time conversions
@ExportMessage
static boolean isDate(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Meta meta = receiver.getKlass().getMeta();
    return instanceOf(receiver, meta.java_time_LocalDate) || instanceOf(receiver, meta.java_time_LocalDateTime) || instanceOf(receiver, meta.java_time_Instant) || instanceOf(receiver, meta.java_time_ZonedDateTime) || instanceOf(receiver, meta.java_util_Date);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 50 with ExportMessage

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

the class EspressoInterop method isMemberModifiable.

@ExportMessage
static boolean isMemberModifiable(StaticObject receiver, String member, @Cached @Exclusive LookupInstanceFieldNode lookup) {
    receiver.checkNotForeign();
    Field f = lookup.execute(getInteropKlass(receiver), member);
    if (f != null) {
        return !f.isFinalFlagSet();
    }
    return false;
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) 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