87
87
import java .lang .annotation .Retention ;
88
88
import java .lang .annotation .RetentionPolicy ;
89
89
import java .nio .ByteBuffer ;
90
+ import java .nio .ByteOrder ;
90
91
import java .util .ArrayList ;
91
92
import java .util .Arrays ;
92
93
import java .util .HashMap ;
112
113
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodesFactory .TransformExceptionToNativeNodeGen ;
113
114
import com .oracle .graal .python .builtins .objects .cext .capi .ExternalFunctionNodes .PExternalFunctionWrapper ;
114
115
import com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol ;
115
- import com .oracle .graal .python .builtins .objects .cext .capi .PySequenceArrayWrapper ;
116
116
import com .oracle .graal .python .builtins .objects .cext .capi .PythonClassNativeWrapper ;
117
117
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativePointer ;
118
118
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
154
154
import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetMroStorageNode ;
155
155
import com .oracle .graal .python .lib .PyObjectGetAttr ;
156
156
import com .oracle .graal .python .nodes .ErrorMessages ;
157
+ import com .oracle .graal .python .nodes .PConstructAndRaiseNode ;
157
158
import com .oracle .graal .python .nodes .PGuards ;
158
159
import com .oracle .graal .python .nodes .PNodeWithContext ;
159
160
import com .oracle .graal .python .nodes .PNodeWithRaiseAndIndirectCall ;
170
171
import com .oracle .graal .python .nodes .util .CannotCastException ;
171
172
import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
172
173
import com .oracle .graal .python .runtime .PosixSupportLibrary ;
174
+ import com .oracle .graal .python .runtime .PosixSupportLibrary .PosixException ;
173
175
import com .oracle .graal .python .runtime .PythonContext ;
174
176
import com .oracle .graal .python .runtime .PythonContext .GetThreadStateNode ;
175
177
import com .oracle .graal .python .runtime .PythonOptions ;
202
204
import com .oracle .truffle .api .frame .FrameInstanceVisitor ;
203
205
import com .oracle .truffle .api .frame .VirtualFrame ;
204
206
import com .oracle .truffle .api .interop .InteropLibrary ;
207
+ import com .oracle .truffle .api .interop .InvalidBufferOffsetException ;
205
208
import com .oracle .truffle .api .interop .TruffleObject ;
206
209
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
207
210
import com .oracle .truffle .api .library .CachedLibrary ;
@@ -1757,6 +1760,80 @@ Object set(TruffleString tsName, Object pointer,
1757
1760
}
1758
1761
}
1759
1762
1763
+ /**
1764
+ * A native wrapper for arbitrary byte arrays (i.e. the store of a Python Bytes object) to be
1765
+ * used like a {@code char*} pointer.
1766
+ */
1767
+ @ ExportLibrary (InteropLibrary .class )
1768
+ public static final class PMMapWrapper implements TruffleObject {
1769
+
1770
+ private final PMMap delegate ;
1771
+
1772
+ public PMMapWrapper (PMMap delegate ) {
1773
+ this .delegate = delegate ;
1774
+ }
1775
+
1776
+ @ ExportMessage
1777
+ @ SuppressWarnings ("static-method" )
1778
+ boolean hasBufferElements () {
1779
+ return true ;
1780
+ }
1781
+
1782
+ @ ExportMessage
1783
+ long getBufferSize () {
1784
+ return delegate .getLength ();
1785
+ }
1786
+
1787
+ private final void checkIndex (long idx ) throws InvalidBufferOffsetException {
1788
+ long len = delegate .getLength ();
1789
+ if (idx < 0 || idx >= len ) {
1790
+ throw InvalidBufferOffsetException .create (idx , len );
1791
+ }
1792
+ }
1793
+
1794
+ @ ExportMessage
1795
+ byte readBufferByte (long idx ,
1796
+ @ CachedLibrary (limit = "1" ) PosixSupportLibrary posixSupportLib ,
1797
+ @ Cached PConstructAndRaiseNode raise ) throws InvalidBufferOffsetException {
1798
+ checkIndex (idx );
1799
+ try {
1800
+ return (posixSupportLib .mmapReadByte (PythonContext .get (posixSupportLib ).getPosixSupport (), delegate .getPosixSupportHandle (), idx ));
1801
+ } catch (PosixException e ) {
1802
+ throw raise .raiseOSError (null , e .getErrorCode (), e .getMessageAsTruffleString (), null , null );
1803
+ }
1804
+ }
1805
+
1806
+ @ ExportMessage
1807
+ @ SuppressWarnings ({"static-method" , "unused" })
1808
+ short readBufferShort (ByteOrder order , long byteOffset ) throws UnsupportedMessageException {
1809
+ throw UnsupportedMessageException .create ();
1810
+ }
1811
+
1812
+ @ ExportMessage
1813
+ @ SuppressWarnings ({"static-method" , "unused" })
1814
+ int readBufferInt (ByteOrder order , long byteOffset ) throws UnsupportedMessageException {
1815
+ throw UnsupportedMessageException .create ();
1816
+ }
1817
+
1818
+ @ ExportMessage
1819
+ @ SuppressWarnings ({"static-method" , "unused" })
1820
+ long readBufferLong (ByteOrder order , long byteOffset ) throws UnsupportedMessageException {
1821
+ throw UnsupportedMessageException .create ();
1822
+ }
1823
+
1824
+ @ ExportMessage
1825
+ @ SuppressWarnings ({"static-method" , "unused" })
1826
+ float readBufferFloat (ByteOrder order , long byteOffset ) throws UnsupportedMessageException {
1827
+ throw UnsupportedMessageException .create ();
1828
+ }
1829
+
1830
+ @ ExportMessage
1831
+ @ SuppressWarnings ({"static-method" , "unused" })
1832
+ double readBufferDouble (ByteOrder order , long byteOffset ) throws UnsupportedMessageException {
1833
+ throw UnsupportedMessageException .create ();
1834
+ }
1835
+ }
1836
+
1760
1837
@ CApiBuiltin (ret = CHAR_PTR , args = {PyObject }, call = Ignored )
1761
1838
abstract static class PyTruffle_GetMMapData extends CApiUnaryBuiltinNode {
1762
1839
@@ -1766,7 +1843,7 @@ Object get(PMMap object,
1766
1843
try {
1767
1844
return posixLib .mmapGetPointer (getPosixSupport (), object .getPosixSupportHandle ());
1768
1845
} catch (PosixSupportLibrary .UnsupportedPosixFeatureException e ) {
1769
- return new PySequenceArrayWrapper (object , 1 );
1846
+ return new PMMapWrapper (object );
1770
1847
}
1771
1848
}
1772
1849
}
0 commit comments