From 7d63489f2615609e21befc9d21a0e9f8fe2bf61a Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 8 Feb 2017 11:15:56 +0100 Subject: OP_GETTABLE, OP_SETTABLE, metatable --- src/lvm.js | 18 ++++++++++++++++++ tests/lvm.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/lvm.js b/src/lvm.js index 1f9efd0..2ea2d42 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -102,9 +102,27 @@ class LuaVM { break; } case "OP_GETTABLE": { + let t = L.stack[this.RKB(base, i)]; + let k = L.stack[this.RKC(base, i)]; + + if (!t.ttistable() || !t.value.__index(t, k)) { + // __index + } else { + L.stack[ra] = t.value.__index(t, k); + } break; } case "OP_SETTABLE": { + let t = L.stack[ra]; + let k = L.stack[this.RKB(base, i)]; + let v = L.stack[this.RKC(base, i)]; + + if (!t.ttistable() || !t.value.__index(t, k)) { + // __index + } else { + t.value.__newindex(t, k, v); + } + break; } case "OP_NEWTABLE": { diff --git a/tests/lvm.js b/tests/lvm.js index 5f19455..4c10521 100644 --- a/tests/lvm.js +++ b/tests/lvm.js @@ -450,4 +450,37 @@ test('TEST (false)', function (t) { "goodbye", "Program output is correct" ); +}); + + +test('SETTABLE, GETTABLE', function (t) { + let luaCode = ` + local t = {} + + t[1] = "hello" + t["two"] = "world" + + return t[1], t["two"] + `, vm; + + t.plan(2); + + t.comment("Running following code: \n" + luaCode); + + t.doesNotThrow(function () { + vm = getVM(luaCode); + vm.execute(); + }, "Program executed without errors"); + + t.stritEqual( + vm.L.stack[vm.L.top - 1].value.array[1], + "hello", + "Program output is correct" + ); + + t.stritEqual( + vm.L.stack[vm.L.top - 1].value.hash.get("two"), + "world", + "Program output is correct" + ); }); \ No newline at end of file -- cgit v1.2.3-70-g09d2