From 453a1d35c315cb4b2519c86e4d53a7699bbcdea2 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 5 Oct 2024 02:36:47 +0200 Subject: [PATCH] compiler, runtime: enable go:wasmexport for wasip2 (#4499) * compiler: prefer go:wasmexport over go:export * runtime, targets/wasip2: enable -buildmode=c-shared for wasip2 * runtime: rename import from wasi_run to wasiclirun (PR feedback) --- compiler/symbol.go | 7 +++++++ ...untime_wasm_wasip2.go => runtime_wasip2.go} | 18 +++++++++++------- src/runtime/runtime_wasmentry.go | 2 +- targets/wasip2.json | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) rename src/runtime/{runtime_wasm_wasip2.go => runtime_wasip2.go} (75%) diff --git a/compiler/symbol.go b/compiler/symbol.go index 4a080cbbc..216aa6eec 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -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 diff --git a/src/runtime/runtime_wasm_wasip2.go b/src/runtime/runtime_wasip2.go similarity index 75% rename from src/runtime/runtime_wasm_wasip2.go rename to src/runtime/runtime_wasip2.go index 57e6623d3..ba8f52100 100644 --- a/src/runtime/runtime_wasm_wasip2.go +++ b/src/runtime/runtime_wasip2.go @@ -6,18 +6,19 @@ import ( "unsafe" "internal/wasi/cli/v0.2.0/environment" + wasiclirun "internal/wasi/cli/v0.2.0/run" monotonicclock "internal/wasi/clocks/v0.2.0/monotonic-clock" + + "internal/cm" ) type timeUnit int64 -//export wasi:cli/run@0.2.0#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() { + wasiclirun.Exports.Run = func() cm.BoolResult { + callMain() + return false + } } var args []string @@ -51,3 +52,6 @@ func sleepTicks(d timeUnit) { func ticks() timeUnit { return timeUnit(monotonicclock.Now()) } + +func beforeExit() { +} diff --git a/src/runtime/runtime_wasmentry.go b/src/runtime/runtime_wasmentry.go index 5a135f428..7bb1e1b44 100644 --- a/src/runtime/runtime_wasmentry.go +++ b/src/runtime/runtime_wasmentry.go @@ -1,4 +1,4 @@ -//go:build tinygo.wasm && !wasip2 && !js +//go:build tinygo.wasm && !js package runtime diff --git a/targets/wasip2.json b/targets/wasip2.json index b32a68197..4b0e67591 100644 --- a/targets/wasip2.json +++ b/targets/wasip2.json @@ -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",