diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-25 12:59:42 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-25 12:59:42 -0300 |
commit | 32bf6c9b277d04a35591ca6420cf1518ebc1a7f3 (patch) | |
tree | d0d2cf99489a745e5d54f9a4779629b5095c47f1 /lvm.c | |
parent | 9c43d6a24e2612504686f9efbade264e2246bbfb (diff) | |
download | lua-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
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 81 |
1 files changed, 10 insertions, 71 deletions
@@ -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 | ||
64 | static 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 | |||
97 | static 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 | |||
114 | void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { | 64 | void 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 | ||
179 | static 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 | |||
190 | static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, | 129 | static 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 | ||
204 | static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, | 143 | static 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); |