working go wasm bridge

This commit is contained in:
vedhavyas committed 2019-08-07 12:47:59 +02:00
1 parent d528eeada8
commit 5a771cc80d
5 files changed
+133 -29

No files matched your search

+2 -16
View File
@@ -1,32 +1,18 @@
package main
import "C"
import (
"fmt"
"log"
wasmgo "github.com/vedhavyas/wasm"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
)
func main() {
// Reads the WebAssembly module as bytes.
bytes, err := wasm.ReadBytes("./simple/prog/main.wasm")
err := wasmgo.Bridge.InitWASM("./simple/prog/main.wasm", nil)
if err != nil {
log.Fatal(err)
}
imports, err := wasmgo.Imports()
if err != nil {
if err := wasmgo.Bridge.Run(); err != nil {
log.Fatal(err)
}
// Instantiates the WebAssembly module.
instance, err := wasm.NewInstanceWithImports(bytes, imports)
if err != nil {
log.Fatal(err)
}
defer instance.Close()
fmt.Println(instance.Exports)
}
+8 -3
View File
@@ -7,12 +7,17 @@ import (
"syscall/js"
)
func printWasm(v []js.Value) {
func printWasm(this js.Value, v []js.Value) interface{} {
fmt.Println("Hello from WASM", v)
return nil
}
func main() {
js.Global().Set("printWasm", js.NewCallback(printWasm))
c := make(chan struct{}, 0)
fmt.Println("WASM Go Initialized")
// register functions
js.Global().Set("printWasm", js.FuncOf(printWasm))
fmt.Println("Done...")
<-make(chan struct{})
<-c
}
Binary file not shown.