198 sys.puts("STACK: "+JSON.stringify(this.frame.reg)); |
198 sys.puts("STACK: "+JSON.stringify(this.frame.reg)); |
199 } |
199 } |
200 switch(INS_OPCODE(instruction)) |
200 switch(INS_OPCODE(instruction)) |
201 { |
201 { |
202 case OP_MOVE: |
202 case OP_MOVE: |
203 this.frame.reg[instruction[1]] = this.frame.reg[instruction[2]]; |
203 this.frame.reg[INS_A(instruction)] = this.frame.reg[INS_B(instruction)]; |
204 break; |
204 break; |
205 case OP_LOADNIL: |
205 case OP_LOADNIL: |
206 for(var i = instruction[1];i<=instruction[2];i++) |
206 for(var i = INS_A(instruction);i<=INS_B(instruction);i++) |
207 this.frame.reg[i] = new LValue("nil", null); |
207 this.frame.reg[i] = new LValue("nil", null); |
208 break; |
208 break; |
209 case OP_GETGLOBAL: |
209 case OP_GETGLOBAL: |
210 var name = this.frame.f.constants[instruction[2]]; |
210 var name = this.frame.f.constants[INS_Bx(instruction)]; |
211 this.frame.reg[instruction[1]] = this.frame.f.environment.index(name); |
211 this.frame.reg[INS_A(instruction)] = this.frame.f.environment.index(name); |
212 break; |
212 break; |
213 case OP_SETGLOBAL: |
213 case OP_SETGLOBAL: |
214 var name = this.frame.f.constants[instruction[2]]; |
214 var name = this.frame.f.constants[INS_Bx(instruction)]; |
215 this.frame.f.environment.setIndex(name, this.frame.reg[instruction[1]]); |
215 this.frame.f.environment.setIndex(name, this.frame.reg[instruction[1]]); |
216 break; |
216 break; |
217 case OP_LOADK: |
217 case OP_LOADK: |
218 var value = this.frame.f.constants[instruction[2]]; |
218 var value = this.frame.f.constants[INS_Bx(instruction)]; |
219 this.frame.reg[instruction[1]] = value; |
219 this.frame.reg[INS_A(instruction)] = value; |
220 break; |
220 break; |
221 case OP_CALL: |
221 case OP_CALL: |
222 var f = this.frame.reg[instruction[1]].call(); // return JS or LValue |
222 var f = this.frame.reg[INS_A(instruction)].call(); // return JS or LValue |
223 var args = this.frame.reg.splice(instruction[1]+1, instruction[1]+(instruction[2]-1)); |
223 var args = this.frame.reg.slice(INS_A(instruction)+1, INS_A(instruction)+(INS_B(instruction))); |
224 if(typeof(f) == "function") |
224 if(typeof(f) == "function") |
225 { |
225 { |
226 // JS native function |
226 // JS native function |
227 var ret = f.apply(null, args.map(function (a) { return a.value; })); |
227 var ret = f.apply(null, args.map(function (a) { return a.value; })); |
228 } |
228 } |