rename to examples
This commit is contained in:
3 files changed
+3
-3
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/vedhavyas/go-wasm"
|
||||
)
|
||||
|
||||
func proxy(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)
|
||||
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
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
<-done
|
||||
log.Println("wasm exited", err)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func addition(this js.Value, args []js.Value) interface{} {
|
||||
log.Println("In WASM", args)
|
||||
a, b := args[0].Int(), args[1].Int()
|
||||
return a + b
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := make(chan bool)
|
||||
|
||||
// register functions
|
||||
fun := js.FuncOf(addition)
|
||||
js.Global().Set("addition", fun)
|
||||
|
||||
res := js.Global().Get("proxy").Invoke(1, 2)
|
||||
log.Printf("1 + 2 = %d\n", res.Int())
|
||||
<-ch
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user