Skip to content

Commit d540089

Browse files
Redirects users to tested example (#791)
Rather than clutter both the README and home page with details that can drift and have caused maintenance, this directs users to the tested basic example. This also adds a FAQ entry about TinyGo's main, thanks to @basvanbeek for the help. Signed-off-by: Adrian Cole <adrian@tetrate.io>
1 parent 1f4d945 commit d540089

File tree

12 files changed

+124
-170
lines changed

12 files changed

+124
-170
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ build.examples.as:
5454
build.examples.zig:
5555
@cd examples/allocation/zig/testdata/ && zig build -Drelease-small=true && mv zig-out/lib/greet.wasm .
5656

57-
tinygo_sources := examples/allocation/tinygo/testdata/greet.go imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.go
57+
tinygo_sources := examples/basic/testdata/add.go examples/allocation/tinygo/testdata/greet.go imports/wasi_snapshot_preview1/example/testdata/tinygo/cat.go
5858
.PHONY: build.examples.tinygo
5959
build.examples.tinygo: $(tinygo_sources)
6060
@for f in $^; do \

README.md

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,9 @@ Import wazero and extend your Go application with code written in any language!
1515

1616
## Example
1717

18-
The best way to learn wazero is by trying one of our [examples](examples).
19-
20-
For the impatient, here's a peek of a general flow with wazero:
21-
22-
First, you need to compile your code into the WebAssembly Binary Format (Wasm).
23-
24-
Here's source in [TinyGo](https://wazero.io/languages/tinygo), which exports an
25-
"add" function:
26-
```go
27-
package main
28-
29-
//export add
30-
func add(x, y uint32) uint32 {
31-
return x + y
32-
}
33-
```
34-
35-
Here's the minimal command to build a `%.wasm` binary.
36-
```bash
37-
tinygo build -o add.wasm -target=wasi add.go
38-
```
39-
40-
Finally, you can run that inside your Go application.
41-
```go
42-
func main() {
43-
// Choose the context to use for function calls.
44-
ctx := context.Background()
45-
46-
// Read a WebAssembly binary containing an exported "add" function.
47-
wasm, err := os.ReadFile("./path/to/add.wasm")
48-
if err != nil {
49-
log.Panicln(err)
50-
}
51-
52-
// Create a new WebAssembly Runtime.
53-
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
54-
// WebAssembly 2.0 allows use of any version of TinyGo, including 0.24+.
55-
WithWasmCore2())
56-
defer r.Close(ctx) // This closes everything this Runtime created.
57-
58-
// Instantiate WASI, which implements system I/O such as console output.
59-
if _, err = wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
60-
log.Panicln(err)
61-
}
62-
63-
// Instantiate the module and return its exported functions
64-
module, err := r.InstantiateModuleFromBinary(ctx, wasm)
65-
if err != nil {
66-
log.Panicln(err)
67-
}
68-
69-
// Discover 1+2=3
70-
fmt.Println(module.ExportedFunction("add").Call(ctx, 1, 2))
71-
}
72-
```
73-
74-
Notes:
75-
76-
* The embedding application is often called the "host" in WebAssembly.
77-
* The Wasm binary is often called the "guest" in WebAssembly. Sometimes they
78-
need [imports][imports] to implement features such as console output.
79-
* Many languages compile to (target) Wasm including AssemblyScript, C, C++,
80-
Rust, TinyGo and Zig!
18+
The best way to learn wazero is by trying one of our [examples](examples). The
19+
most [basic example](examples/basic) extends a Go application with an addition
20+
function defined in WebAssembly.
8121

8222
## Deeper dive
8323

example_test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,62 @@
1-
package wazero
1+
package wazero_test
22

33
import (
44
"context"
55
_ "embed"
66
"fmt"
77
"log"
8+
9+
"github.com/tetratelabs/wazero"
10+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
811
)
912

1013
// addWasm was generated by the following:
1114
//
12-
// cd examples/basic/testdata/testdata; wat2wasm --debug-names add.wat
15+
// cd examples/basic/testdata; tinygo build -o add.wasm -target=wasi add.go
1316
//
1417
//go:embed examples/basic/testdata/add.wasm
1518
var addWasm []byte
1619

17-
// This is an example of how to use WebAssembly via adding two numbers.
20+
// This is an example of how to extend a Go application with an addition
21+
// function defined in WebAssembly.
22+
//
23+
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure
24+
// WASI host imports.
1825
//
19-
// See https://github.com/tetratelabs/wazero/tree/main/examples for more.
26+
// A complete project that does the same as this is available here:
27+
// https://github.com/tetratelabs/wazero/tree/main/examples/basic
2028
func Example() {
2129
// Choose the context to use for function calls.
2230
ctx := context.Background()
2331

2432
// Create a new WebAssembly Runtime.
25-
r := NewRuntime(ctx)
33+
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
34+
// WebAssembly 2.0 allows use of any version of TinyGo, including 0.24+.
35+
WithWasmCore2())
36+
defer r.Close(ctx) // This closes everything this Runtime created.
37+
38+
// Instantiate WASI, which implements host functions needed for TinyGo to
39+
// implement `panic`.
40+
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
41+
log.Panicln(err)
42+
}
2643

