Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
zaki-yama committed Jun 21, 2021
1 parent d00353d commit e35d662
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions the-art-of-webassembly/ch03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@

- "When you call a JavaScript function
in WAT, you lose some cycles to overhead. This number isn’t extremely large, but if you execute an external JavaScript function in a loop that iterates 4,000,000 times, it can add up."

### Function Tables

- "Currently, tables only support the anyfunc type (anyfunc is a generic WebAssembly function type), but in the future they might support JavaScript objects and DOM elements as well."
- "Unlike import objects, JavaScript and WebAssembly can dynamically change tables at runtime."
- function table 経由での関数呼び出しは indirect なコールになるためパフォーマンスコストがある
25 changes: 25 additions & 0 deletions the-art-of-webassembly/ch03/table_export.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(module
;; javascript increment function
(import "js" "increment" (func $js_increment (result i32)))
;; javascript decrement function
(import "js" "decrement" (func $js_decrement (result i32)))

(table $tbl (export "tbl") 4 anyfunc) ;; exported table with 4 functions

(global $i (mut i32) (i32.const 0))

(func $increment (export "increment") (result i32)
(global.set $i (i32.add (global.get $i) (i32.const 1))) ;; $i++
global.get $i
)

(func $decrement (export "decrement") (result i32)
(global.set $i (i32.sub (global.get $i) (i32.const 1))) ;; $i--
global.get $i
)

;; populate the table
;; the first parameter is the starting index we want to update.
(elem (i32.const 0) $js_increment $js_decrement $increment $decrement)
)

14 changes: 14 additions & 0 deletions the-art-of-webassembly/ch03/table_test.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(module
(import "js" "tbl" (table $tbl 4 anyfunc))
;; import increment function
(import "js" "increment" (func $increment (result i32)))
;; import decrement function
(import "js" "decrement" (func $decrement (result i32)))

;; import wasm_increment function
(import "js" "wasm_increment" (func $wasm_increment (result i32)))
;; import wasm_decrement function
(import "js" "wasm_decrement" (func $wasm_decrement (result i32)))

;; table function type definitions all i32 and take no parameters
(type $returns_i32 (func (result i32)))

0 comments on commit e35d662

Please sign in to comment.