Files
sdk/pkg/vm/testcases/transformations/ffi/native_fields.dart
T
Simon Binder 150e61a662 [vm/ffi] Support @Native fields
Allow annotating top-level or static fields with `@Native` to create
fields backed by native memory.
By using the `_addressOf` operator implemented in the VM, these fields
can be implemented in the CFE by replacing them with accessors looking
up the pointer and then using existing methods to load and store the
value.

Closes https://github.com/dart-lang/sdk/issues/50551

TEST=tests/ffi/native_assets/asset_*_test.dart
TEST=pkg/analyzer/test/src/diagnostics/ffi_native_test.dart

CoreLibraryReviewExempt: VM & dart2wasm only feature
Change-Id: I61dccc88076723d6a6ba02d7fd848b18e4caf780
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/338020
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-12-15 11:20:23 +00:00

46 lines
798 B
Dart

import 'dart:ffi';
@Native()
external Pointer<Char> aString;
@Native<Int32>()
external int anInt;
@Native<Int>()
external int anotherInt;
final class Vec2d extends Struct {
@Double()
external double x;
@Double()
external double y;
}
final class MyUnion extends Union {
external Vec2d vector;
external Pointer<Vec2d> indirectVector;
}
@Native()
external final Vec2d vector;
@Native()
external MyUnion union;
void main() {
print('first char of string: ${aString.value}');
print('global int: {$anInt}');
aString = nullptr;
anInt++;
final vec = vector;
print('(${vec.x}, ${vec.y})');
union.indirectVector = Native.addressOf(vector);
print(Native.addressOf<Int>(anotherInt));
print(Native.addressOf<Vec2d>(vector));
print(Native.addressOf<MyUnion>(union));
}