summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-02 14:05:01 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-02 14:05:01 +0100
commit005223711f4e11da5c8f24d19d6e8a6b64f603a8 (patch)
treeb5a7b158af9c7a626c52c1f682bc4141d5a43f39
parent8785e3801b3bd4b3add20f004c9492565fb77fbd (diff)
downloadfengari-005223711f4e11da5c8f24d19d6e8a6b64f603a8.tar.gz
fengari-005223711f4e11da5c8f24d19d6e8a6b64f603a8.tar.bz2
fengari-005223711f4e11da5c8f24d19d6e8a6b64f603a8.zip
[Parsing tests] TFORCALL, TFORLOOP
-rw-r--r--README.md2
-rw-r--r--src/lparser.js4
-rw-r--r--tests/lexparse.js49
3 files changed, 52 insertions, 3 deletions
diff --git a/README.md b/README.md
index 837e792..4218110 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-[![Build Status](https://travis-ci.com/giann/fengari.svg?token=PSYuVp8qrrdszprvDFz7&branch=master)](https://travis-ci.com/giann/fengari)
+[![Build Status](https://travis-ci.com/giann/fengari.svg?token=PSYuVp8qrrdszprvDFz7&branch=master)](https://travis-ci.com/giann/fengari) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
# fengari
🐺 φεγγάρι - A Lua VM written in JS ES6 targeting the browser
diff --git a/src/lparser.js b/src/lparser.js
index 9c52c59..7fa3f16 100644
--- a/src/lparser.js
+++ b/src/lparser.js
@@ -1240,7 +1240,7 @@ const forbody = function(ls, base, line, nvars, isnum) {
if (isnum) /* end of scope for declared variables */
endfor = lcode.luaK_codeAsBx(fs, OpCodesI.OP_FORLOOP, base, lcode.NO_JUMP);
else { /* generic for */
- lcode.luaK_codeABC(fs, OpCodesI.OP_TFORLOOP, base + 2, lcode.NO_JUMP);
+ lcode.luaK_codeABC(fs, OpCodesI.OP_TFORCALL, base, 0, nvars);
lcode.luaK_fixline(fs, line);
endfor = lcode.luaK_codeAsBx(fs, OpCodesI.OP_TFORLOOP, base + 2, lcode.NO_JUMP);
}
@@ -1274,7 +1274,7 @@ const forlist = function(ls, indexname) {
let fs = ls.fs;
let e = new expdesc();
let nvars = 4; /* gen, state, control, plus at least one declared var */
- let base = fs.freereg();
+ let base = fs.freereg;
/* create control variables */
new_localvarliteral(ls, "(for generator)");
new_localvarliteral(ls, "(for state)");
diff --git a/tests/lexparse.js b/tests/lexparse.js
index df7cae8..b661d6d 100644
--- a/tests/lexparse.js
+++ b/tests/lexparse.js
@@ -864,4 +864,53 @@ test('Long SETLIST', function (t) {
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
"Program output is correct"
);
+});
+
+
+test('TFORCALL, TFORLOOP', function (t) {
+ let luaCode = `
+ local iterator = function (t, i)
+ i = i + 1
+ local v = t[i]
+ if v then
+ return i, v
+ end
+ end
+
+ local iprs = function(t)
+ return iterator, t, 0
+ end
+
+ local t = {1, 2, 3}
+ local r = 0
+ for k,v in iprs(t) do
+ r = r + v
+ end
+
+ return r
+ `, L;
+
+ t.plan(3);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lapi.lua_load(L, null, luaCode, "test", "text");
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tonumber(L, -1),
+ 6,
+ "Program output is correct"
+ );
}); \ No newline at end of file