mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
jsutil: Refactoring
This commit is contained in:
parent
ab61a2d23c
commit
4396978d4b
@ -34,56 +34,66 @@ func ArrayBufferToSlice(value js.Value) []byte {
|
|||||||
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sliceToByteSlice(s interface{}) []byte {
|
func sliceToByteSlice(s interface{}) (bs []byte) {
|
||||||
switch s := s.(type) {
|
switch s := s.(type) {
|
||||||
case []int8:
|
case []int8:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []int16:
|
case []int16:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 2
|
h.Len *= 2
|
||||||
h.Cap *= 2
|
h.Cap *= 2
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []int32:
|
case []int32:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 4
|
h.Len *= 4
|
||||||
h.Cap *= 4
|
h.Cap *= 4
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []int64:
|
case []int64:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 8
|
h.Len *= 8
|
||||||
h.Cap *= 8
|
h.Cap *= 8
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []uint8:
|
case []uint8:
|
||||||
return s
|
return s
|
||||||
case []uint16:
|
case []uint16:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 2
|
h.Len *= 2
|
||||||
h.Cap *= 2
|
h.Cap *= 2
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []uint32:
|
case []uint32:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 4
|
h.Len *= 4
|
||||||
h.Cap *= 4
|
h.Cap *= 4
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []uint64:
|
case []uint64:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 8
|
h.Len *= 8
|
||||||
h.Cap *= 8
|
h.Cap *= 8
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []float32:
|
case []float32:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 4
|
h.Len *= 4
|
||||||
h.Cap *= 4
|
h.Cap *= 4
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
case []float64:
|
case []float64:
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
||||||
h.Len *= 8
|
h.Len *= 8
|
||||||
h.Cap *= 8
|
h.Cap *= 8
|
||||||
return *(*[]byte)(unsafe.Pointer(h))
|
bs = *(*[]byte)(unsafe.Pointer(h))
|
||||||
|
runtime.KeepAlive(s)
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
|
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
||||||
@ -91,48 +101,40 @@ func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
|||||||
case []int8:
|
case []int8:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s))
|
a := js.Global().Get("Uint8Array").New(len(s))
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
|
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
|
||||||
case []int16:
|
case []int16:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||||
case []int32:
|
case []int32:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||||
case []uint8:
|
case []uint8:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s))
|
a := js.Global().Get("Uint8Array").New(len(s))
|
||||||
js.CopyBytesToJS(a, s)
|
js.CopyBytesToJS(a, s)
|
||||||
runtime.KeepAlive(s)
|
|
||||||
return a, func() {}
|
return a, func() {}
|
||||||
case []uint16:
|
case []uint16:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
a := js.Global().Get("Uint8Array").New(len(s) * 2)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||||
case []uint32:
|
case []uint32:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||||
case []float32:
|
case []float32:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
a := js.Global().Get("Uint8Array").New(len(s) * 4)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||||
case []float64:
|
case []float64:
|
||||||
a := js.Global().Get("Uint8Array").New(len(s) * 8)
|
a := js.Global().Get("Uint8Array").New(len(s) * 8)
|
||||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||||
runtime.KeepAlive(s)
|
|
||||||
buf := a.Get("buffer")
|
buf := a.Get("buffer")
|
||||||
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}
|
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user