42
42
43
43
import static com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol .FUN_GET_BYTE_ARRAY_TYPE_ID ;
44
44
import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
45
+ import static com .oracle .graal .python .util .PythonUtils .byteArraySupport ;
45
46
46
47
import java .lang .reflect .Field ;
48
+ import java .nio .ByteOrder ;
47
49
48
- import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext .LLVMType ;
49
- import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .GetLLVMType ;
50
50
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PCallCapiFunction ;
51
51
import com .oracle .graal .python .builtins .objects .cext .capi .DynamicObjectNativeWrapper .PythonObjectNativeWrapper ;
52
52
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
63
63
import com .oracle .truffle .api .dsl .Cached .Shared ;
64
64
import com .oracle .truffle .api .interop .InteropLibrary ;
65
65
import com .oracle .truffle .api .interop .InvalidArrayIndexException ;
66
+ import com .oracle .truffle .api .interop .InvalidBufferOffsetException ;
66
67
import com .oracle .truffle .api .library .ExportLibrary ;
67
68
import com .oracle .truffle .api .library .ExportMessage ;
68
69
import com .oracle .truffle .api .strings .TruffleString ;
@@ -269,7 +270,6 @@ void toNative(
269
270
* used like a {@code char*} pointer.
270
271
*/
271
272
@ ExportLibrary (InteropLibrary .class )
272
- @ ExportLibrary (value = NativeTypeLibrary .class , useForAOT = false )
273
273
public static final class CByteArrayWrapper extends CArrayWrapper {
274
274
275
275
public CByteArrayWrapper (byte [] delegate ) {
@@ -281,10 +281,67 @@ public byte[] getByteArray() {
281
281
}
282
282
283
283
@ 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 () {
285
292
return getByteArray ().length ;
286
293
}
287
294
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
+
288
345
@ ExportMessage
289
346
@ SuppressWarnings ("static-method" )
290
347
boolean hasArrayElements () {
@@ -315,21 +372,8 @@ Object readArrayElement(long index,
315
372
}
316
373
317
374
@ 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 ();
333
377
}
334
378
335
379
@ ExportMessage
0 commit comments