Skip to content

Commit 91c6dc1

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Pointer.asExternalTypedData to extension method
Issue: #38610 Change-Id: Ib07f50b23e3be2bce2d7b973c0f0196884397952 Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-reload-mac-release-simdbc64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-precomp-mac-release-simarm_x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121384 Commit-Queue: Daco Harkes <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 545a06b commit 91c6dc1

File tree

8 files changed

+352
-85
lines changed

8 files changed

+352
-85
lines changed

runtime/tools/ffi/sdk_lib_ffi_generator.dart

+63-22
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ import 'package:args/args.dart';
1515
// Configuration.
1616
//
1717

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+
];
3131

3232
//
3333
// Generator.
@@ -41,13 +41,11 @@ main(List<String> arguments) {
4141
generate(path, "ffi_patch.g.dart", generatePatchExtension);
4242
}
4343

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) {
4646
final buffer = StringBuffer();
4747
generateHeader(buffer);
48-
nativeToDartType.forEach((String nativeType, String dartType) {
49-
generator(buffer, nativeType, dartType);
50-
});
48+
configuration.forEach((Config c) => generator(buffer, c));
5149
generateFooter(buffer);
5250

5351
final fullPath = path.resolve(fileName).path;
@@ -73,8 +71,12 @@ void generateHeader(StringBuffer buffer) {
7371
buffer.write(header);
7472
}
7573

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+
7880
final storeTrunctateInt = """
7981
/// Note that ints which do not fit in `$nativeType` are truncated.
8082
""";
@@ -92,6 +94,19 @@ void generatePublicExtension(
9294

9395
final loadSignExtend = _isInt(nativeType) ? loadSignExtendInt : "";
9496

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+
95110
// TODO(dartdoc-bug): Use [] instead of ``, once issue
96111
// https://github.com/dart-lang/dartdoc/issues/2039 is fixed.
97112
buffer.write("""
@@ -124,13 +139,25 @@ $loadSignExtend ///
124139
$storeTruncate ///
125140
/// Note that `address` needs to be aligned to the size of `$nativeType`.
126141
external void operator []=(int index, $dartType value);
142+
143+
$asTypedList
127144
}
128145
129146
""");
130147
}
131148

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+
134161
buffer.write("""
135162
extension ${nativeType}Pointer on Pointer<$nativeType> {
136163
@patch
@@ -144,6 +171,8 @@ extension ${nativeType}Pointer on Pointer<$nativeType> {
144171
145172
@patch
146173
operator []=(int index, $dartType value) => _store$nativeType(this, index, value);
174+
175+
$asTypedList
147176
}
148177
149178
""");
@@ -185,3 +214,15 @@ Uri dartfmtPath() {
185214
// pinned fully supports extension methods.
186215
return Uri.parse("dartfmt");
187216
}
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;

sdk/lib/_internal/vm/lib/ffi_patch.dart

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// All imports must be in all FFI patch files to not depend on the order
66
// the patches are applied.
77
import "dart:_internal" show patch;
8-
import 'dart:typed_data' show TypedData;
8+
import 'dart:typed_data';
99

1010
const Map<Type, int> _knownSizes = {
1111
Int8: 1,
@@ -268,6 +268,9 @@ extension Int8Pointer on Pointer<Int8> {
268268

269269
@patch
270270
operator []=(int index, int value) => _storeInt8(this, index, value);
271+
272+
@patch
273+
Int8List asTypedList(int elements) => _asExternalTypedData(this, elements);
271274
}
272275

273276
extension Int16Pointer on Pointer<Int16> {
@@ -282,6 +285,9 @@ extension Int16Pointer on Pointer<Int16> {
282285

283286
@patch
284287
operator []=(int index, int value) => _storeInt16(this, index, value);
288+
289+
@patch
290+
Int16List asTypedList(int elements) => _asExternalTypedData(this, elements);
285291
}
286292

287293
extension Int32Pointer on Pointer<Int32> {
@@ -296,6 +302,9 @@ extension Int32Pointer on Pointer<Int32> {
296302

297303
@patch
298304
operator []=(int index, int value) => _storeInt32(this, index, value);
305+
306+
@patch
307+
Int32List asTypedList(int elements) => _asExternalTypedData(this, elements);
299308
}
300309

301310
extension Int64Pointer on Pointer<Int64> {
@@ -310,6 +319,9 @@ extension Int64Pointer on Pointer<Int64> {
310319

311320
@patch
312321
operator []=(int index, int value) => _storeInt64(this, index, value);
322+
323+
@patch
324+
Int64List asTypedList(int elements) => _asExternalTypedData(this, elements);
313325
}
314326

315327
extension Uint8Pointer on Pointer<Uint8> {
@@ -324,6 +336,9 @@ extension Uint8Pointer on Pointer<Uint8> {
324336

325337
@patch
326338
operator []=(int index, int value) => _storeUint8(this, index, value);
339+
340+
@patch
341+
Uint8List asTypedList(int elements) => _asExternalTypedData(this, elements);
327342
}
328343

329344
extension Uint16Pointer on Pointer<Uint16> {
@@ -338,6 +353,9 @@ extension Uint16Pointer on Pointer<Uint16> {
338353

339354
@patch
340355
operator []=(int index, int value) => _storeUint16(this, index, value);
356+
357+
@patch
358+
Uint16List asTypedList(int elements) => _asExternalTypedData(this, elements);
341359
}
342360

343361
extension Uint32Pointer on Pointer<Uint32> {
@@ -352,6 +370,9 @@ extension Uint32Pointer on Pointer<Uint32> {
352370

353371
@patch
354372
operator []=(int index, int value) => _storeUint32(this, index, value);
373+
374+
@patch
375+
Uint32List asTypedList(int elements) => _asExternalTypedData(this, elements);
355376
}
356377

357378
extension Uint64Pointer on Pointer<Uint64> {
@@ -366,6 +387,9 @@ extension Uint64Pointer on Pointer<Uint64> {
366387

367388
@patch
368389
operator []=(int index, int value) => _storeUint64(this, index, value);
390+
391+
@patch
392+
Uint64List asTypedList(int elements) => _asExternalTypedData(this, elements);
369393
}
370394

371395
extension IntPtrPointer on Pointer<IntPtr> {
@@ -394,6 +418,9 @@ extension FloatPointer on Pointer<Float> {
394418

395419
@patch
396420
operator []=(int index, double value) => _storeFloat(this, index, value);
421+
422+
@patch
423+
Float32List asTypedList(int elements) => _asExternalTypedData(this, elements);
397424
}
398425

399426
extension DoublePointer on Pointer<Double> {
@@ -408,6 +435,9 @@ extension DoublePointer on Pointer<Double> {
408435

409436
@patch
410437
operator []=(int index, double value) => _storeDouble(this, index, value);
438+
439+
@patch
440+
Float64List asTypedList(int elements) => _asExternalTypedData(this, elements);
411441
}
412442

413443
//

0 commit comments

Comments
 (0)