Skip to content

Commit 1c0de15

Browse files
committed
Use interop buffer API in CByteArrayWrapper
1 parent 431092e commit 1c0de15

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

graalpython/com.oracle.graal.python.cext/src/bytesobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, void *ptr,
518518
PyObject* bytes_subtype_new(PyTypeObject *type, int8_t* contents, Py_ssize_t n) {
519519
PyObject* bytes = type->tp_alloc(type, n);
520520
if (bytes != NULL) {
521-
memcpy(((PyBytesObject*)bytes)->ob_sval, contents, n+1);
521+
char* dst = ((PyBytesObject*)bytes)->ob_sval;
522+
memcpy(dst, contents, n);
523+
dst[n] = '\0';
522524
}
523525
return bytes;
524526
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CArrayWrappers.java

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
import static com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol.FUN_GET_BYTE_ARRAY_TYPE_ID;
4444
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
45+
import static com.oracle.graal.python.util.PythonUtils.byteArraySupport;
4546

4647
import java.lang.reflect.Field;
48+
import java.nio.ByteOrder;
4749

48-
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext.LLVMType;
49-
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetLLVMType;
5050
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction;
5151
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PythonObjectNativeWrapper;
5252
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper;
@@ -63,6 +63,7 @@
6363
import com.oracle.truffle.api.dsl.Cached.Shared;
6464
import com.oracle.truffle.api.interop.InteropLibrary;
6565
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
66+
import com.oracle.truffle.api.interop.InvalidBufferOffsetException;
6667
import com.oracle.truffle.api.library.ExportLibrary;
6768
import com.oracle.truffle.api.library.ExportMessage;
6869
import com.oracle.truffle.api.strings.TruffleString;
@@ -269,7 +270,6 @@ void toNative(
269270
* used like a {@code char*} pointer.
270271
*/
271272
@ExportLibrary(InteropLibrary.class)
272-
@ExportLibrary(value = NativeTypeLibrary.class, useForAOT = false)
273273
public static final class CByteArrayWrapper extends CArrayWrapper {
274274

275275
public CByteArrayWrapper(byte[] delegate) {
@@ -281,10 +281,67 @@ public byte[] getByteArray() {
281281
}
282282

283283
@ExportMessage
284-
long getArraySize() {
284+
@SuppressWarnings("static-method")
285+
boolean hasBufferElements() {
286+
return true;
287+
}
288+
289+
@ExportMessage
290+
@ExportMessage(name = "getArraySize")
291+
long getBufferSize() {
285292
return getByteArray().length;
286293
}
287294

295+
@ExportMessage
296+
byte readBufferByte(long byteOffset) throws InvalidBufferOffsetException {
297+
try {
298+
return getByteArray()[(int) byteOffset];
299+
} catch (ArrayIndexOutOfBoundsException e) {
300+
CompilerDirectives.transferToInterpreterAndInvalidate();
301+
throw InvalidBufferOffsetException.create(byteOffset, getByteArray().length);
302+
}
303+
}
304+
305+
@ExportMessage
306+
short readBufferShort(ByteOrder order, long byteOffset) throws InvalidBufferOffsetException {
307+
try {
308+
return byteArraySupport(order).getShort(getByteArray(), byteOffset);
309+
} catch (IndexOutOfBoundsException e) {
310+
CompilerDirectives.transferToInterpreterAndInvalidate();
311+
throw InvalidBufferOffsetException.create(byteOffset, getByteArray().length);
312+
}
313+
}
314+
315+
@ExportMessage
316+
int readBufferInt(ByteOrder order, long byteOffset) throws InvalidBufferOffsetException {
317+
try {
318+
return byteArraySupport(order).getInt(getByteArray(), byteOffset);
319+
} catch (IndexOutOfBoundsException e) {
320+
CompilerDirectives.transferToInterpreterAndInvalidate();
321+
throw InvalidBufferOffsetException.create(byteOffset, getByteArray().length);
322+
}
323+
}
324+
325+
@ExportMessage
326+
long readBufferLong(ByteOrder order, long byteOffset) throws InvalidBufferOffsetException {
327+
try {
328+
return byteArraySupport(order).getLong(getByteArray(), byteOffset);
329+
} catch (IndexOutOfBoundsException e) {
330+
CompilerDirectives.transferToInterpreterAndInvalidate();
331+
throw InvalidBufferOffsetException.create(byteOffset, getByteArray().length);
332+
}
333+
}
334+
335+
@ExportMessage
336+
float readBufferFloat(ByteOrder order, long byteOffset) throws InvalidBufferOffsetException {
337+
return Float.intBitsToFloat(readBufferInt(order, byteOffset));
338+
}
339+
340+
@ExportMessage
341+
double readBufferDouble(ByteOrder order, long byteOffset) throws InvalidBufferOffsetException {
342+
return Double.longBitsToDouble(readBufferLong(order, byteOffset));
343+
}
344+
288345
@ExportMessage
289346
@SuppressWarnings("static-method")
290347
boolean hasArrayElements() {
@@ -315,21 +372,8 @@ Object readArrayElement(long index,
315372
}
316373

317374
@ExportMessage
318-
boolean isArrayElementReadable(long identifier) {
319-
return 0 <= identifier && identifier < getArraySize();
320-
}
321-
322-
@ExportMessage
323-
@SuppressWarnings("static-method")
324-
boolean hasNativeType() {
325-
return true;
326-
}
327-
328-
@ExportMessage
329-
@SuppressWarnings("static-method")
330-
Object getNativeType(
331-
@Cached GetLLVMType getLLVMType) {
332-
return getLLVMType.execute(LLVMType.int8_ptr_t);
375+
boolean isArrayElementReadable(long index) {
376+
return 0 <= index && index < getBufferSize();
333377
}
334378

335379
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public final class PythonUtils {
117117
}
118118
}
119119

120+
public static ByteArraySupport byteArraySupport(ByteOrder order) {
121+
return order == ByteOrder.LITTLE_ENDIAN ? ByteArraySupport.littleEndian() : ByteArraySupport.bigEndian();
122+
}
123+
120124
private PythonUtils() {
121125
// no instances
122126
}

0 commit comments

Comments
 (0)