diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-12-13 13:52:21 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-12-13 13:52:21 -0200 |
| commit | 24f6e236a3346183fe8a946568e6b0cd864abd42 (patch) | |
| tree | 0c266908c66968922572e942545c26d17a394fbc | |
| parent | 9f594ca6f54483509e39bbb4054e0ca01961e1e8 (diff) | |
| download | lua-24f6e236a3346183fe8a946568e6b0cd864abd42.tar.gz lua-24f6e236a3346183fe8a946568e6b0cd864abd42.tar.bz2 lua-24f6e236a3346183fe8a946568e6b0cd864abd42.zip | |
'moveresults' and 'luaD_poscall' moved up in the file
Diffstat (limited to '')
| -rw-r--r-- | ldo.c | 134 |
1 files changed, 67 insertions, 67 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 2.155 2016/09/08 16:36:26 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.156 2016/09/20 16:37:45 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 | */ |
| @@ -325,6 +325,72 @@ static void tryfuncTM (lua_State *L, StkId func) { | |||
| 325 | } | 325 | } |
| 326 | 326 | ||
| 327 | 327 | ||
| 328 | /* | ||
| 329 | ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. | ||
| 330 | ** Handle most typical cases (zero results for commands, one result for | ||
| 331 | ** expressions, multiple results for tail calls/single parameters) | ||
| 332 | ** separated. | ||
| 333 | */ | ||
| 334 | static int moveresults (lua_State *L, const TValue *firstResult, StkId res, | ||
| 335 | int nres, int wanted) { | ||
| 336 | switch (wanted) { /* handle typical cases separately */ | ||
| 337 | case 0: break; /* nothing to move */ | ||
| 338 | case 1: { /* one result needed */ | ||
| 339 | if (nres == 0) /* no results? */ | ||
| 340 | firstResult = luaO_nilobject; /* adjust with nil */ | ||
| 341 | setobjs2s(L, res, firstResult); /* move it to proper place */ | ||
| 342 | break; | ||
| 343 | } | ||
| 344 | case LUA_MULTRET: { | ||
| 345 | int i; | ||
| 346 | for (i = 0; i < nres; i++) /* move all results to correct place */ | ||
| 347 | setobjs2s(L, res + i, firstResult + i); | ||
| 348 | L->top = res + nres; | ||
| 349 | return 0; /* wanted == LUA_MULTRET */ | ||
| 350 | } | ||
| 351 | default: { | ||
| 352 | int i; | ||
| 353 | if (wanted <= nres) { /* enough results? */ | ||
| 354 | for (i = 0; i < wanted; i++) /* move wanted results to correct place */ | ||
| 355 | setobjs2s(L, res + i, firstResult + i); | ||
| 356 | } | ||
| 357 | else { /* not enough results; use all of them plus nils */ | ||
| 358 | for (i = 0; i < nres; i++) /* move all results to correct place */ | ||
| 359 | setobjs2s(L, res + i, firstResult + i); | ||
| 360 | for (; i < wanted; i++) /* complete wanted number of results */ | ||
| 361 | setnilvalue(res + i); | ||
| 362 | } | ||
| 363 | break; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | L->top = res + wanted; /* top points after the last result */ | ||
| 367 | return 1; | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | /* | ||
| 372 | ** Finishes a function call: calls hook if necessary, removes CallInfo, | ||
| 373 | ** moves current number of results to proper place; returns 0 iff call | ||
| 374 | ** wanted multiple (variable number of) results. | ||
| 375 | */ | ||
| 376 | int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | ||
| 377 | StkId res; | ||
| 378 | int wanted = ci->nresults; | ||
| 379 | if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { | ||
| 380 | if (L->hookmask & LUA_MASKRET) { | ||
| 381 | ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ | ||
| 382 | luaD_hook(L, LUA_HOOKRET, -1); | ||
| 383 | firstResult = restorestack(L, fr); | ||
| 384 | } | ||
| 385 | L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ | ||
| 386 | } | ||
| 387 | res = ci->func; /* res == final position of 1st result */ | ||
| 388 | L->ci = ci->previous; /* back to caller */ | ||
| 389 | /* move results to proper place */ | ||
| 390 | return moveresults(L, firstResult, res, nres, wanted); | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 328 | 394 | ||
| 329 | #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) | 395 | #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) |
| 330 | 396 | ||
| @@ -406,72 +472,6 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { | |||
| 406 | 472 | ||
| 407 | 473 | ||
| 408 | /* | 474 | /* |
| 409 | ** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. | ||
| 410 | ** Handle most typical cases (zero results for commands, one result for | ||
| 411 | ** expressions, multiple results for tail calls/single parameters) | ||
| 412 | ** separated. | ||
| 413 | */ | ||
| 414 | static int moveresults (lua_State *L, const TValue *firstResult, StkId res, | ||
| 415 | int nres, int wanted) { | ||
| 416 | switch (wanted) { /* handle typical cases separately */ | ||
| 417 | case 0: break; /* nothing to move */ | ||
| 418 | case 1: { /* one result needed */ | ||
| 419 | if (nres == 0) /* no results? */ | ||
| 420 | firstResult = luaO_nilobject; /* adjust with nil */ | ||
| 421 | setobjs2s(L, res, firstResult); /* move it to proper place */ | ||
| 422 | break; | ||
| 423 | } | ||
| 424 | case LUA_MULTRET: { | ||
| 425 | int i; | ||
| 426 | for (i = 0; i < nres; i++) /* move all results to correct place */ | ||
| 427 | setobjs2s(L, res + i, firstResult + i); | ||
| 428 | L->top = res + nres; | ||
| 429 | return 0; /* wanted == LUA_MULTRET */ | ||
| 430 | } | ||
| 431 | default: { | ||
| 432 | int i; | ||
| 433 | if (wanted <= nres) { /* enough results? */ | ||
| 434 | for (i = 0; i < wanted; i++) /* move wanted results to correct place */ | ||
| 435 | setobjs2s(L, res + i, firstResult + i); | ||
| 436 | } | ||
| 437 | else { /* not enough results; use all of them plus nils */ | ||
| 438 | for (i = 0; i < nres; i++) /* move all results to correct place */ | ||
| 439 | setobjs2s(L, res + i, firstResult + i); | ||
| 440 | for (; i < wanted; i++) /* complete wanted number of results */ | ||
| 441 | setnilvalue(res + i); | ||
| 442 | } | ||
| 443 | break; | ||
| 444 | } | ||
| 445 | } | ||
| 446 | L->top = res + wanted; /* top points after the last result */ | ||
| 447 | return 1; | ||
| 448 | } | ||
| 449 | |||
| 450 | |||
| 451 | /* | ||
| 452 | ** Finishes a function call: calls hook if necessary, removes CallInfo, | ||
| 453 | ** moves current number of results to proper place; returns 0 iff call | ||
| 454 | ** wanted multiple (variable number of) results. | ||
| 455 | */ | ||
| 456 | int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | ||
| 457 | StkId res; | ||
| 458 | int wanted = ci->nresults; | ||
| 459 | if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { | ||
| 460 | if (L->hookmask & LUA_MASKRET) { | ||
| 461 | ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ | ||
| 462 | luaD_hook(L, LUA_HOOKRET, -1); | ||
| 463 | firstResult = restorestack(L, fr); | ||
| 464 | } | ||
| 465 | L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ | ||
| 466 | } | ||
| 467 | res = ci->func; /* res == final position of 1st result */ | ||
| 468 | L->ci = ci->previous; /* back to caller */ | ||
| 469 | /* move results to proper place */ | ||
| 470 | return moveresults(L, firstResult, res, nres, wanted); | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | /* | ||
| 475 | ** Check appropriate error for stack overflow ("regular" overflow or | 475 | ** Check appropriate error for stack overflow ("regular" overflow or |
| 476 | ** overflow while handling stack overflow). If 'nCalls' is larger than | 476 | ** overflow while handling stack overflow). If 'nCalls' is larger than |
| 477 | ** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but | 477 | ** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but |
