Skip to content

Commit 46041b9

Browse files
committed
fix: use console_error_panic_hook correctly
1 parent 89abf22 commit 46041b9

5 files changed

Lines changed: 12 additions & 17 deletions

File tree

engine/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ crate-type = ["cdylib", "rlib"]
1313

1414
[features]
1515
default = []
16+
debug-panic-hook = ["dep:console_error_panic_hook"]
1617

1718
[dependencies]
1819
wasm-bindgen = "0.2.108"
1920
js-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.
2521
console_error_panic_hook = { version = "0.1.7", optional = true }
2622

2723
[dev-dependencies]

engine/src/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff 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
```

engine/src/board.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;
99
use wasm_bindgen::JsValue;
1010

1111
use 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)]
2223
pub 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,

engine/src/game.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::board::Player;
55
use crate::board::*;
66
use crate::console_log;
77
use crate::strategy::*;
8+
use crate::utils;
89

910
#[wasm_bindgen]
1011
pub struct Game {
@@ -37,6 +38,7 @@ impl Game {
3738
#[wasm_bindgen]
3839
impl 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 {}),

engine/src/utils.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ macro_rules! console_log {
1010
}
1111
}
1212

13-
#[allow(dead_code)]
1413
pub 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
}

0 commit comments

Comments
 (0)