Implement LValue.len() and OP_LEN (no metamethod yet)

Mon, 22 Nov 2010 02:34:26 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 22 Nov 2010 02:34:26 +0000
changeset 116
b78e86780939
parent 115
1555cb5a97b3
child 117
8f1045a7f1d6

Implement LValue.len() and OP_LEN (no metamethod yet)

lvm.js file | annotate | diff | comparison | revisions
--- a/lvm.js	Mon Nov 22 02:33:56 2010 +0000
+++ b/lvm.js	Mon Nov 22 02:34:26 2010 +0000
@@ -19,7 +19,7 @@
 var OP_POW = 17;
 var OP_UNM = 18;
 var OP_NOT = 19;
-//VAR OP_LEN = 20;
+var OP_LEN = 20;
 var OP_CONCAT = 21;
 var OP_JMP = 22;
 var OP_EQ = 23;
@@ -160,6 +160,17 @@
 			}
 		}
 		return false;
+	},
+	len: function ()
+	{
+		switch(this.type)
+		{
+		case "string":
+		case "table":
+			return this.value.length;
+		default:
+			throw "attempt to get length of a "+this.type+" value";
+		}
 	}
 };
 
@@ -652,6 +663,10 @@
 				var RB = frame.reg[INS_B(instruction)];
 				frame.reg[INS_A(instruction)] = new LValue(this, "boolean", !RB.truth());
 				break;
+			case OP_LEN:
+				var RB = frame.reg[INS_B(instruction)];
+				frame.reg[INS_A(instruction)] = new LValue(this, "number", RB.len());
+				break;
 			case OP_EQ:
 				var A = INS_A(instruction);
 				var RB = RK(frame, INS_B(instruction));

mercurial