annotate lib/lua/lua-5.2.2/ldebug.c @ 2222:66bd74aa9f5e

smk video draw
author Ritor1
date Tue, 18 Feb 2014 19:43:54 +0600
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: ldebug.c,v 2.90 2012/08/16 17:34:28 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Debug Interface
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stdarg.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <stddef.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #define ldebug_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lapi.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lcode.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lopcodes.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 #include "ltm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #include "lvm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 static int currentpc (CallInfo *ci) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 lua_assert(isLua(ci));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 static int currentline (CallInfo *ci) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 return getfuncline(ci_func(ci)->p, currentpc(ci));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 ** this function can be called asynchronous (e.g. during a signal)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 if (func == NULL || mask == 0) { /* turn off hooks? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 mask = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 func = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 if (isLua(L->ci))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 L->oldpc = L->ci->u.l.savedpc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 L->hook = func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 L->basehookcount = count;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 resethookcount(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 L->hookmask = cast_byte(mask);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 LUA_API lua_Hook lua_gethook (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 return L->hook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 LUA_API int lua_gethookmask (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 return L->hookmask;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 LUA_API int lua_gethookcount (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 return L->basehookcount;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 if (level < 0) return 0; /* invalid (negative) level */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 level--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 if (level == 0 && ci != &L->base_ci) { /* level found? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 status = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 ar->i_ci = ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 else status = 0; /* no such level */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 static const char *upvalname (Proto *p, int uv) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 if (s == NULL) return "?";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 else return getstr(s);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 int nparams = clLvalue(ci->func)->p->numparams;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 if (n >= ci->u.l.base - ci->func - nparams)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 return NULL; /* no such vararg */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 *pos = ci->func + nparams + n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 return "(*vararg)"; /* generic name for any vararg */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 static const char *findlocal (lua_State *L, CallInfo *ci, int n,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 StkId *pos) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 const char *name = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 StkId base;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 if (isLua(ci)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 if (n < 0) /* access to vararg values? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 return findvararg(ci, -n, pos);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 base = ci->u.l.base;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 base = ci->func + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 if (name == NULL) { /* no 'standard' name? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 name = "(*temporary)"; /* generic name for any valid slot */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 return NULL; /* no name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 *pos = base + (n - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 return name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 const char *name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 if (ar == NULL) { /* information about non-active function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 if (!isLfunction(L->top - 1)) /* not a Lua function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 name = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 else /* consider live variables at function start (parameters) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 else { /* active function; get information through 'ar' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 StkId pos = 0; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 name = findlocal(L, ar->i_ci, n, &pos);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 if (name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 setobj2s(L, L->top, pos);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 return name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 StkId pos = 0; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 const char *name = findlocal(L, ar->i_ci, n, &pos);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 if (name)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 setobjs2s(L, pos, L->top - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 L->top--; /* pop value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 return name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 static void funcinfo (lua_Debug *ar, Closure *cl) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 if (noLuaClosure(cl)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 ar->source = "=[C]";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 ar->linedefined = -1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 ar->lastlinedefined = -1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 ar->what = "C";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 Proto *p = cl->l.p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 ar->source = p->source ? getstr(p->source) : "=?";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 ar->linedefined = p->linedefined;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 ar->lastlinedefined = p->lastlinedefined;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 static void collectvalidlines (lua_State *L, Closure *f) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 if (noLuaClosure(f)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 setnilvalue(L->top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 TValue v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 int *lineinfo = f->l.p->lineinfo;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 Table *t = luaH_new(L); /* new table to store active lines */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 sethvalue(L, L->top, t); /* push it on stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 Closure *f, CallInfo *ci) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 int status = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 for (; *what; what++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 switch (*what) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 case 'S': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 funcinfo(ar, f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 case 'l': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 case 'u': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 ar->nups = (f == NULL) ? 0 : f->c.nupvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 if (noLuaClosure(f)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 ar->isvararg = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 ar->nparams = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 ar->isvararg = f->l.p->is_vararg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 ar->nparams = f->l.p->numparams;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 case 't': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 case 'n': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 /* calling function is a known Lua function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 if (ci && !(ci->callstatus & CIST_TAIL) && isLua(ci->previous))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 ar->namewhat = getfuncname(L, ci->previous, &ar->name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 ar->namewhat = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 if (ar->namewhat == NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 ar->namewhat = ""; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 ar->name = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 case 'L':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 case 'f': /* handled by lua_getinfo */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 default: status = 0; /* invalid option */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 Closure *cl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 StkId func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 if (*what == '>') {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 ci = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 func = L->top - 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 api_check(L, ttisfunction(func), "function expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 what++; /* skip the '>' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 L->top--; /* pop function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 ci = ar->i_ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 func = ci->func;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 lua_assert(ttisfunction(ci->func));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 cl = ttisclosure(func) ? clvalue(func) : NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 status = auxgetinfo(L, what, ar, cl, ci);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 if (strchr(what, 'f')) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 setobjs2s(L, L->top, func);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 if (strchr(what, 'L'))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 collectvalidlines(L, cl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 ** Symbolic Execution
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 static const char *getobjname (Proto *p, int lastpc, int reg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 const char **name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 ** find a "name" for the RK value 'c'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 static void kname (Proto *p, int pc, int c, const char **name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 if (ISK(c)) { /* is 'c' a constant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 TValue *kvalue = &p->k[INDEXK(c)];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 if (ttisstring(kvalue)) { /* literal constant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 *name = svalue(kvalue); /* it is its own name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 /* else no reasonable name found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 else { /* 'c' is a register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 const char *what = getobjname(p, pc, c, name); /* search for 'c' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 if (what && *what == 'c') { /* found a constant name? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 return; /* 'name' already filled */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 /* else no reasonable name found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 *name = "?"; /* no reasonable name found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 ** try to find last instruction before 'lastpc' that modified register 'reg'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 static int findsetreg (Proto *p, int lastpc, int reg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 int pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 int setreg = -1; /* keep last instruction that changed 'reg' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 for (pc = 0; pc < lastpc; pc++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 Instruction i = p->code[pc];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 OpCode op = GET_OPCODE(i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 int a = GETARG_A(i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 case OP_LOADNIL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 int b = GETARG_B(i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 setreg = pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 case OP_TFORCALL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348 if (reg >= a + 2) setreg = pc; /* affect all regs above its base */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 case OP_CALL:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 case OP_TAILCALL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 if (reg >= a) setreg = pc; /* affect all registers above base */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 case OP_JMP: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 int b = GETARG_sBx(i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 int dest = pc + 1 + b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 /* jump is forward and do not skip `lastpc'? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 if (pc < dest && dest <= lastpc)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 pc += b; /* do the jump */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 case OP_TEST: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 if (reg == a) setreg = pc; /* jumped code can change 'a' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 if (testAMode(op) && reg == a) /* any instruction that set A */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 setreg = pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 return setreg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 static const char *getobjname (Proto *p, int lastpc, int reg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 const char **name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 int pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 *name = luaF_getlocalname(p, reg + 1, lastpc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382 if (*name) /* is a local? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 return "local";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 /* else try symbolic execution */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 pc = findsetreg(p, lastpc, reg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 if (pc != -1) { /* could find instruction? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 Instruction i = p->code[pc];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 OpCode op = GET_OPCODE(i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 case OP_MOVE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 int b = GETARG_B(i); /* move from 'b' to 'a' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 if (b < GETARG_A(i))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 return getobjname(p, pc, b, name); /* get name for 'b' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396 case OP_GETTABUP:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397 case OP_GETTABLE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 int k = GETARG_C(i); /* key index */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 int t = GETARG_B(i); /* table index */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 ? luaF_getlocalname(p, t + 1, pc)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 : upvalname(p, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 kname(p, pc, k, name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 case OP_GETUPVAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 *name = upvalname(p, GETARG_B(i));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 return "upvalue";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 case OP_LOADK:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 case OP_LOADKX: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 int b = (op == OP_LOADK) ? GETARG_Bx(i)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 : GETARG_Ax(p->code[pc + 1]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 if (ttisstring(&p->k[b])) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 *name = svalue(&p->k[b]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 return "constant";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 case OP_SELF: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 int k = GETARG_C(i); /* key index */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 kname(p, pc, k, name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 return "method";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 default: break; /* go through to return NULL */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 return NULL; /* could not find reasonable name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 TMS tm;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 Proto *p = ci_func(ci)->p; /* calling function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 int pc = currentpc(ci); /* calling instruction index */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 Instruction i = p->code[pc]; /* calling instruction */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 switch (GET_OPCODE(i)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 case OP_CALL:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 case OP_TAILCALL: /* get function name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 return getobjname(p, pc, GETARG_A(i), name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 case OP_TFORCALL: { /* for iterator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 *name = "for iterator";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 return "for iterator";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 /* all other instructions can call only through metamethods */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 case OP_SELF:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 case OP_GETTABUP:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 case OP_GETTABLE: tm = TM_INDEX; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 case OP_SETTABUP:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 case OP_SETTABLE: tm = TM_NEWINDEX; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 case OP_EQ: tm = TM_EQ; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 case OP_ADD: tm = TM_ADD; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 case OP_SUB: tm = TM_SUB; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 case OP_MUL: tm = TM_MUL; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 case OP_DIV: tm = TM_DIV; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 case OP_MOD: tm = TM_MOD; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 case OP_POW: tm = TM_POW; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458 case OP_UNM: tm = TM_UNM; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 case OP_LEN: tm = TM_LEN; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 case OP_LT: tm = TM_LT; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 case OP_LE: tm = TM_LE; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 case OP_CONCAT: tm = TM_CONCAT; break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 return NULL; /* else no useful name can be found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 *name = getstr(G(L)->tmname[tm]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 return "metamethod";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 ** only ANSI way to check whether a pointer points to an array
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 ** (used only for error messages, so efficiency is not a big concern)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 static int isinstack (CallInfo *ci, const TValue *o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 StkId p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 for (p = ci->u.l.base; p < ci->top; p++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 if (o == p) return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 static const char *getupvalname (CallInfo *ci, const TValue *o,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 const char **name) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 LClosure *c = ci_func(ci);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 for (i = 0; i < c->nupvalues; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 if (c->upvals[i]->v == o) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 *name = upvalname(c->p, i);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 return "upvalue";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 const char *name = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 const char *t = objtypename(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 const char *kind = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 if (isLua(ci)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 if (!kind && isinstack(ci, o)) /* no? try a register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 kind = getobjname(ci_func(ci)->p, currentpc(ci),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509 cast_int(o - ci->u.l.base), &name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 if (kind)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 op, kind, name, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 luaG_runerror(L, "attempt to %s a %s value", op, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 lua_assert(!ttisstring(p1) && !ttisnumber(p2));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 luaG_typeerror(L, p1, "concatenate");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526 l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527 TValue temp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 if (luaV_tonumber(p1, &temp) == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 p2 = p1; /* first operand is wrong */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 luaG_typeerror(L, p2, "perform arithmetic on");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 const char *t1 = objtypename(p1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 const char *t2 = objtypename(p2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 if (t1 == t2)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 luaG_runerror(L, "attempt to compare two %s values", t1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 static void addinfo (lua_State *L, const char *msg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 if (isLua(ci)) { /* is Lua code? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 char buff[LUA_IDSIZE]; /* add file:line information */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 int line = currentline(ci);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 TString *src = ci_func(ci)->p->source;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 if (src)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 else { /* no source available; use "?" instead */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 buff[0] = '?'; buff[1] = '\0';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 l_noret luaG_errormsg (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 if (L->errfunc != 0) { /* is there an error handling function? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562 StkId errfunc = restorestack(L, L->errfunc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 setobjs2s(L, L->top, L->top - 1); /* move argument */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 setobjs2s(L, L->top - 1, errfunc); /* push function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 L->top++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 luaD_call(L, L->top - 2, 1, 0); /* call it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 luaD_throw(L, LUA_ERRRUN);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 va_list argp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 va_start(argp, fmt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 addinfo(L, luaO_pushvfstring(L, fmt, argp));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 va_end(argp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 luaG_errormsg(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580