Skip to content
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

[ffi] @Native for global fixed-size arrays #54337

Closed
dcharkes opened this issue Dec 13, 2023 · 2 comments
Closed

[ffi] @Native for global fixed-size arrays #54337

dcharkes opened this issue Dec 13, 2023 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi P3 A lower priority bug or feature request triaged Issue has been triaged by sub team

Comments

@dcharkes
Copy link
Contributor

https://dart-review.googlesource.com/c/sdk/+/338020 adds support for #50551, but does not add support for global fixed size arrays.

double foo[4];

double return_double() {
  return foo[1];
}

void set_double(double value) {
  foo[2] = value;
}

Could theoretically be bound to.

@Array(4)
@Native()
external Array<Double> foo;

It just so happens to be that currently the following works because the memory layout is identical.

@Native()
external Pointer<Double> foo;

The advantage of supporting the latter is that we can do bound checks on use.

@simolus3 Thanks for making the Array limitation explicit in https://dart-review.googlesource.com/c/sdk/+/338020. Here are some of my thoughts about it.

Re dart-lang/native#860, should we temporarily support generating Pointers for fixed-size global arrays? That would make it a breaking change later to make it Arrays, though. On the other hand, if users would like to use those fields than without generating Pointers, users can't use FFIgen at all.

@dcharkes dcharkes added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi labels Dec 13, 2023
@simolus3
Copy link
Contributor

That would make it a breaking change later to make it Arrays, though.

Technically yes, but thanks to all the extensions in dart:ffi which provide operator[] and operator[]= on pointers too, migrating between pointers and arrays is unlikely to require users to change their code.

@a-siva a-siva added P3 A lower priority bug or feature request triaged Issue has been triaged by sub team labels Dec 14, 2023
@simolus3
Copy link
Contributor

Re dart-lang/native#860, should we temporarily support generating Pointers for fixed-size global arrays?

As it turns out, we're already generating pointers for global arrays in ffigen. That's understandable, since there is no public mechanism for getting from a pointer to an array instance, and so the generated code based on looking up bindings manually can't do any better.

This gives us a good opportunity to get this right with native fields though. I have opened https://dart-review.googlesource.com/c/sdk/+/341860 to support array fields in the SDK, and I've updated the ffigen PR to make use of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi P3 A lower priority bug or feature request triaged Issue has been triaged by sub team
Projects
None yet
Development

No branches or pull requests

3 participants