summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-22 21:32:41 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-22 23:17:25 +1000
commit4f4e76d8de5fea7e636bdf5b5e271a2216531345 (patch)
tree8abdf0fcd550506a1d6af6f2e160d4fef3d08e8c /src
parente4c9580d20924a0db1ff7ed0d30da9b71dbb5066 (diff)
downloadfengari-4f4e76d8de5fea7e636bdf5b5e271a2216531345.tar.gz
fengari-4f4e76d8de5fea7e636bdf5b5e271a2216531345.tar.bz2
fengari-4f4e76d8de5fea7e636bdf5b5e271a2216531345.zip
src/lvm.js: Introduce cvt2num at mirror cvt2str
Diffstat (limited to 'src')
-rw-r--r--src/lvm.js25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/lvm.js b/src/lvm.js
index bd370a6..83feb9b 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -804,9 +804,10 @@ const luaV_tointeger = function(obj, mode) {
return luaconf.lua_numbertointeger(f);
} else if (obj.ttisinteger()) {
return obj.value;
- } else if (obj.ttisstring()) {
- let n = lobject.luaO_str2num(obj.svalue());
- return n !== false ? luaV_tointeger(n, mode) : false;
+ } else if (cvt2num(obj)) {
+ let v = lobject.luaO_str2num(obj.svalue());
+ if (v !== false)
+ return luaV_tointeger(v, mode);
}
return false;
@@ -816,13 +817,14 @@ const tointeger = function(o) {
return o.ttisinteger() ? o.value : luaV_tointeger(o, 0);
};
-const tonumber = function(v) {
- if (v.ttnov() === CT.LUA_TNUMBER)
- return v.value;
+const tonumber = function(o) {
+ if (o.ttnov() === CT.LUA_TNUMBER)
+ return o.value;
- if (v.ttnov() === CT.LUA_TSTRING) {
- let number = lobject.luaO_str2num(v.svalue());
- return number ? number.value : false;
+ if (cvt2num(o)) { /* string convertible to number? */
+ let v = lobject.luaO_str2num(o.svalue());
+ if (v !== false)
+ return v.value;
}
return false;
@@ -962,6 +964,10 @@ const cvt2str = function(o) {
return o.ttisnumber();
};
+const cvt2num = function(o) {
+ return o.ttisstring();
+};
+
const tostring = function(L, i) {
let o = L.stack[i];
@@ -1095,6 +1101,7 @@ module.exports.RC = RC;
module.exports.RKB = RKB;
module.exports.RKC = RKC;
module.exports.cvt2str = cvt2str;
+module.exports.cvt2num = cvt2num;
module.exports.dojump = dojump;
module.exports.donextjump = donextjump;
module.exports.forlimit = forlimit;