Skip to content

Commit 077cd66

Browse files
committed
Fix tests, outline work
1 parent 67e53c6 commit 077cd66

File tree

3 files changed

+159
-119
lines changed

3 files changed

+159
-119
lines changed

readme.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Light & fast WAT compiler.
44
5-
Provides bare minimum WAT to WASM compilation without unnecessary syntax complexities (see [limitations](#limitations)).<br/>
5+
Provides bare minimum WAT to WASM compilation.<br/>
66
Useful as WASM API layer, eg. for hi-level languages or for dynamic (in-browser?) compilation.
77
<!--, eg. [sonl](https://github.com/audio-lab/sonl). -->
88

@@ -41,9 +41,26 @@ const {double} = instance.exports
4141
double(108) // 216
4242
```
4343

44-
## Compiler
44+
## API
4545

46-
WAT tree can be compiled directly, bypassing text parsing:
46+
### Parse
47+
48+
Parser converts input Wasm text string to syntax tree.
49+
50+
```js
51+
import { parse } from 'watr
52+
53+
parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (f64.const 2)))`)
54+
55+
// [
56+
// 'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
57+
// ['f64.mul', ['local.get', 0], ['f64.const', 2]]
58+
// ]
59+
```
60+
61+
### Compile
62+
63+
Compiles Wasm tree or text into wasm binary. Lightweight alternative to [wabt/wat2wasm](https://github.com/WebAssembly/wabt).
4764
4865
```js
4966
import { compile } from 'watr'
@@ -59,7 +76,42 @@ const {double} = instance.exports
5976
double(108) // 216
6077
```
6178
79+
### Print (in progress)
80+
81+
Format input Wasm text string or tree into pretty or minified form (depending on config).
6282
83+
```js
84+
import { print } from 'watr'
85+
86+
const tree = [
87+
'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
88+
['f64.mul', ['local.get', 0], ['f64.const', 2]]
89+
]
90+
91+
// pretty-print
92+
const str = print(tree, {
93+
indent: ' ', // indentation characters
94+
newline: '\n', // new line charactes
95+
pad: 1, // pad start of each line
96+
comments: true // keep comments
97+
})
98+
// (func (export "double")
99+
// (param f64) (result f64)
100+
// (f64.mul
101+
// (local.get 0)
102+
// (f64.const 2)))
103+
104+
// minify
105+
const str = print(tree, {
106+
indent: false,
107+
newline: false,
108+
pad: 0,
109+
comments: false
110+
})
111+
// (func (export "double")(param f64)(result f64)(f64.mul (local.get 0)(f64.const 2)))
112+
```
113+
114+
<!--
63115
## Limitations
64116
65117
Ambiguous syntax is prohibited in favor of explicit lispy notation. Each instruction must have prefix signature with parenthesized immediates and arguments.
@@ -92,11 +144,7 @@ end
92144
```wast
93145
(f32.const 0x1.fffffep+127) ;; ✘ floating HEX - not supported
94146
```
95-
96-
```wast
97-
(global.set $pc (;(i32.const <new-pc>);)) ;; ✘ fallthrough arguments
98-
(global.set $pc (i32.const 0)) ;; ✔ explicit arguments
99-
```
147+
-->
100148
101149
Inline style is supported for compatibility, but discrouraged.
102150
It may also miss some edge cases and nice error messages.
@@ -110,19 +158,6 @@ For better REPL/dev experience use [wabt](https://github.com/AssemblyScript/wabt
110158
* [WASM binary encoding](https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md)
111159
112160
<!--
113-
Main goal is to get very fluent with wasm text.
114-
115-
Experiments:
116-
117-
* [x] global read/write use in function
118-
* [x] scopes: refer, goto
119-
* [x] stack: understanding named and full references
120-
* [x] memory: reading/writing global memory
121-
* [x] memory: creating arrays on the go
122-
* [x] memory: passing pointer to a function
123-
* [x] benchmark array setting agains js loop
124-
→ it's faster almost twice
125-
126161
## Refs
127162
128163
* [mdn wasm text format](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format)
@@ -142,7 +177,7 @@ Experiments:
142177
143178
## Alternatives
144179
145-
* [wabt](https://www.npmjs.com/package/wabt) − port of WABT for the web, industry standard.
180+
* [wabt](https://www.npmjs.com/package/wabt) − port of WABT for the web, de-facto standard.
146181
* [wat-compiler](https://www.npmjs.com/package/wat-compiler) − compact alternative for WABT, older brother of _watr_.
147182
* [web49](https://github.com/FastVM/Web49)
148183

0 commit comments

Comments
 (0)