Mon, 22 Nov 2010 19:38:12 +0000
Change LVM.call() to take an LValue rather than the raw native/LFunction object. Also add LVM.loadstring(chunk, env) and use that for loading luac.out.
lvm.js | file | annotate | diff | comparison | revisions |
--- a/lvm.js Mon Nov 22 05:06:37 2010 +0000 +++ b/lvm.js Mon Nov 22 19:38:12 2010 +0000 @@ -50,8 +50,7 @@ LValue.prototype = { call: function (args) { - var f = this.precall(); - var ret = this.vm.call(f, args); + var ret = this.vm.call(this, args); if(typeof(ret) == "undefined") ret = []; return ret; @@ -204,7 +203,7 @@ if(debugMode) { var pi = this.instructions[this.instructions.length-1]; - sys.puts("Pos: "+(this.pos-4)+" Ins: "+ins+" OP: "+INS_OPCODE(pi)+" A: "+INS_A(pi)+" B: "+INS_B(pi)+" C: "+INS_C(pi)+" Bx: "+INS_Bx(pi)+" sBx: "+(INS_Bx(pi)-0x1FFFE)); + //sys.puts("Pos: "+(this.pos-4)+" Ins: "+ins+" OP: "+INS_OPCODE(pi)+" A: "+INS_A(pi)+" B: "+INS_B(pi)+" C: "+INS_C(pi)+" Bx: "+INS_Bx(pi)+" sBx: "+(INS_Bx(pi)-0x1FFFE)); } } @@ -397,21 +396,22 @@ typeof(value)+" from Javascript to Lua"; } }, - call: function (lfFunction, args) + call: function (func, args) { - if(typeof(lfFunction) == "function") + var f = func.precall(); + if(typeof(f) == "function") { - return lfFunction.apply(this, args); + return f.apply(this, args); } else { - var frame = {f:lfFunction,pc:0,entry:true}; + var frame = {f:f,pc:0,entry:true}; if(args) frame.reg = args.slice(0); else frame.reg = []; this.callstack.push(frame); - for(var i=frame.reg.length;i<lfFunction.maxStackSize;i++) + for(var i=frame.reg.length;i<f.maxStackSize;i++) frame.reg[i] = this.LValue(null); return this.run(frame); } @@ -514,7 +514,7 @@ frame.f = f; frame.pc = 0; frame.reg = args; break; case OP_CALL: - var f = frame.reg[INS_A(instruction)].precall(); // return JS or LValue + var f = frame.reg[INS_A(instruction)].precall(); // return JS or LFunction var A = INS_A(instruction), B = INS_B(instruction), C = INS_C(instruction); var undefined; var args; @@ -527,7 +527,7 @@ if(typeof(f) == "function") { // JS native function - var ret = this.call(f, args); + var ret = this.call(frame.reg[INS_A(instruction)], args); // Insert ret to reg starting at R(A), with C-1 limit var nresults = ret.length; var nexpected; @@ -628,8 +628,8 @@ case OP_TFORLOOP: var A = INS_A(instruction); var C = INS_C(instruction); - var f = frame.reg[A].precall(); // Iterator function - var rets = this.call(f, [frame.reg[A+1], frame.reg[A+2]]); + var RA = frame.reg[A]; // Iterator function + var rets = this.call(RA, [frame.reg[A+1], frame.reg[A+2]]); frame.reg.length = A+3; for(var i = 0; i<C; i++) frame.reg[A+3+i] = rets[i]; @@ -758,6 +758,12 @@ for(var k in lib) t.setIndex(this.LValue(k), this.LValue(lib[k])); + }, + loadstring: function (chunk, env) + { + var c = new LBinaryChunk(this, chunk); + var f = new LFunction(this, c, env); + return new LValue(this, "function", f); } }; @@ -766,7 +772,6 @@ var fs=require("fs"); var sys=require("sys"); -var c = new LBinaryChunk(testvm, fs.readFileSync("luac.out", "binary")); var _G = testvm.LValue([]); @@ -891,7 +896,7 @@ _G.setMetatable(mt); -var f = new LFunction(testvm, c, _G); +var f = testvm.loadstring(fs.readFileSync("luac.out", "binary"), _G); var ret = testvm.call(f); if(ret)