Skip to content

Commit

Permalink
syscall/js: add TypedArrayOf
Browse files Browse the repository at this point in the history
The new function js.TypedArrayOf returns a JavaScript typed array for
a given slice.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

This change also changes js.ValueOf to not accept a []byte any more.

Fixes #25532.

Change-Id: I8c7bc98ca4e21c3514d19eee7a1f92388d74ab2a
Reviewed-on: https://go-review.googlesource.com/121215
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
neelance authored and bradfitz committed Jun 27, 2018
1 parent 63a4acb commit bafe466
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/crypto/rand/rand_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var jsCrypto = js.Global().Get("crypto")
type reader struct{}

func (r *reader) Read(b []byte) (int, error) {
jsCrypto.Call("getRandomValues", b)
a := js.TypedArrayOf(b)
jsCrypto.Call("getRandomValues", a)
a.Release()
return len(b), nil
}
8 changes: 6 additions & 2 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func (r *streamReader) Read(p []byte) (n int, err error) {
return
}
value := make([]byte, result.Get("value").Get("byteLength").Int())
js.ValueOf(value).Call("set", result.Get("value"))
a := js.TypedArrayOf(value)
a.Call("set", result.Get("value"))
a.Release()
bCh <- value
})
defer success.Close()
Expand Down Expand Up @@ -227,7 +229,9 @@ func (r *arrayReader) Read(p []byte) (n int, err error) {
// Wrap the input ArrayBuffer with a Uint8Array
uint8arrayWrapper := js.Global().Get("Uint8Array").New(args[0])
value := make([]byte, uint8arrayWrapper.Get("byteLength").Int())
js.ValueOf(value).Call("set", uint8arrayWrapper)
a := js.TypedArrayOf(value)
a.Call("set", uint8arrayWrapper)
a.Release()
bCh <- value
})
defer success.Close()
Expand Down
16 changes: 12 additions & 4 deletions src/syscall/fs_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ func Read(fd int, b []byte) (int, error) {
return n, err
}

n, err := fsCall("readSync", fd, b, 0, len(b))
a := js.TypedArrayOf(b)
n, err := fsCall("readSync", fd, a, 0, len(b))
a.Release()
if err != nil {
return 0, err
}
Expand All @@ -395,7 +397,9 @@ func Write(fd int, b []byte) (int, error) {
return n, err
}

n, err := fsCall("writeSync", fd, b, 0, len(b))
a := js.TypedArrayOf(b)
n, err := fsCall("writeSync", fd, a, 0, len(b))
a.Release()
if err != nil {
return 0, err
}
Expand All @@ -405,15 +409,19 @@ func Write(fd int, b []byte) (int, error) {
}

func Pread(fd int, b []byte, offset int64) (int, error) {
n, err := fsCall("readSync", fd, b, 0, len(b), offset)
a := js.TypedArrayOf(b)
n, err := fsCall("readSync", fd, a, 0, len(b), offset)
a.Release()
if err != nil {
return 0, err
}
return n.Int(), nil
}

