diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-08-09 08:44:52 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-08-10 09:10:10 +0200 |
commit | 7729b811e5d0523b3ad613284c8674876f0d8321 (patch) | |
tree | 78417d69381405a9a9cd1c0c03363f2137c9b803 | |
parent | 976ddcc997f1bc299c2e1e2b23b4ff79e7bbfcb3 (diff) | |
download | fengari-7729b811e5d0523b3ad613284c8674876f0d8321.tar.gz fengari-7729b811e5d0523b3ad613284c8674876f0d8321.tar.bz2 fengari-7729b811e5d0523b3ad613284c8674876f0d8321.zip |
loadfile and dofile are available in the browser
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | src/lbaselib.js | 52 |
2 files changed, 23 insertions, 34 deletions
@@ -30,11 +30,6 @@ In the browser `require` and `package.loadlib` try to find a file by making sync - `lua_gc`/`collectgarbage`: Fengari relies on the JS garbage collector and does not implement its own. - The following functions are only available in Node: - - `luaL_dofile` - - `luaL_loadfilex` - - `luaL_loadfile` - - `loadfile` - - `dofile` - The entire `io` lib - `os.remove` - `os.rename` diff --git a/src/lbaselib.js b/src/lbaselib.js index b2a9951..6947f94 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -333,13 +333,36 @@ const luaB_load = function(L) { return load_aux(L, status, env); }; +const luaB_loadfile = function(L) { + let fname = lauxlib.luaL_optstring(L, 1, null); + let mode = lauxlib.luaL_optstring(L, 2, null); + let env = !lua.lua_isnone(L, 3) ? 3 : 0; /* 'env' index or 0 if no 'env' */ + let status = lauxlib.luaL_loadfilex(L, fname, mode); + return load_aux(L, status, env); +}; + +const dofilecont = function(L, d1, d2) { + return lua.lua_gettop(L) - 1; +}; + +const luaB_dofile = function(L) { + let fname = lauxlib.luaL_optstring(L, 1, null); + lua.lua_settop(L, 1); + if (lauxlib.luaL_loadfile(L, fname) !== lua.LUA_OK) + return lua.lua_error(L); + lua.lua_callk(L, 0, lua.LUA_MULTRET, 0, dofilecont); + return dofilecont(L, 0, 0); +}; + const base_funcs = { "assert": luaB_assert, "collectgarbage": luaB_collectgarbage, + "dofile": luaB_dofile, "error": luaB_error, "getmetatable": luaB_getmetatable, "ipairs": luaB_ipairs, "load": luaB_load, + "loadfile": luaB_loadfile, "next": luaB_next, "pairs": luaB_pairs, "pcall": luaB_pcall, @@ -356,35 +379,6 @@ const base_funcs = { "xpcall": luaB_xpcall }; -// Only with Node -if (!WEB) { - - const luaB_loadfile = function(L) { - let fname = lauxlib.luaL_optstring(L, 1, null); - let mode = lauxlib.luaL_optstring(L, 2, null); - let env = !lua.lua_isnone(L, 3) ? 3 : 0; /* 'env' index or 0 if no 'env' */ - let status = lauxlib.luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); - }; - - const dofilecont = function(L, d1, d2) { - return lua.lua_gettop(L) - 1; - }; - - const luaB_dofile = function(L) { - let fname = lauxlib.luaL_optstring(L, 1, null); - lua.lua_settop(L, 1); - if (lauxlib.luaL_loadfile(L, fname) !== lua.LUA_OK) - return lua.lua_error(L); - lua.lua_callk(L, 0, lua.LUA_MULTRET, 0, dofilecont); - return dofilecont(L, 0, 0); - }; - - base_funcs.loadfile = luaB_loadfile; - base_funcs.dofile = luaB_dofile; - -} - const luaopen_base = function(L) { /* open lib into global table */ lua.lua_pushglobaltable(L); |