Skip to content

Expose external typed arrays in dart_api_dl.h #54465

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

Closed
johnmccutchan opened this issue Dec 28, 2023 · 6 comments
Closed

Expose external typed arrays in dart_api_dl.h #54465

johnmccutchan opened this issue Dec 28, 2023 · 6 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@johnmccutchan
Copy link
Contributor

I'd like to be able to construct external typed arrays in dart_api_dl.h.

Can I expose the APIs?

@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Dec 28, 2023
@dcharkes
Copy link
Contributor

@johnmccutchan What is your use case?

Why not pass a Pointer as return value from an FFI call or as an argument in a callback? And then use asTypedList to take ownership of the data and attach a finalizer that calls free?

@johnmccutchan
Copy link
Contributor Author

johnmccutchan commented Dec 29, 2023

I tried that but I don't like the lifecycle requirements and ergonomics.

Correct me if I'm wrong but I would need something like the following for the finalizer:

class NativeTypedData<T> implements ffi.Finalizable {
  // ...finalizer
  // ffi.Pointer<T>

  Object /* one of TypedLists. */ asTypedList();
}

The main problem I have with it is that the lifecycle is tied to NativeTypedData and not to the, say, Float32List that I actually want.

And unfortunately, I can't make a specialized NativeTypedData that implements, say, Float32List because implements Float32List is a compilation error.

I'm going to be vending these arrays out to user code and the life cycle will be too subtle and tricky if they have to keep this other object alive when they are really only interested in the typed list.

Maybe I'm holding Finalizable wrong?

@dcharkes
Copy link
Contributor

dcharkes commented Jan 2, 2024

Correct me if I'm wrong but I would need something like the following for the finalizer:

class NativeTypedData<T> implements ffi.Finalizable {
  // ...finalizer
  // ffi.Pointer<T>

  Object /* one of TypedLists. */ asTypedList();
}

No, you should not be needing a Finalizable, asTypedList has a finalizer argument.

I'm going to be vending these arrays out to user code and the life cycle will be too subtle and tricky if they have to keep this other object alive when they are really only interested in the typed list.

Yes, you want to create that Float32List with a finalizer argument such that the finalizer is attached to the Float32List and you can give that one to the user.

@johnmccutchan
Copy link
Contributor Author

Okay, so that solves the finalizer part.

Now I'll need to convert it back to a float* when passing it to native code, I suppose I should use a leaf function and have it automatically unwrapped ?

@dcharkes
Copy link
Contributor

dcharkes commented Jan 2, 2024

Now I'll need to convert it back to a float* when passing it to native code, I suppose I should use a leaf function and have it automatically unwrapped ?

Correct. 👌

(Please note you need to be on the dev/main branch for that, it landed in December. https://dart-review.googlesource.com/c/sdk/+/338620)

@johnmccutchan
Copy link
Contributor Author

Yes, I've been using it already.

It would be nice if ffigen supported generating the code for unwrapping leaf functions. Unless I missed something I have to manually call LookupFunction with the correct type arguments.

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.
Projects
None yet
Development

No branches or pull requests

3 participants