From 2fcd2a4b326f386a3689e40f7f717496cd03e72e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 28 Dec 2017 18:05:00 +1100 Subject: src/lauxlib.js: synchronous xhr is allowed in workers. use arraybuffer reponse type where possible --- src/lauxlib.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/lauxlib.js') diff --git a/src/lauxlib.js b/src/lauxlib.js index 5687242..51c5749 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -746,16 +746,22 @@ if (typeof process === "undefined") { throw new Error("Can't read stdin in the browser"); } else { lua.lua_pushfstring(L, lua.to_luastring("@%s"), filename); - let path = lua.to_uristring(filename); let xhr = new XMLHttpRequest(); xhr.open("GET", path, false); - // TODO: find a way to load bytes instead of js string + /* XXX: Synchronous xhr in main thread always returns a js string + Additionally, some browsers make console noise if you even attempt to set responseType + */ + if (typeof window === "undefined") { + xhr.responseType = "arraybuffer"; + } xhr.send(); - if (xhr.status >= 200 && xhr.status <= 299) { - /* TODO: Synchronous xhr alway return a js string */ - lf.f = lua.to_luastring(xhr.response); + if (typeof xhr.response === "string") { + lf.f = lua.to_luastring(xhr.response); + } else { + lf.f = new Uint8Array(xhr.response); + } } else { lf.err = xhr.status; return errfile(L, "open", fnameindex, { message: `${xhr.status}: ${xhr.statusText}` }); -- cgit v1.2.3-54-g00ecf