Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 463cff5

Browse files
committed
Provide an option to set compiled library file path
1 parent 20e1013 commit 463cff5

6 files changed

Lines changed: 45 additions & 5 deletions

File tree

flutter_ffi_plugin/bin/src/message.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ hash_map.insert(
504504
import 'dart:typed_data';
505505
import 'package:rinf/rinf.dart';
506506
507-
Future<void> initializeRust() async {
507+
Future<void> initializeRust({String? compiledLibPath}) async {
508+
setCompiledLibPath(compiledLibPath);
508509
await prepareInterface(handleRustSignal);
509510
startRustLogic();
510511
}

flutter_ffi_plugin/lib/rinf.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import 'src/exports.dart';
66

77
export 'src/interface.dart' show RustSignal;
88

9+
/// Sets the exact file path of the dynamic library
10+
/// compiled from the `hub` crate.
11+
/// On the web, this function sets the path to the JavaScript module
12+
/// that needs to be loaded.
13+
/// This function might not be necessary for major platforms
14+
/// but can be useful when the app runs on embedded devices.
15+
void setCompiledLibPath(String? path) {
16+
setCompiledLibPathExtern(path);
17+
}
18+
919
/// Prepares the native interface
1020
/// needed to communicate with Rust.
1121
Future<void> prepareInterface(HandleRustSignal handleRustSignal) async {
@@ -18,9 +28,9 @@ void startRustLogic() async {
1828
}
1929

2030
/// Terminates all Rust tasks.
21-
/// Doing so before closing the Flutter app
22-
/// can prevent potential memory errors that may occur
23-
/// when Rust attempts to send data after the Dart VM has been turned off.
31+
/// Calling this function before closing the Flutter app
32+
/// can prevent potential resource leaks that may occur
33+
/// if the Rust side is abruptly terminated.
2434
/// Please note that on the web, this function does not have any effect,
2535
/// as tasks are managed by the JavaScript runtime, not Rust.
2636
void stopRustLogic() async {

flutter_ffi_plugin/lib/src/interface_os.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import 'dart:isolate';
77
import 'interface.dart';
88
import 'dart:convert';
99

10+
void setCompiledLibPathExtern(String? path) {
11+
setDynamicLibPath(path);
12+
}
13+
1014
Future<void> prepareInterfaceExtern(
1115
HandleRustSignal handleRustSignal,
1216
) async {

flutter_ffi_plugin/lib/src/interface_web.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import 'interface.dart';
77
import 'dart:async';
88
import 'dart:convert';
99

10+
void setCompiledLibPathExtern(String? path) {
11+
setJsLibPath(path);
12+
}
13+
1014
Future<void> prepareInterfaceExtern(
1115
HandleRustSignal handleRustSignal,
1216
) async {

flutter_ffi_plugin/lib/src/load_os.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import 'dart:io' as io;
22
import 'dart:ffi';
33

4+
String? dynamicLibPath;
45
final rustLibrary = loadRustLibrary();
56

7+
void setDynamicLibPath(String? path) {
8+
dynamicLibPath = path;
9+
}
10+
611
DynamicLibrary loadRustLibrary() {
12+
// Use provided dynamic library path if possible.
13+
final path = dynamicLibPath;
14+
if (path != null) {
15+
return DynamicLibrary.open(path);
16+
}
17+
18+
// Otherewise, use the default path.
719
if (io.Platform.isLinux) {
820
return DynamicLibrary.open('libhub.so');
921
} else if (io.Platform.isAndroid) {

flutter_ffi_plugin/lib/src/load_web.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import 'dart:js' as js;
44
import 'dart:html';
55
import 'dart:async';
66

7+
String? jsLibPath;
8+
79
// When Dart performs hot restart,
810
// the `rinf` object is already defined
911
// as a global JavaScript variable.
1012
final wasAlreadyLoaded = js.context.hasProperty("rinf");
1113

14+
void setJsLibPath(String? path) {
15+
jsLibPath = path;
16+
}
17+
1218
Future<void> loadJsFile() async {
1319
if (wasAlreadyLoaded) {
1420
return;
@@ -17,10 +23,13 @@ Future<void> loadJsFile() async {
1723
final loadCompleter = Completer<void>();
1824
js.context['completeRinfLoad'] = loadCompleter.complete;
1925

26+
// Use the default JavaScript path unless provided.
27+
final path = jsLibPath ?? "/pkg/hub.js";
28+
2029
final scriptElement = ScriptElement();
2130
scriptElement.type = "module";
2231
scriptElement.innerHtml = '''
23-
import init, * as wasmBindings from "/pkg/hub.js";
32+
import init, * as wasmBindings from "$path";
2433
await init();
2534
window.rinf = { ...wasmBindings };
2635
completeRinfLoad();

0 commit comments

Comments
 (0)