native icon indicating copy to clipboard operation
native copied to clipboard

`@Native`s and `@ResourceIdentifier`

Open dcharkes opened this issue 1 year ago • 2 comments
trafficstars

We should consider making @Natives automatically be mapped to @ResourceIdentifiers.

@Natives have a symbol and assetId (both possibly inferred). https://api.dart.dev/stable/3.3.3/dart-ffi/Native-class.html

@ResourceIdentifiers only have a Object? metadata. https://pub.dev/documentation/meta/latest/meta/ResourceIdentifier/ResourceIdentifier.html

A possible mapping would be

@Native<>(assetId: 'foo', symbol: 'bar')

->

@ResourceIdentifier({
  'assetId': 'foo',
  'symbol': 'bar',
})

Alternatively, we can make FFIgen generate the resource identifier annotations. Are there any cases where we don't want the resource identifier information? Maybe if a package only supports dylibs, so tree shaking cannot be used anyways, then it should run FFIgen without adding the annotations.

(If we want to do the mapping automatically in the Dart SDK, we should move this issue there. If we want to generate the annotations in FFIgen, we should keep it here.)

cc @mosuem thoughts?

dcharkes avatar Apr 09 '24 10:04 dcharkes

SGTM - I think I prefer using the approach of adding ResourceIdentifier in ffigen, it makes the process a bit more visible for an otherwise already very opaque feature. Also, it pollutes the resources.json file less.

mosuem avatar Apr 09 '24 14:04 mosuem

One slight issue is that the assetId might not entirely be obvious when generating. So maybe assetId should be optional.

Something along the lines of:

# ffigen.yaml
native:
  assetId: package:my_package/src/bindings_generated.dart
  resource-identifers: true

dcharkes avatar Apr 09 '24 14:04 dcharkes

Alternatively, we can make FFIgen generate the resource identifier annotations.

We settled on this, and it's already tracked in other issues.

dcharkes avatar Aug 30 '24 15:08 dcharkes