lvm.js

changeset 45
4d2cf0b5235d
parent 42
741b953fcc5f
child 46
33ae4e0de8cc
--- a/lvm.js	Mon May 24 12:54:20 2010 +0100
+++ b/lvm.js	Mon May 24 14:08:50 2010 +0100
@@ -289,76 +289,74 @@
 	},
 	run: function(frame)
 	{
-		var instructions = frame.f.instructions;
-		var reg = frame.reg;
 		var instruction;
 		while(this.callstack.length>0)
 		{
-			instruction = instructions[frame.pc++];
+			instruction = frame.f.instructions[frame.pc++];
 			if(debugMode)
 			{
 				sys.puts("PC: "+(frame.pc-1)+" OP: "+instruction[0]);
-				sys.puts("STACK: "+sys.inspect(reg));
+				sys.puts("STACK: "+sys.inspect(frame.reg));
 			}
 			switch(INS_OPCODE(instruction))
 			{
 			case OP_MOVE:
-				reg[INS_A(instruction)] = reg[INS_B(instruction)];
+				frame.reg[INS_A(instruction)] = frame.reg[INS_B(instruction)];
 				break;
 			case OP_LOADNIL:
 				for(var i = INS_A(instruction);i<=INS_B(instruction);i++)
-					reg[i] = new LValue("nil", null);
+					frame.reg[i] = new LValue("nil", null);
 				break;
 			case OP_LOADBOOL:
-				reg[INS_A(instruction)] = new LValue("boolean", INS_B(instruction)!=0);
+				frame.reg[INS_A(instruction)] = new LValue("boolean", INS_B(instruction)!=0);
 				if(INS_C(instruction)!=0)
 					frame.pc++;
 				break;
 			case OP_GETUPVAL:
-				reg[INS_A(instruction)] = frame.f.upvalues[INS_B(instruction)];
+				frame.reg[INS_A(instruction)] = frame.f.upvalues[INS_B(instruction)];
 				break;
 			case OP_GETGLOBAL:
 				var name = frame.f.constants[INS_Bx(instruction)];
-				reg[INS_A(instruction)] = frame.f.environment.index(name);
+				frame.reg[INS_A(instruction)] = frame.f.environment.index(name);
 				break;
 			case OP_SETUPVAL:
-				var reg = reg[INS_A(instruction)];
+				var reg = frame.reg[INS_A(instruction)];
 				var upvalue = frame.f.upvalues[INS_B(instruction)];
 				upvalue.type = reg.type;
 				upvalue.value = reg.value;
 				break;
 			case OP_SETGLOBAL:
 				var name = frame.f.constants[INS_Bx(instruction)];
-				frame.f.environment.setIndex(name, reg[instruction[1]]);
+				frame.f.environment.setIndex(name, frame.reg[instruction[1]]);
 				break;
 			case OP_LOADK:
 				var constant = frame.f.constants[INS_Bx(instruction)];
-				reg[INS_A(instruction)] = new LValue(constant.type, constant.value);
+				frame.reg[INS_A(instruction)] = new LValue(constant.type, constant.value);
 				break;
 			case OP_NEWTABLE:
-				reg[INS_A(instruction)] = new LValue("table", {});
+				frame.reg[INS_A(instruction)] = new LValue("table", {});
 				break;
 			case OP_GETTABLE:
 				var C = INS_C(instruction);
-				var keysource = (C&256)?frame.f.constants:reg;
+				var keysource = (C&256)?frame.f.constants:frame.reg;
 				var key = keysource[C&0xff];
-				var value = reg[INS_B(instruction)].index(key).value;
-				reg[INS_A(instruction)] = new LValueFromValue(value);
+				var value = frame.reg[INS_B(instruction)].index(key).value;
+				frame.reg[INS_A(instruction)] = new LValueFromValue(value);
 				break;
 			case OP_SETTABLE:
 				var C = INS_C(instruction);
-				var valuesource = (C&256)?frame.f.constants:reg;
+				var valuesource = (C&256)?frame.f.constants:frame.reg;
 				var value = valuesource[C&0xff];
 
 				var B = INS_B(instruction);
-				var keysource = (B&256)?frame.f.constants:reg;
+				var keysource = (B&256)?frame.f.constants:frame.reg;
 				var key = keysource[B&0xff];
-				reg[INS_A(instruction)].setIndex(key, value);
+				frame.reg[INS_A(instruction)].setIndex(key, value);
 				break;
 			case OP_CALL:
-				var f = reg[INS_A(instruction)].call(); // return JS or LValue
+				var f = frame.reg[INS_A(instruction)].call(); // return JS or LValue
 				var A = INS_A(instruction), B = INS_B(instruction), undefined;
-				var args = reg.slice(A+1, B==0?undefined:(A+B));
+				var args = frame.reg.slice(A+1, B==0?undefined:(A+B));
 				for(var i=args.length+1;i<f.maxStackSize;i++)
 					args[i] = new LValue("nil", null);
 				if(typeof(f) == "function")
@@ -378,14 +376,14 @@
 				var prototype_id = INS_Bx(instruction);
 				var chunk = frame.f.chunk.prototypes[prototype_id];
 				var f = new LFunction(chunk, frame.f.environment);
-				reg[INS_A(instruction)] = new LValue("function", f);
+				frame.reg[INS_A(instruction)] = new LValue("function", f);
 				for(var i=0;i<chunk.numUpvalues;i++)
 				{
-					var upval_instruction = instructions[frame.pc++];
+					var upval_instruction = frame.f.instructions[frame.pc++];
 					switch(INS_OPCODE(upval_instruction))
 					{
 					case OP_MOVE:
-						f.upvalues[i] = reg[INS_B(upval_instruction)];
+						f.upvalues[i] = frame.reg[INS_B(upval_instruction)];
 						break;
 					case OP_GETUPVAL:
 						f.upvalues[i] = frame.f.upvalues[INS_B(upval_instruction)];
@@ -407,37 +405,37 @@
 						rets = oldFrame.reg.slice(INS_A(instruction),INS_A(instruction)+(INS_B(instruction)-1));
 					var i;
 					for(i=0;(oldFrame.retCount == 0||i<oldFrame.retCount)&&i<rets.length;i++)
-						reg[oldFrame.retAt+i] = rets[i];
-					if(oldFrame.retAt+i<reg.length)
-						reg.splice(0,oldFrame.retAt+i);
+						frame.reg[oldFrame.retAt+i] = rets[i];
+					if(oldFrame.retAt+i<frame.reg.length)
+						frame.reg.splice(0,oldFrame.retAt+i);
 				}
 				break;
 			case OP_SELF:
-				var table = reg[INS_B(instruction)];
-				reg[INS_A(instruction)+1] = table;
+				var table = frame.reg[INS_B(instruction)];
+				frame.reg[INS_A(instruction)+1] = table;
 				var C = INS_C(instruction);
-				var keysource = (C&256)?frame.f.constants:reg;
+				var keysource = (C&256)?frame.f.constants:frame.reg;
 				var key = keysource[C&0xff];
-				reg[INS_A(instruction)] = table.index(key);
+				frame.reg[INS_A(instruction)] = table.index(key);
 				break;
 			case OP_FORPREP:
 				frame.pc+=(INS_sBx(instruction));
 				var A = INS_A(instruction);
-				reg[A].value -= reg[A+2].value;
-				reg[A+3] = new LValue("number", null);
+				frame.reg[A].value -= frame.reg[A+2].value;
+				frame.reg[A+3] = new LValue("number", null);
 				break;
 			case OP_FORLOOP:
 				var A = INS_A(instruction);
-				var RA = reg[A];
-				RA.value += reg[A+2].value;
-				if(RA.value <= reg[A+1].value)
+				var RA = frame.reg[A];
+				RA.value += frame.reg[A+2].value;
+				if(RA.value <= frame.reg[A+1].value)
 				{
 					frame.pc += INS_sBx(instruction);
-					reg[A+3].value = RA.value;
+					frame.reg[A+3].value = RA.value;
 				}
 				break;
 			case OP_TEST:
-				var RA = reg[INS_A(instruction)];
+				var RA = frame.reg[INS_A(instruction)];
 				var RA_bool = RA.type == "nil" || (RA.type == "boolean" && RA.value == false);
 				if(RA_bool == (INS_C(instruction)!=0))
 					frame.pc++;

mercurial