This commit is contained in:
vedhavyas committed 2019-08-21 19:24:07 -07:00
1 parent 2a22ca2a7f
commit 6029286b43
5 files changed
+74 -32

No files matched your search

+24 -9
View File
@@ -1,36 +1,51 @@
package main
import (
"context"
"log"
"github.com/vedhavyas/go-wasm"
)
func proxy(b *wasm.Bridge) wasm.Func {
func addProxy(b *wasm.Bridge) wasm.Func {
return func(args []interface{}) (i interface{}, e error) {
log.Println("In Go", args)
return b.CallFunc("addition", args)
}
}
func main() {
b, err := wasm.BridgeFromFile("test", "./examples/wasm/main.wasm", nil)
func multiply(b *wasm.Bridge, a int) (int, error) {
m, err := b.CallFunc("multiplier", nil)
if err != nil {
log.Fatal(err)
return 0, err
}
err = b.SetFunc("proxy", proxy(b))
return a * int(m.(float64)), nil
}
func main() {
b, err := wasm.BridgeFromFile("test", "./examples/wasm/main.wasm", nil)
if err != nil {
panic(err)
}
init, done := make(chan error), make(chan bool)
go b.Run(init, done)
err = b.SetFunc("addProxy", addProxy(b))
if err != nil {
panic(err)
}
init := make(chan error)
ctx, cancF := context.WithCancel(context.Background())
defer cancF()
go b.Run(ctx, init)
err = <-init
if err != nil {
panic(err)
}
<-done
log.Println("wasm exited", err)
mul, err := multiply(b, 10)
if err != nil {
panic(err)
}
log.Printf("Multiplier: %v\n", mul)
}
+7 -3
View File
@@ -13,14 +13,18 @@ func addition(this js.Value, args []js.Value) interface{} {
return a + b
}
func multiplier(this js.Value, args []js.Value) interface{} {
return 10
}
func main() {
ch := make(chan bool)
// register functions
fun := js.FuncOf(addition)
js.Global().Set("addition", fun)
js.Global().Set("addition", js.FuncOf(addition))
js.Global().Set("multiplier", js.FuncOf(multiplier))
res := js.Global().Get("proxy").Invoke(1, 2)
res := js.Global().Get("addProxy").Invoke(1, 2)
log.Printf("1 + 2 = %d\n", res.Int())
<-ch
}
Binary file not shown.