aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lvm.js19
-rw-r--r--tests/lvm.js26
2 files changed, 45 insertions, 0 deletions
diff --git a/src/lvm.js b/src/lvm.js
index e0901c5..98dfa76 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -449,6 +449,25 @@ class LuaVM {
break;
}
case "OP_VARARG": {
+ let b = i.B - 1;
+ let n = base - ci.funcOff - cl.p.numparams - 1;
+ let j
+
+ if (n < 0) /* less arguments than parameters? */
+ n = 0; /* no vararg arguments */
+
+ if (b < 0) {
+ b = n; /* get all var. arguments */
+ base = ci.u.l.base;
+ ra = this.RA(i); /* previous call may change the stack */
+ L.top = ra + n;
+ }
+
+ for (j = 0; j < b && j < n; j++)
+ L.stack[ra + j] = L.stack[base - n - j];
+
+ for (; j < b; j++) /* complete required results with nil */
+ L.stack[ra + j] = new TValue(CT.LUA_TNIL, null);
break;
}
case "OP_EXTRAARG": {
diff --git a/tests/lvm.js b/tests/lvm.js
index c454bcf..62f9fff 100644
--- a/tests/lvm.js
+++ b/tests/lvm.js
@@ -244,4 +244,30 @@ test('TAILCALL', function (t) {
3,
"Program output is correct"
);
+});
+
+
+test('VARARG', function (t) {
+ let luaCode = `
+ local f = function (a, b)
+ return a + b
+ end
+
+ return f(1,2)
+ `, vm;
+
+ t.plan(2);
+
+ t.comment("Running following code: \n" + luaCode);
+
+ t.doesNotThrow(function () {
+ vm = getVM(luaCode);
+ vm.execute();
+ }, "Program executed without errors");
+
+ t.strictEqual(
+ vm.L.stack[vm.L.top - 1].value,
+ 3,
+ "Program output is correct"
+ );
}); \ No newline at end of file