aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/lvm.js11
-rw-r--r--tests/lvm.js29
3 files changed, 41 insertions, 1 deletions
diff --git a/README.md b/README.md
index e4e8611..2aca446 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@
- [x] OP_SETTABUP
- [x] OP_SETTABLE
- [x] OP_NEWTABLE
- - [ ] OP_SELF
+ - [x] OP_SELF
- [x] OP_ADD
- [x] OP_SUB
- [x] OP_MUL
diff --git a/src/lvm.js b/src/lvm.js
index 7b70c01..7ca6921 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -149,6 +149,17 @@ class LuaVM {
break;
}
case "OP_SELF": {
+ let table = L.stack[this.RB(base, i)];
+ let key = this.RKC(base, k, i);
+
+ L.stack[ra + 1] = table;
+
+ // if (!table.ttistable() || !table.metatable.__index(table, key)) {
+ // // __index
+ // } else {
+ L.stack[ra] = table.metatable.__index(table, key);
+ // }
+
break;
}
case "OP_ADD": {
diff --git a/tests/lvm.js b/tests/lvm.js
index 1af0c48..3d7325f 100644
--- a/tests/lvm.js
+++ b/tests/lvm.js
@@ -601,4 +601,33 @@ test('SETTABUP, GETTABUP', function (t) {
"world",
"Program output is correct"
);
+});
+
+
+test('SELF', function (t) {
+ let luaCode = `
+ local t = {}
+
+ t.value = "hello"
+ t.get = function (self)
+ return self.value
+ end
+
+ return t:get()
+ `, vm;
+
+ t.plan(1);
+
+ 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,
+ "hello",
+ "Program output is correct"
+ );
}); \ No newline at end of file