diff --git a/automate/__main__.py b/automate/__main__.py index d0fa5d42..d444894d 100644 --- a/automate/__main__.py +++ b/automate/__main__.py @@ -55,18 +55,18 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): ) replace_text_in_file( "native/hub/src/lib.rs", - "// use tokio_with_wasm", - "use tokio_with_wasm", + "// use tokio_with_wasm::alias as tokio;", + "use tokio_with_wasm::alias as tokio;", ) replace_text_in_file( "native/hub/Cargo.toml", - "# wasm-bindgen", - "wasm-bindgen", + "# tokio_with_wasm", + "tokio_with_wasm", ) replace_text_in_file( "native/hub/Cargo.toml", - "# tokio_with_wasm", - "tokio_with_wasm", + "# wasm-bindgen", + "wasm-bindgen", ) os.chdir("../") @@ -109,18 +109,18 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): ) replace_text_in_file( "native/hub/src/lib.rs", - "// use tokio_with_wasm", - "use tokio_with_wasm", + "// use tokio_with_wasm::alias as tokio;", + "use tokio_with_wasm::alias as tokio;", ) replace_text_in_file( "native/hub/Cargo.toml", - "# wasm-bindgen", - "wasm-bindgen", + "# tokio_with_wasm", + "tokio_with_wasm", ) replace_text_in_file( "native/hub/Cargo.toml", - "# tokio_with_wasm", - "tokio_with_wasm", + "# wasm-bindgen", + "wasm-bindgen", ) os.chdir("../") diff --git a/documentation/docs/frequently-asked-questions.md b/documentation/docs/frequently-asked-questions.md index d64be28e..b5848f18 100644 --- a/documentation/docs/frequently-asked-questions.md +++ b/documentation/docs/frequently-asked-questions.md @@ -270,7 +270,7 @@ Here are the current constraints of the `wasm32-unknown-unknown` target: - Numerous functionalities within `std::fs` remain unimplemented. - Various features of `std::net` are not available. Consider using `reqwest` crate instead. `reqwest` supports `wasm32-unknown-unknown` and relies on JavaScript to perform network communications. -- `std::thread::spawn` doesn't work. Consider using `tokio_with_wasm::tokio::task::spawn_blocking` instead. +- `std::thread::spawn` doesn't work. Consider using `tokio_with_wasm::task::spawn_blocking` instead. - Several features of `std::time::Instant` are unimplemented. Consider using `chrono` as an alternative. `chrono` supports `wasm32-unknown-unknown` and relies on JavaScript to obtain system time. - In case of a panic in an asynchronous Rust task, it aborts and throws a JavaScript `RuntimeError` [which Rust cannot catch](https://stackoverflow.com/questions/59426545/rust-paniccatch-unwind-no-use-in-webassembly). A recommended practice is to replace `.unwrap` with `.expect` or handle errors with `Err` instances. diff --git a/flutter_ffi_plugin/example/native/hub/Cargo.toml b/flutter_ffi_plugin/example/native/hub/Cargo.toml index de173be1..f3d5ea84 100755 --- a/flutter_ffi_plugin/example/native/hub/Cargo.toml +++ b/flutter_ffi_plugin/example/native/hub/Cargo.toml @@ -14,7 +14,12 @@ crate-type = ["lib", "cdylib", "staticlib"] [dependencies] rinf = "6.12.1" prost = "0.12.6" -# tokio = { version = "1", features = ["sync"] } -wasm-bindgen = "0.2.92" # Uncomment this line to target the web -tokio_with_wasm = "0.4.4" # Uncomment this line to target the web +tokio = { version = "1", features = ["rt", "sync", "macros", "time"] } +tokio_with_wasm = { version = "0.5.3", features = [ + "rt", + "sync", + "macros", + "time", +] } +wasm-bindgen = "0.2.92" sample_crate = { path = "../sample_crate" } diff --git a/flutter_ffi_plugin/example/native/hub/src/lib.rs b/flutter_ffi_plugin/example/native/hub/src/lib.rs index 26bc5a85..d5a61020 100755 --- a/flutter_ffi_plugin/example/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/example/native/hub/src/lib.rs @@ -5,7 +5,7 @@ mod messages; mod sample_functions; // use tokio; -use tokio_with_wasm::tokio; // Uncomment this line to target the web +use tokio_with_wasm::alias as tokio; rinf::write_interface!(); diff --git a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs index 03b31f61..2205bb4b 100755 --- a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs +++ b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs @@ -209,5 +209,13 @@ pub async fn run_debug_tests() { } debug_print!("Debug tests completed!"); - panic!("INTENTIONAL DEBUG PANIC"); + + tokio::spawn(async { + // Panic in a separate task + // to avoid memory leak on the web. + // On the web (`wasm32-unknown-unknown`), + // catching and unwinding panics is not possible. + // It is better to avoid panicking code at all costs on the web. + panic!("INTENTIONAL DEBUG PANIC"); + }); } diff --git a/flutter_ffi_plugin/template/native/hub/Cargo.toml b/flutter_ffi_plugin/template/native/hub/Cargo.toml index c8be858e..dd484221 100644 --- a/flutter_ffi_plugin/template/native/hub/Cargo.toml +++ b/flutter_ffi_plugin/template/native/hub/Cargo.toml @@ -15,5 +15,7 @@ crate-type = ["lib", "cdylib", "staticlib"] rinf = "6.12.1" prost = "0.12.6" tokio = { version = "1", features = ["sync"] } -# wasm-bindgen = "0.2.92" # Uncomment this line to target the web -# tokio_with_wasm = "0.4.4" # Uncomment this line to target the web + +# Uncomment below to target the web. +# tokio_with_wasm = { version = "0.5.3", features = ["sync"] } +# wasm-bindgen = "0.2.92" diff --git a/flutter_ffi_plugin/template/native/hub/src/lib.rs b/flutter_ffi_plugin/template/native/hub/src/lib.rs index e1a12658..9c2519f4 100644 --- a/flutter_ffi_plugin/template/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/template/native/hub/src/lib.rs @@ -3,8 +3,8 @@ mod messages; -use tokio; -// use tokio_with_wasm::tokio; // Uncomment this line to target the web +use tokio; // Comment this line to target the web. +// use tokio_with_wasm::alias as tokio; // Uncomment this line to target the web. rinf::write_interface!();