diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-05-09 11:36:14 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-05-09 11:36:14 +0200 |
commit | 697ef457e1b0eabd3aa9fb2332a5d04478b39603 (patch) | |
tree | 4d93ec7e3d1cc698a5c54de9785d688bf510ed15 /src/lauxlib.js | |
parent | 3e439ed653093e6e124a4997f64053164f1043b8 (diff) | |
download | fengari-697ef457e1b0eabd3aa9fb2332a5d04478b39603.tar.gz fengari-697ef457e1b0eabd3aa9fb2332a5d04478b39603.tar.bz2 fengari-697ef457e1b0eabd3aa9fb2332a5d04478b39603.zip |
Webpack (single bundle for now)
Diffstat (limited to 'src/lauxlib.js')
-rw-r--r-- | src/lauxlib.js | 257 |
1 files changed, 126 insertions, 131 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js index c0f1ecf..4f55e13 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -617,149 +617,144 @@ const luaL_newlib = function(L, l) { }; // Only with Node -if (typeof require === "function") { - let fs = false; - try { - fs = require('fs'); - } catch (e) {} - - if (fs) { - class LoadF { - constructor() { - this.n = NaN; /* number of pre-read characters */ - this.f = null; /* file being read */ - this.buff = new Buffer(1024); /* area for reading file */ - this.pos = 0; /* current position in file */ - this.binary = false; - } +if (!WEB) { + const fs = require('fs'); + + class LoadF { + constructor() { + this.n = NaN; /* number of pre-read characters */ + this.f = null; /* file being read */ + this.buff = new Buffer(1024); /* area for reading file */ + this.pos = 0; /* current position in file */ + this.binary = false; } + } - const toDataView = function(buffer) { - let ab = new ArrayBuffer(buffer.length); - let au = new Uint8Array(ab); - for (let i = 0; i < buffer.length; i++) - au[i] = buffer[i]; - return new DataView(ab); - }; - - const getF = function(L, ud) { - let lf = ud; - let bytes = 0; - if (lf.n > 0) { /* are there pre-read characters to be read? */ - lf.n = 0; /* no more pre-read characters */ - } else { /* read a block from file */ - lf.buff.fill(0); - bytes = fs.readSync(lf.f, lf.buff, 0, lf.buff.length, lf.pos); /* read block */ - lf.pos += bytes; - } - if (bytes > 0) - return lf.binary ? toDataView(lf.buff) : lf.buff.slice(0, bytes); - else return null; - }; - - const errfile = function(L, what, fnameindex, error) { - let serr = error.message; - let filename = lua.lua_tostring(L, fnameindex).slice(1); - lua.lua_pushstring(L, lua.to_luastring(`cannot ${what} ${lua.to_jsstring(filename)}: ${serr}`)); - lua.lua_remove(L, fnameindex); - return lua.LUA_ERRFILE; - }; - - const getc = function(lf) { - let b = new Buffer(1); - let bytes = fs.readSync(lf.f, b, 0, 1, lf.pos); + const toDataView = function(buffer) { + let ab = new ArrayBuffer(buffer.length); + let au = new Uint8Array(ab); + for (let i = 0; i < buffer.length; i++) + au[i] = buffer[i]; + return new DataView(ab); + }; + + const getF = function(L, ud) { + let lf = ud; + let bytes = 0; + if (lf.n > 0) { /* are there pre-read characters to be read? */ + lf.n = 0; /* no more pre-read characters */ + } else { /* read a block from file */ + lf.buff.fill(0); + bytes = fs.readSync(lf.f, lf.buff, 0, lf.buff.length, lf.pos); /* read block */ lf.pos += bytes; - return bytes > 0 ? b.readUInt8() : null; - }; - - const skipBOM = function(lf) { - let p = [0XEF, 0XBB, 0XBF]; /* UTF-8 BOM mark */ - lf.n = 0; - let c; - do { + } + if (bytes > 0) + return lf.binary ? toDataView(lf.buff) : lf.buff.slice(0, bytes); + else return null; + }; + + const errfile = function(L, what, fnameindex, error) { + let serr = error.message; + let filename = lua.lua_tostring(L, fnameindex).slice(1); + lua.lua_pushstring(L, lua.to_luastring(`cannot ${what} ${lua.to_jsstring(filename)}: ${serr}`)); + lua.lua_remove(L, fnameindex); + return lua.LUA_ERRFILE; + }; + + const getc = function(lf) { + let b = new Buffer(1); + let bytes = fs.readSync(lf.f, b, 0, 1, lf.pos); + lf.pos += bytes; + return bytes > 0 ? b.readUInt8() : null; + }; + + const skipBOM = function(lf) { + let p = [0XEF, 0XBB, 0XBF]; /* UTF-8 BOM mark */ + lf.n = 0; + let c; + do { + c = getc(lf); + if (c === null || c !== p[0]) return c; + p = p.slice(1); + lf.buff[lf.n++] = c; /* to be read by the parser */ + } while (p.length > 0); + lf.n = 0; /* prefix matched; discard it */ + return getc(lf); /* return next character */ + }; + + /* + ** reads the first character of file 'f' and skips an optional BOM mark + ** in its beginning plus its first line if it starts with '#'. Returns + ** true if it skipped the first line. In any case, '*cp' has the + ** first "valid" character of the file (after the optional BOM and + ** a first-line comment). + */ + const skipcomment = function(lf) { + let c = skipBOM(lf); + if (c === '#'.charCodeAt(0)) { /* first line is a comment (Unix exec. file)? */ + do { /* skip first line */ c = getc(lf); - if (c === null || c !== p[0]) return c; - p = p.slice(1); - lf.buff[lf.n++] = c; /* to be read by the parser */ - } while (p.length > 0); - lf.n = 0; /* prefix matched; discard it */ - return getc(lf); /* return next character */ - }; - - /* - ** reads the first character of file 'f' and skips an optional BOM mark - ** in its beginning plus its first line if it starts with '#'. Returns - ** true if it skipped the first line. In any case, '*cp' has the - ** first "valid" character of the file (after the optional BOM and - ** a first-line comment). - */ - const skipcomment = function(lf) { - let c = skipBOM(lf); - if (c === '#'.charCodeAt(0)) { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(lf); - } while (c && c !== '\n'.charCodeAt(0)); - - return { - skipped: true, - c: getc(lf) /* skip end-of-line, if present */ - }; - } else { - lf.pos--; - return { - skipped: false, - c: c - }; - } - }; - - const luaL_loadfilex = function(L, filename, mode) { - let lf = new LoadF(); - let fnameindex = lua.lua_gettop(L) + 1; /* index of filename on the stack */ - if (filename === null) { - lua.lua_pushliteral(L, "=stdin"); - lf.f = process.stdin.fd; - } else { - let jsfilename = lua.to_jsstring(filename); - lua.lua_pushliteral(L, `@${jsfilename}`); - try { - lf.f = fs.openSync(jsfilename, "r"); - } catch (e) { - return errfile(L, "open", fnameindex, e); - } - } + } while (c && c !== '\n'.charCodeAt(0)); + return { + skipped: true, + c: getc(lf) /* skip end-of-line, if present */ + }; + } else { + lf.pos--; + return { + skipped: false, + c: c + }; + } + }; + + const luaL_loadfilex = function(L, filename, mode) { + let lf = new LoadF(); + let fnameindex = lua.lua_gettop(L) + 1; /* index of filename on the stack */ + if (filename === null) { + lua.lua_pushliteral(L, "=stdin"); + lf.f = process.stdin.fd; + } else { + let jsfilename = lua.to_jsstring(filename); + lua.lua_pushliteral(L, `@${jsfilename}`); try { - let com; - if ((com = skipcomment(lf)).skipped) /* read initial portion */ - lf.buff[lf.n++] = '\n'.charCodeAt(0); /* add line to correct line numbers */ + lf.f = fs.openSync(jsfilename, "r"); + } catch (e) { + return errfile(L, "open", fnameindex, e); + } + } - if (com.c === lua.LUA_SIGNATURE.charCodeAt(0) && filename) { /* binary file? */ - lf.binary = true; - } + try { + let com; + if ((com = skipcomment(lf)).skipped) /* read initial portion */ + lf.buff[lf.n++] = '\n'.charCodeAt(0); /* add line to correct line numbers */ - let status = lua.lua_load(L, getF, lf, lua.lua_tostring(L, -1), mode); - if (filename) fs.closeSync(lf.f); /* close file (even in case of errors) */ - lua.lua_remove(L, fnameindex); - return status; - } catch (err) { - lua.lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ - return errfile(L, "read", fnameindex, err); + if (com.c === lua.LUA_SIGNATURE.charCodeAt(0) && filename) { /* binary file? */ + lf.binary = true; } - }; - const luaL_loadfile = function(L, filename) { - return luaL_loadfilex(L, filename, null); - }; + let status = lua.lua_load(L, getF, lf, lua.lua_tostring(L, -1), mode); + if (filename) fs.closeSync(lf.f); /* close file (even in case of errors) */ + lua.lua_remove(L, fnameindex); + return status; + } catch (err) { + lua.lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ + return errfile(L, "read", fnameindex, err); + } + }; - const luaL_dofile = function(L, filename) { - return (luaL_loadfile(L, filename) || lua.lua_pcall(L, 0, lua.LUA_MULTRET, 0)); - }; + const luaL_loadfile = function(L, filename) { + return luaL_loadfilex(L, filename, null); + }; - module.exports.luaL_dofile = luaL_dofile; - module.exports.luaL_loadfilex = luaL_loadfilex; - module.exports.luaL_loadfile = luaL_loadfile; - } + const luaL_dofile = function(L, filename) { + return (luaL_loadfile(L, filename) || lua.lua_pcall(L, 0, lua.LUA_MULTRET, 0)); + }; + + module.exports.luaL_dofile = luaL_dofile; + module.exports.luaL_loadfilex = luaL_loadfilex; + module.exports.luaL_loadfile = luaL_loadfile; } const lua_writestringerror = function(s) { |