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] How to add native code? #53959

Closed
medz opened this issue Nov 5, 2023 · 4 comments
Closed

[ffi] How to add native code? #53959

medz opened this issue Nov 5, 2023 · 4 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi type-question A question about expected behavior or functionality

Comments

@medz
Copy link

medz commented Nov 5, 2023

I have a unique requirement, I usually develop dylibs in Rust and I always have to inject something into dylib specific functions.

When I saw napi I thought it was amazing and I spent many days studying it.

Let me first talk about my ffi needs. I want to use dart to develop a library. This library defines many functions such as a.
I develop a dylib using Rust, and then I write in rust:

extern "C" {
     fn a(value: *mut c_char);
}

#[no_mangle]
pub extern "C" b() {
     let hi = "hello";
     a(hi.into_raw());
}

Then I do this in Dart:

void a(String value) => print(value);

final lib = DynamicLibrary('b.dylib');

final b = lib.lookupFunction('b');
b();

I know of course that I can pass dart 's a function to b (from dylib), but that's inconvenient, and I'd like to develop some other language compatible libraries to dart.

When I researched napi I found that they implement this.

I tried using C and Rust, and after compiling Rust to a dylib, I can declare extern "C" in Rust to have the dylib call functions written in C code!

So the question is, how to declare native functions written in Dart to dylib in Dart FFI?

@medz
Copy link
Author

medz commented Nov 5, 2023

I want to add some defined constants or functions to the Dart native for Dylib to call.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. type-question A question about expected behavior or functionality library-ffi labels Nov 5, 2023
@mraleph
Copy link
Member

mraleph commented Nov 7, 2023

There is currently no functionality to achieve what you want beyond that way you have already discovered, i.e. you can manually pass pointers to Dart functions to the native code.

We have discussed adding something like @ffi.Export() annotation to dart:ffi to facilitate exporting of Dart functions - but we have not gotten to it so far (@dcharkes do we have an issue tracking ffi.Export functionality somewhere?).

There is one subtle problem that makes such functionality complicated: Dart has isolates and native languages don't have them. So if you want to expose Dart functions to native code like C or Rust a big question arises: when native code calls a Dart function which isolate does it use?

Unfortunately so far we did not find a good answer to this question and that makes reverse binding (e.g. calling Dart from native side) more complicated.

@dcharkes
Copy link
Contributor

dcharkes commented Nov 7, 2023

We have discussed adding something like @ffi.Export() annotation to dart:ffi to facilitate exporting of Dart functions - but we have not gotten to it so far (@dcharkes do we have an issue tracking ffi.Export functionality somewhere?).

@mraleph
Copy link
Member

mraleph commented Nov 10, 2023

Duplicate of #51383

@mraleph mraleph marked this as a duplicate of #51383 Nov 10, 2023
@mraleph mraleph closed this as completed Nov 10, 2023
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 type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

4 participants