true bidirctional
This commit is contained in:
5 files changed
+56
-21
No files matched your search
+12
-2
@@ -7,12 +7,24 @@ import (
|
||||
wasmgo "github.com/vedhavyas/wasm"
|
||||
)
|
||||
|
||||
func proxy(b *wasmgo.Bridge) wasmgo.Func {
|
||||
return func(args []interface{}) (i interface{}, e error) {
|
||||
fmt.Println("In Go", args)
|
||||
return b.CallFunc("addition", args)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
b, err := wasmgo.BridgeFromFile("test", "./simple/prog/main.wasm", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = b.SetFunc("proxy", proxy(b))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
init, done := make(chan error), make(chan bool)
|
||||
go b.Run(init, done)
|
||||
err = <-init
|
||||
@@ -20,8 +32,6 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res, err := b.CallFunc("printWasm", &[]interface{}{"Hello from Go"})
|
||||
fmt.Println(res, err)
|
||||
<-done
|
||||
fmt.Println("wasm exited", err)
|
||||
}
|
||||
+11
-6
@@ -8,17 +8,22 @@ import (
|
||||
)
|
||||
|
||||
// TODO: log seems to cause an issue
|
||||
func printWasm(this js.Value, v []js.Value) interface{} {
|
||||
fmt.Println(v[0].String())
|
||||
return "Hello from WASM"
|
||||
func addition(this js.Value, args []js.Value) interface{} {
|
||||
fmt.Println("In WASM", args)
|
||||
this.Set()
|
||||
this.SetIndex()
|
||||
a, b := args[0].Int(), args[1].Int()
|
||||
return a + b
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := make(chan bool)
|
||||
fmt.Println("WASM-Go Initialized")
|
||||
|
||||
// register functions
|
||||
fun := js.FuncOf(printWasm)
|
||||
js.Global().Set("printWasm", fun)
|
||||
fun := js.FuncOf(addition)
|
||||
js.Global().Set("addition", fun)
|
||||
|
||||
res := js.Global().Get("proxy").Invoke(1, 2)
|
||||
fmt.Printf("1 + 2 = %d\n", res.Int())
|
||||
<-ch
|
||||
}
|
||||
Binary file not shown.
Reference in new issue
Block a user