diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-06 09:45:25 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-06 09:45:25 -0300 |
| commit | ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7 (patch) | |
| tree | b79618aaceaa71a89f2dfe4a571d34ed55d78c83 | |
| parent | 046a3d6173792b7d4d4d26a4e063e2fe383c10a7 (diff) | |
| download | lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.tar.gz lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.tar.bz2 lua-ad3816d0d15b9e681a39dfe90b78ecd8e68aaec7.zip | |
luaD_call is more uniform
Diffstat (limited to '')
| -rw-r--r-- | ldebug.c | 11 | ||||
| -rw-r--r-- | ldo.c | 46 | ||||
| -rw-r--r-- | ldo.h | 4 | ||||
| -rw-r--r-- | lvm.c | 27 |
4 files changed, 42 insertions, 46 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldebug.c,v 1.44 2000/10/05 12:14:08 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 1.45 2000/10/05 13:00:17 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 | */ |
| @@ -92,8 +92,8 @@ static int lua_nups (StkId f) { | |||
| 92 | 92 | ||
| 93 | int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { | 93 | int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { |
| 94 | int refi; | 94 | int refi; |
| 95 | if (lineinfo == NULL) return -1; /* no line info */ | 95 | if (lineinfo == NULL || pc == -1) |
| 96 | else if (pc == -1) return refline; /* function preamble */ | 96 | return -1; /* no line info or function is not active */ |
| 97 | refi = prefi ? *prefi : 0; | 97 | refi = prefi ? *prefi : 0; |
| 98 | if (lineinfo[refi] < 0) | 98 | if (lineinfo[refi] < 0) |
| 99 | refline += -lineinfo[refi++]; | 99 | refline += -lineinfo[refi++]; |
| @@ -124,7 +124,10 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { | |||
| 124 | static int lua_currentpc (StkId f) { | 124 | static int lua_currentpc (StkId f) { |
| 125 | CallInfo *ci = infovalue(f); | 125 | CallInfo *ci = infovalue(f); |
| 126 | LUA_ASSERT(isLmark(f), "function has no pc"); | 126 | LUA_ASSERT(isLmark(f), "function has no pc"); |
| 127 | return (*ci->pc - ci->func->f.l->code) - 1; | 127 | if (ci->pc) |
| 128 | return (*ci->pc - ci->func->f.l->code) - 1; | ||
| 129 | else | ||
| 130 | return -1; /* function is not active */ | ||
| 128 | } | 131 | } |
| 129 | 132 | ||
| 130 | 133 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.102 2000/10/05 12:14:08 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.103 2000/10/05 13:00:17 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 | */ |
| @@ -41,7 +41,7 @@ void luaD_init (lua_State *L, int stacksize) { | |||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | void luaD_checkstack (lua_State *L, int n) { | 43 | void luaD_checkstack (lua_State *L, int n) { |
| 44 | if (L->stack_last-L->top <= n) { /* stack overflow? */ | 44 | if (L->stack_last - L->top <= n) { /* stack overflow? */ |
| 45 | if (L->stack_last-L->stack > (L->stacksize-1)) { | 45 | if (L->stack_last-L->stack > (L->stacksize-1)) { |
| 46 | /* overflow while handling overflow: do what?? */ | 46 | /* overflow while handling overflow: do what?? */ |
| 47 | L->top -= EXTRA_STACK; | 47 | L->top -= EXTRA_STACK; |
| @@ -112,19 +112,19 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) { | |||
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | 114 | ||
| 115 | void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | 115 | static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, |
| 116 | const char *event) { | 116 | const char *event) { |
| 117 | if (L->allowhooks) { | 117 | if (L->allowhooks) { |
| 118 | lua_Debug ar; | 118 | lua_Debug ar; |
| 119 | ar._func = func; | 119 | ar._func = func; |
| 120 | ar.event = event; | 120 | ar.event = event; |
| 121 | infovalue(func)->pc = NULL; /* function is not active */ | ||
| 121 | dohook(L, &ar, callhook); | 122 | dohook(L, &ar, callhook); |
| 122 | } | 123 | } |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 125 | 126 | ||
| 126 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | 127 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { |
| 127 | lua_Hook callhook = L->callhook; | ||
| 128 | int nup = cl->nupvalues; /* number of upvalues */ | 128 | int nup = cl->nupvalues; /* number of upvalues */ |
| 129 | StkId old_Cbase = L->Cbase; | 129 | StkId old_Cbase = L->Cbase; |
| 130 | int n; | 130 | int n; |
| @@ -132,11 +132,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | |||
| 132 | luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ | 132 | luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ |
| 133 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ | 133 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ |
| 134 | *(L->top++) = cl->upvalue[n]; | 134 | *(L->top++) = cl->upvalue[n]; |
| 135 | if (callhook) | ||
| 136 | luaD_callHook(L, base-1, callhook, "call"); | ||
| 137 | n = (*cl->f.c)(L); /* do the actual call */ | 135 | n = (*cl->f.c)(L); /* do the actual call */ |
| 138 | if (callhook) /* same hook that was active at entry */ | ||
| 139 | luaD_callHook(L, base-1, callhook, "return"); | ||
| 140 | L->Cbase = old_Cbase; /* restore old C base */ | 136 | L->Cbase = old_Cbase; /* restore old C base */ |
| 141 | return L->top - n; /* return index of first result */ | 137 | return L->top - n; /* return index of first result */ |
| 142 | } | 138 | } |
| @@ -159,6 +155,7 @@ void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) { | |||
| 159 | ** The number of results is nResults, unless nResults=LUA_MULTRET. | 155 | ** The number of results is nResults, unless nResults=LUA_MULTRET. |
| 160 | */ | 156 | */ |
| 161 | void luaD_call (lua_State *L, StkId func, int nResults) { | 157 | void luaD_call (lua_State *L, StkId func, int nResults) { |
| 158 | lua_Hook callhook; | ||
| 162 | StkId firstResult; | 159 | StkId firstResult; |
| 163 | CallInfo ci; | 160 | CallInfo ci; |
| 164 | Closure *cl; | 161 | Closure *cl; |
| @@ -175,22 +172,29 @@ void luaD_call (lua_State *L, StkId func, int nResults) { | |||
| 175 | ci.func = cl; | 172 | ci.func = cl; |
| 176 | infovalue(func) = &ci; | 173 | infovalue(func) = &ci; |
| 177 | ttype(func) = LUA_TMARK; | 174 | ttype(func) = LUA_TMARK; |
| 178 | if (cl->isC) | 175 | callhook = L->callhook; |
| 179 | firstResult = callCclosure(L, cl, func+1); | 176 | if (callhook) |
| 180 | else | 177 | luaD_callHook(L, func, callhook, "call"); |
| 181 | firstResult = luaV_execute(L, cl, func+1); | 178 | firstResult = (cl->isC ? callCclosure(L, cl, func+1) : |
| 179 | luaV_execute(L, cl, func+1)); | ||
| 180 | if (callhook) /* same hook that was active at entry */ | ||
| 181 | luaD_callHook(L, func, callhook, "return"); | ||
| 182 | LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); | 182 | LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); |
| 183 | /* adjust the number of results */ | ||
| 184 | if (nResults == LUA_MULTRET) | ||
| 185 | nResults = L->top - firstResult; | ||
| 186 | else | ||
| 187 | luaD_adjusttop(L, firstResult, nResults); | ||
| 188 | /* move results to `func' (to erase parameters and function) */ | 183 | /* move results to `func' (to erase parameters and function) */ |
| 189 | while (nResults) { | 184 | if (nResults == LUA_MULTRET) { |
| 190 | *func++ = *(L->top - nResults); | 185 | while (firstResult < L->top) /* copy all results */ |
| 191 | nResults--; | 186 | *func++ = *firstResult++; |
| 187 | L->top = func; | ||
| 188 | } | ||
| 189 | else { /* copy at most `nResults' */ | ||
| 190 | for (; nResults > 0 && firstResult < L->top; nResults--) | ||
| 191 | *func++ = *firstResult++; | ||
| 192 | L->top = func; | ||
| 193 | for (; nResults > 0; nResults--) { /* if there are not enough results */ | ||
| 194 | ttype(L->top) = LUA_TNIL; /* adjust the stack */ | ||
| 195 | incr_top; /* must check stack space */ | ||
| 196 | } | ||
| 192 | } | 197 | } |
| 193 | L->top = func; | ||
| 194 | luaC_checkGC(L); | 198 | luaC_checkGC(L); |
| 195 | } | 199 | } |
| 196 | 200 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.h,v 1.26 2000/10/04 12:16:08 roberto Exp roberto $ | 2 | ** $Id: ldo.h,v 1.27 2000/10/05 13:00:17 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 | */ |
| @@ -22,8 +22,6 @@ | |||
| 22 | void luaD_init (lua_State *L, int stacksize); | 22 | void luaD_init (lua_State *L, int stacksize); |
| 23 | void luaD_adjusttop (lua_State *L, StkId base, int extra); | 23 | void luaD_adjusttop (lua_State *L, StkId base, int extra); |
| 24 | void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook); | 24 | void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook); |
| 25 | void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | ||
| 26 | const char *event); | ||
| 27 | void luaD_call (lua_State *L, StkId func, int nResults); | 25 | void luaD_call (lua_State *L, StkId func, int nResults); |
| 28 | void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults); | 26 | void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults); |
| 29 | void luaD_checkstack (lua_State *L, int n); | 27 | void luaD_checkstack (lua_State *L, int n); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.143 2000/10/05 12:14:08 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.144 2000/10/05 13:00:17 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 | */ |
| @@ -67,7 +67,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ | |||
| 67 | static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) { | 67 | static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) { |
| 68 | CallInfo *ci = infovalue(base-1); | 68 | CallInfo *ci = infovalue(base-1); |
| 69 | int *lineinfo = ci->func->f.l->lineinfo; | 69 | int *lineinfo = ci->func->f.l->lineinfo; |
| 70 | int pc = (*ci->pc - 1) - ci->func->f.l->code; | 70 | int pc = (*ci->pc - ci->func->f.l->code) - 1; |
| 71 | int newline; | 71 | int newline; |
| 72 | if (pc == 0) { /* may be first time? */ | 72 | if (pc == 0) { /* may be first time? */ |
| 73 | ci->line = 1; | 73 | ci->line = 1; |
| @@ -349,22 +349,18 @@ static void adjust_varargs (lua_State *L, StkId base, int nfixargs) { | |||
| 349 | ** Returns n such that the the results are between [n,top). | 349 | ** Returns n such that the the results are between [n,top). |
| 350 | */ | 350 | */ |
| 351 | StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | 351 | StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { |
| 352 | const Proto *tf = cl->f.l; | 352 | const Proto *const tf = cl->f.l; |
| 353 | StkId top; /* keep top local, for performance */ | 353 | StkId top; /* keep top local, for performance */ |
| 354 | const Instruction *pc = tf->code; | 354 | const Instruction *pc = tf->code; |
| 355 | TString **kstr = tf->kstr; | 355 | TString **const kstr = tf->kstr; |
| 356 | lua_Hook callhook = L->callhook; | 356 | const lua_Hook linehook = L->linehook; |
| 357 | lua_Hook linehook; /* set it only after calling eventual call hook */ | ||
| 358 | infovalue(base-1)->pc = &pc; | 357 | infovalue(base-1)->pc = &pc; |
| 359 | luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); | 358 | luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); |
| 360 | if (tf->is_vararg) /* varargs? */ | 359 | if (tf->is_vararg) /* varargs? */ |
| 361 | adjust_varargs(L, base, tf->numparams); | 360 | adjust_varargs(L, base, tf->numparams); |
| 362 | else | 361 | else |
| 363 | luaD_adjusttop(L, base, tf->numparams); | 362 | luaD_adjusttop(L, base, tf->numparams); |
| 364 | if (callhook) | ||
| 365 | luaD_callHook(L, base-1, callhook, "call"); | ||
| 366 | top = L->top; | 363 | top = L->top; |
| 367 | linehook = L->linehook; | ||
| 368 | /* main loop of interpreter */ | 364 | /* main loop of interpreter */ |
| 369 | for (;;) { | 365 | for (;;) { |
| 370 | const Instruction i = *pc++; | 366 | const Instruction i = *pc++; |
| @@ -373,12 +369,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 373 | switch (GET_OPCODE(i)) { | 369 | switch (GET_OPCODE(i)) { |
| 374 | case OP_END: { | 370 | case OP_END: { |
| 375 | L->top = top; | 371 | L->top = top; |
| 376 | goto endloop; | 372 | return top; |
| 377 | } | 373 | } |
| 378 | case OP_RETURN: { | 374 | case OP_RETURN: { |
| 379 | L->top = top; | 375 | L->top = top; |
| 380 | top = base+GETARG_U(i); | 376 | return base+GETARG_U(i); |
| 381 | goto endloop; | ||
| 382 | } | 377 | } |
| 383 | case OP_CALL: { | 378 | case OP_CALL: { |
| 384 | int nres = GETARG_B(i); | 379 | int nres = GETARG_B(i); |
| @@ -391,8 +386,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 391 | case OP_TAILCALL: { | 386 | case OP_TAILCALL: { |
| 392 | L->top = top; | 387 | L->top = top; |
| 393 | luaD_call(L, base+GETARG_A(i), LUA_MULTRET); | 388 | luaD_call(L, base+GETARG_A(i), LUA_MULTRET); |
| 394 | top = base+GETARG_B(i); | 389 | return base+GETARG_B(i); |
| 395 | goto endloop; | ||
| 396 | } | 390 | } |
| 397 | case OP_PUSHNIL: { | 391 | case OP_PUSHNIL: { |
| 398 | int n = GETARG_U(i); | 392 | int n = GETARG_U(i); |
| @@ -712,8 +706,5 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 712 | break; | 706 | break; |
| 713 | } | 707 | } |
| 714 | } | 708 | } |
| 715 | } endloop: | 709 | } |
| 716 | if (callhook) /* same hook that was active at entry */ | ||
| 717 | luaD_callHook(L, base-1, callhook, "return"); | ||
| 718 | return top; | ||
| 719 | } | 710 | } |
