mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
ebitenutil: Fix OpenFile() so it works with wasm (#674)
Fixes #673 * Change to use `js.NewCallback` * Use `uint8contentWrapper := js.Global().Get("Uint8Array").New(content)`, without this all the bytes in `data` stays zeroed. (TypedArray cannot use `set` on an ArrayBuffer)
This commit is contained in:
parent
c25f5b03e0
commit
2938afd82a
@ -39,7 +39,7 @@ func OpenFile(path string) (ReadSeekCloser, error) {
|
|||||||
req := js.Global().Get("XMLHttpRequest").New()
|
req := js.Global().Get("XMLHttpRequest").New()
|
||||||
req.Call("open", "GET", path, true)
|
req.Call("open", "GET", path, true)
|
||||||
req.Set("responseType", "arraybuffer")
|
req.Set("responseType", "arraybuffer")
|
||||||
req.Call("addEventListener", "load", func() {
|
loadCallback := js.NewCallback(func([]js.Value) {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
status := req.Get("status").Int()
|
status := req.Get("status").Int()
|
||||||
if 200 <= status && status < 400 {
|
if 200 <= status && status < 400 {
|
||||||
@ -48,19 +48,24 @@ func OpenFile(path string) (ReadSeekCloser, error) {
|
|||||||
}
|
}
|
||||||
err = errors.New(fmt.Sprintf("http error: %d", status))
|
err = errors.New(fmt.Sprintf("http error: %d", status))
|
||||||
})
|
})
|
||||||
req.Call("addEventListener", "error", func() {
|
defer loadCallback.Release()
|
||||||
|
req.Call("addEventListener", "load", loadCallback)
|
||||||
|
errorCallback := js.NewCallback(func([]js.Value) {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
err = errors.New(fmt.Sprintf("XMLHttpRequest error: %s", req.Get("statusText").String()))
|
err = errors.New(fmt.Sprintf("XMLHttpRequest error: %s", req.Get("statusText").String()))
|
||||||
})
|
})
|
||||||
|
req.Call("addEventListener", "error", errorCallback)
|
||||||
|
defer errorCallback.Release()
|
||||||
req.Call("send")
|
req.Call("send")
|
||||||
<-ch
|
<-ch
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := make([]byte, content.Get("byteLength").Int())
|
uint8contentWrapper := js.Global().Get("Uint8Array").New(content)
|
||||||
|
data := make([]byte, uint8contentWrapper.Get("byteLength").Int())
|
||||||
arr := js.TypedArrayOf(data)
|
arr := js.TypedArrayOf(data)
|
||||||
arr.Call("set", content)
|
arr.Call("set", uint8contentWrapper)
|
||||||
arr.Release()
|
arr.Release()
|
||||||
f := &file{bytes.NewReader(data)}
|
f := &file{bytes.NewReader(data)}
|
||||||
return f, nil
|
return f, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user