summaryrefslogtreecommitdiff
path: root/src/lbaselib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-05-22 15:44:14 +1000
committerdaurnimator <quae@daurnimator.com>2017-05-22 15:44:14 +1000
commit5da3fdc853268b197040ec63cc2dadc489358977 (patch)
tree075312a5ee09c38052eedeeee8bdd4424c33507d /src/lbaselib.js
parent280b5e851aecddd668a635cbcd6eba217d6dc0ba (diff)
downloadfengari-5da3fdc853268b197040ec63cc2dadc489358977.tar.gz
fengari-5da3fdc853268b197040ec63cc2dadc489358977.tar.bz2
fengari-5da3fdc853268b197040ec63cc2dadc489358977.zip
src/lbaselib.js: tonumber shouldn't ignore trailing junk
Sadly I couldn't figure out a way to continue use of parseInt
Diffstat (limited to 'src/lbaselib.js')
-rw-r--r--src/lbaselib.js19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js
index 613adc0..dfd39bb 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -154,6 +154,21 @@ const luaB_ipairs = function(L) {
return 3;
};
+const b_str2int = function(s, base) {
+ let r = /^[\t\v\f \n\r]*([\+\-]?)0*([0-9A-Za-z]+)[\t\v\f \n\r]*$/.exec(lua.to_jsstring(s));
+ if (!r) return null;
+ let neg = r[1] === "-";
+ let digits = r[2];
+ let n = 0;
+ for (let si=0; si<digits.length; si++) {
+ let digit = /\d/.test(digits[si]) ? (digits.charCodeAt(si) - '0'.charCodeAt(0))
+ : (digits[si].toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0) + 10);
+ if (digit >= base) return null; /* invalid numeral */
+ n = ((n * base)|0) + digit;
+ }
+ return (neg ? -n : n)|0;
+};
+
const luaB_tonumber = function(L) {
if (lua.lua_type(L, 2) <= 0) { /* standard conversion? */
lauxlib.luaL_checkany(L, 1);
@@ -170,8 +185,8 @@ const luaB_tonumber = function(L) {
lauxlib.luaL_checktype(L, 1, lua.LUA_TSTRING); /* no numbers as strings */
let s = lua.lua_tostring(L, 1);
lauxlib.luaL_argcheck(L, 2 <= base && base <= 36, 2, lua.to_luastring("base out of range", true));
- let n = parseInt(lua.to_jsstring(s), base);
- if (!isNaN(n)) {
+ let n = b_str2int(s, base);
+ if (n !== null) {
lua.lua_pushinteger(L, n);
return 1;
}