27-
// Add a module to the runtime named "wasm/math" which exports one function
28-
// "add", implemented in WebAssembly.
44+
// Instantiate the guest Wasm into the same runtime. It exports the `add`
45+
// function, implemented in WebAssembly.
2946
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
3047
if err != nil {
3148
log.Panicln(err)
3249
}
33-
defer mod.Close(ctx)
34-
35-
// Get a function that can be reused until its module is closed:
36-
add := mod.ExportedFunction("add")
3750

51+
// Call the `add` function and print the results to the console.
3852
x, y := uint64(1), uint64(2)
39-
results, err := add.Call(ctx, x, y)
53+
results, err := mod.ExportedFunction("add").Call(ctx, x, y)
4054
if err != nil {
4155
log.Panicln(err)
4256
}
4357

44-
fmt.Printf("%s: %d + %d = %d\n", mod.Name(), x, y, results[0])
58+
fmt.Printf("%d + %d = %d\n", x, y, results[0])
4559

4660
// Output:
47-
// wasm/math: 1 + 2 = 3
61+
// 1 + 2 = 3
4862
}

examples/basic/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
11
## Basic example
22

3-
This example shows how to use both WebAssembly and Go-defined functions with wazero.
3+
This example shows how to extend a Go application with an addition function
4+
defined in WebAssembly.
5+
6+
Ex.
7+
```bash
8+
$ go run add.go 7 9
9+
7 + 9 = 16
10+
```
11+
12+
### Compilation
13+
14+
wazero is a WebAssembly runtime, embedded in your host application. To run
15+
WebAssembly functions, you need access to a WebAssembly Binary (Wasm),
16+
typically a `%.wasm` file.
17+
18+
[add.wasm](testdata/add.wasm) was compiled from [add.go](testdata/add.go) with
19+
[TinyGo][1], as it is the most common way to compile Go source to Wasm. Here's
20+
the minimal command to build a `%.wasm` binary.
21+
22+
```bash
23+
cd testdata; tinygo build -o add.wasm -target=wasi add.go
24+
```
25+
26+
### Notes
27+
28+
* Many other languages compile to (target) Wasm including AssemblyScript, C,
29+
C++, Rust, and Zig!
30+
* The embedding application is often called the "host" in WebAssembly.
31+
* The Wasm binary is often called the "guest" in WebAssembly. Sometimes they
32+
need [imports](../../imports) to implement features such as console output.
33+
TinyGo's `wasi` target, requires [WASI][2] imports.
34+
35+
[1]: https://wazero.io/languages/tinygo
36+
[2]: https://wazero.io/specs/#wasi

examples/basic/add.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,55 @@ import (
99
"strconv"
1010

1111
"github.com/tetratelabs/wazero"
12+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1213
)
1314

1415
// addWasm was generated by the following:
1516
//
16-
// cd testdata; wat2wasm --debug-names add.wat
17+
// cd testdata; tinygo build -o add.wasm -target=wasi add.go
1718
//
1819
//go:embed testdata/add.wasm
1920
var addWasm []byte
2021

21-
// main implements a basic function in both Go and WebAssembly.
22+
// main is an example of how to extend a Go application with an addition
23+
// function defined in WebAssembly.
24+
//
25+
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure
26+
// WASI host imports.
2227
func main() {
2328
// Choose the context to use for function calls.
2429
ctx := context.Background()
2530

2631
// Create a new WebAssembly Runtime.
27-
r := wazero.NewRuntime(ctx)
32+
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
33+
// WebAssembly 2.0 allows use of any version of TinyGo, including 0.24+.
34+
WithWasmCore2())
2835
defer r.Close(ctx) // This closes everything this Runtime created.
2936

30-
// Add a module to the runtime named "wasm/math" which exports one function "add", implemented in WebAssembly.
31-
wasm, err := r.InstantiateModuleFromBinary(ctx, addWasm)
37+
// Instantiate WASI, which implements host functions needed for TinyGo to
38+
// implement `panic`.
39+
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
40+
log.Panicln(err)
41+
}
42+
43+
// Instantiate the guest Wasm into the same runtime. It exports the `add`
44+
// function, implemented in WebAssembly.
45+
mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
3246
if err != nil {
3347
log.Panicln(err)
3448
}
3549

3650
// Read two args to add.
3751
x, y := readTwoArgs()
3852

39-
// Call the `add` function in the module and print the results to the console.
40-
add := wasm.ExportedFunction("add")
53+
// Call the `add` function and print the results to the console.
54+
add := mod.ExportedFunction("add")
4155
results, err := add.Call(ctx, x, y)
4256
if err != nil {
4357
log.Panicln(err)
4458
}
4559

