# HG changeset patch # User Matthew Wild # Date 1290393266 0 # Node ID b78e86780939689cc93577a87abc8031f3a4a7a2 # Parent 1555cb5a97b3e84ae501ad83171ad065251f38f9 Implement LValue.len() and OP_LEN (no metamethod yet) diff -r 1555cb5a97b3 -r b78e86780939 lvm.js --- 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));