Skip to content

Commit 3e032fb

Browse files
committed
Simplify examples without dummy main
wasm-bindgen no longer autostarts main on worker threads: rustwasm/wasm-bindgen#3236
1 parent 1097fe7 commit 3e032fb

File tree

10 files changed

+48
-92
lines changed

10 files changed

+48
-92
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ log = "0.4"
3737
env_logger = "0.7"
3838

3939
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
40-
console_log = { version = "0.2", features = ["color"] }
40+
console_log = { version = "1.0", features = ["color"] }
4141
console_error_panic_hook = "0.1"

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ cargo install wasm-bindgen-cli
5050

5151
#### wasm-pack
5252

53-
- 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`.
53+
- Install `wasm-pack`:
54+
```bash
55+
cargo install wasm-pack
56+
```
57+
- 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`.
5458
- Serve `./examples-wasm-pack/module` or `./examples-wasm-pack/no-module`, respectively, over HTTP and open `simple.html` in browser. Inspect console output.
5559

5660
### Example output

examples-wasm-pack/Cargo.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ edition = "2021"
99
crate-type = ["cdylib", "rlib"]
1010

1111
[dependencies]
12-
wasm_thread = { path = "../." }
12+
wasm_thread = { path = "../" }
1313
log = "0.4"
14-
env_logger = "0.7"
1514
wasm-bindgen = "0.2"
16-
web-sys = { version = "0.3", features = ["Blob", "DedicatedWorkerGlobalScope", "MessageEvent", "Url", "Worker", "WorkerType", "WorkerOptions"]}
17-
js-sys = "0.3"
18-
futures = "0.3"
19-
async-channel = "1.4"
20-
console_log = { version = "0.2", features = ["color"] }
15+
console_log = { version = "1.0", features = ["color"] }
2116
console_error_panic_hook = "0.1"

examples-wasm-pack/module/simple.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<html>
2-
<head>
3-
<meta charset="UTF-8" />
4-
</head>
5-
<body>
6-
<script type="module">
7-
import init, {run} from "./target/simple.js";
8-
init()
9-
.then(() => {
10-
run()
11-
});
12-
</script>
13-
</body>
14-
</html>
2+
3+
<head>
4+
<meta charset="UTF-8" />
5+
</head>
6+
7+
<body>
8+
<script type="module">
9+
import init from "./target/simple.js";
10+
init();
11+
</script>
12+
</body>
13+
14+
</html>
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<html>
2-
<head>
3-
<meta charset="UTF-8" />
4-
</head>
5-
<body>
6-
<script src="./target/simple.js"></script>
7-
<script type="text/javascript">
8-
wasm_bindgen('./target/simple_bg.wasm').then((wasm) => {
9-
wasm.run();
10-
});
11-
</script>
12-
</body>
13-
</html>
2+
3+
<head>
4+
<meta charset="UTF-8" />
5+
</head>
6+
7+
<body>
8+
<script src="./target/simple.js"></script>
9+
<script type="text/javascript">
10+
wasm_bindgen('./target/simple_bg.wasm');
11+
</script>
12+
</body>
13+
14+
</html>

examples-wasm-pack/src/lib.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
11
use std::time::Duration;
22

3-
#[cfg(not(target_arch = "wasm32"))]
4-
use std::thread;
5-
#[cfg(target_arch = "wasm32")]
3+
use wasm_bindgen::prelude::*;
64
use wasm_thread as thread;
75

8-
#[cfg(target_arch = "wasm32")]
9-
mod wasm {
10-
use crate::main;
11-
use wasm_bindgen::prelude::*;
12-
13-
// Prevent `wasm_bindgen` from autostarting main on all spawned threads
14-
#[wasm_bindgen(start)]
15-
pub fn dummy_main() {}
16-
17-
// Export explicit run function to start main
18-
#[wasm_bindgen]
19-
pub fn run() {
20-
console_log::init().unwrap();
21-
console_error_panic_hook::set_once();
22-
main();
23-
}
24-
}
25-
6+
#[wasm_bindgen(start)]
267
fn main() {
27-
#[cfg(not(target_arch = "wasm32"))]
28-
env_logger::init_from_env(
29-
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
30-
);
8+
console_log::init().unwrap();
9+
console_error_panic_hook::set_once();
3110

3211
for _ in 0..2 {
3312
thread::spawn(|| {
3413
for i in 1..3 {
35-
log::info!(
36-
"hi number {} from the spawned thread {:?}!",
37-
i,
38-
thread::current().id()
39-
);
14+
log::info!("hi number {} from the spawned thread {:?}!", i, thread::current().id());
4015
thread::sleep(Duration::from_millis(1));
4116
}
4217
});
4318
}
4419

4520
for i in 1..3 {
46-
log::info!(
47-
"hi number {} from the main thread {:?}!",
48-
i,
49-
thread::current().id()
50-
);
21+
log::info!("hi number {} from the main thread {:?}!", i, thread::current().id());
5122
}
5223
}

examples/simple.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
<body>
88
<script src="./target/simple.js"></script>
99
<script type="text/javascript">
10-
wasm_bindgen('./target/simple_bg.wasm').then(async (wasm) => {
11-
await wasm.run();
12-
});
10+
wasm_bindgen('./target/simple_bg.wasm');
1311
</script>
1412
</body>
1513

examples/simple.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@ use std::time::Duration;
22

33
use wasm_thread as thread;
44

5-
#[cfg(target_arch = "wasm32")]
6-
mod wasm {
7-
use wasm_bindgen::prelude::*;
8-
9-
use crate::main;
10-
11-
// Prevent `wasm_bindgen` from autostarting main on all spawned threads
12-
#[wasm_bindgen(start)]
13-
pub fn dummy_main() {}
14-
15-
// Export explicit run function to start main
16-
#[wasm_bindgen]
17-
pub fn run() {
5+
fn main() {
6+
#[cfg(target_arch = "wasm32")]
7+
{
188
console_log::init().unwrap();
199
console_error_panic_hook::set_once();
20-
main();
2110
}
22-
}
2311

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

@@ -82,7 +69,7 @@ fn main() {
8269
}));
8370

8471
// Wait for all threads, otherwise program exits before threads finish execution.
85-
// We can't do blocking join on wasm main thread though.
72+
// We can't do blocking join on wasm main thread though, but the browser window will continue running.
8673
#[cfg(not(target_arch = "wasm32"))]
8774
for handle in threads {
8875
handle.join().unwrap();

src/wasm32/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl Builder {
288288

289289
#[cfg(feature = "es_modules")]
290290
{
291-
load_module_workers_polyfill();
291+
utils::load_module_workers_polyfill();
292292
options.type_(WorkerType::Module);
293293
}
294294
#[cfg(not(feature = "es_modules"))]

src/wasm32/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub fn is_web_worker_thread() -> bool {
2727
}
2828

2929
#[cfg(feature = "es_modules")]
30-
#[wasm_bindgen(module = "/src/js/module_workers_polyfill.min.js")]
30+
#[wasm_bindgen(module = "/src/wasm32/js/module_workers_polyfill.min.js")]
3131
extern "C" {
32-
fn load_module_workers_polyfill();
32+
pub fn load_module_workers_polyfill();
3333
}
3434

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

0 commit comments

Comments
 (0)