Documentation
¶
Overview ¶
Package watgo provides the public high-level Go API for parsing, validating, decoding, and encoding WebAssembly modules.
The package is centered on semantic IR represented by wasmir.Module. Text-format WAT and binary WASM both normalize into that IR:
- ParseWAT parses and lowers WAT into semantic IR.
- DecodeWASM decodes binary WASM into semantic IR.
- ValidateModule validates semantic IR.
- EncodeWASM encodes semantic IR back to binary WASM.
- CompileWATToWASM is the end-to-end convenience helper for WAT-to-WASM.
A typical pipeline is:
m, err := watgo.ParseWAT(src)
if err != nil {
// handle parse/lower diagnostics
}
if err := watgo.ValidateModule(m); err != nil {
// handle validation diagnostics
}
wasm, err := watgo.EncodeWASM(m)
if err != nil {
// handle encoding diagnostics
}
See the package examples for complete runnable usage snippets.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompileWATToWASM ¶
CompileWATToWASM parses, lowers, validates, and encodes text-format WebAssembly to binary WebAssembly.
This is the public end-to-end convenience API when the caller wants binary output directly from WAT input.
Example ¶
package main
import (
"fmt"
"github.com/eliben/watgo"
)
func main() {
wasm, err := watgo.CompileWATToWASM([]byte(`(module
(func (export "answer") (result i32)
i32.const 42
)
)`))
if err != nil {
panic(err)
}
fmt.Println(len(wasm) > 0)
}
Output: true
func DecodeWASM ¶
DecodeWASM decodes binary WebAssembly into semantic IR.
DecodeWASM performs binary decoding into wasmir.Module, but does not run semantic validation. Call ValidateModule on the returned module before using it when you need a validated module.
On failure, DecodeWASM returns a non-nil error. Decode errors are typically returned as diag.ErrorList values.
Example ¶
package main
import (
"fmt"
"github.com/eliben/watgo"
)
func canonicalAddModuleBytes() []byte {
return []byte{
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f,
0x03, 0x02, 0x01, 0x00,
0x07, 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00,
0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b,
}
}
func main() {
m, err := watgo.DecodeWASM(canonicalAddModuleBytes())
if err != nil {
panic(err)
}
fmt.Println(len(m.Types), len(m.Funcs))
}
Output: 1 1
func EncodeWASM ¶
EncodeWASM encodes semantic IR as binary WebAssembly.
EncodeWASM does not implicitly validate m. Call ValidateModule first when you need validation before encoding.
On failure, EncodeWASM returns a non-nil error. Encoder failures are typically returned as diag.ErrorList values.
Example ¶
package main
import (
"fmt"
"github.com/eliben/watgo"
)
func main() {
m, err := watgo.ParseWAT([]byte(`(module
(func (export "answer") (result i32)
i32.const 42
)
)`))
if err != nil {
panic(err)
}
if err := watgo.ValidateModule(m); err != nil {
panic(err)
}
wasm, err := watgo.EncodeWASM(m)
if err != nil {
panic(err)
}
fmt.Println(len(wasm) > 0)
}
Output: true
func ParseWAT ¶
ParseWAT parses and lowers text-format WebAssembly into semantic IR.
ParseWAT performs text parsing plus lowering into wasmir.Module, but does not run semantic validation. Call ValidateModule on the returned module before encoding or executing it when you need a validated module.
On failure, ParseWAT returns a non-nil error. Most parser and lowering failures are returned as diag.ErrorList values.
Example ¶
package main
import (
"fmt"
"github.com/eliben/watgo"
)
func main() {
m, err := watgo.ParseWAT([]byte(`(module
(func (export "answer") (result i32)
i32.const 42
)
)`))
if err != nil {
panic(err)
}
fmt.Println(len(m.Funcs), len(m.Exports))
}
Output: 1 1
func ValidateModule ¶
ValidateModule validates a semantic WebAssembly module.
This is the public validation entry point over wasmir.Module. It reports semantic issues such as type mismatches, invalid indices, and malformed instruction sequences. Validation errors are typically returned as diag.ErrorList values.
Example ¶
package main
import (
"fmt"
"github.com/eliben/watgo"
)
func main() {
m, err := watgo.ParseWAT([]byte(`(module
(func (export "answer") (result i32)
i32.const 42
)
)`))
if err != nil {
panic(err)
}
fmt.Println(watgo.ValidateModule(m) == nil)
}
Output: true
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
watgo
command
|
|
|
internal
|
|
|
instrdef
Package instrdef defines the shared instruction catalog used across watgo's internal compiler pipeline.
|
Package instrdef defines the shared instruction catalog used across watgo's internal compiler pipeline. |
|
valhint
Package valhint carries optional validator-side metadata for lowered WAT.
|
Package valhint carries optional validator-side metadata for lowered WAT. |
|
tests
|
|
|
Package wasmir defines watgo's public semantic WebAssembly IR.
|
Package wasmir defines watgo's public semantic WebAssembly IR. |