Skip to content

[ffigen] Auto-load native asset #3338

@liamappelbe

Description

@liamappelbe

Running into a funny case in the FFIgen migration from test/setup.dart to a build hook. If you have an ObjC API that contains only classes and methods, then it's entirely possible that the lazy loading of @Native will never trigger.

ObjC classes are loaded getClass("Foo"), and methods are looked up using registerName("bar") and invoked using a cast of msgSendPointer. All the actual functions come from package:objective_c, so @Native only has to load the package:objective_c dylib. As far as @Native is concerned, the generated bindings might not load anything.

For built in frameworks this is not a problem because all the classes are already loaded. But for classes defined in native code that the user builds using a build hook, they'll get runtime errors due to failing to load the class.

The current work around is to load the library by either invoking a native function defined in the dylib first, or explicitly loading the dylib using DynamicLibrary.open. Either approach loads the classes into the process. But the first approach requires writing an unnecessary native function, and the second only works if you have a path to the dylib (which usually isn't true for build hooks).

@dcharkes is there an API for loading a native asset by its ID? Similar to DynamicLibrary.open, but accepting an asset ID?

If not I might have to start code-genning an empty native function in the generated .m file, so I can auto-invoke it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions