2019-06-02 19:37:57 +02:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build go1.13
|
|
|
|
|
|
|
|
package jsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"syscall/js"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Uint8ArrayToSlice(value js.Value) []byte {
|
|
|
|
s := make([]byte, value.Get("byteLength").Int())
|
|
|
|
js.CopyBytesToGo(s, value)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func ArrayBufferToSlice(value js.Value) []byte {
|
|
|
|
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
|
|
|
}
|
|
|
|
|
2019-06-17 03:48:36 +02:00
|
|
|
func sliceToByteSlice(s interface{}) (bs []byte) {
|
2019-06-02 19:37:57 +02:00
|
|
|
switch s := s.(type) {
|
|
|
|
case []int8:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []int16:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 2
|
|
|
|
h.Cap *= 2
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []int32:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []int64:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 8
|
|
|
|
h.Cap *= 8
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []uint8:
|
|
|
|
return s
|
|
|
|
case []uint16:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 2
|
|
|
|
h.Cap *= 2
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []uint32:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []uint64:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 8
|
|
|
|
h.Cap *= 8
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []float32:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
case []float64:
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 8
|
|
|
|
h.Cap *= 8
|
2019-06-17 03:48:36 +02:00
|
|
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(s)
|
2019-06-02 19:37:57 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
|
|
|
|
}
|
2019-06-17 03:48:36 +02:00
|
|
|
return
|
2019-06-02 19:37:57 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 16:59:00 +02:00
|
|
|
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
|
|
|
|
|
|
|
|
func getTemporaryUint8Array(size int) js.Value {
|
|
|
|
if l := temporaryBuffer.Get("byteLength").Int(); l < size {
|
|
|
|
for l < size {
|
|
|
|
l *= 2
|
|
|
|
}
|
|
|
|
temporaryBuffer = js.Global().Get("ArrayBuffer").New(l)
|
|
|
|
}
|
|
|
|
return js.Global().Get("Uint8Array").New(temporaryBuffer, 0, size)
|
|
|
|
}
|
|
|
|
|
2019-06-02 19:37:57 +02:00
|
|
|
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
|
|
|
switch s := s.(type) {
|
|
|
|
case []int8:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s))
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
|
|
|
|
case []int16:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 2)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
|
|
|
case []int32:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 4)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
|
|
|
case []uint8:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s))
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, s)
|
|
|
|
return a, func() {}
|
|
|
|
case []uint16:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 2)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
|
|
|
case []uint32:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 4)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
|
|
|
case []float32:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 4)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
|
|
|
case []float64:
|
2019-09-26 16:59:00 +02:00
|
|
|
a := getTemporaryUint8Array(len(s) * 8)
|
2019-06-02 19:37:57 +02:00
|
|
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("jsutil: unexpected value at SliceToTypedArray: %T", s))
|
|
|
|
}
|
|
|
|
}
|