watr 0.0.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/amp.wasm +0 -0
- package/example/amp.wat +81 -0
- package/example/array.wat +26 -0
- package/example/global.wasm +0 -0
- package/example/global.wat +28 -0
- package/example/loops.wasm +0 -0
- package/example/loops.wat +34 -0
- package/example/memory.wasm +0 -0
- package/example/memory.wat +34 -0
- package/example/multivar.wasm +0 -0
- package/example/multivar.wat +13 -0
- package/example/stack.wasm +0 -0
- package/example/stack.wat +38 -0
- package/example/table.wasm +0 -0
- package/example/table.wat +24 -0
- package/example/types.wasm +0 -0
- package/example/types.wat +21 -0
- package/lib/wabt.js +36 -0
- package/lib/wat-compiler.js +1 -0
- package/package.json +14 -4
- package/plan.md +21 -0
- package/readme.md +145 -0
- package/repl.html +44 -0
- package/src/compile.js +456 -0
- package/src/parse.js +31 -0
- package/src/watr.js +8 -0
- package/test/compile.js +1090 -0
- package/test/examples.js +155 -0
- package/test/index.html +14 -0
- package/test/index.js +2 -0
- package/test/parse.js +179 -0
- package/watr.js +428 -0
- package/watr.min.js +1 -0
package/example/amp.wasm
ADDED
|
Binary file
|
package/example/amp.wat
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
;; test passing array to and back
|
|
2
|
+
(module
|
|
3
|
+
(import "js" "mem" (memory 1))
|
|
4
|
+
(import "console" "log" (func $log (param i32 f32)))
|
|
5
|
+
|
|
6
|
+
(global $blockSize (import "js" "blockSize") (mut i32))
|
|
7
|
+
(global $len (mut i32) (i32.const 0))
|
|
8
|
+
|
|
9
|
+
;; len = blockSize * 4
|
|
10
|
+
(func (global.set $len (i32.mul (global.get $blockSize) (i32.const 4))))
|
|
11
|
+
(start 1)
|
|
12
|
+
|
|
13
|
+
;; gain processing function
|
|
14
|
+
(func $amp (export "amp") (param $amp f32)
|
|
15
|
+
(local $i i32)
|
|
16
|
+
(local $x f32)
|
|
17
|
+
|
|
18
|
+
(loop $gain
|
|
19
|
+
;; x = input[i]
|
|
20
|
+
(local.set $x (f32.load (local.get $i)))
|
|
21
|
+
|
|
22
|
+
;; console.log(i, x)
|
|
23
|
+
;; (call $log (local.get $i) (local.get $x))
|
|
24
|
+
|
|
25
|
+
;; x = x * amp
|
|
26
|
+
(local.set $x (f32.mul (local.get $x) (local.get $amp)))
|
|
27
|
+
|
|
28
|
+
;; input[i] = x * amp
|
|
29
|
+
(f32.store (local.get $i) (local.get $x))
|
|
30
|
+
|
|
31
|
+
;; i++
|
|
32
|
+
(local.set $i (i32.add (local.get $i) (i32.const 4)))
|
|
33
|
+
|
|
34
|
+
;; if (i < len) repeat
|
|
35
|
+
(br_if $gain (i32.lt_s (local.get $i) (global.get $len)))
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
;; multivar amp
|
|
40
|
+
(func $ampMulti (export "ampMulti") (param $amp f32)
|
|
41
|
+
(local i32 f32)
|
|
42
|
+
;; note: indexing here is 1, 2, since 0 is taken by $amp
|
|
43
|
+
|
|
44
|
+
(loop $gain
|
|
45
|
+
;; x = input[i]
|
|
46
|
+
(local.set 2 (f32.load (local.get 1)))
|
|
47
|
+
|
|
48
|
+
;; console.log(i, x)
|
|
49
|
+
;; (call $log (local.get 1) (local.get 2))
|
|
50
|
+
|
|
51
|
+
;; x = x * amp
|
|
52
|
+
(local.set 2 (f32.mul (local.get $amp) (local.get 2)))
|
|
53
|
+
|
|
54
|
+
;; ;; input[i] = x * amp
|
|
55
|
+
(f32.store (local.get 1) (local.get 2))
|
|
56
|
+
|
|
57
|
+
;; ;; i++
|
|
58
|
+
(local.set 1 (i32.add (local.get 1) (i32.const 4)))
|
|
59
|
+
|
|
60
|
+
;; if (i < len) repeat
|
|
61
|
+
(br_if $gain (i32.lt_s (local.get 1) (global.get $len)))
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
;; optimized amp
|
|
66
|
+
(func $ampShort (export "ampShort") (param $amp f32)
|
|
67
|
+
(local i32 f32)
|
|
68
|
+
;; note: indexing here is 1, 0
|
|
69
|
+
|
|
70
|
+
(loop $gain
|
|
71
|
+
;; console.log(i, x)
|
|
72
|
+
;; (call $log (local.get 1) (local.get 0))
|
|
73
|
+
|
|
74
|
+
;; input[i] = input[i] * amp
|
|
75
|
+
(f32.store (local.get 1) (f32.mul (f32.load (local.get 1)) (local.get $amp)))
|
|
76
|
+
|
|
77
|
+
;; if (i++ < len) repeat
|
|
78
|
+
(br_if $gain (i32.lt_s (local.tee 1 (i32.add (local.get 1) (i32.const 4))) (global.get $len)))
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
;; test passing array to and back
|
|
2
|
+
(module
|
|
3
|
+
(import "js" "mem" (memory 1))
|
|
4
|
+
(import "console" "log" (func $log (param i32 f32)))
|
|
5
|
+
|
|
6
|
+
;; gain processing function
|
|
7
|
+
(func $arr (export "arr")
|
|
8
|
+
(local.array i32[] (i32.const 0))
|
|
9
|
+
;; doesn't work, although that's what walt generates
|
|
10
|
+
;; (local.set arr
|
|
11
|
+
;; (subscript
|
|
12
|
+
;; (local.set arr)
|
|
13
|
+
;; (i32.const 0)
|
|
14
|
+
;; )
|
|
15
|
+
;; (i32.const 20)
|
|
16
|
+
;; )
|
|
17
|
+
;; (local.set arr
|
|
18
|
+
;; (subscript
|
|
19
|
+
;; (local.set arr)
|
|
20
|
+
;; (i32.const 1)
|
|
21
|
+
;; )
|
|
22
|
+
;; (i32.const 15)
|
|
23
|
+
;; )
|
|
24
|
+
;; (return arr)
|
|
25
|
+
)
|
|
26
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
(module
|
|
2
|
+
(import "js" "log" (func $log (param i32)))
|
|
3
|
+
|
|
4
|
+
;; (import "js" "g1" (global $g1 (mut i32))) ;; identical to line below
|
|
5
|
+
(global $g1 (import "js" "g1") (mut i32)) ;; import from js
|
|
6
|
+
;; (global $g1 (import "js" "g1") i32) ;; immutable import
|
|
7
|
+
|
|
8
|
+
;; (global $g0 i32 (i32.const 1)) ;; local global immutable (not imported)
|
|
9
|
+
(global $g0 (mut i32) (i32.const 1)) ;; local global mutable, initialized
|
|
10
|
+
|
|
11
|
+
;; ? is that mandatory to initialize that?
|
|
12
|
+
;; yep, initializer is needed, same time we cannot push to stack in global scope, so we use lispy style
|
|
13
|
+
|
|
14
|
+
;; ? is there a short way to initialize?
|
|
15
|
+
;; − this is short way, reminds lispy syntax (operator a b)
|
|
16
|
+
|
|
17
|
+
(func (export "getG0") (result i32)
|
|
18
|
+
global.get $g0
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
(func $getG1 (export "getG1") (result i32)
|
|
22
|
+
global.get $g1
|
|
23
|
+
)
|
|
24
|
+
(func $setG1 (export "setG1") (param i32)
|
|
25
|
+
local.get 0
|
|
26
|
+
global.set $g1
|
|
27
|
+
)
|
|
28
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
(module
|
|
2
|
+
;; import the browser console object, you'll need to pass this in from JavaScript
|
|
3
|
+
(import "console" "log" (func $log (param i32)))
|
|
4
|
+
|
|
5
|
+
;; the difference between block and loop is: you cannot goto beginning of block again
|
|
6
|
+
;; block defines scope for variables, loop adds goto label
|
|
7
|
+
|
|
8
|
+
(func
|
|
9
|
+
;; (loop xxx)
|
|
10
|
+
;; identical to
|
|
11
|
+
;; loop ... end
|
|
12
|
+
|
|
13
|
+
;; create a global variable and initialize it to 0
|
|
14
|
+
(local $i i32)
|
|
15
|
+
|
|
16
|
+
(loop $my_loop
|
|
17
|
+
|
|
18
|
+
;; add one to $i
|
|
19
|
+
local.get $i
|
|
20
|
+
i32.const 1
|
|
21
|
+
i32.add
|
|
22
|
+
local.set $i
|
|
23
|
+
|
|
24
|
+
;; log the current value of $i
|
|
25
|
+
local.get $i
|
|
26
|
+
call $log
|
|
27
|
+
|
|
28
|
+
;; if $i is less than 10 branch to loop
|
|
29
|
+
(br_if $my_loop (i32.lt_s (local.get $i) (i32.const 10)))
|
|
30
|
+
)
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
(start 1) ;; run the first function automatically
|
|
34
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
(module
|
|
2
|
+
(import "js" "log" (func $log (param i32)))
|
|
3
|
+
|
|
4
|
+
;; reexport memory
|
|
5
|
+
;; note: it requires import.
|
|
6
|
+
(import "js" "mem" (memory $mem 2))
|
|
7
|
+
(export "mem" (memory $mem))
|
|
8
|
+
|
|
9
|
+
;; (memory $mem (import "js" "mem") (export "mem") 1) ;; create memory with 1 page - per module
|
|
10
|
+
|
|
11
|
+
;; (import "js" "g" (global $g f32))
|
|
12
|
+
;; (export "g" (global $g))
|
|
13
|
+
|
|
14
|
+
;; (global (export "g") (mut f32) (f32.const 1.5))
|
|
15
|
+
;; (memory $xxx 1) ;; create memory with 1 page - per module
|
|
16
|
+
|
|
17
|
+
(func $populate (export "populate")
|
|
18
|
+
i32.const 0
|
|
19
|
+
i32.const 123
|
|
20
|
+
i32.store
|
|
21
|
+
|
|
22
|
+
i32.const 10
|
|
23
|
+
i32.const 1230
|
|
24
|
+
i32.store
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
(func $get (export "get") (param i32) (result i32)
|
|
28
|
+
(i32.load (local.get 0))
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
(func $set (export "set") (param i32) (param i32)
|
|
32
|
+
(i32.store (local.get 0) (local.get 1))
|
|
33
|
+
)
|
|
34
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
;; test passing array to and back
|
|
2
|
+
(module
|
|
3
|
+
(import "js" "mem" (memory 1))
|
|
4
|
+
(import "console" "log" (func $log (param i32 f32)))
|
|
5
|
+
|
|
6
|
+
(global $blockSize (import "js" "blockSize") (mut i32))
|
|
7
|
+
(global $len (mut i32) (i32.const 0))
|
|
8
|
+
;; len = blockSize * 4
|
|
9
|
+
(func (global.set $len (i32.mul (global.get $blockSize) (i32.const 4))))
|
|
10
|
+
(start 1)
|
|
11
|
+
|
|
12
|
+
;; NOTE: see array.wat
|
|
13
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
;; test multiple values & stack
|
|
2
|
+
(module
|
|
3
|
+
|
|
4
|
+
(global $g0 i32 (i32.const 12))
|
|
5
|
+
|
|
6
|
+
(func i32.const 0 return) ;; TF?
|
|
7
|
+
|
|
8
|
+
(func $get (export "get") (result i32 i32)
|
|
9
|
+
;; note: you cannot push more items to stack than needed for the next op
|
|
10
|
+
;; global.get $g0
|
|
11
|
+
|
|
12
|
+
;; (return (i32.add (global.get $g0) (global.get $g0)) (global.get $g0))
|
|
13
|
+
;; in case of returning multiple values js receives an array
|
|
14
|
+
|
|
15
|
+
;; equivalend notation is this
|
|
16
|
+
(global.get $g0) (global.get $g0) (i32.add)
|
|
17
|
+
(global.get $g0)
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
(func $mul (export "mul") (param i32 i32) (result i32) (i32.mul (local.get 1) (local.get 0)))
|
|
22
|
+
|
|
23
|
+
(func $swap (export "swap") (param i32 i32 i32) (param i32) (result i32 i32)
|
|
24
|
+
(local.get 3) (local.get 0)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
;; (func $fac (param i64) (result i64)
|
|
28
|
+
;; (i64.const 1) (local.get 0)
|
|
29
|
+
;; (loop $l (param i64 i64) (result i64)
|
|
30
|
+
;; (pick 1) (pick 1) (i64.mul)
|
|
31
|
+
;; (pick 1) (i64.const 1) (i64.sub)
|
|
32
|
+
;; (pick 0) (i64.const 0) (i64.gt_u)
|
|
33
|
+
;; (br_if $l)
|
|
34
|
+
;; (pick 1) (return)
|
|
35
|
+
;; )
|
|
36
|
+
;; )
|
|
37
|
+
)
|
|
38
|
+
|
|
Binary file
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
(module
|
|
2
|
+
(table $x 2 funcref)
|
|
3
|
+
(table $y 2 funcref)
|
|
4
|
+
(elem (table $y) (i32.const 0) $f1 $f2)
|
|
5
|
+
|
|
6
|
+
;; w is this?
|
|
7
|
+
;; (elem funcref (ref.func 1) (ref.null func))
|
|
8
|
+
;; (elem declare func 0)
|
|
9
|
+
|
|
10
|
+
(func $f1 (result i32) i32.const 42)
|
|
11
|
+
(func $f2 (result i32) i32.const 13)
|
|
12
|
+
|
|
13
|
+
(type $return_i32 (func (result i32)))
|
|
14
|
+
(func (export "callByIndex") (param $i i32) (result i32)
|
|
15
|
+
local.get $i
|
|
16
|
+
call_indirect $y (type $return_i32))
|
|
17
|
+
|
|
18
|
+
(func (export "getByIndex") (param $i i32) (result funcref)
|
|
19
|
+
;; (return (ref.func $f2)) ;; return direct function
|
|
20
|
+
|
|
21
|
+
;; return from table
|
|
22
|
+
(table.get $y (local.get $i))
|
|
23
|
+
)
|
|
24
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
;; test passing array to and back
|
|
2
|
+
(module
|
|
3
|
+
(import "console" "log" (func $log (param i32 f32 externref)))
|
|
4
|
+
|
|
5
|
+
;; NOTE: JS doesn't allow functions with v128 arguments
|
|
6
|
+
;; (func $run (export "run") (param i32 f32) (param v128) (result i32 f32 v128)
|
|
7
|
+
;; (call $log (local.get 0) (local.get 1) (local.get 2))
|
|
8
|
+
;; (return (local.get 0) (local.get 1) (local.get 2))
|
|
9
|
+
;; )
|
|
10
|
+
|
|
11
|
+
(func $run (export "run") (param i32 f32) (param externref) (result i32 f32 externref)
|
|
12
|
+
(call $log (local.get 0) (local.get 1) (local.get 2))
|
|
13
|
+
(ref.null extern)
|
|
14
|
+
(return (local.get 0) (local.get 1) (local.get 2))
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
(elem func 0)
|
|
18
|
+
(func $f (export "f") (result funcref)
|
|
19
|
+
(return (ref.func 0))
|
|
20
|
+
)
|
|
21
|
+
)
|