From 5da3fdc853268b197040ec63cc2dadc489358977 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 15:44:14 +1000 Subject: src/lbaselib.js: tonumber shouldn't ignore trailing junk Sadly I couldn't figure out a way to continue use of parseInt --- src/lbaselib.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/lbaselib.js') 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= 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; } -- cgit v1.2.3-54-g00ecf