Add logging and panic hook
This commit is contained in:
parent
791780ddbb
commit
7a68255eac
@ -19,6 +19,7 @@ wasm-bindgen = "0.2.100"
|
|||||||
# code size when deploying.
|
# code size when deploying.
|
||||||
console_error_panic_hook = { version = "0.1.7", optional = true }
|
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||||
js-sys = "0.3.77"
|
js-sys = "0.3.77"
|
||||||
|
web-sys = { version = "0.3.77", features = ["console"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
wasm-bindgen-test = "0.3.50"
|
wasm-bindgen-test = "0.3.50"
|
||||||
|
18
src/lib.rs
18
src/lib.rs
@ -4,6 +4,12 @@ use std::fmt;
|
|||||||
|
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
macro_rules! log {
|
||||||
|
( $( $t:tt )* ) => {
|
||||||
|
web_sys::console::log_1(&format!( $( $t )* ).into());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn alert(s: &str);
|
fn alert(s: &str);
|
||||||
@ -55,6 +61,8 @@ impl Universe {
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Universe {
|
impl Universe {
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
|
utils::set_panic_hook();
|
||||||
|
|
||||||
let mut next = self.cells.clone();
|
let mut next = self.cells.clone();
|
||||||
|
|
||||||
for row in 0..self.height {
|
for row in 0..self.height {
|
||||||
@ -63,6 +71,14 @@ impl Universe {
|
|||||||
let cell = self.cells[idx];
|
let cell = self.cells[idx];
|
||||||
let live_neighbors = self.live_neighbor_count(row, col);
|
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) {
|
let next_cell = match (cell, live_neighbors) {
|
||||||
// Rule 1: Any live cell with fewer than two live neighbours
|
// Rule 1: Any live cell with fewer than two live neighbours
|
||||||
// dies, as if caused by underpopulation.
|
// dies, as if caused by underpopulation.
|
||||||
@ -80,6 +96,8 @@ impl Universe {
|
|||||||
(otherwise, _) => otherwise,
|
(otherwise, _) => otherwise,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//log!(" it becomes {:?}", next_cell);
|
||||||
|
|
||||||
next[idx] = next_cell;
|
next[idx] = next_cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user