bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
14.81k stars 1.24k forks source link

wasmtime main.wasm ,then encounter an error: `gojs::runtime.scheduleTimeoutEvent` has not been defined #8841

Closed dylanpoe closed 1 week ago

dylanpoe commented 1 week ago

hello, I use golang build a main.wasm files

//go:build js && wasm

// main.go
package main

import (
    "syscall/js"
)

func main() {
    js.Global().Call("alert", "Hello World")
}

and build the main.wasm by the code

 GOOS=js GOARCH=wasm go build -o  main.wasm

then use the wasmtime main.wasm, got the error

Error: failed to run main module `main.wasm`

Caused by:
    0: failed to instantiate "main.wasm"
    1: unknown import: `gojs::runtime.scheduleTimeoutEvent` has not been defined

hot t fix it ?

bjorn3 commented 1 week ago

GOOS=js uses a Go specific syscall interface which is only implemented by the JS code available at https://github.com/golang/go/blob/master/misc/wasm/wasm_exec.js This only works in the browser and with Node.JS and is incompatible with any native WASM runtime. For Wasmtime and other native WASM runtimes you have to use GOOS=wasip1 instead and are not allowed to use the syscalls/js package.