summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-15 08:29:52 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-15 08:29:52 +0100
commit19b47119bad0ef5965841a51c21c6c2d99c77ce9 (patch)
treea47042790d0ef0ba59559746ce252982c7621c2e /tests
parent5b8e5dfa30c4afe595d7ae87022dac85f0428f1d (diff)
downloadfengari-19b47119bad0ef5965841a51c21c6c2d99c77ce9.tar.gz
fengari-19b47119bad0ef5965841a51c21c6c2d99c77ce9.tar.bz2
fengari-19b47119bad0ef5965841a51c21c6c2d99c77ce9.zip
__call
Diffstat (limited to 'tests')
-rw-r--r--tests/ltm.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/ltm.js b/tests/ltm.js
index 17277ca..e365a2a 100644
--- a/tests/ltm.js
+++ b/tests/ltm.js
@@ -1257,4 +1257,64 @@ test('__concat', function (t) {
"hello",
"Program output is correct"
);
+});
+
+
+test('__call', function (t) {
+ let luaCode = `
+ local mt = {
+ __call = function (a, ...)
+ return "hello", ...
+ end
+ }
+
+ local t = {}
+
+ -- setmetatable(t, mt)
+
+ return t("world","wow")
+ `, L;
+
+ t.plan(4);
+
+ t.comment("Running following code: \n" + luaCode);
+
+ // main <hello.lua:0,0> (10 instructions at 0x7fc4c9403210)
+ // 0+ params, 5 slots, 1 upvalue, 2 locals, 3 constants, 1 function
+ // 1 [1] NEWTABLE 0 0 1
+ // 2 [4] CLOSURE 1 0 ; 0x7fc4c9403440
+ // 3 [4] SETTABLE 0 -1 1 ; "__call" -
+ // 4 [7] NEWTABLE 1 0 0
+ // 5 [11] MOVE 2 1
+ // 6 [11] LOADK 3 -2 ; "world"
+ // 7 [11] LOADK 4 -3 ; "wow"
+ // 8 [11] TAILCALL 2 3 0 <=== We stop here
+ // 9 [11] RETURN 2 0
+ // 10 [11] RETURN 0 1
+
+ t.doesNotThrow(function () {
+ L = getState(luaCode);
+ }, "Bytecode parsed without errors");
+
+ L.stack[0].p.code[7].breakpoint = true;
+
+ t.doesNotThrow(function () {
+ ldo.luaD_call(L, 0, -1);
+ }, "First part of the program executed without errors");
+
+ L.ci.pcOff--;
+ L.stack[0].p.code[7].breakpoint = false;
+
+ t.comment("We manually set t's metatable to mt");
+ L.stack[2].metatable = L.stack[1];
+
+ t.doesNotThrow(function () {
+ VM.luaV_execute(L);
+ }, "Second part of the program executed without errors");
+
+ t.deepEqual(
+ L.stack.slice(L.top - 3, L.top).map(function (e) { return e.value }),
+ ["hello", "world", "wow"],
+ "Program output is correct"
+ );
}); \ No newline at end of file