func Pwrite(fd int, b []byte, offset int64) (int, error) {
n, err := fsCall("writeSync", fd, b, 0, len(b), offset)
a := js.TypedArrayOf(b)
n, err := fsCall("writeSync", fd, a, 0, len(b), offset)
a.Release()
if err != nil {
return 0, err
}
Expand Down
23 changes: 14 additions & 9 deletions src/syscall/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ func Global() Value {
return valueGlobal
}

var uint8Array = valueGlobal.Get("Uint8Array")

// ValueOf returns x as a JavaScript value.
// ValueOf returns x as a JavaScript value:
//
// | Go | JavaScript |
// | --------------------- | --------------------- |
// | js.Value | [its value] |
// | js.TypedArray | [typed array] |
// | js.Callback | function |
// | nil | null |
// | bool | boolean |
// | integers and floats | number |
// | string | string |
func ValueOf(x interface{}) Value {
switch x := x.(type) {
case Value:
return x
case TypedArray:
return x.Value
case Callback:
return x.enqueueFn
case nil:
Expand Down Expand Up @@ -128,13 +138,8 @@ func ValueOf(x interface{}) Value {
return floatValue(x)
case string:
return makeValue(stringVal(x))
case []byte:
if len(x) == 0 {
return uint8Array.New(memory.Get("buffer"), 0, 0)
}
return uint8Array.New(memory.Get("buffer"), unsafe.Pointer(&x[0]), len(x))
default:
panic("invalid value")
panic("ValueOf: invalid value")
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/syscall/js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ func TestObject(t *testing.T) {
}
}

func TestTypedArrayOf(t *testing.T) {
testTypedArrayOf(t, "[]int8", []int8{0, -42, 0}, -42)
testTypedArrayOf(t, "[]int16", []int16{0, -42, 0}, -42)
testTypedArrayOf(t, "[]int32", []int32{0, -42, 0}, -42)
testTypedArrayOf(t, "[]uint8", []uint8{0, 42, 0}, 42)
testTypedArrayOf(t, "[]uint16", []uint16{0, 42, 0}, 42)
testTypedArrayOf(t, "[]uint32", []uint32{0, 42, 0}, 42)
testTypedArrayOf(t, "[]float32", []float32{0, -42.5, 0}, -42.5)
testTypedArrayOf(t, "[]float64", []float64{0, -42.5, 0}, -42.5)
}

func testTypedArrayOf(t *testing.T, name string, slice interface{}, want float64) {
t.Run(name, func(t *testing.T) {
a := js.TypedArrayOf(slice)
got := a.Index(1).Float()
a.Release()
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
})
}

func TestNaN(t *testing.T) {
want := js.ValueOf(math.NaN())
got := dummys.Get("NaN")
Expand Down
103 changes: 103 additions & 0 deletions src/syscall/js/typedarray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm

package js

import (
"sync"
"unsafe"
)

var (
int8Array = Global().Get("Int8Array")
int16Array = Global().Get("Int16Array")
int32Array = Global().Get("Int32Array")
uint8Array = Global().Get("Uint8Array")
uint16Array = Global().Get("Uint16Array")
uint32Array = Global().Get("Uint32Array")
float32Array = Global().Get("Float32Array")
float64Array = Global().Get("Float64Array")
)

// TypedArray represents a JavaScript typed array.
type TypedArray struct {
Value
}

// Release frees up resources allocated for the typed array.
// The typed array and its buffer must not be accessed after calling Release.
func (a TypedArray) Release() {
openTypedArraysMutex.Lock()
delete(openTypedArrays, a)
openTypedArraysMutex.Unlock()
}

var (
openTypedArraysMutex sync.Mutex
openTypedArrays = make(map[TypedArray]interface{})
)

// TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array.
// It can be passed to functions of this package that accept interface{}, for example Value.Set and Value.Call.
//
// The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64.
// Passing an unsupported value causes a panic.
//
// TypedArray.Release must be called to free up resources when the typed array will not be used any more.
func TypedArrayOf(slice interface{}) TypedArray {
a := TypedArray{typedArrayOf(slice)}
openTypedArraysMutex.Lock()
openTypedArrays[a] = slice
openTypedArraysMutex.Unlock()
return a
}

func typedArrayOf(slice interface{}) Value {
switch slice := slice.(type) {
case []int8:
if len(slice) == 0 {
return int8Array.New(memory.Get("buffer"), 0, 0)
}
return int8Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []int16:
if len(slice) == 0 {
return int16Array.New(memory.Get("buffer"), 0, 0)
}
return int16Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []int32:
if len(slice) == 0 {
return int32Array.New(memory.Get("buffer"), 0, 0)
}
return int32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []uint8:
if len(slice) == 0 {
return uint8Array.New(memory.Get("buffer"), 0, 0)
}
return uint8Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []uint16:
if len(slice) == 0 {
return uint16Array.New(memory.Get("buffer"), 0, 0)
}
return uint16Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []uint32:
if len(slice) == 0 {
return uint32Array.New(memory.Get("buffer"), 0, 0)
}
return uint32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []float32:
if len(slice) == 0 {
return float32Array.New(memory.Get("buffer"), 0, 0)
}
return float32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
case []float64:
if len(slice) == 0 {
return float64Array.New(memory.Get("buffer"), 0, 0)
}
return float64Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice))
default:
panic("TypedArrayOf: not a supported slice")
}
}

0 comments on commit bafe466

Please sign in to comment.