summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-25 12:59:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-25 12:59:42 -0300
commit32bf6c9b277d04a35591ca6420cf1518ebc1a7f3 (patch)
treed0d2cf99489a745e5d54f9a4779629b5095c47f1
parent9c43d6a24e2612504686f9efbade264e2246bbfb (diff)
downloadlua-32bf6c9b277d04a35591ca6420cf1518ebc1a7f3.tar.gz
lua-32bf6c9b277d04a35591ca6420cf1518ebc1a7f3.tar.bz2
lua-32bf6c9b277d04a35591ca6420cf1518ebc1a7f3.zip
functions 'traceexec', 'callTM', and 'call_binTM' moved to other
files to make 'lvm.c' a little smaller
-rw-r--r--ldebug.c35
-rw-r--r--ldebug.h4
-rw-r--r--ltm.c31
-rw-r--r--ltm.h7
-rw-r--r--lvm.c81
5 files changed, 83 insertions, 75 deletions
diff --git a/ldebug.c b/ldebug.c
index 5624be1d..17baf27c 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.89 2012/01/20 22:05:50 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.90 2012/08/16 17:34:28 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -578,3 +578,36 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
578 luaG_errormsg(L); 578 luaG_errormsg(L);
579} 579}
580 580
581
582void luaG_traceexec (lua_State *L) {
583 CallInfo *ci = L->ci;
584 lu_byte mask = L->hookmask;
585 int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
586 if (counthook)
587 resethookcount(L); /* reset count */
588 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
589 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
590 return; /* do not call hook again (VM yielded, so it did not move) */
591 }
592 if (counthook)
593 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
594 if (mask & LUA_MASKLINE) {
595 Proto *p = ci_func(ci)->p;
596 int npc = pcRel(ci->u.l.savedpc, p);
597 int newline = getfuncline(p, npc);
598 if (npc == 0 || /* call linehook when enter a new function, */
599 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
600 newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
601 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
602 }
603 L->oldpc = ci->u.l.savedpc;
604 if (L->status == LUA_YIELD) { /* did hook yield? */
605 if (counthook)
606 L->hookcount = 1; /* undo decrement to zero */
607 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
608 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
609 ci->func = L->top - 1; /* protect stack below results */
610 luaD_throw(L, LUA_YIELD);
611 }
612}
613
diff --git a/ldebug.h b/ldebug.h
index 7af551e2..946b37b3 100644
--- a/ldebug.h
+++ b/ldebug.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.h,v 2.6 2011/06/02 19:31:40 roberto Exp roberto $ 2** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 roberto Exp roberto $
3** Auxiliary functions from Debug Interface module 3** Auxiliary functions from Debug Interface module
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -30,5 +30,7 @@ LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
30 const TValue *p2); 30 const TValue *p2);
31LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 31LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
32LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 32LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
33LUAI_FUNC void luaG_traceexec (lua_State *L);
34
33 35
34#endif 36#endif
diff --git a/ltm.c b/ltm.c
index 0f0cb91b..1e7bf829 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp roberto $ 2** $Id: ltm.c,v 2.15 2013/04/12 19:07:09 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,6 +12,7 @@
12 12
13#include "lua.h" 13#include "lua.h"
14 14
15#include "ldo.h"
15#include "lobject.h" 16#include "lobject.h"
16#include "lstate.h" 17#include "lstate.h"
17#include "lstring.h" 18#include "lstring.h"
@@ -75,3 +76,31 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
75 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
76} 77}
77 78
79
80void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
81 const TValue *p2, TValue *p3, int hasres) {
82 ptrdiff_t result = savestack(L, p3);
83 setobj2s(L, L->top++, f); /* push function */
84 setobj2s(L, L->top++, p1); /* 1st argument */
85 setobj2s(L, L->top++, p2); /* 2nd argument */
86 if (!hasres) /* no result? 'p3' is third argument */
87 setobj2s(L, L->top++, p3); /* 3rd argument */
88 /* metamethod may yield only when called from Lua code */
89 luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
90 if (hasres) { /* if has result, move it to its place */
91 p3 = restorestack(L, result);
92 setobjs2s(L, p3, --L->top);
93 }
94}
95
96
97int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
98 StkId res, TMS event) {
99 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
100 if (ttisnil(tm))
101 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
102 if (ttisnil(tm)) return 0;
103 luaT_callTM(L, tm, p1, p2, res, 1);
104 return 1;
105}
106
diff --git a/ltm.h b/ltm.h
index f2caa9e6..cce6e6d9 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp roberto $ 2** $Id: ltm.h,v 2.12 2013/04/12 19:07:09 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -54,4 +54,9 @@ LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
54 TMS event); 54 TMS event);
55LUAI_FUNC void luaT_init (lua_State *L); 55LUAI_FUNC void luaT_init (lua_State *L);
56 56
57LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
58 const TValue *p2, TValue *p3, int hasres);
59LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
60 StkId res, TMS event);
61
57#endif 62#endif
diff --git a/lvm.c b/lvm.c
index bf2a2132..54d99d21 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.157 2013/04/15 15:44:46 roberto Exp roberto $ 2** $Id: lvm.c,v 2.158 2013/04/16 18:43:05 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -61,56 +61,6 @@ int luaV_tostring (lua_State *L, StkId obj) {
61} 61}
62 62
63 63
64static void traceexec (lua_State *L) {
65 CallInfo *ci = L->ci;
66 lu_byte mask = L->hookmask;
67 int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
68 if (counthook)
69 resethookcount(L); /* reset count */
70 if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */
71 ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */
72 return; /* do not call hook again (VM yielded, so it did not move) */
73 }
74 if (counthook)
75 luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */
76 if (mask & LUA_MASKLINE) {
77 Proto *p = ci_func(ci)->p;
78 int npc = pcRel(ci->u.l.savedpc, p);
79 int newline = getfuncline(p, npc);
80 if (npc == 0 || /* call linehook when enter a new function, */
81 ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */
82 newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */
83 luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */
84 }
85 L->oldpc = ci->u.l.savedpc;
86 if (L->status == LUA_YIELD) { /* did hook yield? */
87 if (counthook)
88 L->hookcount = 1; /* undo decrement to zero */
89 ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
90 ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
91 ci->func = L->top - 1; /* protect stack below results */
92 luaD_throw(L, LUA_YIELD);
93 }
94}
95
96
97static void callTM (lua_State *L, const TValue *f, const TValue *p1,
98 const TValue *p2, TValue *p3, int hasres) {
99 ptrdiff_t result = savestack(L, p3);
100 setobj2s(L, L->top++, f); /* push function */
101 setobj2s(L, L->top++, p1); /* 1st argument */
102 setobj2s(L, L->top++, p2); /* 2nd argument */
103 if (!hasres) /* no result? 'p3' is third argument */
104 setobj2s(L, L->top++, p3); /* 3rd argument */
105 /* metamethod may yield only when called from Lua code */
106 luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
107 if (hasres) { /* if has result, move it to its place */
108 p3 = restorestack(L, result);
109 setobjs2s(L, p3, --L->top);
110 }
111}
112
113
114void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { 64void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
115 int loop; 65 int loop;
116 for (loop = 0; loop < MAXTAGLOOP; loop++) { 66 for (loop = 0; loop < MAXTAGLOOP; loop++) {
@@ -128,7 +78,7 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
128 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) 78 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
129 luaG_typeerror(L, t, "index"); 79 luaG_typeerror(L, t, "index");
130 if (ttisfunction(tm)) { 80 if (ttisfunction(tm)) {
131 callTM(L, tm, t, key, val, 1); 81 luaT_callTM(L, tm, t, key, val, 1);
132 return; 82 return;
133 } 83 }
134 t = tm; /* else repeat with 'tm' */ 84 t = tm; /* else repeat with 'tm' */
@@ -167,7 +117,7 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
167 luaG_typeerror(L, t, "index"); 117 luaG_typeerror(L, t, "index");
168 /* there is a metamethod */ 118 /* there is a metamethod */
169 if (ttisfunction(tm)) { 119 if (ttisfunction(tm)) {
170 callTM(L, tm, t, key, val, 0); 120 luaT_callTM(L, tm, t, key, val, 0);
171 return; 121 return;
172 } 122 }
173 t = tm; /* else repeat with 'tm' */ 123 t = tm; /* else repeat with 'tm' */
@@ -176,17 +126,6 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
176} 126}
177 127
178 128
179static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
180 StkId res, TMS event) {
181 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
182 if (ttisnil(tm))
183 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
184 if (ttisnil(tm)) return 0;
185 callTM(L, tm, p1, p2, res, 1);
186 return 1;
187}
188
189
190static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, 129static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
191 TMS event) { 130 TMS event) {
192 const TValue *tm1 = fasttm(L, mt1, event); 131 const TValue *tm1 = fasttm(L, mt1, event);
@@ -203,7 +142,7 @@ static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
203 142
204static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, 143static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
205 TMS event) { 144 TMS event) {
206 if (!call_binTM(L, p1, p2, L->top, event)) 145 if (!luaT_callbinTM(L, p1, p2, L->top, event))
207 return -1; /* no metamethod */ 146 return -1; /* no metamethod */
208 else 147 else
209 return !l_isfalse(L->top); 148 return !l_isfalse(L->top);
@@ -295,7 +234,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
295 return gcvalue(t1) == gcvalue(t2); 234 return gcvalue(t1) == gcvalue(t2);
296 } 235 }
297 if (tm == NULL) return 0; /* no TM? */ 236 if (tm == NULL) return 0; /* no TM? */
298 callTM(L, tm, t1, t2, L->top, 1); /* call TM */ 237 luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
299 return !l_isfalse(L->top); 238 return !l_isfalse(L->top);
300} 239}
301 240
@@ -306,7 +245,7 @@ void luaV_concat (lua_State *L, int total) {
306 StkId top = L->top; 245 StkId top = L->top;
307 int n = 2; /* number of elements handled in this pass (at least 2) */ 246 int n = 2; /* number of elements handled in this pass (at least 2) */
308 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { 247 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
309 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) 248 if (!luaT_callbinTM(L, top-2, top-1, top-2, TM_CONCAT))
310 luaG_concaterror(L, top-2, top-1); 249 luaG_concaterror(L, top-2, top-1);
311 } 250 }
312 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ 251 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
@@ -363,7 +302,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
363 break; 302 break;
364 } 303 }
365 } 304 }
366 callTM(L, tm, rb, rb, ra, 1); 305 luaT_callTM(L, tm, rb, rb, ra, 1);
367} 306}
368 307
369 308
@@ -376,7 +315,7 @@ void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
376 lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c)); 315 lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
377 setnvalue(ra, res); 316 setnvalue(ra, res);
378 } 317 }
379 else if (!call_binTM(L, rb, rc, ra, op)) 318 else if (!luaT_callbinTM(L, rb, rc, ra, op))
380 luaG_aritherror(L, rb, rc); 319 luaG_aritherror(L, rb, rc);
381} 320}
382 321
@@ -456,7 +395,7 @@ void luaV_finishOp (lua_State *L) {
456 break; 395 break;
457 } 396 }
458 case OP_CONCAT: { 397 case OP_CONCAT: {
459 StkId top = L->top - 1; /* top when 'call_binTM' was called */ 398 StkId top = L->top - 1; /* top when 'luaT_callbinTM' was called */
460 int b = GETARG_B(inst); /* first element to concatenate */ 399 int b = GETARG_B(inst); /* first element to concatenate */
461 int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ 400 int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
462 setobj2s(L, top - 2, top); /* put TM result in proper position */ 401 setobj2s(L, top - 2, top); /* put TM result in proper position */
@@ -557,7 +496,7 @@ void luaV_execute (lua_State *L) {
557 StkId ra; 496 StkId ra;
558 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && 497 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
559 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 498 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
560 Protect(traceexec(L)); 499 Protect(luaG_traceexec(L));
561 } 500 }
562 /* WARNING: several calls may realloc the stack and invalidate `ra' */ 501 /* WARNING: several calls may realloc the stack and invalidate `ra' */
563 ra = RA(i); 502 ra = RA(i);