This commit is contained in:
vedhavyas committed 2019-09-23 23:37:59 +02:00
1 parent e56a2c5a85
commit 21208d42ac
6 files changed
+2 -2

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
// +build js,wasm
package main
import (
"crypto/rand"
"errors"
"log"
"syscall/js"
"github.com/vedhavyas/go-wasm/go-converts"
)
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 multiplier(this js.Value, args []js.Value) interface{} {
return 10
}
func getBytes(this js.Value, args []js.Value) interface{} {
r := make([]byte, 32)
_, err := rand.Read(r)
if err != nil {
panic(err)
}
return js.TypedArrayOf(r)
}
func getError(this js.Value, args []js.Value) interface{} {
err := errors.New("test errors")
return err.Error()
}
func receiveSendBytes(this js.Value, args []js.Value) interface{} {
b := args[0]
buf := converts.ToBytes(b)
return js.TypedArrayOf(buf)
}
func main() {
ch := make(chan bool)
// register functions
js.Global().Set("addition", js.FuncOf(addition))
js.Global().Set("multiplier", js.FuncOf(multiplier))
js.Global().Set("getBytes", js.FuncOf(getBytes))
js.Global().Set("getError", js.FuncOf(getError))
js.Global().Set("bytes", js.FuncOf(receiveSendBytes))
res := js.Global().Get("addProxy").Invoke(1, 2)
log.Printf("1 + 2 = %d\n", res.Int())
<-ch
}
BIN
View File
Binary file not shown.