|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.objects.memoryview;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.TOBYTES; |
| 44 | + |
43 | 45 | import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
|
44 | 46 | import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
|
45 | 47 | import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
|
46 |
| -import com.oracle.graal.python.nodes.util.CastToByteNode; |
| 48 | +import com.oracle.truffle.api.CompilerDirectives; |
47 | 49 | import com.oracle.truffle.api.dsl.Cached;
|
48 | 50 | import com.oracle.truffle.api.dsl.Cached.Shared;
|
| 51 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
49 | 52 | import com.oracle.truffle.api.library.CachedLibrary;
|
50 | 53 | import com.oracle.truffle.api.library.ExportLibrary;
|
51 | 54 | import com.oracle.truffle.api.library.ExportMessage;
|
@@ -76,15 +79,13 @@ int getBufferLength(
|
76 | 79 | @ExportMessage
|
77 | 80 | byte[] getBufferBytes(
|
78 | 81 | @Shared("readNativeMemoryViewNode") @Cached ReadAttributeFromDynamicObjectNode readNativeMemoryViewNode,
|
79 |
| - @CachedLibrary(limit = "1") PythonObjectLibrary lib, |
80 |
| - @Cached PInteropSubscriptNode subscriptNode, |
81 |
| - @Cached CastToByteNode castToByteNode) { |
| 82 | + @CachedLibrary(limit = "2") PythonObjectLibrary lib) { |
82 | 83 | Object nativeMemoryViewObject = readNativeMemoryViewNode.execute(getStorage(), C_MEMORYVIEW);
|
83 |
| - int len = lib.length(nativeMemoryViewObject); |
84 |
| - byte[] data = new byte[len]; |
85 |
| - for (int i = 0; i < data.length; i++) { |
86 |
| - data[i] = castToByteNode.execute(null, subscriptNode.execute(nativeMemoryViewObject, i)); |
| 84 | + Object bytes = lib.lookupAndCallRegularMethod(nativeMemoryViewObject, null, TOBYTES); |
| 85 | + try { |
| 86 | + return lib.getBufferBytes(bytes); |
| 87 | + } catch (UnsupportedMessageException e) { |
| 88 | + throw CompilerDirectives.shouldNotReachHere("memoryview.tobytes didn't return bytes"); |
87 | 89 | }
|
88 |
| - return data; |
89 | 90 | }
|
90 | 91 | }
|
0 commit comments