aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-08-02 08:40:27 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-08-10 09:10:08 +0200
commit79e59c208d4624cc63c787cbcf4c989f2aeaaf60 (patch)
tree7eb3a96684f255dd14442085e52fcebeb17485db
parente27371519c9674e9891f8dbdf4413b98ab567026 (diff)
downloadfengari-79e59c208d4624cc63c787cbcf4c989f2aeaaf60.tar.gz
fengari-79e59c208d4624cc63c787cbcf4c989f2aeaaf60.tar.bz2
fengari-79e59c208d4624cc63c787cbcf4c989f2aeaaf60.zip
LoadF: buff is an Array in the browser, use pre-read chars
-rw-r--r--src/lauxlib.js18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 6489dc7..9a6666a 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -702,7 +702,7 @@ 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.buff = WEB ? new Array(1024) : new Buffer(1024); /* area for reading file */
this.pos = 0; /* current position in file */
this.err = void 0;
}
@@ -711,6 +711,14 @@ class LoadF {
if (WEB) {
const getF = function(L, ud) {
let lf = ud;
+
+ if (lf.f !== null && lf.n > 0) { /* are there pre-read characters to be read? */
+ let bytes = lf.n; /* return them (chars already in buffer) */
+ lf.n = 0; /* no more pre-read characters */
+ lf.f = lf.f.slice(lf.pos); /* we won't use lf.buff anymore */
+ return lf.buff.slice(0, bytes);
+ }
+
let f = lf.f;
lf.f = null;
return f;
@@ -726,9 +734,9 @@ if (WEB) {
if (filename === null) {
throw new Error("Can't read stdin in the browser");
} else {
- let jsfilename = lua.to_jsstring(filename);
- lua.lua_pushfstring(L, "@%s", filename);
+ lua.lua_pushfstring(L, lua.to_luastring("@%s"), filename);
+ let jsfilename = lua.to_jsstring(filename);
let xhr = new XMLHttpRequest();
xhr.open("GET", jsfilename, false);
// TODO: find a way to load bytes instead of js string
@@ -798,9 +806,9 @@ if (WEB) {
lua.lua_pushliteral(L, "=stdin");
lf.f = process.stdin.fd;
} else {
- let jsfilename = lua.to_jsstring(filename);
- lua.lua_pushfstring(L, "@%s", filename);
+ lua.lua_pushfstring(L, lua.to_luastring("@%s"), filename);
try {
+ let jsfilename = lua.to_jsstring(filename);
lf.f = fs.openSync(jsfilename, "r");
if (!fs.fstatSync(lf.f).isFile())
throw new Error(`${jsfilename} is not a readable file`);