Skip to content

Commit

Permalink
Simplify examples without dummy main
Browse files Browse the repository at this point in the history
wasm-bindgen no longer autostarts main on worker threads:
rustwasm/wasm-bindgen#3236
  • Loading branch information
chemicstry committed Mar 10, 2023
1 parent 1097fe7 commit 3e032fb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ log = "0.4"
env_logger = "0.7"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
console_log = { version = "0.2", features = ["color"] }
console_log = { version = "1.0", features = ["color"] }
console_error_panic_hook = "0.1"
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ cargo install wasm-bindgen-cli

#### wasm-pack

- Build with `./examples-wasm-pack/web-build.sh` for an example targeting `web`, and `./examples/wasm-pack/web-build-no-module.sh` for an example targeting `no-modules`.
- Install `wasm-pack`:
```bash
cargo install wasm-pack
```
- Build with `./examples-wasm-pack/web-build.sh` for an example targeting `web`, and `./examples-wasm-pack/web-build-no-module.sh` for an example targeting `no-modules`.
- Serve `./examples-wasm-pack/module` or `./examples-wasm-pack/no-module`, respectively, over HTTP and open `simple.html` in browser. Inspect console output.

### Example output
Expand Down
9 changes: 2 additions & 7 deletions examples-wasm-pack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm_thread = { path = "../." }
wasm_thread = { path = "../" }
log = "0.4"
env_logger = "0.7"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Blob", "DedicatedWorkerGlobalScope", "MessageEvent", "Url", "Worker", "WorkerType", "WorkerOptions"]}
js-sys = "0.3"
futures = "0.3"
async-channel = "1.4"
console_log = { version = "0.2", features = ["color"] }
console_log = { version = "1.0", features = ["color"] }
console_error_panic_hook = "0.1"
26 changes: 13 additions & 13 deletions examples-wasm-pack/module/simple.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script type="module">
import init, {run} from "./target/simple.js";
init()
.then(() => {
run()
});
</script>
</body>
</html>

<head>
<meta charset="UTF-8" />
</head>

<body>
<script type="module">
import init from "./target/simple.js";
init();
</script>
</body>

</html>
25 changes: 13 additions & 12 deletions examples-wasm-pack/no-module/simple.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm').then((wasm) => {
wasm.run();
});
</script>
</body>
</html>

<head>
<meta charset="UTF-8" />
</head>

<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm');
</script>
</body>

</html>
41 changes: 6 additions & 35 deletions examples-wasm-pack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
use std::time::Duration;

#[cfg(not(target_arch = "wasm32"))]
use std::thread;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use wasm_thread as thread;

#[cfg(target_arch = "wasm32")]
mod wasm {
use crate::main;
use wasm_bindgen::prelude::*;

// Prevent `wasm_bindgen` from autostarting main on all spawned threads
#[wasm_bindgen(start)]
pub fn dummy_main() {}

// Export explicit run function to start main
#[wasm_bindgen]
pub fn run() {
console_log::init().unwrap();
console_error_panic_hook::set_once();
main();
}
}

#[wasm_bindgen(start)]
fn main() {
#[cfg(not(target_arch = "wasm32"))]
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
console_log::init().unwrap();
console_error_panic_hook::set_once();

for _ in 0..2 {
thread::spawn(|| {
for i in 1..3 {
log::info!(
"hi number {} from the spawned thread {:?}!",
i,
thread::current().id()
);
log::info!("hi number {} from the spawned thread {:?}!", i, thread::current().id());
thread::sleep(Duration::from_millis(1));
}
});
}

for i in 1..3 {
log::info!(
"hi number {} from the main thread {:?}!",
i,
thread::current().id()
);
log::info!("hi number {} from the main thread {:?}!", i, thread::current().id());
}
}
4 changes: 1 addition & 3 deletions examples/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm').then(async (wasm) => {
await wasm.run();
});
wasm_bindgen('./target/simple_bg.wasm');
</script>
</body>

Expand Down
21 changes: 4 additions & 17 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@ use std::time::Duration;

use wasm_thread as thread;

#[cfg(target_arch = "wasm32")]
mod wasm {
use wasm_bindgen::prelude::*;

use crate::main;

// Prevent `wasm_bindgen` from autostarting main on all spawned threads
#[wasm_bindgen(start)]
pub fn dummy_main() {}

// Export explicit run function to start main
#[wasm_bindgen]
pub fn run() {
fn main() {
#[cfg(target_arch = "wasm32")]
{
console_log::init().unwrap();
console_error_panic_hook::set_once();
main();
}
}

fn main() {
#[cfg(not(target_arch = "wasm32"))]
env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"));

Expand Down Expand Up @@ -82,7 +69,7 @@ fn main() {
}));

// Wait for all threads, otherwise program exits before threads finish execution.
// We can't do blocking join on wasm main thread though.
// We can't do blocking join on wasm main thread though, but the browser window will continue running.
#[cfg(not(target_arch = "wasm32"))]
for handle in threads {
handle.join().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/wasm32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Builder {

#[cfg(feature = "es_modules")]
{
load_module_workers_polyfill();
utils::load_module_workers_polyfill();
options.type_(WorkerType::Module);
}
#[cfg(not(feature = "es_modules"))]
Expand Down
4 changes: 2 additions & 2 deletions src/wasm32/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub fn is_web_worker_thread() -> bool {
}

#[cfg(feature = "es_modules")]
#[wasm_bindgen(module = "/src/js/module_workers_polyfill.min.js")]
#[wasm_bindgen(module = "/src/wasm32/js/module_workers_polyfill.min.js")]
extern "C" {
fn load_module_workers_polyfill();
pub fn load_module_workers_polyfill();
}

/// Extracts path of the `wasm_bindgen` generated .js shim script.
Expand Down

0 comments on commit 3e032fb

Please sign in to comment.