From 5b8e5dfa30c4afe595d7ae87022dac85f0428f1d Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 15 Feb 2017 08:21:51 +0100 Subject: __concat --- README.md | 2 +- tests/ltm.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a6e972..4aee3cc 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ - [x] `__gc` (unavailable) - [x] `__mode` (unavailable) - [x] `__len` - - [ ] `__concat` + - [x] `__concat` - [ ] `__call` - [ ] `__tostring` - [ ] `__pairs` diff --git a/tests/ltm.js b/tests/ltm.js index 00ea0e6..17277ca 100644 --- a/tests/ltm.js +++ b/tests/ltm.js @@ -1191,6 +1191,67 @@ test('__len', function (t) { VM.luaV_execute(L); }, "Second part of the program executed without errors"); + t.strictEqual( + L.stack[L.top - 1].value, + "hello", + "Program output is correct" + ); +}); + + +test('__concat', function (t) { + let luaCode = ` + local mt = { + __concat = function (a) + return "hello" + end + } + + local t = {} + + -- setmetatable(t, mt) + + return t .. " world" + `, L; + + t.plan(4); + + t.comment("Running following code: \n" + luaCode); + + // main (9 instructions at 0x7ffacdc03210) + // 0+ params, 4 slots, 1 upvalue, 2 locals, 2 constants, 1 function + // 1 [1] NEWTABLE 0 0 1 + // 2 [4] CLOSURE 1 0 ; 0x7ffacdc03440 + // 3 [4] SETTABLE 0 -1 1 ; "__concat" - + // 4 [7] NEWTABLE 1 0 0 + // 5 [11] MOVE 2 1 + // 6 [11] LOADK 3 -2 ; " world" + // 7 [11] CONCAT 2 2 3 <=== We stop here + // 8 [11] RETURN 2 2 + // 9 [11] RETURN 0 1 + // + // ... + + t.doesNotThrow(function () { + L = getState(luaCode); + }, "Bytecode parsed without errors"); + + L.stack[0].p.code[6].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[6].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.strictEqual( L.stack[L.top - 1].value, "hello", -- cgit v1.2.3-70-g09d2