46-
fmt.Printf("%s: %d + %d = %d\n", wasm.Name(), x, y, results[0])
60+
fmt.Printf("%d + %d = %d\n", x, y, results[0])
4761
}
4862

4963
func readTwoArgs() (uint64, uint64) {

examples/basic/add_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ import (
1212
// go run add.go 7 9
1313
func Test_main(t *testing.T) {
1414
stdout, _ := maintester.TestMain(t, main, "add", "7", "9")
15-
require.Equal(t, `wasm/math: 7 + 9 = 16
15+
require.Equal(t, `7 + 9 = 16
1616
`, stdout)
1717
}

examples/basic/testdata/add.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
//export add
4+
func add(x, y uint32) uint32 {
5+
return x + y
6+
}
7+
8+
// main is required for the `wasi` target, even if it isn't used.
9+
// See https://wazero.io/languages/tinygo/#why-do-i-have-to-define-main
10+
func main() {}

examples/basic/testdata/add.wasm

2.62 KB
Binary file not shown.

examples/basic/testdata/add.wat

Lines changed: 0 additions & 15 deletions
This file was deleted.

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module github.com/tetratelabs/wazero
44
// https://go.dev/doc/devel/release (1 version behind latest)
55
go 1.18
66

7-
// Retract the first tag, which had the wrong naming convention. Also, retract
8-
// the first beta as it had a memory bug and quickly fixed in v1.0.0-beta.2.
9-
retract [v1.0.0-beta1, v1.0.0-beta.1]
7+
// Wrong naming convention
8+
retract v1.0.0-beta1
9+
10+
// Has memory bug and quickly fixed in v1.0.0-beta.2.
11+
retract v1.0.0-beta.1

site/content/_index.md

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,9 @@ wazero is the only zero dependency WebAssembly runtime written in Go.
1111

1212
## Example
1313

14-
The best way to learn wazero is by trying one of our [examples][1]
15-
16-
For the impatient, here's a peek of a general flow with wazero:
17-
18-
First, you need to compile your code into the WebAssembly Binary Format (Wasm).
19-
20-
Here's source in [TinyGo]({{< ref "/languages/tinygo" >}}), which exports an
21-
"add" function:
22-
```go
23-
package main
24-
25-
//export add
26-
func add(x, y uint32) uint32 {
27-
return x + y
28-
}
29-
```
30-
31-
Here's the minimal command to build a `%.wasm` binary.
32-
```bash
33-
tinygo build -o add.wasm -target=wasi add.go
34-
```
35-
36-
Finally, you can run that inside your Go application.
37-
```go
38-
func main() {
39-
// Choose the context to use for function calls.
40-
ctx := context.Background()
41-
42-
// Read a WebAssembly binary containing an exported "add" function.
43-
wasm, err := os.ReadFile("./path/to/add.wasm")
44-
if err != nil {
45-
log.Panicln(err)
46-
}
47-
48-
// Create a new WebAssembly Runtime.
49-
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().
50-
// WebAssembly 2.0 allows use of any version of TinyGo, including 0.24+.
51-
WithWasmCore2())
52-
defer r.Close(ctx) // This closes everything this Runtime created.
53-
54-
// Instantiate WASI, which implements system I/O such as console output.
55-
if _, err = wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
56-
log.Panicln(err)
57-
}
58-
59-
// Instantiate the module and return its exported functions
60-
module, err := r.InstantiateModuleFromBinary(ctx, wasm)
61-
if err != nil {
62-
log.Panicln(err)
63-
}
64-
65-
// Discover 1+2=3
66-
fmt.Println(module.ExportedFunction("add").Call(ctx, 1, 2))
67-
}
68-
```
69-
70-
Notes:
71-
72-
* The Wasm binary is often called the "guest" in WebAssembly.
73-
* The embedding application is often called the "host" in WebAssembly.
74-
* Many languages compile to (target) Wasm including AssemblyScript, C, C++,
75-
Rust, TinyGo and Zig!
14+
The best way to learn wazero is by trying one of our [examples][1]. The
15+
most [basic example][2] extends a Go application with an addition function
16+
defined in WebAssembly.
7617

7718
## Why zero?
7819

@@ -93,8 +34,9 @@ go get github.com/tetratelabs/wazero@main
9334

9435
wazero will release its first beta at the end of August 2022, and finalize
9536
1.0 once Go 1.20 is released in Feb 2023. Meanwhile, please practice the
96-
current APIs to ensure they work for you, and give us a [star][2] if you are
37+
current APIs to ensure they work for you, and give us a [star][3] if you are
9738
enjoying it so far!
9839

9940
[1]: https://github.com/tetratelabs/wazero/blob/main/examples
100-
[2]: https://github.com/tetratelabs/wazero/stargazers
41+
[2]: https://github.com/tetratelabs/wazero/blob/main/examples/basic
42+
[3]: https://github.com/tetratelabs/wazero/stargazers

0 commit comments

Comments
 (0)