Skip to content

[ffi] How to add native code? #53959

Closed
Closed
@medz

Description

@medz

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffitype-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions