Sun, 21 Nov 2010 21:25:24 +0000
Implement OP_MUL (no metamethod yet)
0 | 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 | 3 | var OP_LOADK = 1; |
39 | 4 | var OP_LOADBOOL = 2; |
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
|
5 | 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
|
6 | var OP_GETUPVAL = 4; |
0 | 7 | var OP_GETGLOBAL = 5; |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
8 | var OP_GETTABLE = 6; |
6 | 9 | 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
|
10 | var OP_SETUPVAL = 8; |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
11 | var OP_SETTABLE = 9; |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
12 | var OP_NEWTABLE = 10; |
36 | 13 | var OP_SELF = 11; |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
14 | var OP_ADD = 12; |
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
15 | var OP_SUB = 13; |
110
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
16 | var OP_MUL = 14; |
99
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
17 | var OP_DIV = 15; |
108
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
18 | //var OP_MOD = 16; |
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
19 | //var OP_POW = 17; |
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
20 | //var OP_UNM = 18; |
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
21 | //var OP_NOT = 19; |
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
22 | //VAR OP_LEN = 20; |
101
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
23 | var OP_CONCAT = 21; |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
24 | var OP_JMP = 22; |
81 | 25 | var OP_EQ = 23; |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
26 | var OP_LT = 24; |
90
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
27 | var OP_LE = 25; |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
28 | var OP_TEST = 26; |
59 | 29 | var OP_TESTSET = 27; |
0 | 30 | var OP_CALL = 28; |
108
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
31 | //var OP_TAILCALL = 29; |
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
|
32 | var OP_RETURN = 30; |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
33 | var OP_FORLOOP = 31; |
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
34 | var OP_FORPREP = 32; |
102 | 35 | var OP_TFORLOOP = 33; |
103 | 36 | var OP_SETLIST = 34; |
108
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
37 | //var OP_CLOSE = 35; |
15
5240eaff785f
Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
38 | var OP_CLOSURE = 36; |
108
c5ba02673ba9
Add unimplemented OP_* codes as reference
Matthew Wild <mwild1@gmail.com>
parents:
107
diff
changeset
|
39 | //var OP_VARARG = 37; |
0 | 40 | |
91
298efa276aff
Disable debugMode, accidentally committed
Matthew Wild <mwild1@gmail.com>
parents:
90
diff
changeset
|
41 | var debugMode = false; |
8 | 42 | |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
43 | function LValue(vm, type, value) |
0 | 44 | { |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
45 | this.vm = vm; |
0 | 46 | 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
|
47 | this.value = value; |
0 | 48 | } |
49 | ||
50 | LValue.prototype = { | |
51 | call: function (args) | |
52 | { | |
49
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
53 | var f = this.precall(); |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
54 | var ret = this.vm.call(f, args); |
49
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
55 | if(typeof(ret) == "undefined") |
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
56 | ret = []; |
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
57 | return ret; |
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
58 | }, |
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
59 | precall: function () |
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
60 | { |
3
6f338fbf0abc
Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
61 | if(this.type == "function") |
6f338fbf0abc
Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
62 | return this.value; |
6f338fbf0abc
Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
63 | else |
6f338fbf0abc
Throw an error if trying to call a non-function
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
64 | throw "Attempt to call a " + this.type + " value"; |
0 | 65 | }, |
54
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
66 | index: function (key, raw) |
0 | 67 | { |
68 | if(this.type == "table") | |
69 | { | |
54
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
70 | var val; |
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
71 | if(key.value in this.value) |
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
72 | return this.value[key.value]; |
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
73 | else if(raw != true && this.metatable && this.metatable.type != "nil") |
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
74 | { |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
75 | var __index = this.metatable.index(this.vm.LValue("__index")); |
60
430a0b155703
Support for tables in __index
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
76 | if(__index.type == "function") |
54
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
77 | { |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
78 | return this.vm.LValue(__index.call([this, key])[0]); |
54
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
79 | } |
60
430a0b155703
Support for tables in __index
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
80 | else if(__index.type != "nil") |
430a0b155703
Support for tables in __index
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
81 | return __index.index(key); |
54
5e0bdf7f234f
Support for __index metamethod
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
82 | } |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
83 | return this.vm.LValue(null); |
0 | 84 | } |
7
00ec5f6e7579
Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
85 | else |
00ec5f6e7579
Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
86 | throw "Attempt to index a " + this.type + " value"; |
0 | 87 | }, |
88 | setIndex: function (key, value) | |
89 | { | |
90 | if(this.type == "table") | |
91 | { | |
92 | this.value[key.value] = value; | |
93 | } | |
7
00ec5f6e7579
Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
94 | else |
00ec5f6e7579
Add errors for when trying to index non-tables
Matthew Wild <mwild1@gmail.com>
parents:
6
diff
changeset
|
95 | throw "Attempt to index a " + this.type + " value"; |
53 | 96 | }, |
97 | setMetatable: function (metatable) | |
98 | { | |
99 | if(metatable.type == "table") | |
100 | this.metatable = metatable; | |
101 | else if(metatable.type == "nil") | |
102 | this.metatable = null; | |
103 | else | |
104 | throw "Attempt to set a "+metatable.type+" value as a metatable"; | |
61 | 105 | }, |
106 | toString: function () | |
107 | { | |
108 | switch(this.type) | |
109 | { | |
110 | case "nil": | |
111 | return "nil"; | |
112 | default: | |
113 | return this.value.toString(); | |
114 | } | |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
115 | }, |
86
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
116 | truth: function () |
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
117 | { |
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
118 | if(this.type == "nil" || (this.type == "boolean" && this.value == false)) |
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
119 | return false; |
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
120 | return true; |
97678a1565d4
Add LValue.truth() to return whether a value is (not(false) and not(nil))
Matthew Wild <mwild1@gmail.com>
parents:
85
diff
changeset
|
121 | }, |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
122 | add: function (op2) |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
123 | { |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
124 | var metamethod; |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
125 | var __add = this.vm.LValue("__add"); |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
126 | if(this.metatable) |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
127 | metamethod = this.metatable.index(__add); |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
128 | if((!metamethod || metamethod.type == "nil") && op2.metatable) |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
129 | metamethod = op2.metatable.index(__add); |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
130 | if(metamethod && metamethod.type != "nil") |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
131 | { |
75
aaecd573ee30
Support for __add metamethod
Matthew Wild <mwild1@gmail.com>
parents:
74
diff
changeset
|
132 | return metamethod.call([this, op2]); |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
133 | } |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
134 | else if((this.type == "number" || this.type == "string") |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
135 | && (op2.type == "number" || op2.type == "string")) |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
136 | { |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
137 | // Plain addition |
76
d7d7a6bb90cd
Fix OP_ADD code to use new LValue format
Matthew Wild <mwild1@gmail.com>
parents:
75
diff
changeset
|
138 | return this.vm.LValue(parseFloat(this.value, 10) + parseFloat(op2.value, 10)); |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
139 | } |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
140 | else |
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
141 | throw "Attempt to perform arithmetic on a "+this.type+" and "+op2.type; |
80
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
142 | }, |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
143 | equals: function (op2) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
144 | { |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
145 | if(this.type != op2.type) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
146 | return false; |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
147 | if(this.value == op2.value) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
148 | return true; |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
149 | var __eq = this.vm.LValue("__eq"); |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
150 | if(this.metatable && op2.metatable) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
151 | { |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
152 | var metamethod1 = this.metatable.index(__eq); |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
153 | var metamethod2 = op2.metatable.index(__eq); |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
154 | if(metamethod1.equals(metamethod2)) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
155 | { |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
156 | var result = metamethod1.call([this, op2]); |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
157 | return (result[0].type != "nil" |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
158 | && (result[0].type != "boolean" || result[0].value == true) |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
159 | ); |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
160 | } |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
161 | } |
beb0bb936aca
Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)
Matthew Wild <mwild1@gmail.com>
parents:
79
diff
changeset
|
162 | return false; |
0 | 163 | } |
164 | }; | |
165 | ||
92
e9da9847121a
Pass sourceName to child chunks (in binary files it's only set in the top-level chunk)
Matthew Wild <mwild1@gmail.com>
parents:
91
diff
changeset
|
166 | function LBinaryChunk(vm, chunk, start, sourceName) |
0 | 167 | { |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
168 | 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
|
169 | this.pos = start||12; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
170 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
171 | this.sourceName = this.readString(); |
92
e9da9847121a
Pass sourceName to child chunks (in binary files it's only set in the top-level chunk)
Matthew Wild <mwild1@gmail.com>
parents:
91
diff
changeset
|
172 | if(sourceName) |
e9da9847121a
Pass sourceName to child chunks (in binary files it's only set in the top-level chunk)
Matthew Wild <mwild1@gmail.com>
parents:
91
diff
changeset
|
173 | this.sourceName = sourceName; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
174 | this.lineDefined = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
175 | this.lastLineDefined = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
176 | this.numUpvalues = this.readByte(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
177 | this.numParameters = this.readByte(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
178 | this.isVararg = this.readByte(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
179 | this.maxStackSize = this.readByte(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
180 | |
0 | 181 | this.instructions = []; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
182 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
183 | this.numInstructions = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
184 | for(var i=0;i<this.numInstructions;i++) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
185 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
186 | var ins = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
187 | this.instructions.push([ |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
188 | ins&0x3F, // Opcode |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
189 | (ins>>6)&0xFF, // Field A |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
190 | (ins>>23)&0x1FF, // Field B |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
191 | (ins>>14)&0x1FF // Field C |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
192 | ]); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
193 | if(debugMode) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
194 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
195 | 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
|
196 | 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
|
197 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
198 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
199 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
200 | this.constants = []; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
201 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
202 | this.numConstants = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
203 | for(var i=0;i<this.numConstants;i++) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
204 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
205 | var type = this.readByte(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
206 | switch(type) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
207 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
208 | case 0: // Nil |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
209 | this.constants.push(new LValue(vm, "nil", null)); |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
210 | break; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
211 | case 1: // Boolean |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
212 | this.constants.push(new LValue(vm, "boolean", this.readByte())); // FIXME type |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
213 | break; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
214 | case 3: // Number |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
215 | this.constants.push(new LValue(vm, "number", this.readNumber())); |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
216 | break; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
217 | case 4: // String |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
218 | this.constants.push(new LValue(vm, "string", this.readString())); |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
219 | break; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
220 | default: |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
221 | throw "Invalid constant type "+type+" in bytecode"; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
222 | } |
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 | |
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
|
225 | 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
|
226 | |
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
|
227 | 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
|
228 | 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
|
229 | { |
92
e9da9847121a
Pass sourceName to child chunks (in binary files it's only set in the top-level chunk)
Matthew Wild <mwild1@gmail.com>
parents:
91
diff
changeset
|
230 | var p = new LBinaryChunk(vm, chunk, this.pos, this.sourceName); |
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
|
231 | 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
|
232 | 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
|
233 | } |
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
|
234 | |
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
|
235 | 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
|
236 | |
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
|
237 | 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
|
238 | 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
|
239 | { |
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
|
240 | 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
|
241 | } |
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
|
242 | |
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
|
243 | 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
|
244 | 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
|
245 | 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
|
246 | { |
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
|
247 | 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
|
248 | } |
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
|
249 | |
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
|
250 | 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
|
251 | 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
|
252 | 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
|
253 | { |
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
|
254 | 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
|
255 | } |
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
|
256 | |
0 | 257 | return this; |
258 | } | |
259 | ||
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
260 | LBinaryChunk.prototype = { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
261 | readBytes: function (n) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
262 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
263 | return this.chunk.slice(this.pos, this.pos+=n); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
264 | }, |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
265 | readByte: function () |
0 | 266 | { |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
267 | return this.readBytes(1).charCodeAt(0); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
268 | }, |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
269 | readInt: function () |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
270 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
271 | //FIXME: Endianness |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
272 | return this.readByte() | (this.readByte()<<8) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
273 | | (this.readByte()<<16) | (this.readByte()<<24); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
274 | }, |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
275 | readString: function () |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
276 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
277 | var len = this.readInt(); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
278 | return this.readBytes(len).substring(0,len-1); |
0 | 279 | }, |
12
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
280 | readNumber: function () |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
281 | { |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
282 | //FIXME: Endianness |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
283 | 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
|
284 | 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
|
285 | |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
286 | var sign = (bytes[0]>>7)&0x1; |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
287 | 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
|
288 | |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
289 | 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
|
290 | + (bytes[2] * Math.pow(2,40)) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
291 | + (bytes[3] * Math.pow(2,32)) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
292 | + (bytes[4] * Math.pow(2,24)) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
293 | + (bytes[5] * Math.pow(2,16)) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
294 | + (bytes[6] * Math.pow(2,8)) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
295 | + bytes[7]; |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
296 | |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
297 | if(exp != 0x000 && exp != 0x7FF) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
298 | { |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
299 | 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
|
300 | return n; |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
301 | } |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
302 | else if(exp == 0x000) |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
303 | { |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
304 | return sign*0; |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
305 | } |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
306 | else |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
307 | return frac==0?sign*Infinity:NaN; |
7d748aba47ab
Initial stab at reading Number constants from bytecode
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
308 | } |
0 | 309 | }; |
310 | ||
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
311 | function INS_OPCODE(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
312 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
313 | return ins[0]; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
314 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
315 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
316 | function INS_A(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
317 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
318 | return ins[1]; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
319 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
320 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
321 | function INS_B(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
322 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
323 | return ins[2]; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
324 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
325 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
326 | function INS_C(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
327 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
328 | return ins[3]; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
329 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
330 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
331 | function INS_Bx(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
332 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
333 | return ((INS_C(ins))|(INS_B(ins)<<9)); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
334 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
335 | |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
336 | function INS_sBx(ins) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
337 | { |
32
035aacc192d8
Fix off-by-one in calculating the value of sBx
Matthew Wild <mwild1@gmail.com>
parents:
31
diff
changeset
|
338 | return (INS_Bx(ins)-0x1FFFF); |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
339 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
340 | |
77
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
341 | function RK(frame, R) |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
342 | { |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
343 | var keysource = (R&0x100)?frame.f.constants:frame.reg; |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
344 | return keysource[R&0xff]; |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
345 | } |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
346 | |
43
ecfa7896af35
Give LFunctions a vm property
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
347 | function LFunction(vm, chunk, env) |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
348 | { |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
349 | function F() {}; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
350 | F.prototype = chunk; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
351 | var o = new F(); |
43
ecfa7896af35
Give LFunctions a vm property
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
352 | o.vm = vm; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
353 | 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
|
354 | 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
|
355 | o.upvalues = []; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
356 | return o; |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
357 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
358 | |
0 | 359 | function LVM() |
360 | { | |
361 | this.callstack = []; | |
362 | this.stack = []; | |
363 | return this; | |
364 | } | |
365 | ||
366 | LVM.prototype = { | |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
367 | LValue: function (value) |
0 | 368 | { |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
369 | switch(typeof(value)) |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
370 | { |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
371 | case "number": |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
372 | return new LValue(this, "number", value); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
373 | case "string": |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
374 | return new LValue(this, "string", value); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
375 | case "function": |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
376 | return new LValue(this, "function", value); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
377 | case "object": |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
378 | if(value == null) |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
379 | return new LValue(this, "nil", value); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
380 | else |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
381 | return new LValue(this, "table", value); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
382 | case "undefined": |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
383 | return new LValue(this, "nil", null); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
384 | default: |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
385 | throw "Not able to convert type " + |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
386 | typeof(value)+" from Javascript to Lua"; |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
387 | } |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
388 | }, |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
389 | call: function (lfFunction, args) |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
390 | { |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
391 | if(typeof(lfFunction) == "function") |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
392 | { |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
393 | return lfFunction.apply(this, args); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
394 | } |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
395 | else |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
396 | { |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
397 | var frame = {f:lfFunction,pc:0,entry:true}; |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
398 | if(args) |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
399 | frame.reg = args.slice(0); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
400 | else |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
401 | frame.reg = []; |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
402 | this.callstack.push(frame); |
82
72fe2316bd67
Fix for passing arguments to the entry call frame
Matthew Wild <mwild1@gmail.com>
parents:
81
diff
changeset
|
403 | for(var i=frame.reg.length;i<lfFunction.maxStackSize;i++) |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
404 | frame.reg[i] = this.LValue(null); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
405 | return this.run(frame); |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
406 | } |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
407 | }, |
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
408 | run: function(frame) |
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
409 | { |
0 | 410 | var instruction; |
411 | while(this.callstack.length>0) | |
412 | { | |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
413 | instruction = frame.f.instructions[frame.pc++]; |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
414 | if(debugMode) |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
415 | { |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
416 | sys.puts("PC: "+(frame.pc-1)+" OP: "+instruction[0]); |
106
04eb68b0f3e3
Prettier stack printing in debugMode
Matthew Wild <mwild1@gmail.com>
parents:
105
diff
changeset
|
417 | for(var i = 0; i < frame.reg.length; i++) |
107
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
418 | { |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
419 | var entry = frame.reg[i]; |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
420 | if(entry && entry.type) |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
421 | sys.puts("\t"+i+":\t("+entry.type+") "+entry.toString()); |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
422 | else |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
423 | sys.puts("\t"+i+": "+entry); |
40b25248703b
Fix exception in stack printing for some tests when debug mode is enabled
Matthew Wild <mwild1@gmail.com>
parents:
106
diff
changeset
|
424 | } |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
425 | } |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
426 | switch(INS_OPCODE(instruction)) |
0 | 427 | { |
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
|
428 | case OP_MOVE: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
429 | frame.reg[INS_A(instruction)] = 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
|
430 | 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
|
431 | case OP_LOADNIL: |
10
ce2f27fa25a4
Use new notation for accessing instruction fields
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
432 | for(var i = INS_A(instruction);i<=INS_B(instruction);i++) |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
433 | frame.reg[i] = new LValue(this, "nil", null); |
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
|
434 | break; |
39 | 435 | case OP_LOADBOOL: |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
436 | frame.reg[INS_A(instruction)] = new LValue(this, "boolean", INS_B(instruction)!=0); |
39 | 437 | if(INS_C(instruction)!=0) |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
438 | frame.pc++; |
39 | 439 | 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
|
440 | case OP_GETUPVAL: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
441 | frame.reg[INS_A(instruction)] = frame.f.upvalues[INS_B(instruction)]; |
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
|
442 | break; |
0 | 443 | case OP_GETGLOBAL: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
444 | var name = frame.f.constants[INS_Bx(instruction)]; |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
445 | frame.reg[INS_A(instruction)] = frame.f.environment.index(name); |
0 | 446 | 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
|
447 | case OP_SETUPVAL: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
448 | var reg = frame.reg[INS_A(instruction)]; |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
449 | var upvalue = frame.f.upvalues[INS_B(instruction)]; |
26 | 450 | upvalue.type = reg.type; |
451 | 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
|
452 | break; |
6 | 453 | case OP_SETGLOBAL: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
454 | var name = frame.f.constants[INS_Bx(instruction)]; |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
455 | frame.f.environment.setIndex(name, frame.reg[instruction[1]]); |
6 | 456 | break; |
0 | 457 | case OP_LOADK: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
458 | var constant = frame.f.constants[INS_Bx(instruction)]; |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
459 | frame.reg[INS_A(instruction)] = new LValue(this, constant.type, constant.value); |
0 | 460 | break; |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
461 | case OP_NEWTABLE: |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
462 | frame.reg[INS_A(instruction)] = new LValue(this, "table", {}); |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
463 | break; |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
464 | case OP_GETTABLE: |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
465 | var C = INS_C(instruction); |
78
a856f1c3364f
Update all opcodes to use RK() that need it
Matthew Wild <mwild1@gmail.com>
parents:
77
diff
changeset
|
466 | var value = frame.reg[INS_B(instruction)].index(RK(frame, C)).value; |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
467 | frame.reg[INS_A(instruction)] = this.LValue(value); |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
468 | break; |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
469 | case OP_SETTABLE: |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
470 | var C = INS_C(instruction); |
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
471 | var B = INS_B(instruction); |
85
25470393fe74
Fix OP_SETTABLE to use RK() helper
Matthew Wild <mwild1@gmail.com>
parents:
83
diff
changeset
|
472 | frame.reg[INS_A(instruction)].setIndex(RK(frame, B), RK(frame, C)); |
28
d14b47c3870f
Support for OP_NEWTABLE, OP_GETTABLE, OP_SETTABLE
Matthew Wild <mwild1@gmail.com>
parents:
27
diff
changeset
|
473 | break; |
0 | 474 | case OP_CALL: |
49
052bd6e8daf6
Give LValue precall() and call() methods, give functions a vm property.
Matthew Wild <mwild1@gmail.com>
parents:
48
diff
changeset
|
475 | var f = frame.reg[INS_A(instruction)].precall(); // return JS or LValue |
63
be8bcd01409b
New interface for native Javascript functions called from Lua
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
476 | var A = INS_A(instruction), B = INS_B(instruction), C = INS_C(instruction); |
be8bcd01409b
New interface for native Javascript functions called from Lua
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
477 | var undefined; |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
478 | var args = frame.reg.slice(A+1, B==0?undefined:(A+B)); |
38
ba347b4b655f
Initialise all stack slots to nil when calling a chunk
Matthew Wild <mwild1@gmail.com>
parents:
37
diff
changeset
|
479 | for(var i=args.length+1;i<f.maxStackSize;i++) |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
480 | args[i] = new LValue(this, "nil", null); |
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
|
481 | 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
|
482 | { |
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
|
483 | // JS native function |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
484 | var ret = this.call(f, args); |
79
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
485 | // Insert ret to reg starting at R(A), with C-1 limit |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
486 | var nresults = ret.length; |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
487 | var nexpected; |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
488 | if(C == 0) |
63
be8bcd01409b
New interface for native Javascript functions called from Lua
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
489 | { |
79
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
490 | nexpected = nresults; |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
491 | frame.reg = frame.reg.slice(0, A+nexpected); |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
492 | } |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
493 | else |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
494 | nexpected = C-1; |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
495 | for(var i = 0;;i++) |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
496 | { |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
497 | if(i < nresults) |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
498 | frame.reg[A+i] = ret[i]; |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
499 | else if(i < nexpected) |
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
500 | frame.reg[A+i] = new LValue(this, "nil", null); |
63
be8bcd01409b
New interface for native Javascript functions called from Lua
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
501 | else |
79
94c0441b7321
Fix native function return values for the new calling interface
Matthew Wild <mwild1@gmail.com>
parents:
78
diff
changeset
|
502 | break; |
63
be8bcd01409b
New interface for native Javascript functions called from Lua
Matthew Wild <mwild1@gmail.com>
parents:
62
diff
changeset
|
503 | } |
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
|
504 | } |
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
|
505 | 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
|
506 | { |
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
|
507 | // Lua function |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
508 | frame = {f:f,pc:0,reg:args, |
51
b429e7a73de7
More reliable detection of when we're at a native/Lua call boundary
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
509 | retAt:INS_A(instruction),retCount:INS_C(instruction), |
b429e7a73de7
More reliable detection of when we're at a native/Lua call boundary
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
510 | entry:false}; |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
511 | this.callstack.push(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
|
512 | } |
0 | 513 | break; |
15
5240eaff785f
Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
514 | case OP_CLOSURE: |
5240eaff785f
Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
515 | var prototype_id = INS_Bx(instruction); |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
516 | var chunk = frame.f.chunk.prototypes[prototype_id]; |
43
ecfa7896af35
Give LFunctions a vm property
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
517 | var f = new LFunction(this, chunk, frame.f.environment); |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
518 | frame.reg[INS_A(instruction)] = new LValue(this, "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
|
519 | 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
|
520 | { |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
521 | var upval_instruction = frame.f.instructions[frame.pc++]; |
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
|
522 | 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
|
523 | { |
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
|
524 | case OP_MOVE: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
525 | f.upvalues[i] = frame.reg[INS_B(upval_instruction)]; |
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
|
526 | 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
|
527 | case OP_GETUPVAL: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
528 | f.upvalues[i] = frame.f.upvalues[INS_B(upval_instruction)]; |
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
|
529 | 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
|
530 | 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
|
531 | 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
|
532 | } |
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
|
533 | } |
15
5240eaff785f
Implement basic OP_CLOSURE (no support for upvalues yet)
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
534 | break; |
0 | 535 | case OP_RETURN: |
19
8c9c1752272b
OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
536 | var oldFrame = this.callstack.pop(); |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
537 | frame = this.callstack[this.callstack.length-1]; |
47
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
538 | var rets; |
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
539 | if(INS_B(instruction) == 0) |
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
540 | rets = oldFrame.reg.slice(INS_A(instruction)); |
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
541 | else |
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
542 | rets = oldFrame.reg.slice(INS_A(instruction),INS_A(instruction)+(INS_B(instruction)-1)); |
51
b429e7a73de7
More reliable detection of when we're at a native/Lua call boundary
Matthew Wild <mwild1@gmail.com>
parents:
50
diff
changeset
|
543 | if(!oldFrame.entry) |
19
8c9c1752272b
OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
544 | { |
8c9c1752272b
OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
545 | var i; |
64
77552f3c2c39
Correct off-by-one in return value limits
Matthew Wild <mwild1@gmail.com>
parents:
63
diff
changeset
|
546 | for(i=0;(oldFrame.retCount == 0||i<(oldFrame.retCount-1))&&i<rets.length;i++) |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
547 | frame.reg[oldFrame.retAt+i] = rets[i]; |
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
548 | if(oldFrame.retAt+i<frame.reg.length) |
65
134ddfbb2005
splice != slice. Dear Javascript, this is only one reason for why I don't like you.
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
549 | frame.reg.slice(0,oldFrame.retAt+i); |
19
8c9c1752272b
OP_RETURN: Support for return values from Lua functions \o/
Matthew Wild <mwild1@gmail.com>
parents:
18
diff
changeset
|
550 | } |
47
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
551 | else |
b8c4273edbbb
Support for return values from LVM.call()
Matthew Wild <mwild1@gmail.com>
parents:
46
diff
changeset
|
552 | return rets; |
0 | 553 | break; |
36 | 554 | case OP_SELF: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
555 | var table = frame.reg[INS_B(instruction)]; |
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
556 | frame.reg[INS_A(instruction)+1] = table; |
36 | 557 | var C = INS_C(instruction); |
78
a856f1c3364f
Update all opcodes to use RK() that need it
Matthew Wild <mwild1@gmail.com>
parents:
77
diff
changeset
|
558 | frame.reg[INS_A(instruction)] = table.index(RK(frame, C)); |
36 | 559 | break; |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
560 | case OP_FORPREP: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
561 | frame.pc+=(INS_sBx(instruction)); |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
562 | var A = INS_A(instruction); |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
563 | frame.reg[A].value -= frame.reg[A+2].value; |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
564 | break; |
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
565 | case OP_FORLOOP: |
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
566 | var A = INS_A(instruction); |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
567 | var RA = frame.reg[A]; |
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
568 | RA.value += frame.reg[A+2].value; |
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
569 | if(RA.value <= frame.reg[A+1].value) |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
570 | { |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
571 | frame.pc += INS_sBx(instruction); |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
572 | frame.reg[A+3].value = RA.value; |
34
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
573 | } |
2c3d73c76d0f
OP_FORPREP, OP_FORLOOP: Support for numerical for loops
Matthew Wild <mwild1@gmail.com>
parents:
33
diff
changeset
|
574 | break; |
102 | 575 | case OP_TFORLOOP: |
576 | var A = INS_A(instruction); | |
577 | var C = INS_C(instruction); | |
578 | var f = frame.reg[A].precall(); // Iterator function | |
579 | var rets = this.call(f, [frame.reg[A+1], frame.reg[A+2]]); | |
580 | frame.reg.length = A+3; | |
581 | for(var i = 0; i<C; i++) | |
582 | frame.reg[A+3+i] = rets[i]; | |
583 | if(frame.reg[A+3].type != "nil") | |
584 | frame.reg[A+2] = frame.reg[A+3]; | |
585 | else | |
586 | frame.pc++; // Skip JMP to start | |
587 | continue; | |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
588 | case OP_TEST: |
45
4d2cf0b5235d
Backed out changeset 741b953fcc5f (premature optimisations == bad)
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
589 | var RA = frame.reg[INS_A(instruction)]; |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
590 | var RA_bool = RA.type == "nil" || (RA.type == "boolean" && RA.value == false); |
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
591 | if(RA_bool == (INS_C(instruction)!=0)) |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
592 | frame.pc++; |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
593 | break; |
59 | 594 | case OP_TESTSET: |
595 | var RB = frame.reg[INS_B(instruction)]; | |
596 | var RB_bool = RB.type == "nil" || (RB.type == "boolean" && RB.value == false); | |
597 | if(RB_bool == (INS_C(instruction)!=0)) | |
598 | frame.pc++; | |
599 | else | |
600 | frame.reg[INS_A(instruction)] = RB; | |
601 | break; | |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
602 | case OP_JMP: |
41
70eb0eb5e7e8
Split LVM.run() into LVM.call()/LVM.run()
Matthew Wild <mwild1@gmail.com>
parents:
40
diff
changeset
|
603 | frame.pc+=INS_sBx(instruction); |
40
3a074ec1790f
Implement OP_TEST and OP_JMP
Matthew Wild <mwild1@gmail.com>
parents:
39
diff
changeset
|
604 | break; |
101
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
605 | case OP_CONCAT: |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
606 | var A = INS_A(instruction); |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
607 | var B = INS_B(instruction); |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
608 | var C = INS_C(instruction); |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
609 | var values = []; |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
610 | for(var i = B; i<=C; i++) |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
611 | values.push(frame.reg[i].value); |
f1247747f025
Implement OP_CONCAT (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
100
diff
changeset
|
612 | frame.reg[A] = new LValue(this, "string", values.join()); |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
613 | case OP_ADD: |
77
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
614 | var RB = RK(frame, INS_B(instruction)); |
ab8ff064d808
Add new RK() helper and fix OP_ADD to use it (so it loads constants properly)
Matthew Wild <mwild1@gmail.com>
parents:
76
diff
changeset
|
615 | var RC = RK(frame, INS_C(instruction)); |
72
dc73a60b3c06
Give LValue an add() method, to eventually respect metamethods
Matthew Wild <mwild1@gmail.com>
parents:
71
diff
changeset
|
616 | frame.reg[INS_A(instruction)] = RB.add(RC); |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
617 | break; |
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
618 | case OP_SUB: |
95
c461e20b609a
Fix OP_SUB to use RK() too...
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
619 | var RB = RK(frame, INS_B(instruction)); |
c461e20b609a
Fix OP_SUB to use RK() too...
Matthew Wild <mwild1@gmail.com>
parents:
93
diff
changeset
|
620 | var RC = RK(frame, INS_C(instruction)); |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
621 | frame.reg[INS_A(instruction)] = new LValue(this, "number", RB.value - RC.value); |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
622 | break; |
110
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
623 | case OP_MUL: |
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
624 | var RB = RK(frame, INS_B(instruction)); |
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
625 | var RC = RK(frame, INS_C(instruction)); |
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
626 | frame.reg[INS_A(instruction)] = new LValue(this, "number", RB.value * RC.value); |
6a6e5ef39eaf
Implement OP_MUL (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
627 | break; |
99
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
628 | case OP_DIV: |
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
629 | var RB = RK(frame, INS_B(instruction)); |
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
630 | var RC = RK(frame, INS_C(instruction)); |
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
631 | frame.reg[INS_A(instruction)] = new LValue(this, "number", RB.value / RC.value); |
633e414a496c
Implement OP_DIV (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
632 | break; |
89
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
633 | case OP_EQ: |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
634 | var A = INS_A(instruction); |
88 | 635 | var RB = RK(frame, INS_B(instruction)); |
636 | var RC = RK(frame, INS_C(instruction)); | |
89
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
637 | if(RB.equals(RC) != (A!=0)) |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
638 | frame.pc++; |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
639 | break; |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
640 | case OP_LT: |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
641 | var A = INS_A(instruction); |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
642 | var RB = RK(frame, INS_B(instruction)); |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
643 | var RC = RK(frame, INS_C(instruction)); |
a9cef9e01154
Fix OP_LT to respect A, and move OP_EQ to live next to it in the switch() for readability
Matthew Wild <mwild1@gmail.com>
parents:
88
diff
changeset
|
644 | if(RB.value < RC.value != (A!=0)) |
58
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
645 | frame.pc++; |
40d0b0429428
Implement OP_ADD, OP_SUB, OP_LT
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
646 | break; |
90
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
647 | case OP_LE: |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
648 | var A = INS_A(instruction); |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
649 | var RB = RK(frame, INS_B(instruction)); |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
650 | var RC = RK(frame, INS_C(instruction)); |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
651 | if(RB.value <= RC.value != (A!=0)) |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
652 | frame.pc++; |
40edbc05fbb9
Implement OP_LE (no metamethod yet)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
653 | break; |
103 | 654 | case OP_SETLIST: |
655 | var A = INS_A(instruction); | |
656 | var RA = frame.reg[A]; | |
657 | var B = INS_B(instruction); | |
658 | var C = INS_C(instruction); | |
659 | if(C == 0) | |
660 | throw "Dynamic table construction not yet implemented"; | |
661 | // #define LFIELDS_PER_FLUSH 50 // Lua 5.1 | |
662 | var baseindex = (C-1)*50; | |
663 | var index = new LValue(this, "number", 1); | |
664 | for(var i = 1; i<=B; index.value=(baseindex+(++i))) | |
665 | RA.setIndex(index, frame.reg[A+i]); | |
666 | break; | |
0 | 667 | default: |
11
1b3267149cbd
Throw error on unhandled opcodes
Matthew Wild <mwild1@gmail.com>
parents:
10
diff
changeset
|
668 | throw "Unhandled opcode: "+INS_OPCODE(instruction); |
0 | 669 | } |
670 | } | |
96
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
671 | }, |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
672 | registerLib: function (env, name, lib) |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
673 | { |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
674 | var t; |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
675 | if(name) |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
676 | { |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
677 | t = this.LValue({}); // Create env[name] and put fns in there |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
678 | env.setIndex(this.LValue(name), t); |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
679 | } |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
680 | else |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
681 | t = env; // Import directly into env |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
682 | |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
683 | for(var k in lib) |
e4324d9a824c
Add LVM().registerLib(env, name, lib) to register a table of native functions to an environment.
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
684 | t.setIndex(this.LValue(k), this.LValue(lib[k])); |
0 | 685 | } |
686 | }; | |
687 | ||
56
d02c7932cbf2
Move try {} block so it covers more of the VM initialization
Matthew Wild <mwild1@gmail.com>
parents:
55
diff
changeset
|
688 | try{ |
0 | 689 | 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
|
690 | |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
691 | var fs=require("fs"); |
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
692 | var sys=require("sys"); |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
693 | var c = new LBinaryChunk(testvm, fs.readFileSync("luac.out", "binary")); |
9
3f055c9ab80e
Add a bytecode interpreter \o/
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
694 | |
97
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
695 | var _G = testvm.LValue({}); |
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
|
696 | |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
697 | // Standard library |
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
|
698 | |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
699 | var baselib = { |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
700 | print: function () |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
701 | { |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
702 | var args = Array.prototype.slice.call(arguments); |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
703 | sys.print(args[0].toString()); |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
704 | for(var i = 1; i<args.length; i++) |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
705 | sys.print("\t"+args[i].toString()); |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
706 | sys.print("\n"); |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
707 | return []; |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
708 | }, |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
709 | setmetatable: function (table, metatable) |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
710 | { |
74
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
711 | if(arguments.length!=2) |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
712 | throw "setmetatable expects 2 arguments, got "+arguments.length; |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
713 | |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
714 | table.setMetatable(metatable); |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
715 | return [table]; |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
716 | }, |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
717 | type: function (o) |
a4a6adee1459
New Lua<->native function interface, simpler. Added type() to baselib.
Matthew Wild <mwild1@gmail.com>
parents:
73
diff
changeset
|
718 | { |
83
0e6b19731508
Fix type() in baselib to return an LValue
Matthew Wild <mwild1@gmail.com>
parents:
82
diff
changeset
|
719 | return [this.LValue(o.type)]; |
87
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
720 | }, |
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
721 | assert: function (expr, message) |
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
722 | { |
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
723 | if(!expr.truth()) |
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
724 | throw message; |
07d19518e89a
Add assert() to baselib, real tests possible at last
Matthew Wild <mwild1@gmail.com>
parents:
86
diff
changeset
|
725 | return [expr]; |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
726 | } |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
727 | }; |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
728 | |
97
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
729 | var math = { |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
730 | ldexp: function (m, e) |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
731 | { |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
732 | return [this.LValue(m.value*Math.pow(2, e.value))]; |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
733 | }, |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
734 | floor: function (x) |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
735 | { |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
736 | return [this.LValue(Math.floor(x.value))]; |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
737 | } |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
738 | }; |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
739 | |
104
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
740 | var _patternClasses = { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
741 | "a": "[A-Za-z]", "A": "[^A-Za-z]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
742 | "p": "[\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
743 | "P": "[^\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
744 | "s": "[\x09-\x0d\x20]", "S": "[^\x09-\x0d\x20]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
745 | "u": "[A-Z]", "U": "[^A-Z]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
746 | "d": "[0-9]", "D": "[^0-9]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
747 | "w": "[0-9A-Za-z]", "W": "[^0-9A-Za-z]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
748 | "x": "[0-9A-Fa-f]", "X": "[^0-9A-Fa-f]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
749 | "z": "[\x00]", "Z": "[^\x00]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
750 | "l": "[a-z]", "L": "[^a-z]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
751 | "c": "[\x00-\x1f\x7f]", "C": "[^\x00-\x1f\x7f]", |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
752 | }; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
753 | |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
754 | var _patternToRegExp = function (patt) |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
755 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
756 | var regexp = ""; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
757 | for(var i=0;i<patt.length;i++) |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
758 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
759 | var c = patt[i]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
760 | if(c == "%") |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
761 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
762 | c = patt[++i]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
763 | if(c == "b") |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
764 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
765 | throw "%b not supported in patterns"; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
766 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
767 | else if(c >= "0" && c <= "9") |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
768 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
769 | regexp += ("\\"+c); |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
770 | continue; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
771 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
772 | else |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
773 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
774 | var cls = _patternClasses[c]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
775 | if(cls) |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
776 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
777 | regexp += cls; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
778 | continue; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
779 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
780 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
781 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
782 | else if(c == "\\" || c == "/") |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
783 | regexp += "\\"; // Escape escapes |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
784 | regexp += c; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
785 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
786 | return new RegExp(regexp, "g"); |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
787 | }; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
788 | |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
789 | var string = { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
790 | gmatch: function (str, patt) |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
791 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
792 | var re = _patternToRegExp(patt.value); |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
793 | var matches = str.value.match(re)||[]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
794 | sys.puts("MATCHES: "+str+" "+matches.length); |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
795 | var curr = 0; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
796 | var iter = function () |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
797 | { |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
798 | return [this.LValue(matches[curr++])]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
799 | }; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
800 | return [this.LValue(iter)]; |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
801 | } |
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
802 | }; |
97
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
803 | testvm.registerLib(_G, null, baselib); |
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
804 | testvm.registerLib(_G, "math", math); |
104
c481f6bfb19b
Add string lib, with gmatch function only for now
Matthew Wild <mwild1@gmail.com>
parents:
103
diff
changeset
|
805 | testvm.registerLib(_G, "string", string); |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
806 | |
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
807 | // Metatable on environment to print out nil global accesses |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
808 | var mt = testvm.LValue({}); |
55
89ecee2cbad1
Add test __index handler to catch use of nil globals
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
809 | mt.setIndex( |
73
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
810 | testvm.LValue("__index"), |
6b43d68abc94
Large overhaul - every LValue (and LBinaryChunk) now has a valid 'vm' property, so we know in which LVM to call it or its metamethods
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
811 | testvm.LValue(function (t, k) { sys.puts("Access of nil global: "+k); }) |
55
89ecee2cbad1
Add test __index handler to catch use of nil globals
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
812 | ); |
97
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
813 | _G.setMetatable(mt); |
67
782274d793ac
Formalise the beginnings of a baselib, add setmetatable.
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
814 | |
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
|
815 | |
97
80cc68dd8222
Rename default_environment to _G, use registerLib() for baselib and add math lib with 2 ldexp, floor
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
816 | var f = new LFunction(testvm, c, _G); |
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
|
817 | |
48 | 818 | var ret = testvm.call(f); |
819 | if(ret) | |
820 | sys.puts("Returned: "+sys.inspect(ret)); | |
29
62f3df8ed204
Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
821 | } |
62f3df8ed204
Remove rawExceptions flag, always print exception's stack trace if it has one
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
822 | 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
|
823 | { |
93
748b51e8f0d6
Print out location at which errors occur, yay!
Matthew Wild <mwild1@gmail.com>
parents:
92
diff
changeset
|
824 | var currframe = testvm.callstack[testvm.callstack.length-1]; |
98
14d2400ec059
Don't try and print current position in the Lua script with no valid frame
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
825 | if(currframe) |
14d2400ec059
Don't try and print current position in the Lua script with no valid frame
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
826 | { |
14d2400ec059
Don't try and print current position in the Lua script with no valid frame
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
827 | var currfunc = currframe.f; |
105
fdcc139587a3
Fix traceback in error handling...
Matthew Wild <mwild1@gmail.com>
parents:
104
diff
changeset
|
828 | var sourceName = (currfunc.sourceName||"=<unknown>").substr(1); |
fdcc139587a3
Fix traceback in error handling...
Matthew Wild <mwild1@gmail.com>
parents:
104
diff
changeset
|
829 | var line = "?"; |
fdcc139587a3
Fix traceback in error handling...
Matthew Wild <mwild1@gmail.com>
parents:
104
diff
changeset
|
830 | if(currfunc.sourceLines && currfunc.sourceLines[currframe.pc]) |
fdcc139587a3
Fix traceback in error handling...
Matthew Wild <mwild1@gmail.com>
parents:
104
diff
changeset
|
831 | line = currfunc.sourceLines[currframe.pc]; |
fdcc139587a3
Fix traceback in error handling...
Matthew Wild <mwild1@gmail.com>
parents:
104
diff
changeset
|
832 | sys.print("lvm.js: "+sourceName+":"+line+": "); |
98
14d2400ec059
Don't try and print current position in the Lua script with no valid frame
Matthew Wild <mwild1@gmail.com>
parents:
97
diff
changeset
|
833 | } |
93
748b51e8f0d6
Print out location at which errors occur, yay!
Matthew Wild <mwild1@gmail.com>
parents:
92
diff
changeset
|
834 | sys.puts(e); |
35
3de1d63ffdf7
Fix for error handling to handle string exceptions
Matthew Wild <mwild1@gmail.com>
parents:
34
diff
changeset
|
835 | if(typeof(e) == "object" && "stack" in e) |
68
5b815d3591e2
Switch to sys.puts for all output for now, leaving browser compatibility for the moment.
Matthew Wild <mwild1@gmail.com>
parents:
67
diff
changeset
|
836 | sys.puts(e.stack); |
44
f5244bce28ce
Exit with failure code on error
Matthew Wild <mwild1@gmail.com>
parents:
43
diff
changeset
|
837 | process.exit(1); |
5 | 838 | } |