Skip to content

compiler, runtime: enable go:wasmexport for wasip2 #4496

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 2 commits 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
7 changes: 7 additions & 0 deletions compiler/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) {
}
}
}

// If both //go:wasmexport and //go:export or //export are declared,
// only honor go:wasmexport.
if info.wasmExport != "" {
// TODO: log warning?
info.exported = false
}
}

// Check whether this function can be used in //go:wasmimport or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"unsafe"

"internal/wasi/cli/v0.2.0/environment"
wasi_run "internal/wasi/cli/v0.2.0/run"
monotonicclock "internal/wasi/clocks/v0.2.0/monotonic-clock"

"internal/cm"
)

type timeUnit int64

//export wasi:cli/[email protected]#run
func __wasi_cli_run_run() uint32 {
// These need to be initialized early so that the heap can be initialized.
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
run()
return 0
func init() {
wasi_run.Exports.Run = func() cm.BoolResult {
callMain()
return false
}
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, any reason not to use //export here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great question! All exports defined in WIT should use //go:wasmexport, including the wiring up from wasi:cli/command#run to main.main. This is the standard pattern for wiring up exports generated from wit-bindgen-go.

  1. Developer generates Go (e.g. wasi:http/proxy)
  2. Developer writes Go library that wraps the low-level APIs in the generated code (e.g. a net/http compatible shim)
  3. In the shim code, developer assigns the relevant Exports struct field to their implementation. For example: https://github.com/ydnar/wasi-http-go/blob/76673aea4c888a0e9b36d20604b3f11a4831bdd8/wasihttp/server.go#L21-L24

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW, I believe //export should still be used for cabi_realloc since the Component Model defines different reentrancy rules for low-level exports like this.

}

var args []string
Expand Down Expand Up @@ -51,3 +52,6 @@ func sleepTicks(d timeUnit) {
func ticks() timeUnit {
return timeUnit(monotonicclock.Now())
}

func beforeExit() {
}
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasmentry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && !wasip2 && !js
//go:build tinygo.wasm && !js

package runtime

Expand Down
1 change: 1 addition & 0 deletions targets/wasip2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"cpu": "generic",
"features": "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext",
"build-tags": ["tinygo.wasm", "wasip2"],
"buildmode": "c-shared",
"goos": "linux",
"goarch": "arm",
"linker": "wasm-ld",
Expand Down
Loading