aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ldebug.c3
-rw-r--r--ldo.c66
-rw-r--r--ldo.h3
-rw-r--r--llimits.h7
-rw-r--r--lparser.c12
-rw-r--r--lstate.c28
-rw-r--r--lstate.h17
-rw-r--r--ltests.h6
-rw-r--r--lvm.c60
9 files changed, 90 insertions, 112 deletions
diff --git a/ldebug.c b/ldebug.c
index 7323f23a..94b3596c 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.143 2017/11/13 12:20:51 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.144 2017/11/13 15:36:52 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*/
@@ -724,6 +724,7 @@ l_noret luaG_errormsg (lua_State *L) {
724 setobjs2s(L, L->top, L->top - 1); /* move argument */ 724 setobjs2s(L, L->top, L->top - 1); /* move argument */
725 setobjs2s(L, L->top - 1, errfunc); /* push function */ 725 setobjs2s(L, L->top - 1, errfunc); /* push function */
726 L->top++; /* assume EXTRA_STACK */ 726 L->top++; /* assume EXTRA_STACK */
727 luaE_incCcalls(L);
727 luaD_callnoyield(L, L->top - 2, 1); /* call it */ 728 luaD_callnoyield(L, L->top - 2, 1); /* call it */
728 } 729 }
729 luaD_throw(L, LUA_ERRRUN); 730 luaD_throw(L, LUA_ERRRUN);
diff --git a/ldo.c b/ldo.c
index 30954123..6b8162d2 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 2.172 2017/11/13 15:36:52 roberto Exp roberto $ 2** $Id: ldo.c,v 2.173 2017/11/21 14:17:35 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -135,7 +135,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
135 135
136 136
137int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { 137int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
138 unsigned short oldnCcalls = L->nCcalls; 138 unsigned short oldnCcalls = L->nCcalls - L->nci;
139 struct lua_longjmp lj; 139 struct lua_longjmp lj;
140 lj.status = LUA_OK; 140 lj.status = LUA_OK;
141 lj.previous = L->errorJmp; /* chain new error handler */ 141 lj.previous = L->errorJmp; /* chain new error handler */
@@ -144,7 +144,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
144 (*f)(L, ud); 144 (*f)(L, ud);
145 ); 145 );
146 L->errorJmp = lj.previous; /* restore old error handler */ 146 L->errorJmp = lj.previous; /* restore old error handler */
147 L->nCcalls = oldnCcalls; 147 L->nCcalls = oldnCcalls + L->nci;
148 return lj.status; 148 return lj.status;
149} 149}
150 150
@@ -299,7 +299,7 @@ static void callhook (lua_State *L, CallInfo *ci, int istail) {
299 299
300/* 300/*
301** Check whether __call metafield of 'func' is a function. If so, put 301** Check whether __call metafield of 'func' is a function. If so, put
302** it in stack below original 'func' so that 'luaD_precall' can call 302** it in stack below original 'func' so that 'luaD_call' can call
303** it. Raise an error if __call metafield is not a function. 303** it. Raise an error if __call metafield is not a function.
304*/ 304*/
305StkId luaD_tryfuncTM (lua_State *L, StkId func) { 305StkId luaD_tryfuncTM (lua_State *L, StkId func) {
@@ -417,13 +417,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) {
417 417
418 418
419/* 419/*
420** Prepares a function call: checks the stack, creates a new CallInfo 420** Call a function (C or Lua). The function to be called is at *func.
421** entry, fills in the relevant information, calls hook if needed. 421** The arguments are on the stack, right after the function.
422** If function is a C function, does the call, too. (Otherwise, leave 422** When returns, all the results are on the stack, starting at the original
423** the execution ('luaV_execute') to the caller, to allow stackless 423** function position.
424** calls.) Returns true iff function has been executed (C function).
425*/ 424*/
426int luaD_precall (lua_State *L, StkId func, int nresults) { 425void luaD_call (lua_State *L, StkId func, int nresults) {
427 lua_CFunction f; 426 lua_CFunction f;
428 TValue *funcv = s2v(func); 427 TValue *funcv = s2v(func);
429 CallInfo *ci; 428 CallInfo *ci;
@@ -449,7 +448,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
449 lua_lock(L); 448 lua_lock(L);
450 api_checknelems(L, n); 449 api_checknelems(L, n);
451 luaD_poscall(L, ci, L->top - n, n); 450 luaD_poscall(L, ci, L->top - n, n);
452 return 1; 451 break;
453 } 452 }
454 case LUA_TLCL: { /* Lua function: prepare its call */ 453 case LUA_TLCL: { /* Lua function: prepare its call */
455 Proto *p = clLvalue(funcv)->p; 454 Proto *p = clLvalue(funcv)->p;
@@ -469,47 +468,19 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
469 ci->callstatus = CIST_LUA; 468 ci->callstatus = CIST_LUA;
470 if (L->hookmask) 469 if (L->hookmask)
471 callhook(L, ci, 0); 470 callhook(L, ci, 0);
472 return 0; 471 luaV_execute(L); /* run the function */
472 break;
473 } 473 }
474 default: { /* not a function */ 474 default: { /* not a function */
475 func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ 475 func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
476 return luaD_precall(L, func, nresults); /* now it must be a function */ 476 luaD_call(L, func, nresults); /* now it must be a function */
477 break;
477 } 478 }
478 } 479 }
479} 480}
480 481
481 482
482/* 483/*
483** Check appropriate error for stack overflow ("regular" overflow or
484** overflow while handling stack overflow). If 'nCalls' is larger than
485** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
486** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
487** allow overflow handling to work)
488*/
489static void stackerror (lua_State *L) {
490 if (L->nCcalls == LUAI_MAXCCALLS)
491 luaG_runerror(L, "C stack overflow");
492 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
493 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
494}
495
496
497/*
498** Call a function (C or Lua). The function to be called is at *func.
499** The arguments are on the stack, right after the function.
500** When returns, all the results are on the stack, starting at the original
501** function position.
502*/
503void luaD_call (lua_State *L, StkId func, int nResults) {
504 if (++L->nCcalls >= LUAI_MAXCCALLS)
505 stackerror(L);
506 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
507 luaV_execute(L); /* call it */
508 L->nCcalls--;
509}
510
511
512/*
513** Similar to 'luaD_call', but does not allow yields during the call 484** Similar to 'luaD_call', but does not allow yields during the call
514*/ 485*/
515void luaD_callnoyield (lua_State *L, StkId func, int nResults) { 486void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
@@ -541,7 +512,7 @@ static void finishCcall (lua_State *L, int status) {
541 n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ 512 n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */
542 lua_lock(L); 513 lua_lock(L);
543 api_checknelems(L, n); 514 api_checknelems(L, n);
544 luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ 515 luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_call' */
545} 516}
546 517
547 518
@@ -629,8 +600,7 @@ static void resume (lua_State *L, void *ud) {
629 StkId firstArg = L->top - n; /* first argument */ 600 StkId firstArg = L->top - n; /* first argument */
630 CallInfo *ci = L->ci; 601 CallInfo *ci = L->ci;
631 if (L->status == LUA_OK) { /* starting a coroutine? */ 602 if (L->status == LUA_OK) { /* starting a coroutine? */
632 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ 603 luaD_call(L, firstArg - 1, LUA_MULTRET);
633 luaV_execute(L); /* call it */
634 } 604 }
635 else { /* resuming from previous yield */ 605 else { /* resuming from previous yield */
636 lua_assert(L->status == LUA_YIELD); 606 lua_assert(L->status == LUA_YIELD);
@@ -645,7 +615,7 @@ static void resume (lua_State *L, void *ud) {
645 api_checknelems(L, n); 615 api_checknelems(L, n);
646 firstArg = L->top - n; /* yield results come from continuation */ 616 firstArg = L->top - n; /* yield results come from continuation */
647 } 617 }
648 luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ 618 luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_call' */
649 } 619 }
650 unroll(L, NULL); /* run continuation */ 620 unroll(L, NULL); /* run continuation */
651 } 621 }
@@ -688,7 +658,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
688 : L->top - (L->ci->func + 1); 658 : L->top - (L->ci->func + 1);
689 L->nny = oldnny; /* restore 'nny' */ 659 L->nny = oldnny; /* restore 'nny' */
690 L->nCcalls--; 660 L->nCcalls--;
691 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); 661 // lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
692 lua_unlock(L); 662 lua_unlock(L);
693 return status; 663 return status;
694} 664}
diff --git a/ldo.h b/ldo.h
index 2640c088..4fba45b5 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 2.33 2017/11/07 13:25:26 roberto Exp roberto $ 2** $Id: ldo.h,v 2.34 2017/11/21 14:18:03 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -47,7 +47,6 @@ typedef void (*Pfunc) (lua_State *L, void *ud);
47LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 47LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
48 const char *mode); 48 const char *mode);
49LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 49LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
50LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
51LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); 50LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n);
52LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 51LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
53LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 52LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
diff --git a/llimits.h b/llimits.h
index 4b35dfcb..4e641008 100644
--- a/llimits.h
+++ b/llimits.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llimits.h,v 1.143 2017/06/01 19:16:34 roberto Exp roberto $ 2** $Id: llimits.h,v 1.144 2017/06/27 11:35:01 roberto Exp roberto $
3** Limits, basic types, and some other 'installation-dependent' definitions 3** Limits, basic types, and some other 'installation-dependent' definitions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -140,10 +140,11 @@ typedef LUAI_UACINT l_uacInt;
140 140
141/* 141/*
142** maximum depth for nested C calls and syntactical nested non-terminals 142** maximum depth for nested C calls and syntactical nested non-terminals
143** in a program. (Value must fit in an unsigned short int.) 143** in a program. (Value must fit in an unsigned short int. It must also
144** be compatible with the size of the C stack.)
144*/ 145*/
145#if !defined(LUAI_MAXCCALLS) 146#if !defined(LUAI_MAXCCALLS)
146#define LUAI_MAXCCALLS 200 147#define LUAI_MAXCCALLS 1000
147#endif 148#endif
148 149
149 150
diff --git a/lparser.c b/lparser.c
index 0637a0b7..3eb83fba 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 2.166 2017/09/28 16:53:29 roberto Exp roberto $ 2** $Id: lparser.c,v 2.167 2017/10/04 21:53:03 roberto Exp roberto $
3** Lua Parser 3** Lua Parser
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -330,11 +330,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
330} 330}
331 331
332 332
333static void enterlevel (LexState *ls) { 333#define enterlevel(ls) luaE_incCcalls((ls)->L)
334 lua_State *L = ls->L;
335 ++L->nCcalls;
336 checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels");
337}
338 334
339 335
340#define leavelevel(ls) ((ls)->L->nCcalls--) 336#define leavelevel(ls) ((ls)->L->nCcalls--)
@@ -1188,9 +1184,9 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
1188 suffixedexp(ls, &nv.v); 1184 suffixedexp(ls, &nv.v);
1189 if (!vkisindexed(nv.v.k)) 1185 if (!vkisindexed(nv.v.k))
1190 check_conflict(ls, lh, &nv.v); 1186 check_conflict(ls, lh, &nv.v);
1191 checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, 1187 luaE_incCcalls(ls->L); /* control recursion depth */
1192 "C levels");
1193 assignment(ls, &nv, nvars+1); 1188 assignment(ls, &nv, nvars+1);
1189 ls->L->nCcalls--;
1194 } 1190 }
1195 else { /* assignment -> '=' explist */ 1191 else { /* assignment -> '=' explist */
1196 int nexps; 1192 int nexps;
diff --git a/lstate.c b/lstate.c
index 54e390b5..22702a00 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.146 2017/11/07 13:25:26 roberto Exp roberto $ 2** $Id: lstate.c,v 2.147 2017/11/13 15:36:52 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -97,8 +97,28 @@ void luaE_setdebt (global_State *g, l_mem debt) {
97} 97}
98 98
99 99
100/*
101** Increment count of "C calls" and check for overflows. In case of
102** a stack overflow, check appropriate error ("regular" overflow or
103** overflow while handling stack overflow). If 'nCalls' is larger than
104** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but
105** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to
106** allow overflow handling to work)
107*/
108void luaE_incCcalls (lua_State *L) {
109 if (++L->nCcalls >= LUAI_MAXCCALLS) {
110 if (L->nCcalls == LUAI_MAXCCALLS)
111 luaG_runerror(L, "C stack overflow");
112 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
113 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
114 }
115}
116
117
100CallInfo *luaE_extendCI (lua_State *L) { 118CallInfo *luaE_extendCI (lua_State *L) {
101 CallInfo *ci = luaM_new(L, CallInfo); 119 CallInfo *ci;
120 luaE_incCcalls(L);
121 ci = luaM_new(L, CallInfo);
102 lua_assert(L->ci->next == NULL); 122 lua_assert(L->ci->next == NULL);
103 L->ci->next = ci; 123 L->ci->next = ci;
104 ci->previous = L->ci; 124 ci->previous = L->ci;
@@ -116,11 +136,13 @@ void luaE_freeCI (lua_State *L) {
116 CallInfo *ci = L->ci; 136 CallInfo *ci = L->ci;
117 CallInfo *next = ci->next; 137 CallInfo *next = ci->next;
118 ci->next = NULL; 138 ci->next = NULL;
139 L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */
119 while ((ci = next) != NULL) { 140 while ((ci = next) != NULL) {
120 next = ci->next; 141 next = ci->next;
121 luaM_free(L, ci); 142 luaM_free(L, ci);
122 L->nci--; 143 L->nci--;
123 } 144 }
145 L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */
124} 146}
125 147
126 148
@@ -130,6 +152,7 @@ void luaE_freeCI (lua_State *L) {
130void luaE_shrinkCI (lua_State *L) { 152void luaE_shrinkCI (lua_State *L) {
131 CallInfo *ci = L->ci; 153 CallInfo *ci = L->ci;
132 CallInfo *next2; /* next's next */ 154 CallInfo *next2; /* next's next */
155 L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */
133 /* while there are two nexts */ 156 /* while there are two nexts */
134 while (ci->next != NULL && (next2 = ci->next->next) != NULL) { 157 while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
135 luaM_free(L, ci->next); /* free next */ 158 luaM_free(L, ci->next); /* free next */
@@ -138,6 +161,7 @@ void luaE_shrinkCI (lua_State *L) {
138 next2->previous = ci; 161 next2->previous = ci;
139 ci = next2; /* keep next's next */ 162 ci = next2; /* keep next's next */
140 } 163 }
164 L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */
141} 165}
142 166
143 167
diff --git a/lstate.h b/lstate.h
index 02715dfb..fad54634 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 2.150 2017/11/07 13:25:26 roberto Exp roberto $ 2** $Id: lstate.h,v 2.151 2017/11/13 15:36:52 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -104,7 +104,7 @@ typedef struct CallInfo {
104 int nyield; /* number of values yielded */ 104 int nyield; /* number of values yielded */
105 } u2; 105 } u2;
106 short nresults; /* expected number of results from this function */ 106 short nresults; /* expected number of results from this function */
107 unsigned short callstatus; 107 lu_byte callstatus;
108} CallInfo; 108} CallInfo;
109 109
110 110
@@ -114,13 +114,11 @@ typedef struct CallInfo {
114#define CIST_OAH (1<<0) /* original value of 'allowhook' */ 114#define CIST_OAH (1<<0) /* original value of 'allowhook' */
115#define CIST_LUA (1<<1) /* call is running a Lua function */ 115#define CIST_LUA (1<<1) /* call is running a Lua function */
116#define CIST_HOOKED (1<<2) /* call is running a debug hook */ 116#define CIST_HOOKED (1<<2) /* call is running a debug hook */
117#define CIST_FRESH (1<<3) /* call is running on a fresh invocation 117#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */
118 of luaV_execute */ 118#define CIST_TAIL (1<<4) /* call was tail called */
119#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 119#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */
120#define CIST_TAIL (1<<5) /* call was tail called */ 120#define CIST_LEQ (1<<6) /* using __lt for __le */
121#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ 121#define CIST_FIN (1<<7) /* call is running a finalizer */
122#define CIST_LEQ (1<<7) /* using __lt for __le */
123#define CIST_FIN (1<<8) /* call is running a finalizer */
124 122
125#define isLua(ci) ((ci)->callstatus & CIST_LUA) 123#define isLua(ci) ((ci)->callstatus & CIST_LUA)
126 124
@@ -256,6 +254,7 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
256LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 254LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
257LUAI_FUNC void luaE_freeCI (lua_State *L); 255LUAI_FUNC void luaE_freeCI (lua_State *L);
258LUAI_FUNC void luaE_shrinkCI (lua_State *L); 256LUAI_FUNC void luaE_shrinkCI (lua_State *L);
257LUAI_FUNC void luaE_incCcalls (lua_State *L);
259 258
260 259
261#endif 260#endif
diff --git a/ltests.h b/ltests.h
index e3d1ca1e..04aa91b3 100644
--- a/ltests.h
+++ b/ltests.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.h,v 2.51 2017/06/27 11:35:31 roberto Exp roberto $ 2** $Id: ltests.h,v 2.52 2017/11/13 12:19:35 roberto Exp roberto $
3** Internal Header for Debugging of the Lua Implementation 3** Internal Header for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -34,6 +34,10 @@
34#define lua_assert(c) assert(c) 34#define lua_assert(c) assert(c)
35 35
36 36
37/* compiled with -O0, Lua uses a lot of C stack space... */
38#undef LUAI_MAXCCALLS
39#define LUAI_MAXCCALLS 300
40
37/* to avoid warnings, and to make sure value is really unused */ 41/* to avoid warnings, and to make sure value is really unused */
38#define UNUSED(x) (x=0, (void)(x)) 42#define UNUSED(x) (x=0, (void)(x))
39 43
diff --git a/lvm.c b/lvm.c
index 457da1dd..ca65c798 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,9 @@
1/* 1/*
2** $Id: lvm.c,v 2.314 2017/11/22 18:41:20 roberto Exp roberto $ 2<<<<<<< lvm.c
3** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp roberto $
4=======
5** $Id: lvm.c,v 2.315 2017/11/22 19:15:44 roberto Exp $
6>>>>>>> 2.315
3** Lua virtual machine 7** Lua virtual machine
4** See Copyright Notice in lua.h 8** See Copyright Notice in lua.h
5*/ 9*/
@@ -829,20 +833,12 @@ void luaV_finishOp (lua_State *L) {
829 833
830 834
831void luaV_execute (lua_State *L) { 835void luaV_execute (lua_State *L) {
832 CallInfo *ci = L->ci; /* local copy of 'L->ci' */ 836 CallInfo *ci = L->ci;
833 LClosure *cl; 837 LClosure *cl = clLvalue(s2v(ci->func));
834 TValue *k; 838 TValue *k = cl->p->k;
835 StkId base; /* local copy of 'ci->func + 1' */ 839 StkId base = ci->func + 1;
836 int trap; 840 int trap = ci->u.l.trap;
837 const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ 841 const Instruction *pc = ci->u.l.savedpc;
838 ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */
839 newframe: /* reentry point when frame changes (call/return) */
840 lua_assert(ci == L->ci);
841 cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */
842 k = cl->p->k; /* local reference to function's constant table */
843 updatetrap(ci);
844 updatebase(ci);
845 pc = ci->u.l.savedpc;
846 /* main loop of interpreter */ 842 /* main loop of interpreter */
847 for (;;) { 843 for (;;) {
848 Instruction i; 844 Instruction i;
@@ -1418,20 +1414,13 @@ void luaV_execute (lua_State *L) {
1418 vmcase(OP_CALL) { 1414 vmcase(OP_CALL) {
1419 int b = GETARG_B(i); 1415 int b = GETARG_B(i);
1420 int nresults = GETARG_C(i) - 1; 1416 int nresults = GETARG_C(i) - 1;
1421 int isC;
1422 if (b != 0) /* fixed number of arguments? */ 1417 if (b != 0) /* fixed number of arguments? */
1423 L->top = ra + b; /* top signals number of arguments */ 1418 L->top = ra + b; /* top signals number of arguments */
1424 /* else previous instruction set top */ 1419 /* else previous instruction set top */
1425 Protect(isC = luaD_precall(L, ra, nresults)); 1420 Protect(luaD_call(L, ra, nresults));
1426 if (isC) { /* C function? */ 1421 if (nresults >= 0) /* fixed number of results? */
1427 if (nresults >= 0) /* fixed number of results? */ 1422 L->top = ci->top; /* correct top */
1428 L->top = ci->top; /* correct top */ 1423 /* else leave top for next instruction */
1429 /* else leave top for next instruction */
1430 }
1431 else { /* Lua function */
1432 ci = L->ci;
1433 goto newframe; /* restart luaV_execute over new Lua function */
1434 }
1435 vmbreak; 1424 vmbreak;
1436 } 1425 }
1437 vmcase(OP_TAILCALL) { 1426 vmcase(OP_TAILCALL) {
@@ -1447,12 +1436,15 @@ void luaV_execute (lua_State *L) {
1447 b++; /* there is now one extra argument */ 1436 b++; /* there is now one extra argument */
1448 } 1437 }
1449 if (!ttisLclosure(s2v(ra))) /* C function? */ 1438 if (!ttisLclosure(s2v(ra))) /* C function? */
1450 Protect(luaD_precall(L, ra, LUA_MULTRET)); /* call it */ 1439 Protect(luaD_call(L, ra, LUA_MULTRET)); /* call it */
1451 else { /* tail call */ 1440 else { /* tail call */
1452 if (cl->p->sizep > 0) /* close upvalues from previous call */ 1441 if (cl->p->sizep > 0) /* close upvalues from previous call */
1453 luaF_close(L, ci->func + 1); 1442 luaF_close(L, ci->func + 1);
1454 luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ 1443 luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
1455 goto newframe; /* restart luaV_execute over new Lua function */ 1444 cl = clLvalue(s2v(ci->func));
1445 k = cl->p->k;
1446 updatebase(ci);
1447 pc = ci->u.l.savedpc;
1456 } 1448 }
1457 vmbreak; 1449 vmbreak;
1458 } 1450 }
@@ -1461,16 +1453,8 @@ void luaV_execute (lua_State *L) {
1461 if (cl->p->sizep > 0) 1453 if (cl->p->sizep > 0)
1462 luaF_close(L, base); 1454 luaF_close(L, base);
1463 savepc(L); 1455 savepc(L);
1464 b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); 1456 luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
1465 if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ 1457 return; /* external invocation: return */
1466 return; /* external invocation: return */
1467 else { /* invocation via reentry: continue execution */
1468 ci = L->ci;
1469 if (b) L->top = ci->top;
1470 lua_assert(isLua(ci));
1471 lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
1472 goto newframe; /* restart luaV_execute over new Lua function */
1473 }
1474 } 1458 }
1475 vmcase(OP_FORLOOP) { 1459 vmcase(OP_FORLOOP) {
1476 if (ttisinteger(s2v(ra))) { /* integer loop? */ 1460 if (ttisinteger(s2v(ra))) { /* integer loop? */