# HG changeset patch # User Matthew Wild # Date 1290182575 0 # Node ID 6b43d68abc943f5a68d242d79e88bd620fbb7b23 # Parent dc73a60b3c06914ab0bb39a885778ae8ddc43b44 Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods diff -r dc73a60b3c06 -r 6b43d68abc94 lvm.js --- a/lvm.js Fri Nov 19 13:09:25 2010 +0000 +++ b/lvm.js Fri Nov 19 16:02:55 2010 +0000 @@ -25,8 +25,9 @@ var debugMode = false; -function LValue(type, value) +function LValue(vm, type, value) { + this.vm = vm; this.type = type||"nil"; this.value = value; } @@ -59,15 +60,15 @@ return this.value[key.value]; else if(raw != true && this.metatable && this.metatable.type != "nil") { - var __index = this.metatable.index(new LValue("string", "__index")); + var __index = this.metatable.index(this.vm.LValue("__index")); if(__index.type == "function") { - return LValueFromValue(__index.call([this, key])[0]); + return this.vm.LValue(__index.call([this, key])[0]); } else if(__index.type != "nil") return __index.index(key); } - return new LValue("nil", null); + return this.vm.LValue(null); } else throw "Attempt to index a " + this.type + " value"; @@ -103,7 +104,7 @@ add: function (op2) { var metamethod; - var __add = LValueFromString("__add"); + var __add = this.vm.LValue("__add"); if(this.metatable) metamethod = this.metatable.index(__add); if((!metamethod || metamethod.type == "nil") && op2.metatable) @@ -122,44 +123,7 @@ } }; -function LValueFromString(string) -{ - return new LValue("string", string); -} - -function LValueFromFunction(vm, func) -{ - var val = new LValue("function", func); - val.vm = vm; - return val; -} - -function LValueFromValue(value) -{ - switch(typeof(value)) - { - case "number": - return new LValue("number", value); - case "string": - return new LValueFromString(value); - case "function": - return new LValueFromFunction(value); - case "object": - if(value == null) - return new LValue("nil", null); - else - return new LValue("table", value); - case "undefined": - return new LValue("nil", null); - default: - sys.puts( "Not able to convert type " + - typeof(value)+" from Javascript to Lua: "+sys.inspect(value)); - throw "Not able to convert type " + - typeof(value)+" from Javascript to Lua"; - } -} - -function LBinaryChunk(chunk, start) +function LBinaryChunk(vm, chunk, start) { this.chunk = chunk; this.pos = start||12; @@ -200,16 +164,16 @@ switch(type) { case 0: // Nil - this.constants.push(new LValue("nil", null)); + this.constants.push(new LValue(vm, "nil", null)); break; case 1: // Boolean - this.constants.push(new LValue("boolean", this.readByte())); + this.constants.push(new LValue(vm, "boolean", this.readByte())); // FIXME type break; case 3: // Number - this.constants.push(new LValue("number", this.readNumber())); + this.constants.push(new LValue(vm, "number", this.readNumber())); break; case 4: // String - this.constants.push(LValueFromString(this.readString())); + this.constants.push(new LValue(vm, "string", this.readString())); break; default: throw "Invalid constant type "+type+" in bytecode"; @@ -221,7 +185,7 @@ this.numPrototypes = this.readInt(); for(var i=0;i