From f0ab842d6e42ae07eb815b601d148cc877f9a3e9 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Mon, 13 Apr 2020 21:38:21 -0230 Subject: [PATCH] Combine `wasm_main`/`main` and add `console_log` --- examples/capture/main.rs | 25 +++++++++++-------------- examples/describe/main.rs | 7 +------ examples/framework.rs | 1 + examples/hello-compute/main.rs | 25 +++++++++++-------------- examples/hello-triangle/main.rs | 7 +------ examples/msaa-line/main.rs | 6 ------ 6 files changed, 25 insertions(+), 46 deletions(-) diff --git a/examples/capture/main.rs b/examples/capture/main.rs index 15296b6018..5f3cc8429d 100644 --- a/examples/capture/main.rs +++ b/examples/capture/main.rs @@ -114,19 +114,16 @@ async fn run() { } } -#[cfg(target_arch = "wasm32")] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] -pub fn wasm_main() { - console_log::init().expect("could not initialize log"); - std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - wasm_bindgen_futures::spawn_local(run()); -} - -#[cfg(target_arch = "wasm32")] -fn main() {} - -#[cfg(not(target_arch = "wasm32"))] fn main() { - env_logger::init(); - futures::executor::block_on(run()); + #[cfg(not(target_arch = "wasm32"))] + { + env_logger::init(); + futures::executor::block_on(run()); + } + #[cfg(target_arch = "wasm32")] + { + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + console_log::init().expect("could not initialize logger"); + wasm_bindgen_futures::spawn_local(run()); + } } diff --git a/examples/describe/main.rs b/examples/describe/main.rs index c2b573960c..2b5d759397 100644 --- a/examples/describe/main.rs +++ b/examples/describe/main.rs @@ -23,12 +23,7 @@ fn main() { #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + console_log::init().expect("could not initialize logger"); wasm_bindgen_futures::spawn_local(run()); } } - -#[cfg(target_arch = "wasm32")] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] -pub fn wasm_main() { - main(); -} diff --git a/examples/framework.rs b/examples/framework.rs index 4445318068..96e96e1ebc 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -159,6 +159,7 @@ pub fn run(title: &str) { #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + console_log::init().expect("could not initialize logger"); use winit::platform::web::WindowExtWebSys; // On wasm, append the canvas to the document body web_sys::window() diff --git a/examples/hello-compute/main.rs b/examples/hello-compute/main.rs index 3851ecf4b3..2afafe2ece 100644 --- a/examples/hello-compute/main.rs +++ b/examples/hello-compute/main.rs @@ -124,21 +124,18 @@ async fn execute_gpu(numbers: Vec) -> Vec { } } -#[cfg(target_arch = "wasm32")] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] -pub fn wasm_main() { - console_log::init().expect("could not initialize log"); - std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - wasm_bindgen_futures::spawn_local(run()); -} - -#[cfg(target_arch = "wasm32")] -fn main() {} - -#[cfg(not(target_arch = "wasm32"))] fn main() { - env_logger::init(); - futures::executor::block_on(run()); + #[cfg(not(target_arch = "wasm32"))] + { + env_logger::init(); + futures::executor::block_on(run()); + } + #[cfg(target_arch = "wasm32")] + { + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + console_log::init().expect("could not initialize logger"); + wasm_bindgen_futures::spawn_local(run()); + } } #[cfg(test)] diff --git a/examples/hello-triangle/main.rs b/examples/hello-triangle/main.rs index 5ad4d016f1..ad7a996d4c 100644 --- a/examples/hello-triangle/main.rs +++ b/examples/hello-triangle/main.rs @@ -137,12 +137,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) { }); } -#[cfg(target_arch = "wasm32")] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] -pub fn wasm_main() { - main(); -} - fn main() { let event_loop = EventLoop::new(); let window = winit::window::Window::new(&event_loop).unwrap(); @@ -154,6 +148,7 @@ fn main() { #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + console_log::init().expect("could not initialize logger"); use winit::platform::web::WindowExtWebSys; // On wasm, append the canvas to the document body web_sys::window() diff --git a/examples/msaa-line/main.rs b/examples/msaa-line/main.rs index ad63c9076c..25368b9b9b 100644 --- a/examples/msaa-line/main.rs +++ b/examples/msaa-line/main.rs @@ -268,12 +268,6 @@ impl framework::Example for Example { } } -#[cfg(target_arch = "wasm32")] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] -pub fn wasm_main() { - main(); -} - fn main() { framework::run::("msaa-line"); }