@@ -15,19 +15,19 @@ import 'package:args/args.dart';
15
15
// Configuration.
16
16
//
17
17
18
- const Map < String , String > nativeToDartType = {
19
- "Int8" : "int" ,
20
- "Int16" : "int" ,
21
- "Int32" : "int" ,
22
- "Int64" : "int" ,
23
- "Uint8" : "int" ,
24
- "Uint16" : "int" ,
25
- "Uint32" : "int" ,
26
- "Uint64" : "int" ,
27
- "IntPtr" : "int" ,
28
- "Float" : "double" ,
29
- "Double" : "double" ,
30
- } ;
18
+ const configuration = [
19
+ Config ( "Int8" , "int" , "Int8List" , 1 ) ,
20
+ Config ( "Int16" , "int" , "Int16List" , 2 ) ,
21
+ Config ( "Int32" , "int" , "Int32List" , 4 ) ,
22
+ Config ( "Int64" , "int" , "Int64List" , 8 ) ,
23
+ Config ( "Uint8" , "int" , "Uint8List" , 1 ) ,
24
+ Config ( "Uint16" , "int" , "Uint16List" , 2 ) ,
25
+ Config ( "Uint32" , "int" , "Uint32List" , 4 ) ,
26
+ Config ( "Uint64" , "int" , "Uint64List" , 8 ) ,
27
+ Config ( "IntPtr" , "int" , kDoNotEmit, kIntPtrElementSize) ,
28
+ Config ( "Float" , "double" , "Float32List" , 4 ) ,
29
+ Config ( "Double" , "double" , "Float64List" , 8 ) ,
30
+ ] ;
31
31
32
32
//
33
33
// Generator.
@@ -41,13 +41,11 @@ main(List<String> arguments) {
41
41
generate (path, "ffi_patch.g.dart" , generatePatchExtension);
42
42
}
43
43
44
- void generate (Uri path, String fileName,
45
- Function ( StringBuffer , String , String ) generator) {
44
+ void generate (
45
+ Uri path , String fileName, Function ( StringBuffer , Config ) generator) {
46
46
final buffer = StringBuffer ();
47
47
generateHeader (buffer);
48
- nativeToDartType.forEach ((String nativeType, String dartType) {
49
- generator (buffer, nativeType, dartType);
50
- });
48
+ configuration.forEach ((Config c) => generator (buffer, c));
51
49
generateFooter (buffer);
52
50
53
51
final fullPath = path.resolve (fileName).path;
@@ -73,8 +71,12 @@ void generateHeader(StringBuffer buffer) {
73
71
buffer.write (header);
74
72
}
75
73
76
- void generatePublicExtension (
77
- StringBuffer buffer, String nativeType, String dartType) {
74
+ void generatePublicExtension (StringBuffer buffer, Config config) {
75
+ final nativeType = config.nativeType;
76
+ final dartType = config.dartType;
77
+ final typedListType = config.typedListType;
78
+ final elementSize = config.elementSize;
79
+
78
80
final storeTrunctateInt = """
79
81
/// Note that ints which do not fit in `$nativeType ` are truncated.
80
82
""" ;
@@ -92,6 +94,19 @@ void generatePublicExtension(
92
94
93
95
final loadSignExtend = _isInt (nativeType) ? loadSignExtendInt : "" ;
94
96
97
+ final asTypedList = typedListType == kDoNotEmit
98
+ ? ""
99
+ : """
100
+ /// Creates a typed list view backed by memory in the address space.
101
+ ///
102
+ /// The returned view will allow access to the memory range from `address`
103
+ /// to `address + ${elementSize > 1 ? '$elementSize * ' : '' }length`.
104
+ ///
105
+ /// The user has to ensure the memory range is accessible while using the
106
+ /// returned list.
107
+ external $typedListType asTypedList(int length);
108
+ """ ;
109
+
95
110
// TODO(dartdoc-bug): Use [] instead of ``, once issue
96
111
// https://github.com/dart-lang/dartdoc/issues/2039 is fixed.
97
112
buffer.write ("""
@@ -124,13 +139,25 @@ $loadSignExtend ///
124
139
$storeTruncate ///
125
140
/// Note that `address` needs to be aligned to the size of `$nativeType `.
126
141
external void operator []=(int index, $dartType value);
142
+
143
+ $asTypedList
127
144
}
128
145
129
146
""" );
130
147
}
131
148
132
- void generatePatchExtension (
133
- StringBuffer buffer, String nativeType, String dartType) {
149
+ void generatePatchExtension (StringBuffer buffer, Config config) {
150
+ final nativeType = config.nativeType;
151
+ final dartType = config.dartType;
152
+ final typedListType = config.typedListType;
153
+
154
+ final asTypedList = typedListType == kDoNotEmit
155
+ ? ""
156
+ : """
157
+ @patch
158
+ $typedListType asTypedList(int elements) => _asExternalTypedData(this, elements);
159
+ """ ;
160
+
134
161
buffer.write ("""
135
162
extension ${nativeType }Pointer on Pointer<$nativeType > {
136
163
@patch
@@ -144,6 +171,8 @@ extension ${nativeType}Pointer on Pointer<$nativeType> {
144
171
145
172
@patch
146
173
operator []=(int index, $dartType value) => _store$nativeType (this, index, value);
174
+
175
+ $asTypedList
147
176
}
148
177
149
178
""" );
@@ -185,3 +214,15 @@ Uri dartfmtPath() {
185
214
// pinned fully supports extension methods.
186
215
return Uri .parse ("dartfmt" );
187
216
}
217
+
218
+ class Config {
219
+ final String nativeType;
220
+ final String dartType;
221
+ final String typedListType;
222
+ final int elementSize;
223
+ const Config (
224
+ this .nativeType, this .dartType, this .typedListType, this .elementSize);
225
+ }
226
+
227
+ const String kDoNotEmit = "donotemit" ;
228
+ const int kIntPtrElementSize = - 1 ;
0 commit comments