summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-02-11 08:53:10 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-02-11 15:23:04 +0100
commita7b98a2e62c49a6c0ced2b57ddcea9bb6bab108e (patch)
tree1420dfdb17660fd5ca243f25d279dbd308263f7e
parent219d7cf24902b53385a1b6999e8bbf2c8512cf0b (diff)
downloadfengari-a7b98a2e62c49a6c0ced2b57ddcea9bb6bab108e.tar.gz
fengari-a7b98a2e62c49a6c0ced2b57ddcea9bb6bab108e.tar.bz2
fengari-a7b98a2e62c49a6c0ced2b57ddcea9bb6bab108e.zip
Fixed bad tailcall, _ENV is a closed upvalue
-rw-r--r--README.md10
-rw-r--r--sandbox/hello.bcbin304 -> 0 bytes
-rw-r--r--sandbox/hello.bc.txt17
-rw-r--r--sandbox/hello.js7
-rw-r--r--sandbox/hello.lua2
-rw-r--r--sandbox/hello2.lua7
-rw-r--r--src/lobject.js3
-rw-r--r--src/lstate.js3
-rw-r--r--src/lvm.js5
-rw-r--r--tests/lvm.js6
10 files changed, 14 insertions, 46 deletions
diff --git a/README.md b/README.md
index 7d2a5db..e4e8611 100644
--- a/README.md
+++ b/README.md
@@ -12,11 +12,11 @@
- [x] OP_LOADNIL
- [x] OP_GETUPVAL
- [x] OP_SETUPVAL
- - [ ] OP_GETTABUP
- - [ ] OP_GETTABLE
- - [ ] OP_SETTABUP
- - [ ] OP_SETTABLE
- - [ ] OP_NEWTABLE
+ - [x] OP_GETTABUP
+ - [x] OP_GETTABLE
+ - [x] OP_SETTABUP
+ - [x] OP_SETTABLE
+ - [x] OP_NEWTABLE
- [ ] OP_SELF
- [x] OP_ADD
- [x] OP_SUB
diff --git a/sandbox/hello.bc b/sandbox/hello.bc
deleted file mode 100644
index 6b88ec8..0000000
--- a/sandbox/hello.bc
+++ /dev/null
Binary files differ
diff --git a/sandbox/hello.bc.txt b/sandbox/hello.bc.txt
deleted file mode 100644
index b41f555..0000000
--- a/sandbox/hello.bc.txt
+++ /dev/null
@@ -1,17 +0,0 @@
-main <sandbox/hello2.lua:0,0> (9 instructions at 0x7ff931403170)
-0+ params, 5 slots, 1 upvalue, 2 locals, 4 constants, 1 function
- 1 [1] LOADK 0 -1 ; 1
- 2 [2] LOADK 1 -2 ; 2
- 3 [3] GETTABUP 2 0 -3 ; _ENV "print"
- 4 [3] MOVE 3 0
- 5 [3] MOVE 4 1
- 6 [3] CALL 2 3 1
- 7 [7] CLOSURE 2 0 ; 0x7ff9314032e0
- 8 [5] SETTABUP 0 -4 2 ; _ENV "c"
- 9 [7] RETURN 0 1
-
-function <sandbox/hello2.lua:5,7> (3 instructions at 0x7ff9314032e0)
-2 params, 3 slots, 0 upvalues, 2 locals, 0 constants, 0 functions
- 1 [6] ADD 2 0 1
- 2 [6] RETURN 2 2
- 3 [7] RETURN 0 1 \ No newline at end of file
diff --git a/sandbox/hello.js b/sandbox/hello.js
deleted file mode 100644
index df32dd8..0000000
--- a/sandbox/hello.js
+++ /dev/null
@@ -1,7 +0,0 @@
-const DataView = require('buffer-dataview');
-const fs = require('fs');
-
-const BytecodeParser = require("../src/lundump.js");
-
-let p = new BytecodeParser(new DataView(fs.readFileSync("./sandbox/hello.bc")))
-p.luaU_undump(); \ No newline at end of file
diff --git a/sandbox/hello.lua b/sandbox/hello.lua
deleted file mode 100644
index b2bf135..0000000
--- a/sandbox/hello.lua
+++ /dev/null
@@ -1,2 +0,0 @@
-local hello = "Hello"
-print (hello.." World!")
diff --git a/sandbox/hello2.lua b/sandbox/hello2.lua
deleted file mode 100644
index bcbc1e0..0000000
--- a/sandbox/hello2.lua
+++ /dev/null
@@ -1,7 +0,0 @@
-local a = 1;
-local b = 2;
-print(a, b);
-
-function c(a, b)
- return a + b
-end \ No newline at end of file
diff --git a/src/lobject.js b/src/lobject.js
index a1ed6ca..4c3290d 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -115,9 +115,10 @@ class LClosure extends TValue {
let _ENV = new UpVal();
_ENV.refcount = 0;
- _ENV.v = 0; // _ENV is on the stack at index 0
+ _ENV.v = null;
_ENV.u.open.next = null;
_ENV.u.open.touched = true;
+ _ENV.u.value = new Table();
this.upvals = [
_ENV
diff --git a/src/lstate.js b/src/lstate.js
index 2c322c3..fcc3eb6 100644
--- a/src/lstate.js
+++ b/src/lstate.js
@@ -28,13 +28,12 @@ class CallInfo {
class lua_State {
constructor(cl) {
- this.top = 2;
+ this.top = 1;
this.ci = new CallInfo(0, cl, 1, 1, null, null);
this.ci.u.l.savedpc = cl.p.code;
this.ci.nresults = LUA_MULTRET;
this.ciOff = 0;
this.stack = [
- new Table(), // _ENV
cl
];
this.openupval = [];
diff --git a/src/lvm.js b/src/lvm.js
index feb0a53..7b70c01 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -431,7 +431,7 @@ class LuaVM {
}
case "OP_TAILCALL": {
if (i.B !== 0) L.top = ra + i.B;
- if (this.precall(ra, L.stack[ra], LUA_MULTRET)) {
+ if (this.precall(ra, L.stack[ra], LUA_MULTRET)) { // JS function
base = ci.u.l.base;
} else {
/* tail call: put called frame (n) in place of caller one (o) */
@@ -445,7 +445,7 @@ class LuaVM {
if (cl.p.p.length > 0) this.closeupvals(oci.u.l.base);
for (let aux = 0; nfuncOff + aux < lim; aux++)
L.stack[ofuncOff + aux] = L.stack[nfuncOff + aux];
-
+ oci.func = nci.func;
oci.u.l.base = ofuncOff + (nci.u.l.base - nfuncOff);
L.top = ofuncOff + (L.top - nfuncOff);
oci.top = L.top;
@@ -718,6 +718,7 @@ class LuaVM {
}
closeupvals(level) {
+ let L = this.L;
while (L.openupval !== null && L.openupval.v >= level) {
let uv = L.openupval;
assert(uv.isopen());
diff --git a/tests/lvm.js b/tests/lvm.js
index df01167..1af0c48 100644
--- a/tests/lvm.js
+++ b/tests/lvm.js
@@ -554,14 +554,14 @@ test('SETUPVAL, GETUPVAL', function (t) {
return f()
`, vm;
- t.plan(1);
+ t.plan(2);
t.comment("Running following code: \n" + luaCode);
- // t.doesNotThrow(function () {
+ t.doesNotThrow(function () {
vm = getVM(luaCode);
vm.execute();
- // }, "Program executed without errors");
+ }, "Program executed without errors");
t.strictEqual(
vm.L.stack[vm.L.top - 1].value,