This commit is contained in:
vedhavyas committed 2019-08-22 10:05:05 -07:00
1 parent 6029286b43
commit 8300aefbed
4 files changed
+69 -1

No files matched your search

+24
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"fmt"
"log"
"github.com/vedhavyas/go-wasm"
@@ -48,4 +49,27 @@ func main() {
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)
}
+18
View File
@@ -3,6 +3,8 @@
package main
import (
"crypto/rand"
"errors"
"log"
"syscall/js"
)
@@ -17,12 +19,28 @@ 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 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))
res := js.Global().Get("addProxy").Invoke(1, 2)
log.Printf("1 + 2 = %d\n", res.Int())
Binary file not shown.