lvm.js

Sun, 23 May 2010 17:30:43 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 23 May 2010 17:30:43 +0100
changeset 37
62c1d9bf3000
parent 36
9e7b57b0b78f
child 38
ba347b4b655f
permissions
-rw-r--r--

Allow LValues with a value of 'false' to be create (it was converted to 'null')

0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
2 var OP_MOVE = 0;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 var OP_LOADK = 1;
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
4 var OP_LOADNIL = 3;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
5 var OP_GETUPVAL = 4;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 var OP_GETGLOBAL = 5;
28
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
7 var OP_GETTABLE = 6;
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
8 var OP_SETGLOBAL = 7;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
9 var OP_SETUPVAL = 8;
28
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
10 var OP_SETTABLE = 9;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
11 var OP_NEWTABLE = 10;
36
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
12 var OP_SELF = 11;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 var OP_CALL = 28;
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
14 var OP_RETURN = 30;
34
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
15 var OP_FORLOOP = 31;
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
16 var OP_FORPREP = 32;
15
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
17 var OP_CLOSURE = 36;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
8
e7de6d1fee96 Add debugMode switch
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
19 var debugMode = false;
e7de6d1fee96 Add debugMode switch
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
20
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 function LValue(type, value)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 this.type = type||"nil";
37
62c1d9bf3000 Allow LValues with a value of 'false' to be create (it was converted to 'null')
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
24 this.value = value;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 LValue.prototype = {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 call: function (args)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 {
3
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
30 if(this.type == "function")
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
31 return this.value;
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
32 else
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
33 throw "Attempt to call a " + this.type + " value";
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 },
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 index: function (key)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if(this.type == "table")
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 {
4
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
39 var val = this.value[key.value];
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
40 if(typeof(val) == "undefined")
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
41 return new LValue("nil", null);
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
42 return val;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 }
7
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
44 else
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
45 throw "Attempt to index a " + this.type + " value";
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 },
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 setIndex: function (key, value)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if(this.type == "table")
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 this.value[key.value] = value;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 }
7
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
53 else
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
54 throw "Attempt to index a " + this.type + " value";
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 function LValueFromString(string)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return new LValue("string", string);
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 function LValueFromFunction(func)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 return new LValue("function", func);
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
27
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
68 function LValueFromValue(value)
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
69 {
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
70 switch(typeof(value))
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
71 {
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
72 case "string":
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
73 return new LValueFromString(value);
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
74 case "function":
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
75 return new LValueFromFunction(value);
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
76 case "object":
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
77 if(value == null)
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
78 return new LValue("nil", value);
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
79 default:
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
80 sys.puts( "Not able to convert type " +
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
81 typeof(value)+" from Javascript to Lua: "+sys.inspect(value));
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
82 throw "Not able to convert type " +
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
83 typeof(value)+" from Javascript to Lua";
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
84 }
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
85 }
35a2203253a6 New LValueFromValue() to convert from any Javascript value (almost) to a LValue
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
86
13
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
87 function LBinaryChunk(chunk, start)
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 {
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
89 this.chunk = chunk;
13
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
90 this.pos = start||12;
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
91
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
92 this.sourceName = this.readString();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
93 this.lineDefined = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
94 this.lastLineDefined = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
95 this.numUpvalues = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
96 this.numParameters = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
97 this.isVararg = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
98 this.maxStackSize = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
99
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 this.instructions = [];
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
101
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
102 this.numInstructions = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
103 for(var i=0;i<this.numInstructions;i++)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
104 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
105 var ins = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
106 this.instructions.push([
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
107 ins&0x3F, // Opcode
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
108 (ins>>6)&0xFF, // Field A
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
109 (ins>>23)&0x1FF, // Field B
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
110 (ins>>14)&0x1FF // Field C
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
111 ]);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
112 if(debugMode)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
113 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
114 var pi = this.instructions[this.instructions.length-1];
31
bc58527bac34 Print sBx field in bytecode dump (for debugging)
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
115 sys.puts("Pos: "+(this.pos-4)+" Ins: "+ins+" OP: "+INS_OPCODE(pi)+" A: "+INS_A(pi)+" B: "+INS_B(pi)+" C: "+INS_C(pi)+" Bx: "+INS_Bx(pi)+" sBx: "+(INS_Bx(pi)-0x1FFFE));
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
116 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
117 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
118
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
119 this.constants = [];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
120
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
121 this.numConstants = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
122 for(var i=0;i<this.numConstants;i++)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
123 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
124 var type = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
125 switch(type)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
126 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
127 case 0: // Nil
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
128 this.constants.push(new LValue("nil", null));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
129 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
130 case 1: // Boolean
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
131 this.constants.push(new LValue("boolean", this.readByte()));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
132 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
133 case 3: // Number
12
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
134 this.constants.push(new LValue("number", this.readNumber()));
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
135 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
136 case 4: // String
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
137 this.constants.push(LValueFromString(this.readString()));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
138 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
139 default:
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
140 throw "Invalid constant type "+type+" in bytecode";
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
141 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
142 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
143
13
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
144 this.prototypes = [];
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
145
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
146 this.numPrototypes = this.readInt();
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
147 for(var i=0;i<this.numPrototypes;i++)
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
148 {
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
149 var p = new LBinaryChunk(chunk, this.pos);
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
150 this.pos = p.pos;
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
151 this.prototypes.push(p);
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
152 }
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
153
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
154 this.sourceLines = [];
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
155
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
156 this.numSourceLines = this.readInt();
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
157 for(var i=0;i<this.numSourceLines;i++)
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
158 {
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
159 this.sourceLines.push(this.readInt());
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
160 }
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
161
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
162 this.localList = [];
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
163 this.numLocalList = this.readInt();
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
164 for(var i=0;i<this.numLocalList;i++)
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
165 {
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
166 this.localList.push([this.readString(),this.readInt(),this.readInt()]);
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
167 }
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
168
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
169 this.upvalueList = [];
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
170 this.numUpvalueList = this.readInt();
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
171 for(var i=0;i<this.numUpvalueList;i++)
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
172 {
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
173 this.upvalueList.push(this.readString());
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
174 }
f259da6951e8 Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
175
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 return this;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
179 LBinaryChunk.prototype = {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
180 readBytes: function (n)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
181 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
182 return this.chunk.slice(this.pos, this.pos+=n);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
183 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
184 readByte: function ()
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 {
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
186 return this.readBytes(1).charCodeAt(0);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
187 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
188 readInt: function ()
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
189 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
190 //FIXME: Endianness
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
191 return this.readByte() | (this.readByte()<<8)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
192 | (this.readByte()<<16) | (this.readByte()<<24);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
193 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
194 readString: function ()
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
195 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
196 var len = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
197 return this.readBytes(len).substring(0,len-1);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 },
12
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
199 readNumber: function ()
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
200 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
201 //FIXME: Endianness
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
202 var bytes = [this.readByte(),this.readByte(),this.readByte(),this.readByte(),
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
203 this.readByte(),this.readByte(),this.readByte(),this.readByte()].reverse();
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
204
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
205 var sign = (bytes[0]>>7)&0x1;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
206 var exp = (bytes[0]&0x7F)<<4 | (bytes[1]&0xf0)>>4;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
207
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
208 var frac = ((bytes[1] & 0x0f) * Math.pow(2,48))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
209 + (bytes[2] * Math.pow(2,40))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
210 + (bytes[3] * Math.pow(2,32))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
211 + (bytes[4] * Math.pow(2,24))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
212 + (bytes[5] * Math.pow(2,16))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
213 + (bytes[6] * Math.pow(2,8))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
214 + bytes[7];
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
215
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
216 if(exp != 0x000 && exp != 0x7FF)
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
217 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
218 var n = (sign==1?-1:1)*Math.pow(2,exp-1023)*(1+(frac/0x10000000000000));
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
219 return n;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
220 }
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
221 else if(exp == 0x000)
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
222 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
223 return sign*0;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
224 }
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
225 else
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
226 return frac==0?sign*Infinity:NaN;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
227 }
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
230 function INS_OPCODE(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
231 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
232 return ins[0];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
233 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
234
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
235 function INS_A(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
236 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
237 return ins[1];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
238 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
239
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
240 function INS_B(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
241 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
242 return ins[2];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
243 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
244
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
245 function INS_C(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
246 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
247 return ins[3];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
248 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
249
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
250 function INS_Bx(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
251 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
252 return ((INS_C(ins))|(INS_B(ins)<<9));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
253 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
254
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
255 function INS_sBx(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
256 {
32
035aacc192d8 Fix off-by-one in calculating the value of sBx
Matthew Wild <mwild1@gmail.com>
parents: 31
diff changeset
257 return (INS_Bx(ins)-0x1FFFF);
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
258 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
259
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
260 function LFunction(chunk, env)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
261 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
262 function F() {};
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
263 F.prototype = chunk;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
264 var o = new F();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
265 o.environment = env;
14
21a2fce50931 Add chunk property to LFunction to show which chunk it came from
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
266 o.chunk = chunk;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
267 o.upvalues = [];
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
268 return o;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
269 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
270
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 function LVM()
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 this.callstack = [];
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 this.stack = [];
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 return this;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 LVM.prototype = {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 run: function (lfFunction)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 {
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
281 this.frame = {f:lfFunction,pc:0,reg:[]};
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
282 this.callstack.push(this.frame);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 var instruction;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 while(this.callstack.length>0)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 {
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
286 instruction = this.frame.f.instructions[this.frame.pc++];
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
287 if(debugMode)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
288 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
289 sys.puts("PC: "+(this.frame.pc-1)+" OP: "+instruction[0]);
21
9b5cc503bc31 Switch from JSON.stringify for debug output to sys.inspect which doesn't bail out on circular references
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
290 sys.puts("STACK: "+sys.inspect(this.frame.reg));
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
291 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
292 switch(INS_OPCODE(instruction))
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 {
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
294 case OP_MOVE:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
295 this.frame.reg[INS_A(instruction)] = this.frame.reg[INS_B(instruction)];
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
296 break;
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
297 case OP_LOADNIL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
298 for(var i = INS_A(instruction);i<=INS_B(instruction);i++)
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
299 this.frame.reg[i] = new LValue("nil", null);
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
300 break;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
301 case OP_GETUPVAL:
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
302 this.frame.reg[INS_A(instruction)] = this.frame.f.upvalues[INS_B(instruction)];
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
303 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 case OP_GETGLOBAL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
305 var name = this.frame.f.constants[INS_Bx(instruction)];
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
306 this.frame.reg[INS_A(instruction)] = this.frame.f.environment.index(name);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 break;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
308 case OP_SETUPVAL:
26
5c7eafb47830 Implement OP_SETUPVALUE \o/
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
309 var reg = this.frame.reg[INS_A(instruction)];
5c7eafb47830 Implement OP_SETUPVALUE \o/
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
310 var upvalue = this.frame.f.upvalues[INS_B(instruction)];
5c7eafb47830 Implement OP_SETUPVALUE \o/
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
311 upvalue.type = reg.type;
5c7eafb47830 Implement OP_SETUPVALUE \o/
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
312 upvalue.value = reg.value;
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
313 break;
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
314 case OP_SETGLOBAL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
315 var name = this.frame.f.constants[INS_Bx(instruction)];
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
316 this.frame.f.environment.setIndex(name, this.frame.reg[instruction[1]]);
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
317 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 case OP_LOADK:
33
6cc1b0a8dd97 Instantiate a new LValue in OP_LOADK, otherwise "constants" aren't so constant...
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
319 var constant = this.frame.f.constants[INS_Bx(instruction)];
6cc1b0a8dd97 Instantiate a new LValue in OP_LOADK, otherwise "constants" aren't so constant...
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
320 this.frame.reg[INS_A(instruction)] = new LValue(constant.type, constant.value);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 break;
28
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
322 case OP_NEWTABLE:
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
323 this.frame.reg[INS_A(instruction)] = new LValue("table", {});
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
324 break;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
325 case OP_GETTABLE:
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
326 var C = INS_C(instruction);
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
327 var keysource = (C&256)?this.frame.f.constants:this.frame.reg;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
328 var key = keysource[C&0xff];
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
329 var value = this.frame.reg[INS_B(instruction)].index(key).value;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
330 this.frame.reg[INS_A(instruction)] = new LValueFromValue(value);
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
331 break;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
332 case OP_SETTABLE:
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
333 var C = INS_C(instruction);
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
334 var valuesource = (C&256)?this.frame.f.constants:this.frame.reg;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
335 var value = valuesource[C&0xff];
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
336
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
337 var B = INS_B(instruction);
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
338 var keysource = (B&256)?this.frame.f.constants:this.frame.reg;
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
339 var key = keysource[B&0xff];
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
340 this.frame.reg[INS_A(instruction)].setIndex(key, value);
d14b47c3870f Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
341 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 case OP_CALL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
343 var f = this.frame.reg[INS_A(instruction)].call(); // return JS or LValue
18
ae0b4ec242a3 Support for B==0 in OP_CALL (signifies params are to top of the stack)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
344 var A = INS_A(instruction), B = INS_B(instruction), undefined;
ae0b4ec242a3 Support for B==0 in OP_CALL (signifies params are to top of the stack)
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
345 var args = this.frame.reg.slice(A+1, B==0?undefined:(A+B));
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
346 if(typeof(f) == "function")
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
347 {
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
348 // JS native function
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
349 var ret = f.apply(null, args.map(function (a) { return a.value; }));
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
350 }
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
351 else
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
352 {
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
353 // Lua function
19
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
354 this.frame = {f:f,pc:0,reg:args,
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
355 retAt:INS_A(instruction),retCount:INS_C(instruction)};
16
9c8710ea2a5a Implement OP_CALL for Lua functions (no return values yet)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
356 this.callstack.push(this.frame);
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
357 }
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 break;
15
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
359 case OP_CLOSURE:
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
360 var prototype_id = INS_Bx(instruction);
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
361 var chunk = this.frame.f.chunk.prototypes[prototype_id];
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
362 var f = new LFunction(chunk, this.frame.f.environment);
24
5c9d8a65c87d It's required to insert the new function into the stack before processing its upvalue instructions, in case it refers to itself.
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
363 this.frame.reg[INS_A(instruction)] = new LValue("function", f);
20
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
364 for(var i=0;i<chunk.numUpvalues;i++)
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
365 {
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
366 var upval_instruction = this.frame.f.instructions[this.frame.pc++];
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
367 switch(INS_OPCODE(upval_instruction))
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
368 {
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
369 case OP_MOVE:
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
370 f.upvalues[i] = this.frame.reg[INS_B(upval_instruction)];
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
371 break;
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
372 case OP_GETUPVAL:
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
373 f.upvalues[i] = this.frame.f.upvalues[INS_B(upval_instruction)];
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
374 break;
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
375 default:
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
376 throw "Invalid upvalue opcode following OP_CLOSURE";
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
377 }
977ae93f612c Support for upvalues in functions \o/ implemented OP_GETUPVAL, and a stub for OP_SETUPVAL.
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
378 }
15
5240eaff785f Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
379 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 case OP_RETURN:
19
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
381 var oldFrame = this.callstack.pop();
17
ac02246fc1d1 Fix OP_RETURN to update this.frame
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
382 this.frame = this.callstack[this.callstack.length-1];
19
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
383 if(this.frame)
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
384 {
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
385 var rets;
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
386 if(INS_B(instruction) == 0)
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
387 rets = oldFrame.reg.slice(INS_A(instruction));
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
388 else
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
389 rets = oldFrame.reg.slice(INS_A(instruction),INS_A(instruction)+(INS_B(instruction)-1));
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
390 var i;
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
391 for(i=0;(oldFrame.retCount == 0||i<oldFrame.retCount)&&i<rets.length;i++)
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
392 this.frame.reg[oldFrame.retAt+i] = rets[i];
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
393 if(oldFrame.retAt+i<this.frame.reg.length)
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
394 this.frame.reg.splice(0,oldFrame.retAt+i);
8c9c1752272b OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
395 }
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 break;
36
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
397 case OP_SELF:
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
398 var table = this.frame.reg[INS_B(instruction)];
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
399 this.frame.reg[INS_A(instruction)+1] = table;
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
400 var C = INS_C(instruction);
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
401 var keysource = (C&256)?this.frame.f.constants:this.frame.reg;
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
402 var key = keysource[C&0xff];
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
403 this.frame.reg[INS_A(instruction)] = table.index(key);
9e7b57b0b78f Implement OP_SELF
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
404 break;
34
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
405 case OP_FORPREP:
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
406 this.frame.pc+=(INS_sBx(instruction));
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
407 var A = INS_A(instruction);
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
408 this.frame.reg[A].value -= this.frame.reg[A+2].value;
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
409 this.frame.reg[A+3] = new LValue("number", null);
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
410 break;
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
411 case OP_FORLOOP:
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
412 var A = INS_A(instruction);
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
413 var RA = this.frame.reg[A];
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
414 RA.value += this.frame.reg[A+2].value;
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
415 if(RA.value <= this.frame.reg[A+1].value)
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
416 {
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
417 this.frame.pc += INS_sBx(instruction);
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
418 this.frame.reg[A+3].value = RA.value;
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
419 }
2c3d73c76d0f OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents: 33
diff changeset
420 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 default:
11
1b3267149cbd Throw error on unhandled opcodes
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
422 throw "Unhandled opcode: "+INS_OPCODE(instruction);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 var testvm = new LVM();
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
429
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
430 var fs=require("fs");
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
431 var sys=require("sys");
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
432 var c = new LBinaryChunk(fs.readFileSync("luac.out", "binary"));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
433
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
434 var default_environment = new LValue("table", {});
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
435
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
436 var print;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
437 if(typeof(document) == "object")
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
438 print = function (a) { document.write(a+"<br/>") }; // Browser
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
439 else
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
440 print = require("sys").puts; // Nodejs
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
441
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
442 default_environment.setIndex(LValueFromString("print"), LValueFromFunction(print));
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
443
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
444 var f = new LFunction(c, default_environment);
2
253863ece36f Implement OP_MOVE, OP_LOADNIL and OP_RETURN. Also change the way OP_CALL is implemented, and update the test code with a more complicated (kind of) sample.
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
445
29
62f3df8ed204 Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
446 try{
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
447 testvm.run(f);
29
62f3df8ed204 Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
448 }
62f3df8ed204 Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
449 catch(e)
25
9e62bc13b30d Add new debug option - rawExceptions - to prevent catching exceptions thrown by the VM, use for debugging the VM.
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
450 {
29
62f3df8ed204 Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
451 print("Error: " + e);
35
3de1d63ffdf7 Fix for error handling to handle string exceptions
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
452 if(typeof(e) == "object" && "stack" in e)
29
62f3df8ed204 Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
453 print(e.stack);
5
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
454 }

mercurial