Skip to content

Commit

Permalink
[GR-31711] [GR-32649] Augment interop capabilities of byte[].
Browse files Browse the repository at this point in the history
PullRequest: graal/9357
  • Loading branch information
mukel committed Jul 22, 2021
2 parents b62f483 + e9639b9 commit 3a4a4af
Show file tree
Hide file tree
Showing 28 changed files with 1,930 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

package com.oracle.truffle.espresso.polyglot;

import java.nio.ByteOrder;

/**
* Provides access to the interoperability message protocol between Truffle languages. Every method
* represents one message specified by the protocol. In the following text we will abbreviate
Expand Down Expand Up @@ -1360,4 +1362,276 @@ public static boolean isMemberExisting(Object receiver, String member) {
public static native Object getDeclaringMetaObject(Object receiver) throws UnsupportedMessageException;

// endregion StackFrame Messages

// region Buffer Messages

/**
* Returns {@code true} if the receiver may have buffer elements.
* <p>
* If this message returns {@code true}, then {@link #getBufferSize(Object)},
* {@link #readBufferByte(Object, long)}, {@link #readBufferShort(Object, ByteOrder, long)},
* {@link #readBufferInt(Object, ByteOrder, long)},
* {@link #readBufferLong(Object, ByteOrder, long)},
* {@link #readBufferFloat(Object, ByteOrder, long)} and
* {@link #readBufferDouble(Object, ByteOrder, long)} must not throw
* {@link UnsupportedMessageException}.
* <p>
* Invoking this message does not cause any observable side-effects.
* <p>
* By default, it returns {@code false}.
*
* @since 21.1
*/
public static native boolean hasBufferElements(Object receiver);

/**
* Returns {@code true} if the receiver is a modifiable buffer.
* <p>
* If this message returns {@code true}, then {@link #getBufferSize(Object)},
* {@link #writeBufferByte(Object, long, byte)},
* {@link #writeBufferShort(Object, ByteOrder, long, short)},
* {@link #writeBufferInt(Object, ByteOrder, long, int)},
* {@link #writeBufferLong(Object, ByteOrder, long, long)},
* {@link #writeBufferFloat(Object, ByteOrder, long, float)} and
* {@link #writeBufferDouble(Object, ByteOrder, long, double)} must not throw
* {@link UnsupportedMessageException}.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native boolean isBufferWritable(Object receiver) throws UnsupportedMessageException;

/**
* Returns the buffer size of the receiver, in bytes.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native long getBufferSize(Object receiver) throws UnsupportedMessageException;

/**
* Reads the byte from the receiver object at the given byte offset from the start of the
* buffer.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the byte at the given index
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= </code>{@link #getBufferSize(Object)}
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* returns {@code false} returns {@code false}
* @since 21.1
*/
public static native byte readBufferByte(Object receiver, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given byte from the receiver object at the given byte offset from the start of the
* buffer.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= </code>{@link #getBufferSize(Object)}
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferByte(Object receiver, long byteOffset, byte value) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Reads the short from the receiver object in the given byte order at the given byte offset
* from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the short at the given byte offset from the start of the buffer
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 1</code>
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native short readBufferShort(Object receiver, ByteOrder order, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given short from the receiver object in the given byte order at the given byte
* offset from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 1</code>
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferShort(Object receiver, ByteOrder order, long byteOffset, short value) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Reads the int from the receiver object in the given byte order at the given byte offset from
* the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the int at the given byte offset from the start of the buffer
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 3</code>
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native int readBufferInt(Object receiver, ByteOrder order, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given int from the receiver object in the given byte order at the given byte
* offset from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 3</code>
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferInt(Object receiver, ByteOrder order, long byteOffset, int value) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Reads the long from the receiver object in the given byte order at the given byte offset from
* the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the int at the given byte offset from the start of the buffer
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 7</code>
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native long readBufferLong(Object receiver, ByteOrder order, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given long from the receiver object in the given byte order at the given byte
* offset from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 7</code>
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferLong(Object receiver, ByteOrder order, long byteOffset, long value) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Reads the float from the receiver object in the given byte order at the given byte offset
* from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the float at the given byte offset from the start of the buffer
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 3</code>
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native float readBufferFloat(Object receiver, ByteOrder order, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given float from the receiver object in the given byte order at the given byte
* offset from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 3</code>
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferFloat(Object receiver, ByteOrder order, long byteOffset, float value) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Reads the double from the receiver object in the given byte order at the given byte offset
* from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
* <p>
* Invoking this message does not cause any observable side-effects.
*
* @return the double at the given byte offset from the start of the buffer
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 7</code>
* @throws UnsupportedMessageException if and only if {@link #hasBufferElements(Object)} returns
* {@code false}
* @since 21.1
*/
public static native double readBufferDouble(Object receiver, ByteOrder order, long byteOffset) throws UnsupportedMessageException, InvalidBufferOffsetException;

/**
* Writes the given double from the receiver object in the given byte order at the given byte
* offset from the start of the buffer.
* <p>
* Unaligned accesses are supported.
* <p>
* The access is <em>not</em> guaranteed to be atomic. Therefore, this message is <em>not</em>
* thread-safe.
*
* @throws InvalidBufferOffsetException if and only if
* <code>byteOffset < 0 || byteOffset >= {@link #getBufferSize(Object)} - 7</code>
* @throws UnsupportedMessageException if and only if either {@link #hasBufferElements(Object)}
* or {@link #isBufferWritable} returns {@code false}
* @since 21.1
*/
public static native void writeBufferDouble(Object receiver, ByteOrder order, long byteOffset, double value) throws UnsupportedMessageException, InvalidBufferOffsetException;

// endregion Buffer Messages
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
package com.oracle.truffle.espresso.polyglot;

/**
* An exception thrown if an array does not contain a element with an index. Interop exceptions are
* supposed to be caught and converted into a guest language error by the caller.
* An exception thrown if an array does not contain a element with an index.
*
* @see #getInvalidIndex()
* @since 21.0
Expand Down Expand Up @@ -100,10 +99,7 @@ public static InvalidArrayIndexException create(long invalidIndex) {
* caused this problem. An example for this is a language specific proxy mechanism that invokes
* guest language code to describe an object. If the guest language code fails to execute and
* this interop exception is a valid interpretation of the error, then the error should be
* provided as cause. The cause can then be used by the source language as new exception cause
* if the {@link InteropException} is translated to a source language error. If the
* {@link InteropException} is discarded, then the cause will most likely get discarded by the
* source language as well.
* provided as cause.
*
* @param invalidIndex the index that could not be accessed
* @param cause the guest language exception that caused the error.
Expand Down
Loading

0 comments on commit 3a4a4af

Please sign in to comment.