From 7a68255eaca3bf8d2ddf7f33ba86e4bd11966dc6 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Mon, 24 Feb 2025 10:33:11 +0100 Subject: [PATCH] Add logging and panic hook --- Cargo.toml | 1 + src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index e2021b7..a6bf808 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 2c0c3b5..36fda2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; } }