Skip to content

examples/wasm: update export example to show the use of the go:wasmimport directive #3589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/examples/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

The examples here show two different ways of using WebAssembly with TinyGo:

1. Defining and exporting functions via the `//export <name>` directive. See
[the export folder](./export) for an example of this. Additionally, the Wasm
module (which has a default value of `env`) can be specified using
`//go:wasm-module <module>`.
1. Defining and exporting functions via the `//go:wasmimport <module>` directive. See
[the export folder](./export) for an example of this. This exports the function to the Wasm
module (which has a default value of `env`). You can also use the Go directive
`//go:export <module>`.
1. Defining and executing a `func main()`. This is similar to how the Go
standard library implementation works. See [the main folder](./main) for an
example of this.
Expand Down
4 changes: 2 additions & 2 deletions src/examples/wasm/export/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
func main() {
}

//export add
//go:wasmimport add
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this update is premature, go:wasmimport is only supported on functions with no body that get linked at runtime to exports of other modules.

Here it seems that the //export directive is intended to ask the compiler to add the function to the list of exports of the module, or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK. I totally misunderstood what was happening here. Thanks for the clarification.

func add(a, b int) int {
return a + b
}

//export update
//go:wasmimport update
func update() {
document := js.Global().Get("document")
aStr := document.Call("getElementById", "a").Get("value").String()
Expand Down