diff options
author | Adrian Cole <[email protected]> | 2022-09-07 16:04:55 +0800 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-09-07 13:31:21 +0200 |
commit | 07cdcc61f772ad6d9ec88b6b9fd877316b579fca (patch) | |
tree | 09b8f266e6b0943c39556c85071d3aa48e8f149d | |
parent | 4ca9b34133aba934891af1a5132cded73ed99754 (diff) | |
download | tinygo-07cdcc61f772ad6d9ec88b6b9fd877316b579fca.tar.gz tinygo-07cdcc61f772ad6d9ec88b6b9fd877316b579fca.zip |
examples: adds summary of wasm examples and fixes callback bug
This adds a summary of each wasm example, as before it was a bit unclear
how to do so. This also fixes the callback example which was broken.
Fixes #2568
Signed-off-by: Adrian Cole <[email protected]>
-rw-r--r-- | src/examples/wasm/README.md | 22 | ||||
-rw-r--r-- | src/examples/wasm/callback/wasm.go | 2 |
2 files changed, 12 insertions, 12 deletions
diff --git a/src/examples/wasm/README.md b/src/examples/wasm/README.md index 0975b587c..b80c33d35 100644 --- a/src/examples/wasm/README.md +++ b/src/examples/wasm/README.md @@ -21,12 +21,14 @@ $ tinygo build -o ./wasm.wasm -target wasm ./main/main.go This creates a `wasm.wasm` file, which we can load in JavaScript and execute in a browser. -This examples folder contains two examples that can be built using `make`: - -```bash -$ make export -``` - +Next, choose which example you want to use: +* [callback](callback): Defines and configures callbacks in Wasm. +* [export](export): Defines callbacks in Wasm, but configures them in JavaScript. +* [invoke](invoke): Invokes a function defined in JavaScript from Wasm. +* [main](main): Prints a message to the JavaScript console from Wasm. +* [slices](slices): Splits an Array defined in JavaScript from Wasm. + +Let's say you chose [main](main), you'd build it like so: ```bash $ make main ``` @@ -42,12 +44,8 @@ Serving ./html on http://localhost:8080 Use your web browser to visit http://localhost:8080. -* The wasm "export" example displays a simple math equation using HTML, with -the result calculated dynamically using WebAssembly. Changing any of the -values on the left hand side triggers the exported wasm `update` function to -recalculate the result. -* The wasm "main" example uses `println` to write to your browser JavaScript -console. You may need to open the browser development tools console to see it. +* Tip: Open the browser development tools (e.g. Right-click, Inspect in + FireFox) to see console output. ## How it works diff --git a/src/examples/wasm/callback/wasm.go b/src/examples/wasm/callback/wasm.go index da86550b4..f350f496c 100644 --- a/src/examples/wasm/callback/wasm.go +++ b/src/examples/wasm/callback/wasm.go @@ -8,10 +8,12 @@ import ( var a, b int func main() { + wait := make(chan struct{}, 0) document := js.Global().Get("document") document.Call("getElementById", "a").Set("oninput", updater(&a)) document.Call("getElementById", "b").Set("oninput", updater(&b)) update() + <-wait } func updater(n *int) js.Func { |