mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
[vm/ffi] Refactor dart:ffi
generator
Refactor to avoid conflicts later with concurrent work on: - https://github.com/dart-lang/sdk/issues/45508 - https://github.com/dart-lang/sdk/issues/44589 Change-Id: I7b7ea2e4ec29115da42b0c196a2952c3cd5d3fa6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349901 Commit-Queue: Martin Kustermann <kustermann@google.com> Auto-Submit: Daco Harkes <dacoharkes@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
parent
f95441f707
commit
28c7f5bd1e
|
@ -7,8 +7,6 @@
|
||||||
// The PointerPointer and PointerStruct extension are written by hand since
|
// The PointerPointer and PointerStruct extension are written by hand since
|
||||||
// those are not repetitive.
|
// those are not repetitive.
|
||||||
|
|
||||||
// ignore_for_file: unused_local_variable
|
|
||||||
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:args/args.dart';
|
import 'package:args/args.dart';
|
||||||
|
@ -31,22 +29,46 @@ const configuration = [
|
||||||
Config("Bool", "bool", kDoNotEmit, 1, since: Version(2, 15)),
|
Config("Bool", "bool", kDoNotEmit, 1, since: Version(2, 15)),
|
||||||
];
|
];
|
||||||
|
|
||||||
const arrayVersion = Version(2, 13);
|
/// A container to generate the extension for.
|
||||||
|
class Container {
|
||||||
|
final String name;
|
||||||
|
|
||||||
|
/// Since annotation for the extension.
|
||||||
|
final Version? since;
|
||||||
|
|
||||||
|
const Container(this.name, this.since);
|
||||||
|
|
||||||
|
static const pointer = Container(
|
||||||
|
'Pointer',
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
static const array = Container(
|
||||||
|
'Array',
|
||||||
|
Version(2, 13),
|
||||||
|
);
|
||||||
|
|
||||||
|
static const values = [
|
||||||
|
pointer,
|
||||||
|
array,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Generator.
|
// Generator.
|
||||||
//
|
//
|
||||||
|
|
||||||
main(List<String> arguments) {
|
void main(List<String> arguments) {
|
||||||
final args = argParser().parse(arguments);
|
update(
|
||||||
Uri path = Uri.parse(args['path']);
|
Uri.file('sdk/lib/ffi/ffi.dart'),
|
||||||
|
generatePublicExtension,
|
||||||
update(Uri.file('sdk/lib/ffi/ffi.dart'), generatePublicExtension);
|
);
|
||||||
update(Uri.file('sdk/lib/_internal/vm/lib/ffi_patch.dart'),
|
update(
|
||||||
generatePatchExtension);
|
Uri.file('sdk/lib/_internal/vm/lib/ffi_patch.dart'),
|
||||||
|
generatePatchExtension,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(Uri fileName, Function(StringBuffer, Config, String) generator) {
|
void update(Uri fileName, Function(StringBuffer, Config, Container) generator) {
|
||||||
final file = File.fromUri(fileName);
|
final file = File.fromUri(fileName);
|
||||||
if (!file.existsSync()) {
|
if (!file.existsSync()) {
|
||||||
print('$fileName does not exist, run from the root of the SDK.');
|
print('$fileName does not exist, run from the root of the SDK.');
|
||||||
|
@ -70,8 +92,11 @@ void update(Uri fileName, Function(StringBuffer, Config, String) generator) {
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
buffer.write(split1[0]);
|
buffer.write(split1[0]);
|
||||||
buffer.write(header);
|
buffer.write(header);
|
||||||
configuration.forEach((Config c) => generator(buffer, c, "Pointer"));
|
for (final container in Container.values) {
|
||||||
configuration.forEach((Config c) => generator(buffer, c, "Array"));
|
for (final config in configuration) {
|
||||||
|
generator(buffer, config, container);
|
||||||
|
}
|
||||||
|
}
|
||||||
buffer.write(footer);
|
buffer.write(footer);
|
||||||
buffer.write(split2[1]);
|
buffer.write(split2[1]);
|
||||||
|
|
||||||
|
@ -99,7 +124,10 @@ void generateHeader(StringBuffer buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void generatePublicExtension(
|
void generatePublicExtension(
|
||||||
StringBuffer buffer, Config config, String container) {
|
StringBuffer buffer,
|
||||||
|
Config config,
|
||||||
|
Container container,
|
||||||
|
) {
|
||||||
final nativeType = config.nativeType;
|
final nativeType = config.nativeType;
|
||||||
final dartType = config.dartType;
|
final dartType = config.dartType;
|
||||||
final typedListType = config.typedListType;
|
final typedListType = config.typedListType;
|
||||||
|
@ -166,9 +194,6 @@ void generatePublicExtension(
|
||||||
truncate = floatTruncate;
|
truncate = floatTruncate;
|
||||||
}
|
}
|
||||||
|
|
||||||
final sizeTimes =
|
|
||||||
elementSize != 1 ? '${bracketOr(sizeOf(elementSize))} * ' : '';
|
|
||||||
|
|
||||||
final alignmentDefault = """
|
final alignmentDefault = """
|
||||||
///
|
///
|
||||||
/// The [address] must be ${sizeOf(elementSize)}-byte aligned.
|
/// The [address] must be ${sizeOf(elementSize)}-byte aligned.
|
||||||
|
@ -207,10 +232,11 @@ $alignment external $typedListType asTypedList(
|
||||||
@Since('3.1') Pointer<Void>? token,
|
@Since('3.1') Pointer<Void>? token,
|
||||||
});
|
});
|
||||||
""";
|
""";
|
||||||
|
final since =
|
||||||
if (container == "Pointer") {
|
Version.latest(config.since, container.since)?.sinceAnnotation ?? '';
|
||||||
final since = config.since?.sinceAnnotation ?? '';
|
switch (container) {
|
||||||
buffer.write("""
|
case Container.pointer:
|
||||||
|
buffer.write("""
|
||||||
/// Extension on [Pointer] specialized for the type argument [$nativeType].
|
/// Extension on [Pointer] specialized for the type argument [$nativeType].
|
||||||
$since extension ${nativeType}Pointer on Pointer<$nativeType> {
|
$since extension ${nativeType}Pointer on Pointer<$nativeType> {
|
||||||
/// The $property at [address].
|
/// The $property at [address].
|
||||||
|
@ -258,10 +284,8 @@ $asTypedList
|
||||||
}
|
}
|
||||||
|
|
||||||
""");
|
""");
|
||||||
} else {
|
case Container.array:
|
||||||
final since =
|
buffer.write("""
|
||||||
Version.latest(config.since, arrayVersion)?.sinceAnnotation ?? '';
|
|
||||||
buffer.write("""
|
|
||||||
/// Bounds checking indexing methods on [Array]s of [$nativeType].
|
/// Bounds checking indexing methods on [Array]s of [$nativeType].
|
||||||
$since extension ${nativeType}Array on Array<$nativeType> {
|
$since extension ${nativeType}Array on Array<$nativeType> {
|
||||||
external $dartType operator [](int index);
|
external $dartType operator [](int index);
|
||||||
|
@ -274,7 +298,10 @@ $since extension ${nativeType}Array on Array<$nativeType> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void generatePatchExtension(
|
void generatePatchExtension(
|
||||||
StringBuffer buffer, Config config, String container) {
|
StringBuffer buffer,
|
||||||
|
Config config,
|
||||||
|
Container container,
|
||||||
|
) {
|
||||||
final nativeType = config.nativeType;
|
final nativeType = config.nativeType;
|
||||||
final dartType = config.dartType;
|
final dartType = config.dartType;
|
||||||
final typedListType = config.typedListType;
|
final typedListType = config.typedListType;
|
||||||
|
@ -305,8 +332,9 @@ void generatePatchExtension(
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
if (container == "Pointer") {
|
switch (container) {
|
||||||
buffer.write("""
|
case Container.pointer:
|
||||||
|
buffer.write("""
|
||||||
@patch
|
@patch
|
||||||
extension ${nativeType}Pointer on Pointer<$nativeType> {
|
extension ${nativeType}Pointer on Pointer<$nativeType> {
|
||||||
@patch
|
@patch
|
||||||
|
@ -329,8 +357,8 @@ $asTypedList
|
||||||
}
|
}
|
||||||
|
|
||||||
""");
|
""");
|
||||||
} else {
|
case Container.array:
|
||||||
buffer.write("""
|
buffer.write("""
|
||||||
@patch
|
@patch
|
||||||
extension ${nativeType}Array on Array<$nativeType> {
|
extension ${nativeType}Array on Array<$nativeType> {
|
||||||
@patch
|
@patch
|
||||||
|
|
Loading…
Reference in a new issue