Skip to content

Commit 150e61a

Browse files
simolus3dcharkes
authored andcommitted
[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 #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 <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Lasse Nielsen <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 39e4804 commit 150e61a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2190
-568
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
- `String.fromCharCodes` now allow `start` and `end` to be after the end of
5252
the `Iterable` argument, just like `skip` and `take` does on an `Iterable`.
5353

54+
#### `dart:ffi`
55+
56+
- In addition to functions, `@Native` can now be used on fields.
57+
- Allow taking the address of native functions and fields via
58+
`Native.addressOf`.
59+
5460
#### `dart:nativewrappers`
5561

5662
- **Breaking Change** [#51896][]: The NativeWrapperClasses are marked `base` so

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5275,15 +5275,47 @@ const MessageCode messageFfiNativeDuplicateAnnotations = const MessageCode(
52755275
"FfiNativeDuplicateAnnotations",
52765276
analyzerCodes: <String>["FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS"],
52775277
problemMessage:
5278-
r"""Native functions must not have more than @Native annotation.""");
5278+
r"""Native functions and fields must not have more than @Native annotation.""");
5279+
5280+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5281+
const Code<Null> codeFfiNativeFieldMissingType =
5282+
messageFfiNativeFieldMissingType;
5283+
5284+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5285+
const MessageCode messageFfiNativeFieldMissingType = const MessageCode(
5286+
"FfiNativeFieldMissingType",
5287+
analyzerCodes: <String>["NATIVE_FIELD_MISSING_TYPE"],
5288+
problemMessage:
5289+
r"""The native type of this field could not be inferred and must be specified in the annotation.""");
5290+
5291+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5292+
const Code<Null> codeFfiNativeFieldMustBeStatic =
5293+
messageFfiNativeFieldMustBeStatic;
5294+
5295+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5296+
const MessageCode messageFfiNativeFieldMustBeStatic = const MessageCode(
5297+
"FfiNativeFieldMustBeStatic",
5298+
analyzerCodes: <String>["NATIVE_FIELD_NOT_STATIC"],
5299+
problemMessage: r"""Native fields must be static.""");
5300+
5301+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5302+
const Code<Null> codeFfiNativeFieldType = messageFfiNativeFieldType;
5303+
5304+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5305+
const MessageCode messageFfiNativeFieldType = const MessageCode(
5306+
"FfiNativeFieldType",
5307+
analyzerCodes: <String>["NATIVE_FIELD_INVALID_TYPE"],
5308+
problemMessage:
5309+
r"""Unsupported type for native fields. Native fields only support pointers, compounds and numeric types.""");
52795310

52805311
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52815312
const Code<Null> codeFfiNativeMustBeExternal = messageFfiNativeMustBeExternal;
52825313

52835314
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52845315
const MessageCode messageFfiNativeMustBeExternal = const MessageCode(
52855316
"FfiNativeMustBeExternal",
5286-
problemMessage: r"""Native functions must be marked external.""");
5317+
problemMessage:
5318+
r"""Native functions and fields must be marked external.""");
52875319

52885320
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
52895321
const Code<Null> codeFfiNativeOnlyNativeFieldWrapperClassCanBePointer =

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,12 @@ FfiCode.MUST_BE_A_SUBTYPE:
17981798
status: noFix
17991799
FfiCode.MUST_RETURN_VOID:
18001800
status: noFix
1801+
FfiCode.NATIVE_FIELD_INVALID_TYPE:
1802+
status: needsEvaluation
1803+
FfiCode.NATIVE_FIELD_MISSING_TYPE:
1804+
status: needsEvaluation
1805+
FfiCode.NATIVE_FIELD_NOT_STATIC:
1806+
status: needsEvaluation
18011807
FfiCode.NON_CONSTANT_TYPE_ARGUMENT:
18021808
status: noFix
18031809
FfiCode.NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER:

pkg/analyzer/lib/src/dart/error/ffi_code.g.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class FfiCode extends AnalyzerErrorCode {
8787
static const FfiCode ARGUMENT_MUST_BE_NATIVE = FfiCode(
8888
'ARGUMENT_MUST_BE_NATIVE',
8989
"Argument to 'Native.addressOf' must be annotated with @Native",
90-
correctionMessage: "Try passing a static function annotated with '@Native'",
90+
correctionMessage:
91+
"Try passing a static function or field annotated with '@Native'",
9192
);
9293

9394
/// No parameters.
@@ -163,7 +164,7 @@ class FfiCode extends AnalyzerErrorCode {
163164
/// No parameters
164165
static const FfiCode FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS = FfiCode(
165166
'FFI_NATIVE_INVALID_MULTIPLE_ANNOTATIONS',
166-
"Native functions must have exactly one `@Native` annotation.",
167+
"Native functions and fields must have exactly one `@Native` annotation.",
167168
correctionMessage: "Try removing the extra annotation.",
168169
);
169170

@@ -368,6 +369,37 @@ class FfiCode extends AnalyzerErrorCode {
368369
hasPublishedDocs: true,
369370
);
370371

372+
/// Parameters:
373+
/// 0: The invalid type.
374+
static const FfiCode NATIVE_FIELD_INVALID_TYPE = FfiCode(
375+
'NATIVE_FIELD_INVALID_TYPE',
376+
"'{0}' is an unsupported type for native fields. Native fields only "
377+
"support pointers or numeric and compound types.",
378+
correctionMessage:
379+
"Try changing the type in the `@Native` annotation to a numeric FFI "
380+
"type, a pointer, or a compound class.",
381+
hasPublishedDocs: true,
382+
);
383+
384+
/// No parameters
385+
static const FfiCode NATIVE_FIELD_MISSING_TYPE = FfiCode(
386+
'NATIVE_FIELD_MISSING_TYPE',
387+
"The native type of this field could not be inferred and must be specified "
388+
"in the annotation.",
389+
correctionMessage:
390+
"Try adding a type parameter extending `NativeType` to the `@Native` "
391+
"annotation.",
392+
hasPublishedDocs: true,
393+
);
394+
395+
/// No parameters
396+
static const FfiCode NATIVE_FIELD_NOT_STATIC = FfiCode(
397+
'NATIVE_FIELD_NOT_STATIC',
398+
"Native fields must be static.",
399+
correctionMessage: "Try adding the modifier 'static' to this field.",
400+
hasPublishedDocs: true,
401+
);
402+
371403
/// Parameters:
372404
/// 0: the name of the function, method, or constructor having type arguments
373405
static const FfiCode NON_CONSTANT_TYPE_ARGUMENT = FfiCode(

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ const List<ErrorCode> errorCodeValues = [
611611
FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE,
612612
FfiCode.MUST_BE_A_SUBTYPE,
613613
FfiCode.MUST_RETURN_VOID,
614+
FfiCode.NATIVE_FIELD_INVALID_TYPE,
615+
FfiCode.NATIVE_FIELD_MISSING_TYPE,
616+
FfiCode.NATIVE_FIELD_NOT_STATIC,
614617
FfiCode.NON_CONSTANT_TYPE_ARGUMENT,
615618
FfiCode.NON_LEAF_CALL_MUST_NOT_TAKE_TYPED_DATA,
616619
FfiCode.NON_NATIVE_FUNCTION_TYPE_ARGUMENT_TO_POINTER,

0 commit comments

Comments
 (0)