Search in sources :

Example 21 with ExportMessage

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

the class HostObject method writeBufferFloat.

@ExportMessage
public void writeBufferFloat(ByteOrder order, long index, float value, @Shared("isBuffer") @Cached IsBufferNode isBuffer, @Shared("error") @Cached BranchProfile error, @Shared("classProfile") @Cached("createClassProfile()") ValueProfile classProfile) throws InvalidBufferOffsetException, UnsupportedMessageException {
    if (!isBuffer.execute(this)) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
    if (index < 0 || Integer.MAX_VALUE < index) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Float.BYTES);
    }
    try {
        final ByteBuffer buffer = (ByteBuffer) classProfile.profile(obj);
        final ByteOrder originalOrder = buffer.order();
        buffer.order(order);
        if (isPEFriendlyBuffer(buffer)) {
            buffer.putFloat((int) index, value);
        } else {
            putBufferFloatBoundary(buffer, (int) index, value);
        }
        buffer.order(originalOrder);
    } catch (IndexOutOfBoundsException e) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Float.BYTES);
    } catch (ReadOnlyBufferException e) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 22 with ExportMessage

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

the class HostObject method writeBufferInt.

@ExportMessage
public void writeBufferInt(ByteOrder order, long index, int value, @Shared("isBuffer") @Cached IsBufferNode isBuffer, @Shared("error") @Cached BranchProfile error, @Shared("classProfile") @Cached("createClassProfile()") ValueProfile classProfile) throws InvalidBufferOffsetException, UnsupportedMessageException {
    if (!isBuffer.execute(this)) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
    if (index < 0 || Integer.MAX_VALUE < index) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Integer.BYTES);
    }
    try {
        final ByteBuffer buffer = (ByteBuffer) classProfile.profile(obj);
        final ByteOrder originalOrder = buffer.order();
        buffer.order(order);
        if (isPEFriendlyBuffer(buffer)) {
            buffer.putInt((int) index, value);
        } else {
            putBufferIntBoundary(buffer, (int) index, value);
        }
        buffer.order(originalOrder);
    } catch (IndexOutOfBoundsException e) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Integer.BYTES);
    } catch (ReadOnlyBufferException e) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 23 with ExportMessage

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

the class HostObject method readBufferShort.

@ExportMessage
public short readBufferShort(ByteOrder order, long index, @Shared("isBuffer") @Cached IsBufferNode isBuffer, @Shared("error") @Cached BranchProfile error, @Shared("classProfile") @Cached("createClassProfile()") ValueProfile classProfile) throws UnsupportedMessageException, InvalidBufferOffsetException {
    if (!isBuffer.execute(this)) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
    if (index < 0 || Integer.MAX_VALUE < index) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Short.BYTES);
    }
    try {
        final ByteBuffer buffer = (ByteBuffer) classProfile.profile(obj);
        final ByteOrder originalOrder = buffer.order();
        buffer.order(order);
        final short result = isPEFriendlyBuffer(buffer) ? buffer.getShort((int) index) : getBufferShortBoundary(buffer, (int) index);
        buffer.order(originalOrder);
        return result;
    } catch (IndexOutOfBoundsException e) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Short.BYTES);
    }
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 24 with ExportMessage

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

the class HostObject method readBufferDouble.

@ExportMessage
public double readBufferDouble(ByteOrder order, long index, @Shared("isBuffer") @Cached IsBufferNode isBuffer, @Shared("error") @Cached BranchProfile error, @Shared("classProfile") @Cached("createClassProfile()") ValueProfile classProfile) throws UnsupportedMessageException, InvalidBufferOffsetException {
    if (!isBuffer.execute(this)) {
        error.enter();
        throw UnsupportedMessageException.create();
    }
    if (index < 0 || Integer.MAX_VALUE < index) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Double.BYTES);
    }
    try {
        final ByteBuffer buffer = (ByteBuffer) classProfile.profile(obj);
        final ByteOrder originalOrder = buffer.order();
        buffer.order(order);
        final double result = isPEFriendlyBuffer(buffer) ? buffer.getDouble((int) index) : getBufferDoubleBoundary(buffer, (int) index);
        buffer.order(originalOrder);
        return result;
    } catch (IndexOutOfBoundsException e) {
        error.enter();
        throw InvalidBufferOffsetException.create(index, Double.BYTES);
    }
}
Also used : ByteOrder(java.nio.ByteOrder) ByteBuffer(java.nio.ByteBuffer) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 25 with ExportMessage

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

the class EspressoInterop method asInstant.

@ExportMessage
@TruffleBoundary
static Instant asInstant(StaticObject receiver, @CachedLibrary("receiver") InteropLibrary receiverLibrary, @Shared("error") @Cached BranchProfile error) throws UnsupportedMessageException {
    receiver.checkNotForeign();
    if (receiverLibrary.isInstant(receiver)) {
        StaticObject instant;
        Meta meta = receiver.getKlass().getMeta();
        if (instanceOf(receiver, meta.java_time_ZonedDateTime)) {
            instant = (StaticObject) meta.java_time_ZonedDateTime_toInstant.invokeDirect(receiver);
        } else if (instanceOf(receiver, meta.java_util_Date)) {
            int index = meta.java_util_Date_toInstant.getVTableIndex();
            Method virtualToInstant = receiver.getKlass().vtableLookup(index);
            instant = (StaticObject) virtualToInstant.invokeDirect(receiver);
        } else {
            instant = receiver;
        }
        assert instanceOf(instant, meta.java_time_Instant);
        long seconds = (long) meta.java_time_Instant_seconds.get(instant);
        int nanos = (int) meta.java_time_Instant_nanos.get(instant);
        return Instant.ofEpochSecond(seconds, nanos);
    }
    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)

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