From 10ce504e5defe8411f089c796dea370e9e14d014 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 9 Aug 2017 10:35:45 +0200 Subject: luaL_loadfile will load via xhr in the browser --- src/lauxlib.js | 2 +- src/loadlib.js | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lauxlib.js b/src/lauxlib.js index d13b2bb..eb174be 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -816,7 +816,7 @@ if (!WEB) { lf.f = lua.to_luastring(xhr.response); } else { lf.err = xhr.status; - return errfile(L, "open", fnameindex, xhr.status); + return errfile(L, "open", fnameindex, { message: `${xhr.status}: ${xhr.statusText}` }); } } let com = skipcomment(lf); diff --git a/src/loadlib.js b/src/loadlib.js index e448381..9400bb0 100644 --- a/src/loadlib.js +++ b/src/loadlib.js @@ -34,7 +34,7 @@ const AUXMARK = [1]; ** Returns the library; in case of error, returns NULL plus an ** error string in the stack. */ -const lsys_load = function(L, path) { +let lsys_load = function(L, path) { try { path = lua.to_jsstring(path); @@ -65,6 +65,22 @@ const lsys_sym = function(L, lib, sym) { } }; +if (WEB) { + lsys_load = function(L, path) { + let xhr = new XMLHttpRequest(); + xhr.open("GET", lua.to_jsstring(path), false); + xhr.send(); + /* TODO: subresource integrity check? */ + + if (xhr.status === 200) { + return eval(xhr.response); + } else { + lua.lua_pushstring(L, lua.to_luastring(`${xhr.status}: ${xhr.statusText}`)); + return null; + } + }; +} + /* ** return registry.LUA_NOENV as a boolean */ -- cgit v1.2.3-70-g09d2