Skip to content

[vm/ffi] Native asset resolution by the Dart VM for FfiNatives #49803

Closed
@dcharkes

Description

@dcharkes

Given the following Dart code.

@FfiNative<Int64 Function(Int64, Int64)>('sum', asset: 'mylib/mylib')
external int sum(int a, int b);

Update 2022-10-19: We're taking a different approach. Below is no longer applicable.

The embedder can register a resolver function to look up symbols in assets.

/**
 * FFI native asset C function pointer resolver callback.
 *
 * \param asset The name of the asset.
 * \param symbol The name of the symbol.
 * \param error Returns NULL if lookup is successful, an error message
 *   otherwise. The caller is responsible for calling free() on the error
 *   message.
 *
 * \return Looks up the symbol in the asset and returns its address in memory.
 *         Returns NULL if the asset or symbol doesn't exist.
 */
typedef const void* (*Dart_NativeAssetResolver)(const char* asset,
                                                const char* symbol,
                                                char** error);

/**
 * Sets the callback used to resolve FFI native functions in assets for an
 * isolate group.
 *
 * The resolved functions are expected to be a C function pointer of the
 * correct signature (as specified in the `@FfiNative<NFT>()` function
 * annotation in Dart code).
 *
 * NOTE: This is an experimental feature and might change in the future.
 *
 * \param isolate_group An isolate group.
 * \param resolver A native function resolver.
 */
DART_EXPORT void Dart_SetNativeAssetResolver(
    Dart_IsolateGroup isolate_group,
    Dart_NativeAssetLookup resolver);

The Dart standalone embedder can receive a mapping from asset to path through a flag. (As follow up, this mapping will be provided automatically by packages which want to bundle native assets.)

Internal design doc

Metadata

Metadata

Assignees

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions