lvm.js

Wed, 07 Apr 2010 02:46:56 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 07 Apr 2010 02:46:56 +0100
changeset 13
f259da6951e8
parent 12
7d748aba47ab
child 14
21a2fce50931
permissions
-rw-r--r--

Support for reading all fields in the chunk (including function prototypes, required for OP_CLOSURE)

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;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 var OP_GETGLOBAL = 5;
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
6 var OP_SETGLOBAL = 7;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 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
8 var OP_RETURN = 30;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
8
e7de6d1fee96 Add debugMode switch
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
10 var debugMode = false;
e7de6d1fee96 Add debugMode switch
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
11
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function LValue(type, value)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 this.type = type||"nil";
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 this.value = value||null;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 LValue.prototype = {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 call: function (args)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 {
3
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
21 if(this.type == "function")
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
22 return this.value;
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
23 else
6f338fbf0abc Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
24 throw "Attempt to call a " + this.type + " 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 index: function (key)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if(this.type == "table")
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 {
4
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
30 var val = this.value[key.value];
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
31 if(typeof(val) == "undefined")
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
32 return new LValue("nil", null);
0eda73eda4ae Return nil for non-existent table keys
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
33 return val;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 }
7
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
35 else
00ec5f6e7579 Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
36 throw "Attempt to index a " + this.type + " value";
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 },
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 setIndex: function (key, value)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if(this.type == "table")
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 this.value[key.value] = value;
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 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function LValueFromString(string)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 return new LValue("string", string);
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function LValueFromFunction(func)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return new LValue("function", func);
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
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
59 function LBinaryChunk(chunk, start)
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 {
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
61 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
62 this.pos = start||12;
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
63
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
64 this.sourceName = this.readString();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
65 this.lineDefined = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
66 this.lastLineDefined = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
67 this.numUpvalues = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
68 this.numParameters = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
69 this.isVararg = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
70 this.maxStackSize = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
71
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 this.instructions = [];
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
73
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
74 this.numInstructions = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
75 for(var i=0;i<this.numInstructions;i++)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
76 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
77 var ins = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
78 this.instructions.push([
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
79 ins&0x3F, // Opcode
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
80 (ins>>6)&0xFF, // Field A
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
81 (ins>>23)&0x1FF, // Field B
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
82 (ins>>14)&0x1FF // Field C
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
83 ]);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
84 if(debugMode)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
85 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
86 var pi = this.instructions[this.instructions.length-1];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
87 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));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
88 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
89 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
90
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
91 this.constants = [];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
92
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
93 this.numConstants = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
94 for(var i=0;i<this.numConstants;i++)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
95 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
96 var type = this.readByte();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
97 switch(type)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
98 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
99 case 0: // Nil
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
100 this.constants.push(new LValue("nil", null));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
101 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
102 case 1: // Boolean
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
103 this.constants.push(new LValue("boolean", this.readByte()));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
104 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
105 case 3: // Number
12
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
106 this.constants.push(new LValue("number", this.readNumber()));
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
107 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
108 case 4: // String
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
109 this.constants.push(LValueFromString(this.readString()));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
110 break;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
111 default:
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
112 throw "Invalid constant type "+type+" in bytecode";
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 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
115
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
116 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
117
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
118 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
119 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
120 {
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
121 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
122 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
123 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
124 }
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
125
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
126 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
127
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
128 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
129 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
130 {
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
131 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
132 }
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
133
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
134 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
135 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
136 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
137 {
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
138 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
139 }
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
140
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
141 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
142 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
143 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
144 {
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 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
146 }
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
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 return this;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
151 LBinaryChunk.prototype = {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
152 readBytes: function (n)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
153 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
154 return this.chunk.slice(this.pos, this.pos+=n);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
155 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
156 readByte: function ()
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 {
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
158 return this.readBytes(1).charCodeAt(0);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
159 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
160 readInt: function ()
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
161 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
162 //FIXME: Endianness
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
163 return this.readByte() | (this.readByte()<<8)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
164 | (this.readByte()<<16) | (this.readByte()<<24);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
165 },
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
166 readString: function ()
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
167 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
168 var len = this.readInt();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
169 return this.readBytes(len).substring(0,len-1);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 },
12
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
171 readNumber: function ()
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
172 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
173 //FIXME: Endianness
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
174 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
175 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
176
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
177 var sign = (bytes[0]>>7)&0x1;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
178 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
179
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
180 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
181 + (bytes[2] * Math.pow(2,40))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
182 + (bytes[3] * Math.pow(2,32))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
183 + (bytes[4] * Math.pow(2,24))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
184 + (bytes[5] * Math.pow(2,16))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
185 + (bytes[6] * Math.pow(2,8))
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
186 + bytes[7];
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
187
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
188 if(exp != 0x000 && exp != 0x7FF)
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
189 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
190 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
191 return n;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
192 }
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
193 else if(exp == 0x000)
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
194 {
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
195 return sign*0;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
196 }
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
197 else
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
198 return frac==0?sign*Infinity:NaN;
7d748aba47ab Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents: 11
diff changeset
199 }
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
202 function INS_OPCODE(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
203 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
204 return ins[0];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
205 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
206
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
207 function INS_A(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
208 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
209 return ins[1];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
210 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
211
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
212 function INS_B(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
213 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
214 return ins[2];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
215 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
216
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
217 function INS_C(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
218 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
219 return ins[3];
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
220 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
221
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
222 function INS_Bx(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
223 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
224 return ((INS_C(ins))|(INS_B(ins)<<9));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
225 }
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
226
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
227 function INS_sBx(ins)
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
228 {
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
229 return INS_Bx(ins)+0x1FFFE;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
230 }
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 function LFunction(chunk, env)
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 function F() {};
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
235 F.prototype = chunk;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
236 var o = new F();
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
237 o.environment = env;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
238 return o;
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
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 function LVM()
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 this.callstack = [];
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 this.stack = [];
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 return this;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 LVM.prototype = {
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 run: function (lfFunction)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 {
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
251 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
252 this.callstack.push(this.frame);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 var instruction;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 while(this.callstack.length>0)
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 {
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
256 instruction = this.frame.f.instructions[this.frame.pc++];
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
257 if(debugMode)
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 sys.puts("PC: "+(this.frame.pc-1)+" OP: "+instruction[0]);
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
260 sys.puts("STACK: "+JSON.stringify(this.frame.reg));
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 switch(INS_OPCODE(instruction))
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 {
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
264 case OP_MOVE:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
265 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
266 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
267 case OP_LOADNIL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
268 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
269 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
270 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 case OP_GETGLOBAL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
272 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
273 this.frame.reg[INS_A(instruction)] = this.frame.f.environment.index(name);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 break;
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
275 case OP_SETGLOBAL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
276 var name = this.frame.f.constants[INS_Bx(instruction)];
6
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
277 this.frame.f.environment.setIndex(name, this.frame.reg[instruction[1]]);
418fd175eaed Implement OP_SETGLOBAL
Matthew Wild <mwild1@gmail.com>
parents: 5
diff changeset
278 break;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 case OP_LOADK:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
280 var value = this.frame.f.constants[INS_Bx(instruction)];
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
281 this.frame.reg[INS_A(instruction)] = value;
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 break;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 case OP_CALL:
10
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
284 var f = this.frame.reg[INS_A(instruction)].call(); // return JS or LValue
ce2f27fa25a4 Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
285 var args = this.frame.reg.slice(INS_A(instruction)+1, INS_A(instruction)+(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
286 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
287 {
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
288 // 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
289 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
290 }
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
291 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
292 {
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
293 // Lua 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
294 }
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 break;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 case OP_RETURN:
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 this.callstack.pop();
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 break;
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 default:
11
1b3267149cbd Throw error on unhandled opcodes
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
300 throw "Unhandled opcode: "+INS_OPCODE(instruction);
0
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 }
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 };
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305
2fcdf7f16d71 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 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
307
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
308 var fs=require("fs");
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
309 var sys=require("sys");
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
310 var c = new LBinaryChunk(fs.readFileSync("luac.out", "binary"));
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
311
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
312 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
313
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
314 var print;
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
315 if(typeof(document) == "object")
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
316 print = function (a) { document.write(a+"<br/>") }; // Browser
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
317 else
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
318 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
319
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
320 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
321
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
322 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
323
5
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
324 try{
9
3f055c9ab80e Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
325 testvm.run(f);
5
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
326 }
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
327 catch(e)
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
328 {
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
329 print("Error: " + e);
c5c9c4f2d1d3 Print error on error
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
330 }

mercurial