forked from chemicstry/wasm_thread
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify examples without dummy main
wasm-bindgen no longer autostarts main on worker threads: rustwasm/wasm-bindgen#3236
- Loading branch information
1 parent
1097fe7
commit 3e032fb
Showing
10 changed files
with
48 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters