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

Converting FFI Handle to Dart Object #49543

Open
liamappelbe opened this issue Jul 27, 2022 · 1 comment
Open

Converting FFI Handle to Dart Object #49543

liamappelbe opened this issue Jul 27, 2022 · 1 comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@liamappelbe
Copy link
Contributor

When using FFI to load a function that has a handle in the signature, you can either put Handle in the signature, or Pointer<Void>. If you use Handle, the FFI trampoline will automatically convert the C Dart_Handle to a Dart Object. If you use Pointer<Void>, you'll just get an opaque value that is C compatible.

I have a use case where I want to put a Dart_PersistentHandle to a Dart Function in a C struct. I used FFI to load Dart_NewPersistentHandle and Dart_DeletePersistentHandle. I loaded Dart_NewPersistentHandle as void*(Handle) so I could pass in an Object and get back something I could stick in the struct, and loaded Dart_DeletePersistentHandle as void(void*) so I could delete the handle later.

But I also wanted to get the Function out of the C struct so I could call it, and I couldn't find a way of converting the Pointer<Void> that was the Dart_PersistentHandle to a Dart Object. My hacky work around was to load another copy of Dart_NewPersistentHandle and Dart_DeletePersistentHandle, this time as Handle(void*) and void(Handle), so I could use the generated trampolines to do the conversion.

Context: dart-lang/native#233

@dcharkes How hard would it be to add a util to package:ffi that can convert Dart_Handle etc to a Dart Object?

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

@liamappelbe could you provide some code snippets to illustrate the problem?

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

No branches or pull requests

3 participants