76 lines
1.2 KiB
Go
76 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/vedhavyas/go-wasm"
|
|
)
|
|
|
|
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 multiply(b *wasm.Bridge, a int) (int, error) {
|
|
m, err := b.CallFunc("multiplier", nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return a * int(m.(float64)), nil
|
|
}
|
|
|
|
func main() {
|
|
b, err := wasm.BridgeFromFile("test", "./examples/wasm/main.wasm", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
mul, err := multiply(b, 10)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Printf("Multiplier: %v\n", mul)
|
|
|
|
res, err := b.CallFunc("getBytes", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
bytes, err := wasm.Bytes(res)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(bytes)
|
|
|
|
res, err = b.CallFunc("getError", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
verr, err := wasm.Error(res)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(verr)
|
|
}
|