aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-02 09:54:45 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-02 11:02:43 +0100
commit490ee0d109671b6f7e9d3d56e8122f41647fed4c (patch)
tree4300c4e89e678859a1777ca1c3dd5ee8d16c3b37
parent61168b0ab774e7be6deb1573fbcc9b1a53a37f4e (diff)
downloadfengari-490ee0d109671b6f7e9d3d56e8122f41647fed4c.tar.gz
fengari-490ee0d109671b6f7e9d3d56e8122f41647fed4c.tar.bz2
fengari-490ee0d109671b6f7e9d3d56e8122f41647fed4c.zip
[Parsing tests] FORPREP, FORLOOP
-rw-r--r--src/lobject.js36
-rw-r--r--src/lparser.js2
-rw-r--r--tests/lexparse.js66
-rw-r--r--tests/lvm.js1
4 files changed, 81 insertions, 24 deletions
diff --git a/src/lobject.js b/src/lobject.js
index bd234a1..0ef6e9a 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -278,32 +278,28 @@ const luaO_utf8desc = function(buff, x) {
return n;
};
-const l_str2dloc = function(s, result, mode) {
- result[0] = mode === 'x' ? parseInt(s, '16') : parseInt(s);
- if (isNaN(result[0])) return null; /* nothing recognized? */
- while (ljstype.lisspace(result[0])) result[0] = result[0].slice(1); /* skip trailing spaces */
- return result[0] === '\0' ? result : null; /* OK if no trailing characters */
+const l_str2dloc = function(s, mode) {
+ let flt = mode === 'x' ? parseInt(s, '16') : parseFloat(s);
+ return !isNaN(flt) && /^\d+(\.\d+)?\0?$/.test(s) ? flt : null; /* OK if no trailing characters */
};
const l_str2d = function(s) {
- let result = [null, null];
let pidx = /[.xXnN]/g.exec(s).index;
- let pmode = pidx ? s.slice(pidx) : null;
+ let pmode = pidx ? s[pidx] : null;
let mode = pmode ? pmode.toLowerCase() : 0;
if (mode === 'n') /* reject 'inf' and 'nan' */
return null;
- let end = l_str2dloc(s, result, mode)[0]; /* try to convert */
+ let end = l_str2dloc(s, mode); /* try to convert */
if (end === null) { /* failed? may be a different locale */
throw new Error("Locale not available to handle number"); // TODO
}
- return [end, result[1]];
+ return end;
};
const MAXBY10 = Number.MAX_SAFE_INTEGER / 10;
const MAXLASTD = Number.MAX_SAFE_INTEGER % 10;
const l_str2int = function(s) {
- let result = [null, null];
let a = 0;
let empty = true;
let neg;
@@ -333,28 +329,22 @@ const l_str2int = function(s) {
while (ljstype.lisspace(s[0])) s = s.slice(1); /* skip trailing spaces */
- if (empty)/* TODO: || s[0] !== "") */ return null; /* something wrong in the numeral */
+ if (empty || s[0] !== "\0") return null; /* something wrong in the numeral */
else {
- result[1] = neg ? -a : a;
- result[0] = s;
- return result;
+ return neg ? -a : a;
}
};
const luaO_str2num = function(s) {
let s2i = l_str2int(s);
- let e = s2i[0];
- let i = s2i[1];
- if (e !== null) { /* try as an integer */
- return new TValue(CT.LUA_TNUMINT, i);
+ if (s2i !== null) { /* try as an integer */
+ return new TValue(CT.LUA_TNUMINT, s2i);
} else { /* else try as a float */
- s2i = l_str2d(s);
- e = s2i[0];
- i = s2i[1];
+ s2i = l_str2d(s.join(''));
- if (e !== null) {
- return new TValue(CT.LUA_TNUMFLT, i);
+ if (s2i !== null) {
+ return new TValue(CT.LUA_TNUMFLT, s2i);
} else
return false; /* conversion failed */
}
diff --git a/src/lparser.js b/src/lparser.js
index c21563f..9c52c59 100644
--- a/src/lparser.js
+++ b/src/lparser.js
@@ -902,7 +902,7 @@ const simpleexp = function(ls, v) {
constructor | FUNCTION body | suffixedexp */
switch (ls.t.token) {
case R.TK_FLT: {
- init_exp(v, expkind.VFLT, 0);
+ init_exp(v, expkind.VKFLT, 0);
v.u.nval = ls.t.seminfo.r;
break;
}
diff --git a/tests/lexparse.js b/tests/lexparse.js
index 7ef5fe3..7a12985 100644
--- a/tests/lexparse.js
+++ b/tests/lexparse.js
@@ -467,4 +467,70 @@ test('TEST (false)', function (t) {
"goodbye",
"Program output is correct"
);
+});
+
+
+test('FORPREP, FORLOOP (int)', function (t) {
+ let luaCode = `
+ local total = 0
+
+ for i = 0, 10 do
+ total = total + i
+ end
+
+ return total
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lapi.lua_load(L, null, luaCode, "test", "text");
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "JS Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tointeger(L, -1),
+ 55,
+ "Program output is correct"
+ );
+});
+
+
+test('FORPREP, FORLOOP (float)', function (t) {
+ let luaCode = `
+ local total = 0
+
+ for i = 0.5, 10.5 do
+ total = total + i
+ end
+
+ return total
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lapi.lua_load(L, null, luaCode, "test", "text");
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "JS Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tonumber(L, -1),
+ 60.5,
+ "Program output is correct"
+ );
}); \ No newline at end of file
diff --git a/tests/lvm.js b/tests/lvm.js
index f9fb347..f5948eb 100644
--- a/tests/lvm.js
+++ b/tests/lvm.js
@@ -445,6 +445,7 @@ test('FORPREP, FORLOOP (int)', function (t) {
);
});
+
test('FORPREP, FORLOOP (float)', function (t) {
let luaCode = `
local total = 0