aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lcode.js12
-rw-r--r--src/lparser.js7
-rw-r--r--tests/lexparse.js60
3 files changed, 74 insertions, 5 deletions
diff --git a/src/lcode.js b/src/lcode.js
index c4b8b81..64e890e 100644
--- a/src/lcode.js
+++ b/src/lcode.js
@@ -90,8 +90,8 @@ const BinOpr = {
OPR_GT: 17,
OPR_GE: 18,
OPR_AND: 19,
- OPR_OR: 21,
- OPR_NOBINOPR: 22
+ OPR_OR: 20,
+ OPR_NOBINOPR: 21
};
const UnOpr = {
@@ -359,7 +359,7 @@ const luaK_patchclose = function(fs, list, level) {
** line information. Return 'i' position.
*/
const luaK_code = function(fs, i) {
- // console.log(OpCodes[i.opcode]);
+ console.log(`${i.opcode}\t${i.A}\t${i.B}\t${i.C}\t${i.Ax}\t${i.Bx}\t${i.sBx}`);
let f = fs.f;
dischargejpc(fs); /* 'pc' will change */
/* put new instruction in code array */
@@ -1163,14 +1163,14 @@ const luaK_posfix = function(fs, op, e1, e2, line) {
assert(e1.t === NO_JUMP); /* list closed by 'luK_infix' */
luaK_dischargevars(fs, e2);
e2.f = luaK_concat(fs, e2.f, e1.f);
- // WARN: *e1 = *e2;
+ e1.to(e2);
break;
}
case BinOpr.OPR_OR: {
assert(e1.f === NO_JUMP); /* list closed by 'luK_infix' */
luaK_dischargevars(fs, e2);
e2.t = luaK_concat(fs, e2.t, e1.t);
- // WARN: *e1 = *e2;
+ e1.to(e2);
break;
}
case BinOpr.OPR_CONCAT: {
@@ -1202,6 +1202,8 @@ const luaK_posfix = function(fs, op, e1, e2, line) {
break;
}
}
+
+ return e1;
};
/*
diff --git a/src/lparser.js b/src/lparser.js
index 86f32f8..ff809b9 100644
--- a/src/lparser.js
+++ b/src/lparser.js
@@ -84,6 +84,13 @@ class expdesc {
this.t = NaN; /* patch list of 'exit when true' */
this.f = NaN; /* patch list of 'exit when false' */
}
+
+ to(e) { // Copy e content to this, cf. luaK_posfix
+ this.k = e.k;
+ this.u = e.u;
+ this.t = e.t;
+ this.f = e.f;
+ }
}
class FuncState {
diff --git a/tests/lexparse.js b/tests/lexparse.js
index 2b0cc46..2470e5d 100644
--- a/tests/lexparse.js
+++ b/tests/lexparse.js
@@ -373,4 +373,64 @@ test('EQ', function (t) {
true,
"Program output is correct"
);
+});
+
+
+test('TESTSET (and)', function (t) {
+ let luaCode = `
+ local a = true
+ local b = "hello"
+
+ return a and b
+ `, 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_tostring(L, -1),
+ "hello",
+ "Program output is correct"
+ );
+});
+
+
+test('TESTSET (or)', function (t) {
+ let luaCode = `
+ local a = false
+ local b = "hello"
+
+ return a or b
+ `, 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_tostring(L, -1),
+ "hello",
+ "Program output is correct"
+ );
}); \ No newline at end of file