Skip to content

Commit

Permalink
Changed load options to enum,
Browse files Browse the repository at this point in the history
  • Loading branch information
vm75 committed Dec 13, 2024
1 parent e7438e2 commit 25b7f72
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 24 deletions.
1 change: 1 addition & 0 deletions tools/update-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ publish() {
read -p "Do you want to publish ${name}? (y/n): " publish

if [[ ${publish} == "y" ]]; then
git add *
cd ${name}
dart pub get
dart pub publish
Expand Down
3 changes: 3 additions & 0 deletions universal_ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.1.0]
* Changed load options to enum

## [1.0.8]
* Fix missing allocator for non-web

Expand Down
14 changes: 7 additions & 7 deletions universal_ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ FfiHelper.load resolves the modulePath to the platform specific path in a variet
#### Simple usage
In the case, it is assumed that all platforms load a shared library from the same relative path.
For example, if the modulePath = 'path/name', then the following paths are used:
- Web: 'path/name.js' or 'path/name.wasm' (if `is-standalone-wasm` option is specified)
- Web: 'path/name.js' or 'path/name.wasm' (if `isStandaloneWasm` option is specified)
- Linux & Android: 'path/name.so'
- Windows: 'path/name.dll'
- macOS & iOS: 'path/name.dylib'

#### Option: is-statically-linked
If the modulePath = 'path/name' and `is-statically-linked` option is specified, then the following paths are used:
- Web: 'path/name.js' or 'path/name.wasm' (if `is-standalone-wasm` option is specified)
#### Option: isStaticallyLinked
If the modulePath = 'path/name' and `isStaticallyLinked` option is specified, then the following paths are used:
- Web: 'path/name.js' or 'path/name.wasm' (if `isStandaloneWasm` option is specified)
- All other platforms: Instead of loading a shared library, calls DynamicLibrary.process().

#### Option: is-ffi-plugin (used for Flutter Ffi Plugin)
If the modulePath = 'path/name' and `is-ffi-plugin` option is specified, then 'path' is ignored and the following paths are used:
- Web: 'assets/package/name/assets/name.js' or 'assets/package/name/assets/name.wasm' (if `is-standalone-wasm` option is specified)
#### Option: isFfiPlugin (used for Flutter Ffi Plugin)
If the modulePath = 'path/name' and `isFfiPlugin` option is specified, then 'path' is ignored and the following paths are used:
- Web: 'assets/package/name/assets/name.js' or 'assets/package/name/assets/name.wasm' (if `isStandaloneWasm` option is specified)
- Linux & Android: 'name.so'
- Windows: 'name.dll'
- macOS & iOS: 'name.framework/name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<bool> init() async {
try {
_ffiHelper = await FfiHelper.load(
'example_ffi_plugin',
options: {'is-ffi-plugin'},
options: {LoadOption.isFfiPlugin},
);

_bindings = ExampleFfiPluginBindings(_ffiHelper.library);
Expand Down
16 changes: 11 additions & 5 deletions universal_ffi/lib/ffi_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ enum AppType {
unknown,
}

enum LoadOption {
isStaticallyLinked,
isFfiPlugin,
isStandaloneWasm,
}

/// Extension on [DynamicLibrary] with asynchronous methods.
extension AsyncDynamicLibrary on DynamicLibrary {
/// Asynchronously opens a dynamic library from the specified [path].
Expand Down Expand Up @@ -75,24 +81,24 @@ class FfiHelper {
///
/// [modulePath]: The path to the module to be loaded.
/// [options]: Optional load options, all defaulting to false.
/// * is-statically-linked: non-web modules are statically linked.
/// * is-ffi-plugin: this is a Ffi plugin.
/// * is-standalone-wasm: indicates whether the wasm is standalone.
/// * isStaticallyLinked: non-web modules are statically linked.
/// * isFfiPlugin: this is a Ffi plugin.
/// * isStandaloneWasm: indicates whether the wasm is standalone.
/// [overrides]: [AppType] specific overrides to the path to the module to be loaded.
/// * Empty override indicates that the module is statically linked.
///
/// Returns a [Future] that completes with an [FfiHelper] instance.
/// Throws an [ArgumentError] if the module cannot be found.
static Future<FfiHelper> load(
String modulePath, {
Set<String> options = const {},
Set<LoadOption> options = const {},
Map<AppType, String> overrides = const {},
}) async {
modulePath = overrides[appType] ?? resolveModulePath(modulePath, options);

// If module path is empty, it is treated as a statically linked library
// This is not supported for Web/Wasm
if (modulePath.isEmpty || options.contains('is-statically-linked')) {
if (modulePath.isEmpty || options.contains(LoadOption.isStaticallyLinked)) {
if (appType == AppType.web) {
throw ArgumentError(
'Statically linked library is not supported for Web/Wasm',
Expand Down
8 changes: 4 additions & 4 deletions universal_ffi/lib/src/dart_ffi/_ffi_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io' show Platform;
import 'package:path/path.dart' as path;
import '../../ffi_helper.dart' show AppType;
import '../../ffi_helper.dart' show AppType, LoadOption;

/// Returns the type of the current app.
///
Expand Down Expand Up @@ -30,15 +30,15 @@ AppType get appType {
///
/// [modulePath] is the path to the shared library module.
/// [options] optional load options.
/// * is-ffi-plugin: this is a Ffi plugin.
String resolveModulePath(String modulePath, Set<String> options) {
/// * isFfiPlugin: this is a Ffi plugin.
String resolveModulePath(String modulePath, Set<LoadOption> options) {
if (modulePath.isEmpty) {
return '';
}

final moduleName = path.basenameWithoutExtension(modulePath);
final moduleDir = path.dirname(modulePath);
final isFfiPlugin = options.contains('is-ffi-plugin');
final isFfiPlugin = options.contains(LoadOption.isFfiPlugin);

late String fileName;
if (Platform.isWindows) {
Expand Down
12 changes: 6 additions & 6 deletions universal_ffi/lib/src/wasm_ffi/_ffi_helper.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:path/path.dart' as path;
import '../../ffi_helper.dart' show AppType;
import '../../ffi_helper.dart' show AppType, LoadOption;

/// Returns the type of the current app.
///
Expand All @@ -14,12 +14,12 @@ AppType get appType {
///
/// [modulePath] is the path to the shared library module.
/// [options] optional load options.
/// * is-ffi-plugin: indicates whether the module is a plugin.
/// * is-standalone-wasm: indicates whether the wasm is standalone.
String resolveModulePath(String modulePath, Set<String> options) {
/// * isSfiPlugin: indicates whether the module is a plugin.
/// * isStandaloneWasm: indicates whether the wasm is standalone.
String resolveModulePath(String modulePath, Set<LoadOption> options) {
var ext = path.extension(modulePath);
final isFfiPlugin = options.contains('is-ffi-plugin');
final isStandaloneWasm = options.contains('is-standalone-wasm');
final isFfiPlugin = options.contains(LoadOption.isFfiPlugin);
final isStandaloneWasm = options.contains(LoadOption.isStandaloneWasm);
if (ext == '') {
ext = isStandaloneWasm ? '.wasm' : '.js';
modulePath = '$modulePath$ext';
Expand Down
2 changes: 1 addition & 1 deletion universal_ffi/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: universal_ffi
description: A drop-in replacement for dart:ffi for all platforms including web (using wasm_ffi).
version: 1.0.8
version: 1.1.0
repository: https://github.com/vm75/native.ffi/tree/main/universal_ffi

environment:
Expand Down
Loading

0 comments on commit 25b7f72

Please sign in to comment.