wasmerio / wasmer-php

🐘🕸️ WebAssembly runtime for PHP
https://wasmerio.github.io/wasmer-php/wasm/
MIT License
1.01k stars 42 forks source link

Go wasm not supported ? #92

Closed xywf221 closed 4 years ago

xywf221 commented 4 years ago

Summary

I compiled a simple wasm instance with go, but it has exceptions.

Fatal error: Uncaught RuntimeException: An error happened while compiling or instantiating the module `/Users/mac-mini/go/src/demo/9/go_main.wasm`:
    8 link errors: (1 of 8) Import not found, namespace: go, name: debug (2 of 8) Import not found, namespace: go, name: runtime.wasmExit (3 of 8) Import not found, namespace: go, name: runtime.wasmWrite (4 of 8) Import not found, namespace: go, name: runtime.nanotime (5 of 8) Import not found, namespace: go, name: runtime.walltime (6 of 8) Import not found, namespace: go, name: runtime.scheduleCallback (7 of 8) Import not found, namespace: go, name: runtime.clearScheduledCallback (8 of 8) Import not found, namespace: go, name: runtime.getRandomData in /private/tmp/php-ext-wasm/lib/Instance.php:84
Stack trace:
#0 /private/tmp/php-ext-wasm/examples/test.php(7): Wasm\Instance->__construct('/Users/mac-mini...')
#1 {main}
  thrown in /private/tmp/php-ext-wasm/lib/Instance.php on line 84

Additional details

Go source code :

package main

func main() {

}

func test() {

}

Compile command : GOOS=js GOARCH=wasm go build -o go_main.wasm 8.go

Hywan commented 4 years ago

Duplicate of https://github.com/wasmerio/wasmer/issues/924.

You've to declare the host functions (imported functions, aka imports) required by Go. Go WebAssembly toolchain is essentially targetting the Web for the moment, and a little jugglery is required to glue everything. Their approach (which is a valid one) is to required a set of imported functions.

Since this is more and more requested, I'm likely to implement this set of imported functions for all platforms (Go for PHP, Python, Ruby, C, C++, Rust etc.)…

Check this documentation to learn more about the PHP host function API: https://github.com/wasmerio/php-ext-wasm/tree/master/lib#function-wasm_new_instance, or https://github.com/wasmerio/php-ext-wasm/tree/master/lib#function-wasm_module_new_instance, or jump directly on this example: https://github.com/wasmerio/php-ext-wasm/blob/master/examples/imported_function.php.