Add logging and panic hook

This commit is contained in:
Fabian Schmidt 2025-02-24 10:33:11 +01:00
parent 791780ddbb
commit 7a68255eac
2 changed files with 19 additions and 0 deletions

View File

@ -19,6 +19,7 @@ wasm-bindgen = "0.2.100"
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
js-sys = "0.3.77"
web-sys = { version = "0.3.77", features = ["console"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.50"

View File

@ -4,6 +4,12 @@ use std::fmt;
use wasm_bindgen::prelude::*;
macro_rules! log {
( $( $t:tt )* ) => {
web_sys::console::log_1(&format!( $( $t )* ).into());
};
}
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
@ -55,6 +61,8 @@ impl Universe {
#[wasm_bindgen]
impl Universe {
pub fn tick(&mut self) {
utils::set_panic_hook();
let mut next = self.cells.clone();
for row in 0..self.height {
@ -63,6 +71,14 @@ impl Universe {
let cell = self.cells[idx];
let live_neighbors = self.live_neighbor_count(row, col);
//log!(
// "cell[{}, {}] is initially {:?} and has {} live neighbors",
// row,
// col,
// cell,
// live_neighbors
//);
let next_cell = match (cell, live_neighbors) {
// Rule 1: Any live cell with fewer than two live neighbours
// dies, as if caused by underpopulation.
@ -80,6 +96,8 @@ impl Universe {
(otherwise, _) => otherwise,
};
//log!(" it becomes {:?}", next_cell);
next[idx] = next_cell;
}
}