#wasm-text #wat #wasm-binary #expression-parser #text-file #text-format #s-expr

app silly-wat-linker

A tool that makes writing WebAssembly Text files easier

10 releases (6 breaking)

0.7.1 Feb 25, 2023
0.6.1 Feb 15, 2023
0.6.0 Sep 10, 2022
0.3.0 Jul 26, 2022

#1836 in Parser implementations

Apache-2.0

95KB
3K SLoC

Silly WAT Linker

SWL is a tool that makes writing WebAssembly Text files easier.

It is future-proof and simple because it doesn’t actually understand WAT. Instead, it is a simplistic S-Expression parser and uses surface-level pattern matching to implement its features. This way, future iterations of WAT with new syntax are unlikely to break this tool.

WAT is the WebAssembly Text format and is defined in the WebAssembly spec.

Usage

SWL can be installed from Cargo:

$ cargo install silly-wat-linker

All work is done on the textual representation. SWL can invoke wabt’s wat2wasm for you to produce a binary file instead:

$ silly-wat-linker ./main.wat      # Emits .wat test file
$ silly-wat-linker -c ./main.wat      # Emits .wasm binary

Features

SWL has a handful of features to make your life easier when hand-writing WAT files. Most features are enabled by default, but you can explicitly select which features to enable using the --features flag.

# This will only run the `size_adjust` and `sort` feature.
$ silly-wat-linker --features size_adjust,sort ./my-file.wat

File Importer (import)

Adds support for importing another .wat file into the current one.

(module
	(import "other_file.wat" (file))
	;; ...
)

Data Importer (data_import)

Allows you to import other files as data segments.

(module
	(data (i32.const 1024) (import "other_file.wat" (raw))
	;; ...
)

Const Expression (constexpr)

Adds const expressions to WAT, allowing evaluation of complex expressions at compile time. Globals that aren’t themselves using const expressions are available.

(module
	(global $DATA_PTR i32 (i32.const 0x4000))
	(data
		(i32.constexpr
			(i32.add
				(global.get $DATA_PTR)
				(i32.const 0x100)))
		"My data")
	;; ...
)

Also supported specifically for the offset attribute for store and load operations:

(module
	(func
		(i32.store
			offset=(i32.constexp
				(i32.add
					(i32.const 0x123)
					(i32.const 0x100)))
			(i32.const 4)))
	;; ...
)

Size Adjuster (size_adjust)

Automatically adjust the size of memory directives to be big enough to hold all active data segments. (This feature is also supposed to do the same for tables and elem segments, but this hasn’t been implemented yet.)

Start Merger (start_merge)

If there are multiple (start) directives (which can easily happen in a multi-file project), SWL will create a new, singular start function that calls all the other start functions.

Sorter (sort)

Sorts all top-level module segments so that imports come first. This feature mostly exists because wat2wasm requires imports to come first.


License Apache-2.0

Dependencies

~6.5MB
~114K SLoC