summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/defs.js21
-rw-r--r--src/fengaricore.js2
-rw-r--r--src/lauxlib.js23
-rw-r--r--src/liolib.js7
-rw-r--r--src/loadlib.js2
-rw-r--r--src/loslib.js14
-rw-r--r--src/luaconf.js11
7 files changed, 49 insertions, 31 deletions
diff --git a/src/defs.js b/src/defs.js
index 98269ea..daa5f70 100644
--- a/src/defs.js
+++ b/src/defs.js
@@ -56,6 +56,7 @@ const luastring_eq = function(a, b) {
return true;
};
+const unicode_error_message = "cannot convert invalid utf8 to javascript string";
const to_jsstring = function(value, from, to, replacement_char) {
if (!is_luastring(value)) throw new TypeError("to_jsstring expects a Uint8Array");
@@ -72,18 +73,18 @@ const to_jsstring = function(value, from, to, replacement_char) {
/* single byte sequence */
str += String.fromCharCode(u0);
} else if (u0 < 0xC2 || u0 > 0xF4) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
} else if (u0 <= 0xDF) {
/* two byte sequence */
if (i >= to) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u1 = value[i++];
if ((u1&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
@@ -91,19 +92,19 @@ const to_jsstring = function(value, from, to, replacement_char) {
} else if (u0 <= 0xEF) {
/* three byte sequence */
if (i+1 >= to) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u1 = value[i++];
if ((u1&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u2 = value[i++];
if ((u2&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
@@ -119,25 +120,25 @@ const to_jsstring = function(value, from, to, replacement_char) {
} else {
/* four byte sequence */
if (i+2 >= to) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u1 = value[i++];
if ((u1&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u2 = value[i++];
if ((u2&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
let u3 = value[i++];
if ((u3&0xC0) !== 0x80) {
- if (!replacement_char) throw RangeError("cannot convert invalid utf8 to javascript string");
+ if (!replacement_char) throw RangeError(unicode_error_message);
str += "�";
continue;
}
diff --git a/src/fengaricore.js b/src/fengaricore.js
index c34b560..1335ba8 100644
--- a/src/fengaricore.js
+++ b/src/fengaricore.js
@@ -11,7 +11,7 @@ const defs = require("./defs.js");
const FENGARI_VERSION_MAJOR = "0";
const FENGARI_VERSION_MINOR = "1";
const FENGARI_VERSION_NUM = 1;
-const FENGARI_VERSION_RELEASE = "0";
+const FENGARI_VERSION_RELEASE = "1";
const FENGARI_VERSION = "Fengari " + FENGARI_VERSION_MAJOR + "." + FENGARI_VERSION_MINOR;
const FENGARI_RELEASE = FENGARI_VERSION + "." + FENGARI_VERSION_RELEASE;
const FENGARI_AUTHORS = "B. Giannangeli, Daurnimator";
diff --git a/src/lauxlib.js b/src/lauxlib.js
index d575e99..2276bd0 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -290,11 +290,19 @@ const luaL_fileresult = function(L, stat, fname, e) {
return 1;
} else {
lua_pushnil(L);
+ let message, errno;
+ if (e) {
+ message = e.message;
+ errno = -e.errno;
+ } else {
+ message = "Success"; /* what strerror(0) returns */
+ errno = 0;
+ }
if (fname)
- lua_pushfstring(L, to_luastring("%s: %s"), fname, to_luastring(e.message));
+ lua_pushfstring(L, to_luastring("%s: %s"), fname, to_luastring(message));
else
- lua_pushstring(L, to_luastring(e.message));
- lua_pushinteger(L, -e.errno);
+ lua_pushstring(L, to_luastring(message));
+ lua_pushinteger(L, errno);
return 3;
}
};
@@ -850,8 +858,9 @@ if (typeof process === "undefined") {
let path = to_uristring(filename);
let xhr = new XMLHttpRequest();
xhr.open("GET", path, false);
- /* XXX: Synchronous xhr in main thread always returns a js string
- Additionally, some browsers make console noise if you even attempt to set responseType
+ /*
+ Synchronous xhr in main thread always returns a js string.
+ Some browsers make console noise if you even attempt to set responseType
*/
if (typeof window === "undefined") {
xhr.responseType = "arraybuffer";
@@ -871,7 +880,7 @@ if (typeof process === "undefined") {
let com = skipcomment(lf);
/* check for signature first, as we don't want to add line number corrections in binary case */
if (com.c === LUA_SIGNATURE[0] && filename) { /* binary file? */
- /* no need to re-open in node.js */
+ /* no need to re-open */
} else if (com.skipped) { /* read initial portion */
lf.buff[lf.n++] = 10 /* '\n'.charCodeAt(0) */; /* add line to correct line numbers */
}
@@ -949,7 +958,7 @@ if (typeof process === "undefined") {
let com = skipcomment(lf);
/* check for signature first, as we don't want to add line number corrections in binary case */
if (com.c === LUA_SIGNATURE[0] && filename) { /* binary file? */
- /* no need to re-open in node.js */
+ /* no need to re-open */
} else if (com.skipped) { /* read initial portion */
lf.buff[lf.n++] = 10 /* '\n'.charCodeAt(0) */; /* add line to correct line numbers */
}
diff --git a/src/liolib.js b/src/liolib.js
index bd78f87..92dda58 100644
--- a/src/liolib.js
+++ b/src/liolib.js
@@ -130,6 +130,11 @@ const io_output = function(L) {
return g_iofile(L, IO_OUTPUT, "w");
};
+/* node <= 6 doesn't support passing a Uint8Array to fs.writeSync */
+const prepare_string_for_write = process.versions.node > 6 ?
+ (s) => s : // identity function
+ (s) => Buffer.from(s.buffer, s.byteOffset, s.byteLength);
+
const g_write = function(L, f, arg) {
let nargs = lua_gettop(L) - arg;
let status = true;
@@ -137,7 +142,7 @@ const g_write = function(L, f, arg) {
for (; nargs--; arg++) {
let s = luaL_checklstring(L, arg);
try {
- status = status && (fs.writeSync(f.fd, Uint8Array.from(s)) === s.length);
+ status = status && (fs.writeSync(f.fd, prepare_string_for_write(s), 0, s.length) === s.length);
} catch (e) {
status = false;
err = e;
diff --git a/src/loadlib.js b/src/loadlib.js
index f6679ba..49cfc5a 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -85,7 +85,7 @@ const global_env = (function() {
return self;
} else {
/* unknown global env */
- return eval('this'); /* use non-strict mode to get global env */
+ return (0, eval)('this'); /* use non-strict mode to get global env */
}
})();
diff --git a/src/loslib.js b/src/loslib.js
index 6fec903..6c6b2f4 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -251,13 +251,17 @@ if (typeof process === "undefined") {
syslib.remove = function(L) {
let filename = luaL_checkstring(L, 1);
try {
- if (fs.lstatSync(filename).isDirectory()) {
- fs.rmdirSync(filename);
+ fs.unlinkSync(filename);
+ } catch (e) {
+ if (e.code === 'EISDIR') {
+ try {
+ fs.rmdirSync(filename);
+ } catch (e) {
+ return luaL_fileresult(L, false, filename, e);
+ }
} else {
- fs.unlinkSync(filename);
+ return luaL_fileresult(L, false, filename, e);
}
- } catch (e) {
- return luaL_fileresult(L, false, filename, e);
}
return luaL_fileresult(L, true);
};
diff --git a/src/luaconf.js b/src/luaconf.js
index 7a6fe40..387e813 100644
--- a/src/luaconf.js
+++ b/src/luaconf.js
@@ -43,12 +43,12 @@ if (typeof process === "undefined") {
const LUA_LDIR = "./lua/" + LUA_VDIR + "/";
module.exports.LUA_LDIR = LUA_LDIR;
- const LUA_JSDIR = "./lua/" + LUA_VDIR + "/";
+ const LUA_JSDIR = LUA_LDIR;
module.exports.LUA_JSDIR = LUA_JSDIR;
const LUA_PATH_DEFAULT = to_luastring(
LUA_LDIR + "?.lua;" + LUA_LDIR + "?/init.lua;" +
- LUA_JSDIR + "?.lua;" + LUA_JSDIR + "?/init.lua;" +
+ /* LUA_JSDIR excluded as it is equal to LUA_LDIR */
"./?.lua;./?/init.lua"
);
module.exports.LUA_PATH_DEFAULT = LUA_PATH_DEFAULT;
@@ -100,15 +100,14 @@ if (typeof process === "undefined") {
const LUA_LDIR2 = LUA_ROOT2 + "share/lua/" + LUA_VDIR + "/";
module.exports.LUA_LDIR = LUA_LDIR;
- const LUA_JSDIR = LUA_ROOT + "share/lua/" + LUA_VDIR + "/";
+ const LUA_JSDIR = LUA_LDIR;
module.exports.LUA_JSDIR = LUA_JSDIR;
- const LUA_JSDIR2 = LUA_ROOT2 + "share/lua/" + LUA_VDIR + "/";
+ const LUA_JSDIR2 = LUA_LDIR2;
const LUA_PATH_DEFAULT = to_luastring(
LUA_LDIR + "?.lua;" + LUA_LDIR + "?/init.lua;" +
LUA_LDIR2 + "?.lua;" + LUA_LDIR2 + "?/init.lua;" +
- LUA_JSDIR + "?.lua;" + LUA_JSDIR + "?/init.lua;" +
- LUA_JSDIR2 + "?.lua;" + LUA_JSDIR2 + "?/init.lua;" +
+ /* LUA_JSDIR(2) excluded as it is equal to LUA_LDIR(2) */
"./?.lua;./?/init.lua"
);
module.exports.LUA_PATH_DEFAULT = LUA_PATH_DEFAULT;