diff --git a/bridge.go b/bridge.go index d925019..4c5256e 100644 --- a/bridge.go +++ b/bridge.go @@ -2,7 +2,9 @@ package wasm import ( "context" + "crypto/rand" "encoding/binary" + "errors" "fmt" "log" "math" @@ -129,6 +131,12 @@ func (b *Bridge) addValues() { }), }} }}, + "crypto": propObject("crypto", map[string]interface{}{ + "getRandomValues": Func(func(args []interface{}) (interface{}, error) { + arr := args[0].(*array) + return rand.Read(arr.data()) + }), + }), "fs": propObject("fs", map[string]interface{}{ "constants": propObject("constants", map[string]interface{}{ "O_WRONLY": syscall.O_WRONLY, @@ -440,7 +448,7 @@ func (b *Bridge) makeFuncWrapper(id, this interface{}, args *[]interface{}) (int b.valuesMu.RUnlock() event := propObject("_pendingEvent", map[string]interface{}{ "id": id, - "this": nil, + "this": goObj, "args": args, }) @@ -473,3 +481,21 @@ func (b *Bridge) SetFunc(fname string, fn Func) error { b.values[5].(*object).props[fname] = &fn return nil } + +func Bytes(v interface{}) ([]byte, error) { + arr, ok := v.(*array) + if !ok { + return nil, fmt.Errorf("got %T instead of bytes", v) + } + + return arr.data(), nil +} + +func Error(v interface{}) (errVal error, err error) { + str, ok := v.(string) + if !ok { + return nil, fmt.Errorf("got %T instead of error", v) + } + + return errors.New(str), nil +} diff --git a/examples/caller/main.go b/examples/caller/main.go index df9dfc9..16468ac 100644 --- a/examples/caller/main.go +++ b/examples/caller/main.go @@ -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) } diff --git a/examples/wasm/main.go b/examples/wasm/main.go index bdccbe5..c8bc057 100644 --- a/examples/wasm/main.go +++ b/examples/wasm/main.go @@ -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()) diff --git a/examples/wasm/main.wasm b/examples/wasm/main.wasm index a8916e5..c5cf12d 100755 Binary files a/examples/wasm/main.wasm and b/examples/wasm/main.wasm differ