pull/2/head
vedhavyas 2019-08-22 10:05:05 -07:00
parent 6029286b43
commit 8300aefbed
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
4 changed files with 69 additions and 1 deletions

View File

@ -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
}

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)
}

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.