add helpers

pull/2/head
vedhavyas 2019-08-23 10:19:32 -07:00
parent 801fd37d20
commit f6ac4e734a
No known key found for this signature in database
GPG Key ID: 317BF0923E3EB7E5
5 changed files with 40 additions and 3 deletions

View File

@ -502,3 +502,17 @@ func Error(v interface{}) (errVal error, err error) {
return errors.New(str), nil
}
func UintArray(v interface{}) *[]uint {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice {
panic("not a slice")
}
buf := make([]uint, rv.Len(), rv.Len())
for i := 0; i < rv.Len(); i++ {
buf[i] = uint(rv.Index(i).Uint())
}
return &buf
}

View File

@ -60,15 +60,15 @@ func main() {
}
log.Println(bytes)
res, err = b.CallFunc("getError", nil)
res, err = b.CallFunc("bytes", []interface{}{wasm.UintArray(bytes)})
if err != nil {
panic(err)
}
verr, err := wasm.Error(res)
nb, err := wasm.Bytes(res)
if err != nil {
panic(err)
}
log.Println(verr)
log.Println(nb)
}

View File

@ -7,6 +7,8 @@ import (
"errors"
"log"
"syscall/js"
"github.com/vedhavyas/go-wasm/go-converts"
)
func addition(this js.Value, args []js.Value) interface{} {
@ -33,6 +35,12 @@ func getError(this js.Value, args []js.Value) interface{} {
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)
@ -41,6 +49,7 @@ func main() {
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())

Binary file not shown.

View File

@ -0,0 +1,14 @@
// +build js,wasm
package converts
import "syscall/js"
func ToBytes(v js.Value) []byte {
buf := make([]byte, v.Length(), v.Length())
for i := 0; i < v.Length(); i++ {
buf[i] = byte(v.Index(i).Int())
}
return buf
}