aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-22 21:21:50 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-22 23:17:22 +1000
commite4c9580d20924a0db1ff7ed0d30da9b71dbb5066 (patch)
tree67b660ec32e31282d0cf305b178bff1da3baba39 /src
parent7e886ba08a443d9653c3033901ae8c83108d3701 (diff)
downloadfengari-e4c9580d20924a0db1ff7ed0d30da9b71dbb5066.tar.gz
fengari-e4c9580d20924a0db1ff7ed0d30da9b71dbb5066.tar.bz2
fengari-e4c9580d20924a0db1ff7ed0d30da9b71dbb5066.zip
Introduce lvm.cvt2str
In future this could be configurable
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js16
-rw-r--r--src/ldebug.js2
-rw-r--r--src/lvm.js9
3 files changed, 19 insertions, 8 deletions
diff --git a/src/lapi.js b/src/lapi.js
index f06fc65..f7ef82e 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -667,8 +667,11 @@ const lua_toboolean = function(L, idx) {
const lua_tolstring = function(L, idx) {
let o = index2addr(L, idx);
- if ((!o.ttisstring() && !o.ttisnumber()))
- return null;
+ if (!o.ttisstring()) {
+ if (!lvm.cvt2str(o)) { /* not convertible? */
+ return null;
+ }
+ }
return o.ttisstring() ? o.svalue() : defs.to_luastring(`${o.value}`);
};
@@ -678,8 +681,11 @@ const lua_tostring = lua_tolstring;
const lua_toljsstring = function(L, idx) {
let o = index2addr(L, idx);
- if ((!o.ttisstring() && !o.ttisnumber()))
- return null;
+ if (!o.ttisstring()) {
+ if (!lvm.cvt2str(o)) { /* not convertible? */
+ return null;
+ }
+ }
return o.ttisstring() ? o.jsstring() : `${o.value}`;
};
@@ -872,7 +878,7 @@ const lua_isnumber = function(L, idx) {
const lua_isstring = function(L, idx) {
let o = index2addr(L, idx);
- return o.ttisstring() || o.ttisnumber();
+ return o.ttisstring() || lvm.cvt2str(o);
};
const lua_isuserdata = function(L, idx) {
diff --git a/src/ldebug.js b/src/ldebug.js
index 1862e55..74a04db 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -546,7 +546,7 @@ const luaG_typeerror = function(L, o, op) {
};
const luaG_concaterror = function(L, p1, p2) {
- if (p1.ttisstring() || p1.ttisnumber()) p1 = p2;
+ if (p1.ttisstring() || lvm.cvt2str(p1)) p1 = p2;
luaG_typeerror(L, p1, defs.to_luastring('concatenate', true));
};
diff --git a/src/lvm.js b/src/lvm.js
index 4804cde..bd370a6 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -958,12 +958,16 @@ const luaV_shiftl = function(x, y) {
}
};
+const cvt2str = function(o) {
+ return o.ttisnumber();
+};
+
const tostring = function(L, i) {
let o = L.stack[i];
if (o.ttisstring()) return true;
- if (o.ttisnumber() && !isNaN(o.value)) {
+ if (cvt2str(o) && !isNaN(o.value)) {
L.stack[i] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, defs.to_luastring(`${o.value}`)));
return true;
}
@@ -985,7 +989,7 @@ const luaV_concat = function(L, total) {
let top = L.top;
let n = 2; /* number of elements handled in this pass (at least 2) */
- if (!(L.stack[top-2].ttisstring() || L.stack[top-2].ttisnumber()) || !tostring(L, top - 1)) {
+ if (!(L.stack[top-2].ttisstring() || cvt2str(L.stack[top-2])) || !tostring(L, top - 1)) {
ltm.luaT_trybinTM(L, L.stack[top-2], L.stack[top-1], top-2, ltm.TMS.TM_CONCAT);
delete L.stack[top - 1];
} else if (isemptystr(L.stack[top-1])) {
@@ -1090,6 +1094,7 @@ module.exports.RB = RB;
module.exports.RC = RC;
module.exports.RKB = RKB;
module.exports.RKC = RKC;
+module.exports.cvt2str = cvt2str;
module.exports.dojump = dojump;
module.exports.donextjump = donextjump;
module.exports.forlimit = forlimit;