Skip to content

Commit

Permalink
[ch10] Stack Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
zaki-yama committed Jul 28, 2021
1 parent 87e3c04 commit 5e983a6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions the-art-of-webassembly/ch10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@

- WAT の要所要所で console.log する
- 識別しやすいように message id という名の index をログ出力する箇所に連番ふっていく、というかなり泥臭いデバッグ手法

## Using Alerts

- 前節のロギング用関数 (`log_f64`) の console.log を alert に変えるだけ

## Stack Trace

Chrome (実際には Edge) の Dev Tools、Wasm の stack trace は本に書かれてる当時よりは読みやすくなってるっぽい。

![]("./stack-trace.png")
Binary file added the-art-of-webassembly/ch10/stack-trace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions the-art-of-webassembly/ch10/stack_trace.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Document</title>
</head>
<body>
<h1>Stack Trace</h1>
<script>
const importObject = {
js: {
log_stack_trace: function (level) {
console.trace(`level=${level}`);
},
},
};

(async () => {
const obj = await WebAssembly.instantiateStreaming(
fetch("stack_trace.wasm"),
importObject
);
obj.instance.exports.call_stack_trace();
})();
</script>
</body>
</html>
Binary file added the-art-of-webassembly/ch10/stack_trace.wasm
Binary file not shown.
25 changes: 25 additions & 0 deletions the-art-of-webassembly/ch10/stack_trace.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(module
(import "js" "log_stack_trace" (func $log_stack_trace (param i32)))

(func $call_level_1 (param $level i32)
local.get $level
call $log_stack_trace
)

(func $call_level_2 (param $level i32)
local.get $level
call $call_level_1
)

(func $call_level_3 (param $level i32)
local.get $level
call $call_level_2
)

(func $call_stack_trace (export "call_stack_trace")
(call $log_stack_trace (i32.const 0))
(call $call_level_1 (i32.const 1))
(call $call_level_2 (i32.const 2))
(call $call_level_3 (i32.const 3))
)
)

0 comments on commit 5e983a6

Please sign in to comment.