diff options
| author | Benoit Giannangeli <giann008@gmail.com> | 2017-08-02 08:03:33 +0200 | 
|---|---|---|
| committer | Benoit Giannangeli <giann008@gmail.com> | 2017-08-10 08:49:23 +0200 | 
| commit | e27371519c9674e9891f8dbdf4413b98ab567026 (patch) | |
| tree | 22f474faee86c007b52a74991e48cf31ef15d31b /src/loadlib.js | |
| parent | 10ce504e5defe8411f089c796dea370e9e14d014 (diff) | |
| download | fengari-e27371519c9674e9891f8dbdf4413b98ab567026.tar.gz fengari-e27371519c9674e9891f8dbdf4413b98ab567026.tar.bz2 fengari-e27371519c9674e9891f8dbdf4413b98ab567026.zip | |
LoadF is the same in the browser and in node
Diffstat (limited to 'src/loadlib.js')
| -rw-r--r-- | src/loadlib.js | 61 | 
1 files changed, 29 insertions, 32 deletions
| diff --git a/src/loadlib.js b/src/loadlib.js index 9400bb0..ecb5cd2 100644 --- a/src/loadlib.js +++ b/src/loadlib.js @@ -34,20 +34,36 @@ const AUXMARK       = [1];  ** Returns the library; in case of error, returns NULL plus an  ** error string in the stack.  */ -let lsys_load = function(L, path) { -    try { -        path = lua.to_jsstring(path); +let lsys_load; +if (WEB) { +    lsys_load = function(L, path) { +        let xhr = new XMLHttpRequest(); +        xhr.open("GET", lua.to_jsstring(path), false); +        xhr.send(); -        // Relative path ? -        if (path.startsWith('.')) -            path = `${process.env.PWD}/${path}`; +        if (xhr.status >= 200 && xhr.status <= 299) { +            return eval(xhr.response); +        } else { +            lua.lua_pushstring(L, lua.to_luastring(`${xhr.status}: ${xhr.statusText}`)); +            return null; +        } +    }; +} else { +    lsys_load = function(L, path) { +        try { +            path = lua.to_jsstring(path); -        return require(path); -    } catch (e) { -        lua.lua_pushstring(L, lua.to_luastring(e.message)); -        return null; -    } -}; +            // Relative path ? +            if (path.startsWith('.')) +                path = `${process.env.PWD}/${path}`; + +            return require(path); +        } catch (e) { +            lua.lua_pushstring(L, lua.to_luastring(e.message)); +            return null; +        } +    }; +}  /*  ** Try to find a function named 'sym' in library 'lib'. @@ -65,22 +81,6 @@ 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  */ @@ -91,9 +91,7 @@ const noenv = function(L) {      return b;  }; -let readable = function() { -    return false; -}; +let readable;  // Only with Node  if (!WEB) { @@ -118,7 +116,6 @@ if (!WEB) {          /* Following GET request done by searcher_Web will be cached */          xhr.open("GET", lua.to_jsstring(filename), false);          xhr.send(); -        /* TODO: subresource integrity check? */          return xhr.status >= 200 && xhr.status <= 299;      }; | 
