go112_js.go (1278B)
1 // Copyright 2019 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !go1.13 16 17 package jsutil 18 19 import ( 20 "syscall/js" 21 ) 22 23 func Uint8ArrayToSlice(value js.Value) []byte { 24 // Note that TypedArrayOf cannot work correcly on Wasm. 25 // See https://github.com/golang/go/issues/31980 26 27 s := make([]byte, value.Get("byteLength").Int()) 28 a := js.TypedArrayOf(s) 29 a.Call("set", value) 30 a.Release() 31 return s 32 } 33 34 func ArrayBufferToSlice(value js.Value) []byte { 35 return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value)) 36 } 37 38 func CopySliceToJS(dst js.Value, src interface{}) { 39 // Note that TypedArrayOf cannot work correcly on Wasm. 40 // See https://github.com/golang/go/issues/31980 41 42 a := js.TypedArrayOf(src) 43 dst.Call("set", a) 44 a.Release() 45 }