Skip to content

Commit

Permalink
chore(web): better panic (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz authored Dec 5, 2023
1 parent d9f93d3 commit ef09b63
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38"
wasm-logger = "0.2.0"
wasm-timer = "0.2.5"
console_error_panic_hook = "0.1.7"


# WGPU stuff
Expand Down
1 change: 1 addition & 0 deletions examples/image-classification-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ wasm-bindgen = { workspace = true }
wasm-bindgen-futures = { workspace = true }
wasm-logger = { workspace = true }
wasm-timer = { workspace = true }
console_error_panic_hook = { workspace = true }

[build-dependencies]
# Used to generate code from ONNX model
Expand Down
10 changes: 7 additions & 3 deletions examples/image-classification-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ use serde::Serialize;
use wasm_bindgen::prelude::*;
use wasm_timer::Instant;

#[wasm_bindgen(start)]
pub fn start() {
// Initialize the logger so that the logs are printed to the console
console_error_panic_hook::set_once();
wasm_logger::init(wasm_logger::Config::default());
}

#[allow(clippy::large_enum_variant)]
/// The model is loaded to a specific backend
pub enum ModelType {
Expand Down Expand Up @@ -49,9 +56,6 @@ impl ImageClassifier {
/// Constructor called by JavaScripts with the new keyword.
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
// Initialize the logger so that the logs are printed to the console
wasm_logger::init(wasm_logger::Config::default());

log::info!("Initializing the image classifier");

Self {
Expand Down
3 changes: 2 additions & 1 deletion examples/mnist-inference-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ burn = { path = "../../burn", default-features = false }
serde = { workspace = true }
wasm-bindgen = { workspace = true }
wasm-bindgen-futures = { workspace = true }
js-sys = "0.3.65"
js-sys = { workspace = true }
console_error_panic_hook = { workspace = true }

[dev-dependencies]
pollster = { workspace = true }
6 changes: 6 additions & 0 deletions examples/mnist-inference-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use crate::state::{build_and_load_model, Backend};

use burn::tensor::Tensor;

#[cfg_attr(target_family = "wasm", wasm_bindgen(start))]
pub fn start() {
console_error_panic_hook::set_once();
}

/// Mnist structure that corresponds to JavaScript class.
/// See:[exporting-rust-struct](https://rustwasm.github.io/wasm-bindgen/contributing/design/exporting-rust-struct.html)
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
Expand All @@ -23,6 +28,7 @@ impl Mnist {
/// Constructor called by JavaScripts with the new keyword.
#[cfg_attr(target_family = "wasm", wasm_bindgen(constructor))]
pub fn new() -> Self {
console_error_panic_hook::set_once();
Self { model: None }
}

Expand Down

0 comments on commit ef09b63

Please sign in to comment.