Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #255 - Attempt to make explanation in debugging section better #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions src/game-of-life/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ features = [
```

For ergonomics, we'll wrap the `console.log` function up in a `println!`-style
macro:
macro, we could add this to `wasm-game-of-life/src/lib.rs` file:

[logging]: ../reference/debugging.html#logging-with-the-console-apis

Expand All @@ -68,35 +68,38 @@ Now, we can start logging messages to the console by inserting calls to `log` in
Rust code. For example, to log each cell's state, live neighbors count, and next
state, we could modify `wasm-game-of-life/src/lib.rs` like this:

```diff
diff --git a/src/lib.rs b/src/lib.rs
index f757641..a30e107 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -123,6 +122,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.
@@ -140,6 +147,8 @@ impl Universe {
(otherwise, _) => otherwise,
};

+ log!(" it becomes {:?}", next_cell);
+
next[idx] = next_cell;
}
}
```rust
#[wasm_bindgen]
impl Universe {
// ...
pub fn tick(&mut self) {
let mut next = self.cells.clone();

for row in 0..self.height {
for col in 0..self.width {
let idx = self.get_index(row, col);
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
);

// ...

log!(" it becomes {:?}", next_cell);

next[idx] = next_cell;
}
}
// ...
}
// ...
}
```

## Using a Debugger to Pause Between Each Tick
Expand Down