-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vm/ffi] Make FfiNative
s more concise
#50097
Comments
I have been thinking about this and discussing this with @leafpetersen couple of times. My original thinking was that we could use views for this, though I don't think the current version of views has all necessary features. If CFE retained typedef reference in the AST rather than performed full substitution then we could simply write: // dart:ffi
typedef Void = void;
// ...
typedef Int32 = int;
typedef Int64 = int;
typedef Float = double;
typedef Double = double;
// ...
@Native()
external Int64 sum(Int64 a, Int64 b);
@Native()
external Void foo(Pointer<Int64> a, Int64 b); Though unfortunately right now CFE is going the opposite way (/cc @johnniwinther @chloestefantsova) and is considering to delete |
@mraleph Java Native Access works with reflections to get the return and parameter types. Considering static metaprogramming is a replacement for dart:mirrors, and the equivalent of reflections in Dart is dart:mirrors, I believe static metaprogramming would be the most correct feature to help solve this problem. |
@Wdestroier I don't think static metaprogramming is going to help us much here. It can't change signatures of the existing methods it can only generate new methods. The problem we are facing here does not really exist in Java - as Java has the full zoo of numeric types. In Dart however there is just |
@mraleph is the primary shortcoming you see with views the inability to use literals (or in general values) of the representation type as initializers? Or are there other places it falls short? |
@leafpetersen I think if we had a way to declare conversions both ways as implicit (both to and from representation type) & CFE preserved view type, rather than fully erasing it, then I think views would do the job. Essentially we want the following to be a valid code: @Native()
external Int64 sum(Int64 a, Int64 b);
int useSum(List<int> v) {
return v.reduce((a, b) => sum(a, b));
// Maybe even the following, but probably okay if this does not type check.
// return v.reduce(sum);
} In other words, we don't really need much of advanced view functionality - what we want is a |
Similar to allowing the overload of implicit and explicit |
This is not really the full picture. We also use the native types (currently subtypes of
Moreover, we'd have to change
(Side note: we want to stop reifying the type argument of Pointer at runtime (#49935). However, we want to keep all static checks that we currently get from using native types as type arguments.) |
My general take is that if we want to support this, it should probably be a separate first class feature that can be used for any type.
If I understand correctly, this is more of an implementation detail around how the CFE represents these?
Yes.
Views can be made subtypes of other view types. I'm not sure if that covers your use case or not. We have considered allowing views to implement class types in the case that the underlying representation type also implements that class type. That is currently not in the proposal though. |
We need to add |
If I'm going to modify the API, I might as well take the |
Bug: #49803 Bug: #50097 Change-Id: Id5b52be88937bcf9245f98e71afa56f079f288f0 Cq-Include-Trybots: luci.dart.try:analyzer-linux-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265085 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
View types will be part of the AST since we need them to do static reasoning on precompiled code. The implementation will be similar to the current implementation of the experimental class ViewType extends DartType {
final View view;
final List<DartType> typeArguments;
final DartType representationType;
...
} where view class View<T>(T it) {}
View<int> method() => ... the representation type of |
See API design discussion in bugs below. TEST=tests/ffi/native_assets/* Design doc: http://go/dart-native-assets Bug: #49803 Bug: #50097 Change-Id: Id6e6eb94c6eb39ccaaa637448583a40ab6110d12 Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64c-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,vm-kernel-precomp-win-debug-x64c-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,dart2wasm-linux-x64-d8-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265084 Reviewed-by: Lasse Nielsen <lrn@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
Bug: #50097 Change-Id: I8d555e27167d13d21d88a2961a0501155119409b CoreLibraryReviewExempt: Isn't used in dart2js, is used in dart2wasm. Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279514 Commit-Queue: Daco Harkes <dacoharkes@google.com> Reviewed-by: Aske Simon Christensen <askesc@google.com>
Our current syntax for "ffi-natives" is still a bit verbose.
1. Rename
FfiNative
toNative
We could consider
@Ffi<...>
and@Native<...>
.sum
is an external function.sum
is a native function.sum
is not an "ffi" function, the mechanism for callingsum
is "ffi".This makes it most natural to change the syntax to:
Courtesy of @mit-mit and @mkustermann.
2. Make the symbol optional
If the symbol in C is identical to the Dart symbol, we don't need to repeat it.
Wish list. Don't write dart types
The native types uniquely define the Dart types, so it would be nice if we could omit the Dart types
However, we would need some kind of transformation before any Dart static analysis runs to replace the native types with the corresponding Dart code. Otherwise, calling a function with Dart integers will not pass type checks.
We would likely need macros for this, if our approach for macros could already support this.
Misc
The old natives are deprecated, so we don't have to worry about naming collision (having to designate the new natives as "new native" or "ffi native"). FWIW the syntax of the deprecated old natives:
See d8d7af1.
The text was updated successfully, but these errors were encountered: