diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
commit | bfd0179042b0b02fb88748d54e56e7e208bb117f (patch) | |
tree | 42d5fd51574054caaf673420fca1ec962d62d2f2 /apps/plugins/lua/ldebug.c | |
parent | 36378988ad4059982742f05f5eb50580b456840a (diff) | |
download | rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.tar.gz rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.zip |
Revert "Update lua plugin to 5.2.3"
FILE typedef to *void needs more work to not break sim and
application builds. I checked only a few random native builds
unfortunately. Sorry for inconvenience.
Diffstat (limited to 'apps/plugins/lua/ldebug.c')
-rw-r--r-- | apps/plugins/lua/ldebug.c | 625 |
1 files changed, 335 insertions, 290 deletions
diff --git a/apps/plugins/lua/ldebug.c b/apps/plugins/lua/ldebug.c index 20d663efff..50ad3d3803 100644 --- a/apps/plugins/lua/ldebug.c +++ b/apps/plugins/lua/ldebug.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldebug.c,v 2.90.1.3 2013/05/16 16:04:15 roberto Exp $ | 2 | ** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ |
3 | ** Debug Interface | 3 | ** Debug Interface |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -30,20 +30,23 @@ | |||
30 | 30 | ||
31 | 31 | ||
32 | 32 | ||
33 | #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) | ||
34 | |||
35 | |||
36 | static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); | 33 | static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); |
37 | 34 | ||
38 | 35 | ||
39 | static int currentpc (CallInfo *ci) { | 36 | static int currentpc (lua_State *L, CallInfo *ci) { |
40 | lua_assert(isLua(ci)); | 37 | if (!isLua(ci)) return -1; /* function is not a Lua function? */ |
41 | return pcRel(ci->u.l.savedpc, ci_func(ci)->p); | 38 | if (ci == L->ci) |
39 | ci->savedpc = L->savedpc; | ||
40 | return pcRel(ci->savedpc, ci_func(ci)->l.p); | ||
42 | } | 41 | } |
43 | 42 | ||
44 | 43 | ||
45 | static int currentline (CallInfo *ci) { | 44 | static int currentline (lua_State *L, CallInfo *ci) { |
46 | return getfuncline(ci_func(ci)->p, currentpc(ci)); | 45 | int pc = currentpc(L, ci); |
46 | if (pc < 0) | ||
47 | return -1; /* only active lua functions have current-line information */ | ||
48 | else | ||
49 | return getline(ci_func(ci)->l.p, pc); | ||
47 | } | 50 | } |
48 | 51 | ||
49 | 52 | ||
@@ -55,8 +58,6 @@ LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { | |||
55 | mask = 0; | 58 | mask = 0; |
56 | func = NULL; | 59 | func = NULL; |
57 | } | 60 | } |
58 | if (isLua(L->ci)) | ||
59 | L->oldpc = L->ci->u.l.savedpc; | ||
60 | L->hook = func; | 61 | L->hook = func; |
61 | L->basehookcount = count; | 62 | L->basehookcount = count; |
62 | resethookcount(L); | 63 | resethookcount(L); |
@@ -83,13 +84,19 @@ LUA_API int lua_gethookcount (lua_State *L) { | |||
83 | LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { | 84 | LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { |
84 | int status; | 85 | int status; |
85 | CallInfo *ci; | 86 | CallInfo *ci; |
86 | if (level < 0) return 0; /* invalid (negative) level */ | ||
87 | lua_lock(L); | 87 | lua_lock(L); |
88 | for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) | 88 | for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { |
89 | level--; | 89 | level--; |
90 | if (level == 0 && ci != &L->base_ci) { /* level found? */ | 90 | if (f_isLua(ci)) /* Lua function? */ |
91 | level -= ci->tailcalls; /* skip lost tail calls */ | ||
92 | } | ||
93 | if (level == 0 && ci > L->base_ci) { /* level found? */ | ||
91 | status = 1; | 94 | status = 1; |
92 | ar->i_ci = ci; | 95 | ar->i_ci = cast_int(ci - L->base_ci); |
96 | } | ||
97 | else if (level < 0) { /* level is of a lost tail call? */ | ||
98 | status = 1; | ||
99 | ar->i_ci = 0; | ||
93 | } | 100 | } |
94 | else status = 0; /* no such level */ | 101 | else status = 0; /* no such level */ |
95 | lua_unlock(L); | 102 | lua_unlock(L); |
@@ -97,78 +104,43 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { | |||
97 | } | 104 | } |
98 | 105 | ||
99 | 106 | ||
100 | static const char *upvalname (Proto *p, int uv) { | 107 | static Proto *getluaproto (CallInfo *ci) { |
101 | TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); | 108 | return (isLua(ci) ? ci_func(ci)->l.p : NULL); |
102 | if (s == NULL) return "?"; | ||
103 | else return getstr(s); | ||
104 | } | 109 | } |
105 | 110 | ||
106 | 111 | ||
107 | static const char *findvararg (CallInfo *ci, int n, StkId *pos) { | 112 | static const char *findlocal (lua_State *L, CallInfo *ci, int n) { |
108 | int nparams = clLvalue(ci->func)->p->numparams; | 113 | const char *name; |
109 | if (n >= ci->u.l.base - ci->func - nparams) | 114 | Proto *fp = getluaproto(ci); |
110 | return NULL; /* no such vararg */ | 115 | if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) |
116 | return name; /* is a local variable in a Lua function */ | ||
111 | else { | 117 | else { |
112 | *pos = ci->func + nparams + n; | 118 | StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; |
113 | return "(*vararg)"; /* generic name for any vararg */ | 119 | if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ |
114 | } | 120 | return "(*temporary)"; |
115 | } | ||
116 | |||
117 | |||
118 | static const char *findlocal (lua_State *L, CallInfo *ci, int n, | ||
119 | StkId *pos) { | ||
120 | const char *name = NULL; | ||
121 | StkId base; | ||
122 | if (isLua(ci)) { | ||
123 | if (n < 0) /* access to vararg values? */ | ||
124 | return findvararg(ci, -n, pos); | ||
125 | else { | ||
126 | base = ci->u.l.base; | ||
127 | name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); | ||
128 | } | ||
129 | } | ||
130 | else | ||
131 | base = ci->func + 1; | ||
132 | if (name == NULL) { /* no 'standard' name? */ | ||
133 | StkId limit = (ci == L->ci) ? L->top : ci->next->func; | ||
134 | if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ | ||
135 | name = "(*temporary)"; /* generic name for any valid slot */ | ||
136 | else | 121 | else |
137 | return NULL; /* no name */ | 122 | return NULL; |
138 | } | 123 | } |
139 | *pos = base + (n - 1); | ||
140 | return name; | ||
141 | } | 124 | } |
142 | 125 | ||
143 | 126 | ||
144 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | 127 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { |
145 | const char *name; | 128 | CallInfo *ci = L->base_ci + ar->i_ci; |
129 | const char *name = findlocal(L, ci, n); | ||
146 | lua_lock(L); | 130 | lua_lock(L); |
147 | if (ar == NULL) { /* information about non-active function? */ | 131 | if (name) |
148 | if (!isLfunction(L->top - 1)) /* not a Lua function? */ | 132 | luaA_pushobject(L, ci->base + (n - 1)); |
149 | name = NULL; | ||
150 | else /* consider live variables at function start (parameters) */ | ||
151 | name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); | ||
152 | } | ||
153 | else { /* active function; get information through 'ar' */ | ||
154 | StkId pos = 0; /* to avoid warnings */ | ||
155 | name = findlocal(L, ar->i_ci, n, &pos); | ||
156 | if (name) { | ||
157 | setobj2s(L, L->top, pos); | ||
158 | api_incr_top(L); | ||
159 | } | ||
160 | } | ||
161 | lua_unlock(L); | 133 | lua_unlock(L); |
162 | return name; | 134 | return name; |
163 | } | 135 | } |
164 | 136 | ||
165 | 137 | ||
166 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { | 138 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { |
167 | StkId pos = 0; /* to avoid warnings */ | 139 | CallInfo *ci = L->base_ci + ar->i_ci; |
168 | const char *name = findlocal(L, ar->i_ci, n, &pos); | 140 | const char *name = findlocal(L, ci, n); |
169 | lua_lock(L); | 141 | lua_lock(L); |
170 | if (name) | 142 | if (name) |
171 | setobjs2s(L, pos, L->top - 1); | 143 | setobjs2s(L, ci->base + (n - 1), L->top - 1); |
172 | L->top--; /* pop value */ | 144 | L->top--; /* pop value */ |
173 | lua_unlock(L); | 145 | lua_unlock(L); |
174 | return name; | 146 | return name; |
@@ -176,45 +148,55 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { | |||
176 | 148 | ||
177 | 149 | ||
178 | static void funcinfo (lua_Debug *ar, Closure *cl) { | 150 | static void funcinfo (lua_Debug *ar, Closure *cl) { |
179 | if (noLuaClosure(cl)) { | 151 | if (cl->c.isC) { |
180 | ar->source = "=[C]"; | 152 | ar->source = "=[C]"; |
181 | ar->linedefined = -1; | 153 | ar->linedefined = -1; |
182 | ar->lastlinedefined = -1; | 154 | ar->lastlinedefined = -1; |
183 | ar->what = "C"; | 155 | ar->what = "C"; |
184 | } | 156 | } |
185 | else { | 157 | else { |
186 | Proto *p = cl->l.p; | 158 | ar->source = getstr(cl->l.p->source); |
187 | ar->source = p->source ? getstr(p->source) : "=?"; | 159 | ar->linedefined = cl->l.p->linedefined; |
188 | ar->linedefined = p->linedefined; | 160 | ar->lastlinedefined = cl->l.p->lastlinedefined; |
189 | ar->lastlinedefined = p->lastlinedefined; | ||
190 | ar->what = (ar->linedefined == 0) ? "main" : "Lua"; | 161 | ar->what = (ar->linedefined == 0) ? "main" : "Lua"; |
191 | } | 162 | } |
192 | luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); | 163 | luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); |
193 | } | 164 | } |
194 | 165 | ||
195 | 166 | ||
167 | static void info_tailcall (lua_Debug *ar) { | ||
168 | ar->name = ar->namewhat = ""; | ||
169 | ar->what = "tail"; | ||
170 | ar->lastlinedefined = ar->linedefined = ar->currentline = -1; | ||
171 | ar->source = "=(tail call)"; | ||
172 | luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); | ||
173 | ar->nups = 0; | ||
174 | } | ||
175 | |||
176 | |||
196 | static void collectvalidlines (lua_State *L, Closure *f) { | 177 | static void collectvalidlines (lua_State *L, Closure *f) { |
197 | if (noLuaClosure(f)) { | 178 | if (f == NULL || f->c.isC) { |
198 | setnilvalue(L->top); | 179 | setnilvalue(L->top); |
199 | api_incr_top(L); | ||
200 | } | 180 | } |
201 | else { | 181 | else { |
202 | int i; | 182 | Table *t = luaH_new(L, 0, 0); |
203 | TValue v; | ||
204 | int *lineinfo = f->l.p->lineinfo; | 183 | int *lineinfo = f->l.p->lineinfo; |
205 | Table *t = luaH_new(L); /* new table to store active lines */ | 184 | int i; |
206 | sethvalue(L, L->top, t); /* push it on stack */ | 185 | for (i=0; i<f->l.p->sizelineinfo; i++) |
207 | api_incr_top(L); | 186 | setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); |
208 | setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ | 187 | sethvalue(L, L->top, t); |
209 | for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ | ||
210 | luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ | ||
211 | } | 188 | } |
189 | incr_top(L); | ||
212 | } | 190 | } |
213 | 191 | ||
214 | 192 | ||
215 | static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, | 193 | static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, |
216 | Closure *f, CallInfo *ci) { | 194 | Closure *f, CallInfo *ci) { |
217 | int status = 1; | 195 | int status = 1; |
196 | if (f == NULL) { | ||
197 | info_tailcall(ar); | ||
198 | return status; | ||
199 | } | ||
218 | for (; *what; what++) { | 200 | for (; *what; what++) { |
219 | switch (*what) { | 201 | switch (*what) { |
220 | case 'S': { | 202 | case 'S': { |
@@ -222,31 +204,15 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, | |||
222 | break; | 204 | break; |
223 | } | 205 | } |
224 | case 'l': { | 206 | case 'l': { |
225 | ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; | 207 | ar->currentline = (ci) ? currentline(L, ci) : -1; |
226 | break; | 208 | break; |
227 | } | 209 | } |
228 | case 'u': { | 210 | case 'u': { |
229 | ar->nups = (f == NULL) ? 0 : f->c.nupvalues; | 211 | ar->nups = f->c.nupvalues; |
230 | if (noLuaClosure(f)) { | ||
231 | ar->isvararg = 1; | ||
232 | ar->nparams = 0; | ||
233 | } | ||
234 | else { | ||
235 | ar->isvararg = f->l.p->is_vararg; | ||
236 | ar->nparams = f->l.p->numparams; | ||
237 | } | ||
238 | break; | ||
239 | } | ||
240 | case 't': { | ||
241 | ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; | ||
242 | break; | 212 | break; |
243 | } | 213 | } |
244 | case 'n': { | 214 | case 'n': { |
245 | /* calling function is a known Lua function? */ | 215 | ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; |
246 | if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) | ||
247 | ar->namewhat = getfuncname(L, ci->previous, &ar->name); | ||
248 | else | ||
249 | ar->namewhat = NULL; | ||
250 | if (ar->namewhat == NULL) { | 216 | if (ar->namewhat == NULL) { |
251 | ar->namewhat = ""; /* not found */ | 217 | ar->namewhat = ""; /* not found */ |
252 | ar->name = NULL; | 218 | ar->name = NULL; |
@@ -265,30 +231,29 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, | |||
265 | 231 | ||
266 | LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { | 232 | LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { |
267 | int status; | 233 | int status; |
268 | Closure *cl; | 234 | Closure *f = NULL; |
269 | CallInfo *ci; | 235 | CallInfo *ci = NULL; |
270 | StkId func; | ||
271 | lua_lock(L); | 236 | lua_lock(L); |
272 | if (*what == '>') { | 237 | if (*what == '>') { |
273 | ci = NULL; | 238 | StkId func = L->top - 1; |
274 | func = L->top - 1; | 239 | luai_apicheck(L, ttisfunction(func)); |
275 | api_check(L, ttisfunction(func), "function expected"); | ||
276 | what++; /* skip the '>' */ | 240 | what++; /* skip the '>' */ |
241 | f = clvalue(func); | ||
277 | L->top--; /* pop function */ | 242 | L->top--; /* pop function */ |
278 | } | 243 | } |
279 | else { | 244 | else if (ar->i_ci != 0) { /* no tail call? */ |
280 | ci = ar->i_ci; | 245 | ci = L->base_ci + ar->i_ci; |
281 | func = ci->func; | ||
282 | lua_assert(ttisfunction(ci->func)); | 246 | lua_assert(ttisfunction(ci->func)); |
247 | f = clvalue(ci->func); | ||
283 | } | 248 | } |
284 | cl = ttisclosure(func) ? clvalue(func) : NULL; | 249 | status = auxgetinfo(L, what, ar, f, ci); |
285 | status = auxgetinfo(L, what, ar, cl, ci); | ||
286 | if (strchr(what, 'f')) { | 250 | if (strchr(what, 'f')) { |
287 | setobjs2s(L, L->top, func); | 251 | if (f == NULL) setnilvalue(L->top); |
288 | api_incr_top(L); | 252 | else setclvalue(L, L->top, f); |
253 | incr_top(L); | ||
289 | } | 254 | } |
290 | if (strchr(what, 'L')) | 255 | if (strchr(what, 'L')) |
291 | collectvalidlines(L, cl); | 256 | collectvalidlines(L, f); |
292 | lua_unlock(L); | 257 | lua_unlock(L); |
293 | return status; | 258 | return status; |
294 | } | 259 | } |
@@ -296,231 +261,315 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { | |||
296 | 261 | ||
297 | /* | 262 | /* |
298 | ** {====================================================== | 263 | ** {====================================================== |
299 | ** Symbolic Execution | 264 | ** Symbolic Execution and code checker |
300 | ** ======================================================= | 265 | ** ======================================================= |
301 | */ | 266 | */ |
302 | 267 | ||
303 | static const char *getobjname (Proto *p, int lastpc, int reg, | 268 | #define check(x) if (!(x)) return 0; |
304 | const char **name); | ||
305 | 269 | ||
270 | #define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) | ||
306 | 271 | ||
307 | /* | 272 | #define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) |
308 | ** find a "name" for the RK value 'c' | 273 | |
309 | */ | 274 | |
310 | static void kname (Proto *p, int pc, int c, const char **name) { | 275 | |
311 | if (ISK(c)) { /* is 'c' a constant? */ | 276 | static int precheck (const Proto *pt) { |
312 | TValue *kvalue = &p->k[INDEXK(c)]; | 277 | check(pt->maxstacksize <= MAXSTACK); |
313 | if (ttisstring(kvalue)) { /* literal constant? */ | 278 | check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); |
314 | *name = svalue(kvalue); /* it is its own name */ | 279 | check(!(pt->is_vararg & VARARG_NEEDSARG) || |
315 | return; | 280 | (pt->is_vararg & VARARG_HASARG)); |
316 | } | 281 | check(pt->sizeupvalues <= pt->nups); |
317 | /* else no reasonable name found */ | 282 | check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); |
318 | } | 283 | check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); |
319 | else { /* 'c' is a register */ | 284 | return 1; |
320 | const char *what = getobjname(p, pc, c, name); /* search for 'c' */ | 285 | } |
321 | if (what && *what == 'c') { /* found a constant name? */ | 286 | |
322 | return; /* 'name' already filled */ | 287 | |
288 | #define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) | ||
289 | |||
290 | int luaG_checkopenop (Instruction i) { | ||
291 | switch (GET_OPCODE(i)) { | ||
292 | case OP_CALL: | ||
293 | case OP_TAILCALL: | ||
294 | case OP_RETURN: | ||
295 | case OP_SETLIST: { | ||
296 | check(GETARG_B(i) == 0); | ||
297 | return 1; | ||
323 | } | 298 | } |
324 | /* else no reasonable name found */ | 299 | default: return 0; /* invalid instruction after an open call */ |
325 | } | 300 | } |
326 | *name = "?"; /* no reasonable name found */ | ||
327 | } | 301 | } |
328 | 302 | ||
329 | 303 | ||
330 | static int filterpc (int pc, int jmptarget) { | 304 | static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { |
331 | if (pc < jmptarget) /* is code conditional (inside a jump)? */ | 305 | switch (mode) { |
332 | return -1; /* cannot know who sets that register */ | 306 | case OpArgN: check(r == 0); break; |
333 | else return pc; /* current position sets that register */ | 307 | case OpArgU: break; |
308 | case OpArgR: checkreg(pt, r); break; | ||
309 | case OpArgK: | ||
310 | check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); | ||
311 | break; | ||
312 | } | ||
313 | return 1; | ||
334 | } | 314 | } |
335 | 315 | ||
336 | 316 | ||
337 | /* | 317 | static Instruction symbexec (const Proto *pt, int lastpc, int reg) { |
338 | ** try to find last instruction before 'lastpc' that modified register 'reg' | ||
339 | */ | ||
340 | static int findsetreg (Proto *p, int lastpc, int reg) { | ||
341 | int pc; | 318 | int pc; |
342 | int setreg = -1; /* keep last instruction that changed 'reg' */ | 319 | int last; /* stores position of last instruction that changed `reg' */ |
343 | int jmptarget = 0; /* any code before this address is conditional */ | 320 | last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ |
321 | check(precheck(pt)); | ||
344 | for (pc = 0; pc < lastpc; pc++) { | 322 | for (pc = 0; pc < lastpc; pc++) { |
345 | Instruction i = p->code[pc]; | 323 | Instruction i = pt->code[pc]; |
346 | OpCode op = GET_OPCODE(i); | 324 | OpCode op = GET_OPCODE(i); |
347 | int a = GETARG_A(i); | 325 | int a = GETARG_A(i); |
326 | int b = 0; | ||
327 | int c = 0; | ||
328 | check(op < NUM_OPCODES); | ||
329 | checkreg(pt, a); | ||
330 | switch (getOpMode(op)) { | ||
331 | case iABC: { | ||
332 | b = GETARG_B(i); | ||
333 | c = GETARG_C(i); | ||
334 | check(checkArgMode(pt, b, getBMode(op))); | ||
335 | check(checkArgMode(pt, c, getCMode(op))); | ||
336 | break; | ||
337 | } | ||
338 | case iABx: { | ||
339 | b = GETARG_Bx(i); | ||
340 | if (getBMode(op) == OpArgK) check(b < pt->sizek); | ||
341 | break; | ||
342 | } | ||
343 | case iAsBx: { | ||
344 | b = GETARG_sBx(i); | ||
345 | if (getBMode(op) == OpArgR) { | ||
346 | int dest = pc+1+b; | ||
347 | check(0 <= dest && dest < pt->sizecode); | ||
348 | if (dest > 0) { | ||
349 | int j; | ||
350 | /* check that it does not jump to a setlist count; this | ||
351 | is tricky, because the count from a previous setlist may | ||
352 | have the same value of an invalid setlist; so, we must | ||
353 | go all the way back to the first of them (if any) */ | ||
354 | for (j = 0; j < dest; j++) { | ||
355 | Instruction d = pt->code[dest-1-j]; | ||
356 | if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; | ||
357 | } | ||
358 | /* if 'j' is even, previous value is not a setlist (even if | ||
359 | it looks like one) */ | ||
360 | check((j&1) == 0); | ||
361 | } | ||
362 | } | ||
363 | break; | ||
364 | } | ||
365 | } | ||
366 | if (testAMode(op)) { | ||
367 | if (a == reg) last = pc; /* change register `a' */ | ||
368 | } | ||
369 | if (testTMode(op)) { | ||
370 | check(pc+2 < pt->sizecode); /* check skip */ | ||
371 | check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); | ||
372 | } | ||
348 | switch (op) { | 373 | switch (op) { |
374 | case OP_LOADBOOL: { | ||
375 | if (c == 1) { /* does it jump? */ | ||
376 | check(pc+2 < pt->sizecode); /* check its jump */ | ||
377 | check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || | ||
378 | GETARG_C(pt->code[pc+1]) != 0); | ||
379 | } | ||
380 | break; | ||
381 | } | ||
349 | case OP_LOADNIL: { | 382 | case OP_LOADNIL: { |
350 | int b = GETARG_B(i); | 383 | if (a <= reg && reg <= b) |
351 | if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ | 384 | last = pc; /* set registers from `a' to `b' */ |
352 | setreg = filterpc(pc, jmptarget); | 385 | break; |
386 | } | ||
387 | case OP_GETUPVAL: | ||
388 | case OP_SETUPVAL: { | ||
389 | check(b < pt->nups); | ||
390 | break; | ||
391 | } | ||
392 | case OP_GETGLOBAL: | ||
393 | case OP_SETGLOBAL: { | ||
394 | check(ttisstring(&pt->k[b])); | ||
395 | break; | ||
396 | } | ||
397 | case OP_SELF: { | ||
398 | checkreg(pt, a+1); | ||
399 | if (reg == a+1) last = pc; | ||
400 | break; | ||
401 | } | ||
402 | case OP_CONCAT: { | ||
403 | check(b < c); /* at least two operands */ | ||
404 | break; | ||
405 | } | ||
406 | case OP_TFORLOOP: { | ||
407 | check(c >= 1); /* at least one result (control variable) */ | ||
408 | checkreg(pt, a+2+c); /* space for results */ | ||
409 | if (reg >= a+2) last = pc; /* affect all regs above its base */ | ||
353 | break; | 410 | break; |
354 | } | 411 | } |
355 | case OP_TFORCALL: { | 412 | case OP_FORLOOP: |
356 | if (reg >= a + 2) /* affect all regs above its base */ | 413 | case OP_FORPREP: |
357 | setreg = filterpc(pc, jmptarget); | 414 | checkreg(pt, a+3); |
415 | /* go through */ | ||
416 | case OP_JMP: { | ||
417 | int dest = pc+1+b; | ||
418 | /* not full check and jump is forward and do not skip `lastpc'? */ | ||
419 | if (reg != NO_REG && pc < dest && dest <= lastpc) | ||
420 | pc += b; /* do the jump */ | ||
358 | break; | 421 | break; |
359 | } | 422 | } |
360 | case OP_CALL: | 423 | case OP_CALL: |
361 | case OP_TAILCALL: { | 424 | case OP_TAILCALL: { |
362 | if (reg >= a) /* affect all registers above base */ | 425 | if (b != 0) { |
363 | setreg = filterpc(pc, jmptarget); | 426 | checkreg(pt, a+b-1); |
427 | } | ||
428 | c--; /* c = num. returns */ | ||
429 | if (c == LUA_MULTRET) { | ||
430 | check(checkopenop(pt, pc)); | ||
431 | } | ||
432 | else if (c != 0) | ||
433 | checkreg(pt, a+c-1); | ||
434 | if (reg >= a) last = pc; /* affect all registers above base */ | ||
364 | break; | 435 | break; |
365 | } | 436 | } |
366 | case OP_JMP: { | 437 | case OP_RETURN: { |
367 | int b = GETARG_sBx(i); | 438 | b--; /* b = num. returns */ |
368 | int dest = pc + 1 + b; | 439 | if (b > 0) checkreg(pt, a+b-1); |
369 | /* jump is forward and do not skip `lastpc'? */ | 440 | break; |
370 | if (pc < dest && dest <= lastpc) { | 441 | } |
371 | if (dest > jmptarget) | 442 | case OP_SETLIST: { |
372 | jmptarget = dest; /* update 'jmptarget' */ | 443 | if (b > 0) checkreg(pt, a + b); |
444 | if (c == 0) { | ||
445 | pc++; | ||
446 | check(pc < pt->sizecode - 1); | ||
373 | } | 447 | } |
374 | break; | 448 | break; |
375 | } | 449 | } |
376 | case OP_TEST: { | 450 | case OP_CLOSURE: { |
377 | if (reg == a) /* jumped code can change 'a' */ | 451 | int nup, j; |
378 | setreg = filterpc(pc, jmptarget); | 452 | check(b < pt->sizep); |
453 | nup = pt->p[b]->nups; | ||
454 | check(pc + nup < pt->sizecode); | ||
455 | for (j = 1; j <= nup; j++) { | ||
456 | OpCode op1 = GET_OPCODE(pt->code[pc + j]); | ||
457 | check(op1 == OP_GETUPVAL || op1 == OP_MOVE); | ||
458 | } | ||
459 | if (reg != NO_REG) /* tracing? */ | ||
460 | pc += nup; /* do not 'execute' these pseudo-instructions */ | ||
379 | break; | 461 | break; |
380 | } | 462 | } |
381 | default: | 463 | case OP_VARARG: { |
382 | if (testAMode(op) && reg == a) /* any instruction that set A */ | 464 | check((pt->is_vararg & VARARG_ISVARARG) && |
383 | setreg = filterpc(pc, jmptarget); | 465 | !(pt->is_vararg & VARARG_NEEDSARG)); |
466 | b--; | ||
467 | if (b == LUA_MULTRET) check(checkopenop(pt, pc)); | ||
468 | checkreg(pt, a+b-1); | ||
384 | break; | 469 | break; |
470 | } | ||
471 | default: break; | ||
385 | } | 472 | } |
386 | } | 473 | } |
387 | return setreg; | 474 | return pt->code[last]; |
475 | } | ||
476 | |||
477 | #undef check | ||
478 | #undef checkjump | ||
479 | #undef checkreg | ||
480 | |||
481 | /* }====================================================== */ | ||
482 | |||
483 | |||
484 | int luaG_checkcode (const Proto *pt) { | ||
485 | return (symbexec(pt, pt->sizecode, NO_REG) != 0); | ||
388 | } | 486 | } |
389 | 487 | ||
390 | 488 | ||
391 | static const char *getobjname (Proto *p, int lastpc, int reg, | 489 | static const char *kname (Proto *p, int c) { |
490 | if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) | ||
491 | return svalue(&p->k[INDEXK(c)]); | ||
492 | else | ||
493 | return "?"; | ||
494 | } | ||
495 | |||
496 | |||
497 | static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, | ||
392 | const char **name) { | 498 | const char **name) { |
393 | int pc; | 499 | if (isLua(ci)) { /* a Lua function? */ |
394 | *name = luaF_getlocalname(p, reg + 1, lastpc); | 500 | Proto *p = ci_func(ci)->l.p; |
395 | if (*name) /* is a local? */ | 501 | int pc = currentpc(L, ci); |
396 | return "local"; | 502 | Instruction i; |
397 | /* else try symbolic execution */ | 503 | *name = luaF_getlocalname(p, stackpos+1, pc); |
398 | pc = findsetreg(p, lastpc, reg); | 504 | if (*name) /* is a local? */ |
399 | if (pc != -1) { /* could find instruction? */ | 505 | return "local"; |
400 | Instruction i = p->code[pc]; | 506 | i = symbexec(p, pc, stackpos); /* try symbolic execution */ |
401 | OpCode op = GET_OPCODE(i); | 507 | lua_assert(pc != -1); |
402 | switch (op) { | 508 | switch (GET_OPCODE(i)) { |
509 | case OP_GETGLOBAL: { | ||
510 | int g = GETARG_Bx(i); /* global index */ | ||
511 | lua_assert(ttisstring(&p->k[g])); | ||
512 | *name = svalue(&p->k[g]); | ||
513 | return "global"; | ||
514 | } | ||
403 | case OP_MOVE: { | 515 | case OP_MOVE: { |
404 | int b = GETARG_B(i); /* move from 'b' to 'a' */ | 516 | int a = GETARG_A(i); |
405 | if (b < GETARG_A(i)) | 517 | int b = GETARG_B(i); /* move from `b' to `a' */ |
406 | return getobjname(p, pc, b, name); /* get name for 'b' */ | 518 | if (b < a) |
519 | return getobjname(L, ci, b, name); /* get name for `b' */ | ||
407 | break; | 520 | break; |
408 | } | 521 | } |
409 | case OP_GETTABUP: | ||
410 | case OP_GETTABLE: { | 522 | case OP_GETTABLE: { |
411 | int k = GETARG_C(i); /* key index */ | 523 | int k = GETARG_C(i); /* key index */ |
412 | int t = GETARG_B(i); /* table index */ | 524 | *name = kname(p, k); |
413 | const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ | 525 | return "field"; |
414 | ? luaF_getlocalname(p, t + 1, pc) | ||
415 | : upvalname(p, t); | ||
416 | kname(p, pc, k, name); | ||
417 | return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; | ||
418 | } | 526 | } |
419 | case OP_GETUPVAL: { | 527 | case OP_GETUPVAL: { |
420 | *name = upvalname(p, GETARG_B(i)); | 528 | int u = GETARG_B(i); /* upvalue index */ |
529 | *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; | ||
421 | return "upvalue"; | 530 | return "upvalue"; |
422 | } | 531 | } |
423 | case OP_LOADK: | ||
424 | case OP_LOADKX: { | ||
425 | int b = (op == OP_LOADK) ? GETARG_Bx(i) | ||
426 | : GETARG_Ax(p->code[pc + 1]); | ||
427 | if (ttisstring(&p->k[b])) { | ||
428 | *name = svalue(&p->k[b]); | ||
429 | return "constant"; | ||
430 | } | ||
431 | break; | ||
432 | } | ||
433 | case OP_SELF: { | 532 | case OP_SELF: { |
434 | int k = GETARG_C(i); /* key index */ | 533 | int k = GETARG_C(i); /* key index */ |
435 | kname(p, pc, k, name); | 534 | *name = kname(p, k); |
436 | return "method"; | 535 | return "method"; |
437 | } | 536 | } |
438 | default: break; /* go through to return NULL */ | 537 | default: break; |
439 | } | 538 | } |
440 | } | 539 | } |
441 | return NULL; /* could not find reasonable name */ | 540 | return NULL; /* no useful name found */ |
442 | } | 541 | } |
443 | 542 | ||
444 | 543 | ||
445 | static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { | 544 | static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { |
446 | TMS tm; | 545 | Instruction i; |
447 | Proto *p = ci_func(ci)->p; /* calling function */ | 546 | if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) |
448 | int pc = currentpc(ci); /* calling instruction index */ | 547 | return NULL; /* calling function is not Lua (or is unknown) */ |
449 | Instruction i = p->code[pc]; /* calling instruction */ | 548 | ci--; /* calling function */ |
450 | switch (GET_OPCODE(i)) { | 549 | i = ci_func(ci)->l.p->code[currentpc(L, ci)]; |
451 | case OP_CALL: | 550 | if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || |
452 | case OP_TAILCALL: /* get function name */ | 551 | GET_OPCODE(i) == OP_TFORLOOP) |
453 | return getobjname(p, pc, GETARG_A(i), name); | 552 | return getobjname(L, ci, GETARG_A(i), name); |
454 | case OP_TFORCALL: { /* for iterator */ | 553 | else |
455 | *name = "for iterator"; | 554 | return NULL; /* no useful name can be found */ |
456 | return "for iterator"; | ||
457 | } | ||
458 | /* all other instructions can call only through metamethods */ | ||
459 | case OP_SELF: | ||
460 | case OP_GETTABUP: | ||
461 | case OP_GETTABLE: tm = TM_INDEX; break; | ||
462 | case OP_SETTABUP: | ||
463 | case OP_SETTABLE: tm = TM_NEWINDEX; break; | ||
464 | case OP_EQ: tm = TM_EQ; break; | ||
465 | case OP_ADD: tm = TM_ADD; break; | ||
466 | case OP_SUB: tm = TM_SUB; break; | ||
467 | case OP_MUL: tm = TM_MUL; break; | ||
468 | case OP_DIV: tm = TM_DIV; break; | ||
469 | case OP_MOD: tm = TM_MOD; break; | ||
470 | case OP_POW: tm = TM_POW; break; | ||
471 | case OP_UNM: tm = TM_UNM; break; | ||
472 | case OP_LEN: tm = TM_LEN; break; | ||
473 | case OP_LT: tm = TM_LT; break; | ||
474 | case OP_LE: tm = TM_LE; break; | ||
475 | case OP_CONCAT: tm = TM_CONCAT; break; | ||
476 | default: | ||
477 | return NULL; /* else no useful name can be found */ | ||
478 | } | ||
479 | *name = getstr(G(L)->tmname[tm]); | ||
480 | return "metamethod"; | ||
481 | } | 555 | } |
482 | 556 | ||
483 | /* }====================================================== */ | ||
484 | |||
485 | |||
486 | 557 | ||
487 | /* | 558 | /* only ANSI way to check whether a pointer points to an array */ |
488 | ** only ANSI way to check whether a pointer points to an array | ||
489 | ** (used only for error messages, so efficiency is not a big concern) | ||
490 | */ | ||
491 | static int isinstack (CallInfo *ci, const TValue *o) { | 559 | static int isinstack (CallInfo *ci, const TValue *o) { |
492 | StkId p; | 560 | StkId p; |
493 | for (p = ci->u.l.base; p < ci->top; p++) | 561 | for (p = ci->base; p < ci->top; p++) |
494 | if (o == p) return 1; | 562 | if (o == p) return 1; |
495 | return 0; | 563 | return 0; |
496 | } | 564 | } |
497 | 565 | ||
498 | 566 | ||
499 | static const char *getupvalname (CallInfo *ci, const TValue *o, | 567 | void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { |
500 | const char **name) { | ||
501 | LClosure *c = ci_func(ci); | ||
502 | int i; | ||
503 | for (i = 0; i < c->nupvalues; i++) { | ||
504 | if (c->upvals[i]->v == o) { | ||
505 | *name = upvalname(c->p, i); | ||
506 | return "upvalue"; | ||
507 | } | ||
508 | } | ||
509 | return NULL; | ||
510 | } | ||
511 | |||
512 | |||
513 | l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | ||
514 | CallInfo *ci = L->ci; | ||
515 | const char *name = NULL; | 568 | const char *name = NULL; |
516 | const char *t = objtypename(o); | 569 | const char *t = luaT_typenames[ttype(o)]; |
517 | const char *kind = NULL; | 570 | const char *kind = (isinstack(L->ci, o)) ? |
518 | if (isLua(ci)) { | 571 | getobjname(L, L->ci, cast_int(o - L->base), &name) : |
519 | kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ | 572 | NULL; |
520 | if (!kind && isinstack(ci, o)) /* no? try a register */ | ||
521 | kind = getobjname(ci_func(ci)->p, currentpc(ci), | ||
522 | cast_int(o - ci->u.l.base), &name); | ||
523 | } | ||
524 | if (kind) | 573 | if (kind) |
525 | luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", | 574 | luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", |
526 | op, kind, name, t); | 575 | op, kind, name, t); |
@@ -529,14 +578,14 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | |||
529 | } | 578 | } |
530 | 579 | ||
531 | 580 | ||
532 | l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { | 581 | void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { |
533 | if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; | 582 | if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; |
534 | lua_assert(!ttisstring(p1) && !ttisnumber(p1)); | 583 | lua_assert(!ttisstring(p1) && !ttisnumber(p1)); |
535 | luaG_typeerror(L, p1, "concatenate"); | 584 | luaG_typeerror(L, p1, "concatenate"); |
536 | } | 585 | } |
537 | 586 | ||
538 | 587 | ||
539 | l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { | 588 | void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { |
540 | TValue temp; | 589 | TValue temp; |
541 | if (luaV_tonumber(p1, &temp) == NULL) | 590 | if (luaV_tonumber(p1, &temp) == NULL) |
542 | p2 = p1; /* first operand is wrong */ | 591 | p2 = p1; /* first operand is wrong */ |
@@ -544,13 +593,14 @@ l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { | |||
544 | } | 593 | } |
545 | 594 | ||
546 | 595 | ||
547 | l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { | 596 | int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { |
548 | const char *t1 = objtypename(p1); | 597 | const char *t1 = luaT_typenames[ttype(p1)]; |
549 | const char *t2 = objtypename(p2); | 598 | const char *t2 = luaT_typenames[ttype(p2)]; |
550 | if (t1 == t2) | 599 | if (t1[2] == t2[2]) |
551 | luaG_runerror(L, "attempt to compare two %s values", t1); | 600 | luaG_runerror(L, "attempt to compare two %s values", t1); |
552 | else | 601 | else |
553 | luaG_runerror(L, "attempt to compare %s with %s", t1, t2); | 602 | luaG_runerror(L, "attempt to compare %s with %s", t1, t2); |
603 | return 0; | ||
554 | } | 604 | } |
555 | 605 | ||
556 | 606 | ||
@@ -558,32 +608,27 @@ static void addinfo (lua_State *L, const char *msg) { | |||
558 | CallInfo *ci = L->ci; | 608 | CallInfo *ci = L->ci; |
559 | if (isLua(ci)) { /* is Lua code? */ | 609 | if (isLua(ci)) { /* is Lua code? */ |
560 | char buff[LUA_IDSIZE]; /* add file:line information */ | 610 | char buff[LUA_IDSIZE]; /* add file:line information */ |
561 | int line = currentline(ci); | 611 | int line = currentline(L, ci); |
562 | TString *src = ci_func(ci)->p->source; | 612 | luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); |
563 | if (src) | ||
564 | luaO_chunkid(buff, getstr(src), LUA_IDSIZE); | ||
565 | else { /* no source available; use "?" instead */ | ||
566 | buff[0] = '?'; buff[1] = '\0'; | ||
567 | } | ||
568 | luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); | 613 | luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); |
569 | } | 614 | } |
570 | } | 615 | } |
571 | 616 | ||
572 | 617 | ||
573 | l_noret luaG_errormsg (lua_State *L) { | 618 | void luaG_errormsg (lua_State *L) { |
574 | if (L->errfunc != 0) { /* is there an error handling function? */ | 619 | if (L->errfunc != 0) { /* is there an error handling function? */ |
575 | StkId errfunc = restorestack(L, L->errfunc); | 620 | StkId errfunc = restorestack(L, L->errfunc); |
576 | if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); | 621 | if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); |
577 | setobjs2s(L, L->top, L->top - 1); /* move argument */ | 622 | setobjs2s(L, L->top, L->top - 1); /* move argument */ |
578 | setobjs2s(L, L->top - 1, errfunc); /* push function */ | 623 | setobjs2s(L, L->top - 1, errfunc); /* push function */ |
579 | L->top++; | 624 | incr_top(L); |
580 | luaD_call(L, L->top - 2, 1, 0); /* call it */ | 625 | luaD_call(L, L->top - 2, 1); /* call it */ |
581 | } | 626 | } |
582 | luaD_throw(L, LUA_ERRRUN); | 627 | luaD_throw(L, LUA_ERRRUN); |
583 | } | 628 | } |
584 | 629 | ||
585 | 630 | ||
586 | l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { | 631 | void luaG_runerror (lua_State *L, const char *fmt, ...) { |
587 | va_list argp; | 632 | va_list argp; |
588 | va_start(argp, fmt); | 633 | va_start(argp, fmt); |
589 | addinfo(L, luaO_pushvfstring(L, fmt, argp)); | 634 | addinfo(L, luaO_pushvfstring(L, fmt, argp)); |