File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,15 +13,11 @@ crate-type = ["cdylib", "rlib"]
1313
1414[features ]
1515default = []
16+ debug-panic-hook = [" dep:console_error_panic_hook" ]
1617
1718[dependencies ]
1819wasm-bindgen = " 0.2.108"
1920js-sys = " 0.3.85"
20-
21- # The `console_error_panic_hook` crate provides better debugging of panics by
22- # logging them with `console.error`. This is great for development, but requires
23- # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
24- # code size when deploying.
2521console_error_panic_hook = { version = " 0.1.7" , optional = true }
2622
2723[dev-dependencies ]
Original file line number Diff line number Diff line change @@ -7,9 +7,11 @@ echo -e "];\n}" >> parameters.rs
77```
88
99## Debugging
10- ``` rs
11- extern crate console_error_panic_hook;
12- use std :: panic;
10+ panic の詳細を DevTools に出したい場合は feature を有効化します。
1311
14- panic :: set_hook (Box :: new (console_error_panic_hook :: hook ));
12+ - feature: ` debug-panic-hook `
13+ - hook 初期化: ` newBoard ` / ` Game::create ` 呼び出し時に ` set_once() ` を実行
14+
15+ ``` sh
16+ wasm-pack build --dev -- --features debug-panic-hook
1517```
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;
99use wasm_bindgen:: JsValue ;
1010
1111use crate :: parameters:: parameters:: PATTERN_INSTANCES ;
12+ use crate :: utils;
1213
1314#[ wasm_bindgen]
1415#[ derive( Clone , Debug , PartialEq ) ]
@@ -20,6 +21,7 @@ pub struct Board {
2021#[ allow( dead_code) ]
2122#[ wasm_bindgen( js_name = newBoard) ]
2223pub fn new_board ( ) -> Board {
24+ utils:: set_panic_hook ( ) ;
2325 Board {
2426 first : 0b_00000000_00000000_00000000_00001000_00010000_00000000_00000000_00000000 ,
2527 second : 0b_00000000_00000000_00000000_00010000_00001000_00000000_00000000_00000000 ,
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ use crate::board::Player;
55use crate :: board:: * ;
66use crate :: console_log;
77use crate :: strategy:: * ;
8+ use crate :: utils;
89
910#[ wasm_bindgen]
1011pub struct Game {
@@ -37,6 +38,7 @@ impl Game {
3738#[ wasm_bindgen]
3839impl Game {
3940 pub fn create ( player_human : Player , opponent_strategy_type : StrategyType ) -> Game {
41+ utils:: set_panic_hook ( ) ;
4042 let current_board = new_board ( ) ;
4143 let opponent_strategy: Box < dyn Strategy > = match opponent_strategy_type {
4244 StrategyType :: NumdiskLookahead => Box :: new ( NumdiskLookaheadStrategy { } ) ,
Original file line number Diff line number Diff line change @@ -10,14 +10,7 @@ macro_rules! console_log {
1010 }
1111}
1212
13- #[ allow( dead_code) ]
1413pub fn set_panic_hook ( ) {
15- // When the `console_error_panic_hook` feature is enabled, we can call the
16- // `set_panic_hook` function at least once during initialization, and then
17- // we will get better error messages if our code ever panics.
18- //
19- // For more details see
20- // https://github.com/rustwasm/console_error_panic_hook#readme
21- #[ cfg( feature = "console_error_panic_hook" ) ]
14+ #[ cfg( all( target_arch = "wasm32" , feature = "debug-panic-hook" ) ) ]
2215 console_error_panic_hook:: set_once ( ) ;
2316}
You can’t perform that action at this time.
0 commit comments