Search in sources :

Example 41 with ExportMessage

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

the class EspressoInterop method asDate.

@ExportMessage
@TruffleBoundary
static LocalDate asDate(StaticObject receiver, @Shared("error") @Cached BranchProfile error) throws UnsupportedMessageException {
    receiver.checkNotForeign();
    if (isDate(receiver)) {
        Meta meta = receiver.getKlass().getMeta();
        if (instanceOf(receiver, meta.java_time_LocalDate)) {
            int year = (int) meta.java_time_LocalDate_year.get(receiver);
            short month = (short) meta.java_time_LocalDate_month.get(receiver);
            short day = (short) meta.java_time_LocalDate_day.get(receiver);
            return LocalDate.of(year, month, day);
        } else if (instanceOf(receiver, meta.java_time_LocalDateTime)) {
            StaticObject localDate = (StaticObject) meta.java_time_LocalDateTime_toLocalDate.invokeDirect(receiver);
            assert instanceOf(localDate, meta.java_time_LocalDate);
            return asDate(localDate, error);
        } else if (instanceOf(receiver, meta.java_time_Instant)) {
            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 localDate = (StaticObject) meta.java_time_ZonedDateTime_toLocalDate.invokeDirect(zonedDateTime);
            assert instanceOf(localDate, meta.java_time_LocalDate);
            return asDate(localDate, error);
        } else if (instanceOf(receiver, meta.java_time_ZonedDateTime)) {
            StaticObject localDate = (StaticObject) meta.java_time_ZonedDateTime_toLocalDate.invokeDirect(receiver);
            assert instanceOf(localDate, meta.java_time_LocalDate);
            return asDate(localDate, error);
        } else if (instanceOf(receiver, meta.java_util_Date)) {
            // return ((Date) obj).toInstant().atZone(UTC).toLocalDate();
            int index = meta.java_util_Date_toInstant.getVTableIndex();
            Method virtualToInstant = receiver.getKlass().vtableLookup(index);
            StaticObject instant = (StaticObject) virtualToInstant.invokeDirect(receiver);
            return asDate(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 42 with ExportMessage

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

the class EspressoInterop method asTimeZone.

@ExportMessage
@TruffleBoundary
static ZoneId asTimeZone(StaticObject receiver, @Shared("error") @Cached BranchProfile error) throws UnsupportedMessageException {
    receiver.checkNotForeign();
    if (isTimeZone(receiver)) {
        Meta meta = receiver.getKlass().getMeta();
        if (instanceOf(receiver, meta.java_time_ZoneId)) {
            int index = meta.java_time_ZoneId_getId.getVTableIndex();
            StaticObject zoneIdEspresso = (StaticObject) receiver.getKlass().vtableLookup(index).invokeDirect(receiver);
            String zoneId = Meta.toHostStringStatic(zoneIdEspresso);
            return ZoneId.of(zoneId, ZoneId.SHORT_IDS);
        } else if (instanceOf(receiver, meta.java_time_ZonedDateTime)) {
            StaticObject zoneId = (StaticObject) meta.java_time_ZonedDateTime_getZone.invokeDirect(receiver);
            return asTimeZone(zoneId, error);
        } else if (instanceOf(receiver, meta.java_time_Instant) || instanceOf(receiver, meta.java_util_Date)) {
            return ZoneId.of("UTC");
        }
    }
    error.enter();
    throw UnsupportedMessageException.create();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 43 with ExportMessage

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

the class EspressoInterop method isDuration.

@ExportMessage
static boolean isDuration(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Meta meta = receiver.getKlass().getMeta();
    return instanceOf(receiver, meta.java_time_Duration);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 44 with ExportMessage

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

the class EspressoInterop method isMemberReadable.

@ExportMessage
static boolean isMemberReadable(StaticObject receiver, String member, @Cached @Exclusive LookupInstanceFieldNode lookupField) {
    receiver.checkNotForeign();
    Field f = lookupField.execute(getInteropKlass(receiver), member);
    if (f != null) {
        return true;
    }
    return // 
    notNull(receiver) && receiver.getKlass() == receiver.getKlass().getMeta().java_lang_Class && (CLASS_TO_STATIC.equals(member) || STATIC_TO_CLASS.equals(member));
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 45 with ExportMessage

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

the class EspressoInterop method isTimeZone.

@ExportMessage
static boolean isTimeZone(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Meta meta = receiver.getKlass().getMeta();
    return instanceOf(receiver, meta.java_time_ZoneId) || 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)

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