diff options
author | Li Jin <dragon-fly@qq.com> | 2020-08-06 01:00:41 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-08-06 01:00:41 +0800 |
commit | 2506c1b429e952245295e54e71dac4b345e88984 (patch) | |
tree | ef9f243b6ca18d07b2734041c93fb496cdd8d796 /src/lua/ldebug.c | |
parent | c74f45ba9778792e6f5a239539d25fd7918252f7 (diff) | |
download | yuescript-2506c1b429e952245295e54e71dac4b345e88984.tar.gz yuescript-2506c1b429e952245295e54e71dac4b345e88984.tar.bz2 yuescript-2506c1b429e952245295e54e71dac4b345e88984.zip |
update Lua 5.4.
Diffstat (limited to 'src/lua/ldebug.c')
-rw-r--r-- | src/lua/ldebug.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/lua/ldebug.c b/src/lua/ldebug.c index afdc2b7..8cb00e5 100644 --- a/src/lua/ldebug.c +++ b/src/lua/ldebug.c | |||
@@ -33,10 +33,8 @@ | |||
33 | 33 | ||
34 | #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) | 34 | #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) |
35 | 35 | ||
36 | 36 | /* inverse of 'pcRel' */ | |
37 | /* Active Lua function (given call info) */ | 37 | #define invpcRel(pc, p) ((p)->code + (pc) + 1) |
38 | #define ci_func(ci) (clLvalue(s2v((ci)->func))) | ||
39 | |||
40 | 38 | ||
41 | static const char *funcnamefromcode (lua_State *L, CallInfo *ci, | 39 | static const char *funcnamefromcode (lua_State *L, CallInfo *ci, |
42 | const char **name); | 40 | const char **name); |
@@ -127,20 +125,18 @@ static void settraps (CallInfo *ci) { | |||
127 | /* | 125 | /* |
128 | ** This function can be called during a signal, under "reasonable" | 126 | ** This function can be called during a signal, under "reasonable" |
129 | ** assumptions. | 127 | ** assumptions. |
130 | ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by | 128 | ** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') |
131 | ** 'resethookcount') are for debug only, and it is no problem if they | 129 | ** are for debug only, and it is no problem if they get arbitrary |
132 | ** get arbitrary values (causes at most one wrong hook call). 'hookmask' | 130 | ** values (causes at most one wrong hook call). 'hookmask' is an atomic |
133 | ** is an atomic value. We assume that pointers are atomic too (e.g., gcc | 131 | ** value. We assume that pointers are atomic too (e.g., gcc ensures that |
134 | ** ensures that for all platforms where it runs). Moreover, 'hook' is | 132 | ** for all platforms where it runs). Moreover, 'hook' is always checked |
135 | ** always checked before being called (see 'luaD_hook'). | 133 | ** before being called (see 'luaD_hook'). |
136 | */ | 134 | */ |
137 | LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { | 135 | LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { |
138 | if (func == NULL || mask == 0) { /* turn off hooks? */ | 136 | if (func == NULL || mask == 0) { /* turn off hooks? */ |
139 | mask = 0; | 137 | mask = 0; |
140 | func = NULL; | 138 | func = NULL; |
141 | } | 139 | } |
142 | if (isLua(L->ci)) | ||
143 | L->oldpc = L->ci->u.l.savedpc; | ||
144 | L->hook = func; | 140 | L->hook = func; |
145 | L->basehookcount = count; | 141 | L->basehookcount = count; |
146 | resethookcount(L); | 142 | resethookcount(L); |
@@ -192,8 +188,8 @@ static const char *upvalname (const Proto *p, int uv) { | |||
192 | static const char *findvararg (CallInfo *ci, int n, StkId *pos) { | 188 | static const char *findvararg (CallInfo *ci, int n, StkId *pos) { |
193 | if (clLvalue(s2v(ci->func))->p->is_vararg) { | 189 | if (clLvalue(s2v(ci->func))->p->is_vararg) { |
194 | int nextra = ci->u.l.nextraargs; | 190 | int nextra = ci->u.l.nextraargs; |
195 | if (n <= nextra) { | 191 | if (n >= -nextra) { /* 'n' is negative */ |
196 | *pos = ci->func - nextra + (n - 1); | 192 | *pos = ci->func - nextra - (n + 1); |
197 | return "(vararg)"; /* generic name for any vararg */ | 193 | return "(vararg)"; /* generic name for any vararg */ |
198 | } | 194 | } |
199 | } | 195 | } |
@@ -206,7 +202,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { | |||
206 | const char *name = NULL; | 202 | const char *name = NULL; |
207 | if (isLua(ci)) { | 203 | if (isLua(ci)) { |
208 | if (n < 0) /* access to vararg values? */ | 204 | if (n < 0) /* access to vararg values? */ |
209 | return findvararg(ci, -n, pos); | 205 | return findvararg(ci, n, pos); |
210 | else | 206 | else |
211 | name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); | 207 | name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); |
212 | } | 208 | } |
@@ -787,18 +783,34 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { | |||
787 | ** previous instruction 'oldpc'. | 783 | ** previous instruction 'oldpc'. |
788 | */ | 784 | */ |
789 | static int changedline (const Proto *p, int oldpc, int newpc) { | 785 | static int changedline (const Proto *p, int oldpc, int newpc) { |
786 | if (p->lineinfo == NULL) /* no debug information? */ | ||
787 | return 0; | ||
790 | while (oldpc++ < newpc) { | 788 | while (oldpc++ < newpc) { |
791 | if (p->lineinfo[oldpc] != 0) | 789 | if (p->lineinfo[oldpc] != 0) |
792 | return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); | 790 | return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); |
793 | } | 791 | } |
794 | return 0; /* no line changes in the way */ | 792 | return 0; /* no line changes between positions */ |
795 | } | 793 | } |
796 | 794 | ||
797 | 795 | ||
796 | /* | ||
797 | ** Traces the execution of a Lua function. Called before the execution | ||
798 | ** of each opcode, when debug is on. 'L->oldpc' stores the last | ||
799 | ** instruction traced, to detect line changes. When entering a new | ||
800 | ** function, 'npci' will be zero and will test as a new line without | ||
801 | ** the need for 'oldpc'; so, 'oldpc' does not need to be initialized | ||
802 | ** before. Some exceptional conditions may return to a function without | ||
803 | ** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is | ||
804 | ** reset to zero. (A wrong but valid 'oldpc' at most causes an extra | ||
805 | ** call to a line hook.) | ||
806 | */ | ||
798 | int luaG_traceexec (lua_State *L, const Instruction *pc) { | 807 | int luaG_traceexec (lua_State *L, const Instruction *pc) { |
799 | CallInfo *ci = L->ci; | 808 | CallInfo *ci = L->ci; |
800 | lu_byte mask = L->hookmask; | 809 | lu_byte mask = L->hookmask; |
810 | const Proto *p = ci_func(ci)->p; | ||
801 | int counthook; | 811 | int counthook; |
812 | /* 'L->oldpc' may be invalid; reset it in this case */ | ||
813 | int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; | ||
802 | if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ | 814 | if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ |
803 | ci->u.l.trap = 0; /* don't need to stop again */ | 815 | ci->u.l.trap = 0; /* don't need to stop again */ |
804 | return 0; /* turn off 'trap' */ | 816 | return 0; /* turn off 'trap' */ |
@@ -819,15 +831,14 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { | |||
819 | if (counthook) | 831 | if (counthook) |
820 | luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ | 832 | luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ |
821 | if (mask & LUA_MASKLINE) { | 833 | if (mask & LUA_MASKLINE) { |
822 | const Proto *p = ci_func(ci)->p; | ||
823 | int npci = pcRel(pc, p); | 834 | int npci = pcRel(pc, p); |
824 | if (npci == 0 || /* call linehook when enter a new function, */ | 835 | if (npci == 0 || /* call linehook when enter a new function, */ |
825 | pc <= L->oldpc || /* when jump back (loop), or when */ | 836 | pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ |
826 | changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ | 837 | changedline(p, oldpc, npci)) { /* enter new line */ |
827 | int newline = luaG_getfuncline(p, npci); | 838 | int newline = luaG_getfuncline(p, npci); |
828 | luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ | 839 | luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ |
829 | } | 840 | } |
830 | L->oldpc = pc; /* 'pc' of last call to line hook */ | 841 | L->oldpc = npci; /* 'pc' of last call to line hook */ |
831 | } | 842 | } |
832 | if (L->status == LUA_YIELD) { /* did hook yield? */ | 843 | if (L->status == LUA_YIELD) { /* did hook yield? */ |
833 | if (counthook) | 844 | if (counthook) |