summaryrefslogtreecommitdiff
path: root/src/lobject.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-12-12 13:45:10 +1100
committerdaurnimator <quae@daurnimator.com>2017-12-12 14:37:53 +1100
commitc5b39b9d292c1a2178aa41c6a95367a81614eb61 (patch)
tree26c9c9d983e70704becc1e85e1371b1be8bae371 /src/lobject.js
parent8dfdad3ee84a73ec7da2312e8c8d893dd8968d17 (diff)
downloadfengari-c5b39b9d292c1a2178aa41c6a95367a81614eb61.tar.gz
fengari-c5b39b9d292c1a2178aa41c6a95367a81614eb61.tar.bz2
fengari-c5b39b9d292c1a2178aa41c6a95367a81614eb61.zip
src/: Have luaO_str2num fill in passed TValue and return length
Diffstat (limited to 'src/lobject.js')
-rw-r--r--src/lobject.js40
1 files changed, 25 insertions, 15 deletions
diff --git a/src/lobject.js b/src/lobject.js
index 91078ed..d816323 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -406,8 +406,10 @@ const lua_strx2number = function(s) {
e += exp1;
}
if (neg) r = -r;
- while (ljstype.lisspace(s[i])) i++; /* skip trailing spaces */
- return i === s.length ? luaconf.ldexp(r, e) : null; /* Only valid if nothing left is s*/
+ return {
+ n: luaconf.ldexp(r, e),
+ i: i
+ };
};
const lua_str2number = function(s) {
@@ -416,15 +418,20 @@ const lua_str2number = function(s) {
} catch (e) {
return null;
}
- /* parseFloat ignores trailing junk, validate with regex first */
- if (!/^[\t\v\f \n\r]*[+-]?([0-9]+\.?[0-9]*|\.[0-9]*)([eE][+-]?[0-9]+)?[\t\v\f \n\r]*$/.test(s))
+ /* use a regex to validate number and also to get length
+ parseFloat ignores trailing junk */
+ let r = /^[\t\v\f \n\r]*[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]*)(?:[eE][+-]?[0-9]+)?/.exec(s);
+ if (!r)
return null;
- let flt = parseFloat(s);
- return !isNaN(flt) ? flt : null;
+ let flt = parseFloat(r[0]);
+ return !isNaN(flt) ? { n: flt, i: r[0].length } : null;
};
const l_str2dloc = function(s, mode) {
- return mode === 'x' ? lua_strx2number(s) : lua_str2number(s);
+ let result = mode === 'x' ? lua_strx2number(s) : lua_str2number(s); /* try to convert */
+ if (result === null) return null;
+ while (ljstype.lisspace(s[result.i])) result.i++; /* skip trailing spaces */
+ return (result.i === s.length || s[result.i] === 0) ? result : null; /* OK if no trailing characters */
};
const l_str2d = function(s) {
@@ -470,24 +477,27 @@ const l_str2int = function(s) {
}
}
while (ljstype.lisspace(s[i])) i++; /* skip trailing spaces */
- if (empty || (i !== s.length)) return null; /* something wrong in the numeral */
+ if (empty || (i !== s.length && s[i] !== 0)) return null; /* something wrong in the numeral */
else {
- return (neg ? -a : a)|0;
+ return {
+ n: (neg ? -a : a)|0,
+ i: i
+ };
}
};
-const luaO_str2num = function(s) {
+const luaO_str2num = function(s, o) {
let s2i = l_str2int(s);
-
if (s2i !== null) { /* try as an integer */
- return new TValue(CT.LUA_TNUMINT, s2i);
+ o.setivalue(s2i.n);
+ return s2i.i+1;
} else { /* else try as a float */
s2i = l_str2d(s);
-
if (s2i !== null) {
- return new TValue(CT.LUA_TNUMFLT, s2i);
+ o.setfltvalue(s2i.n);
+ return s2i.i+1;
} else
- return false; /* conversion failed */
+ return 0; /* conversion failed */
}
};