2009-05-21 19:01:41 +00:00
|
|
|
/*
|
2018-11-08 16:32:45 +00:00
|
|
|
** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $
|
2009-05-21 19:01:41 +00:00
|
|
|
** Stack and Call structure of Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
/* #include <setjmp.h> */
|
2009-05-21 19:01:41 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define ldo_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "ldebug.h"
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
#include "lundump.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
#include "lzio.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Error-recovery functions
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* chain list of long jump buffers */
|
|
|
|
struct lua_longjmp {
|
|
|
|
struct lua_longjmp *previous;
|
|
|
|
luai_jmpbuf b;
|
|
|
|
volatile int status; /* error code */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
|
2009-05-21 19:01:41 +00:00
|
|
|
switch (errcode) {
|
2014-04-02 18:46:06 +00:00
|
|
|
case LUA_ERRMEM: {
|
2019-07-12 10:23:52 +00:00
|
|
|
ptrdiff_t oldtopr = savestack(L, oldtop);
|
|
|
|
setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, MEMERRMSG));
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_ERRERR: {
|
2019-07-12 10:23:52 +00:00
|
|
|
ptrdiff_t oldtopr = savestack(L, oldtop);
|
|
|
|
setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, "error in error handling"));
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
case LUA_ERRSYNTAX:
|
|
|
|
case LUA_ERRRUN: {
|
2009-05-21 19:01:41 +00:00
|
|
|
setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
L->top = oldtop + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
static void restore_stack_limit (lua_State *L) {
|
|
|
|
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
|
|
|
|
if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
|
|
|
|
int inuse = cast_int(L->ci - L->base_ci);
|
|
|
|
if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
|
|
|
|
luaD_reallocCI(L, LUAI_MAXCALLS);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void resetstack (lua_State *L, int status) {
|
|
|
|
L->ci = L->base_ci;
|
|
|
|
L->base = L->ci->base;
|
|
|
|
luaF_close(L, L->base); /* close eventual pending closures */
|
|
|
|
luaD_seterrorobj(L, status, L->base);
|
|
|
|
L->nCcalls = L->baseCcalls;
|
|
|
|
L->allowhook = 1;
|
|
|
|
restore_stack_limit(L);
|
|
|
|
L->errfunc = 0;
|
|
|
|
L->errorJmp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaD_throw (lua_State *L, int errcode) {
|
2019-07-12 10:23:52 +00:00
|
|
|
unfixedstack(L); /* make sure the fixedstack & block_gc flags get reset. */
|
|
|
|
unset_block_gc(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
if (L->errorJmp) {
|
|
|
|
L->errorJmp->status = errcode;
|
|
|
|
LUAI_THROW(L, L->errorJmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
L->status = cast_byte(errcode);
|
|
|
|
if (G(L)->panic) {
|
|
|
|
resetstack(L, errcode);
|
|
|
|
lua_unlock(L);
|
|
|
|
G(L)->panic(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
|
|
|
|
struct lua_longjmp lj;
|
2014-04-02 18:46:06 +00:00
|
|
|
lj.status = 0;
|
2009-05-21 19:01:41 +00:00
|
|
|
lj.previous = L->errorJmp; /* chain new error handler */
|
|
|
|
L->errorJmp = &lj;
|
|
|
|
LUAI_TRY(L, &lj,
|
|
|
|
(*f)(L, ud);
|
|
|
|
);
|
|
|
|
L->errorJmp = lj.previous; /* restore old error handler */
|
|
|
|
return lj.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
static void correctstack (lua_State *L, TValue *oldstack) {
|
|
|
|
CallInfo *ci;
|
|
|
|
GCObject *up;
|
|
|
|
L->top = (L->top - oldstack) + L->stack;
|
|
|
|
for (up = L->openupval; up != NULL; up = up->gch.next)
|
|
|
|
gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
|
2014-04-02 18:46:06 +00:00
|
|
|
for (ci = L->base_ci; ci <= L->ci; ci++) {
|
2009-05-21 19:01:41 +00:00
|
|
|
ci->top = (ci->top - oldstack) + L->stack;
|
2014-04-02 18:46:06 +00:00
|
|
|
ci->base = (ci->base - oldstack) + L->stack;
|
2009-05-21 19:01:41 +00:00
|
|
|
ci->func = (ci->func - oldstack) + L->stack;
|
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
L->base = (L->base - oldstack) + L->stack;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaD_reallocstack (lua_State *L, int newsize) {
|
|
|
|
TValue *oldstack = L->stack;
|
2014-04-02 18:46:06 +00:00
|
|
|
int realsize = newsize + 1 + EXTRA_STACK;
|
|
|
|
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
|
|
|
|
luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
|
|
|
|
L->stacksize = realsize;
|
|
|
|
L->stack_last = L->stack+newsize;
|
2009-05-21 19:01:41 +00:00
|
|
|
correctstack(L, oldstack);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
void luaD_reallocCI (lua_State *L, int newsize) {
|
|
|
|
CallInfo *oldci = L->base_ci;
|
|
|
|
luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
|
|
|
|
L->size_ci = newsize;
|
|
|
|
L->ci = (L->ci - oldci) + L->base_ci;
|
|
|
|
L->end_ci = L->base_ci + L->size_ci - 1;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
void luaD_growstack (lua_State *L, int n) {
|
|
|
|
if (n <= L->stacksize) /* double size is enough? */
|
|
|
|
luaD_reallocstack(L, 2*L->stacksize);
|
|
|
|
else
|
|
|
|
luaD_reallocstack(L, L->stacksize + n);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
static CallInfo *growCI (lua_State *L) {
|
|
|
|
if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
|
|
|
|
luaD_throw(L, LUA_ERRERR);
|
|
|
|
else {
|
|
|
|
luaD_reallocCI(L, 2*L->size_ci);
|
|
|
|
if (L->size_ci > LUAI_MAXCALLS)
|
|
|
|
luaG_runerror(L, "stack overflow");
|
|
|
|
}
|
|
|
|
return ++L->ci;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
void luaD_callhook (lua_State *L, int event, int line) {
|
2009-05-21 19:01:41 +00:00
|
|
|
lua_Hook hook = L->hook;
|
|
|
|
if (hook && L->allowhook) {
|
|
|
|
ptrdiff_t top = savestack(L, L->top);
|
2014-04-02 18:46:06 +00:00
|
|
|
ptrdiff_t ci_top = savestack(L, L->ci->top);
|
2009-05-21 19:01:41 +00:00
|
|
|
lua_Debug ar;
|
|
|
|
ar.event = event;
|
|
|
|
ar.currentline = line;
|
2014-04-02 18:46:06 +00:00
|
|
|
if (event == LUA_HOOKTAILRET)
|
|
|
|
ar.i_ci = 0; /* tail call; no debug information about it */
|
|
|
|
else
|
|
|
|
ar.i_ci = cast_int(L->ci - L->base_ci);
|
2009-05-21 19:01:41 +00:00
|
|
|
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
2014-04-02 18:46:06 +00:00
|
|
|
L->ci->top = L->top + LUA_MINSTACK;
|
|
|
|
lua_assert(L->ci->top <= L->stack_last);
|
2009-05-21 19:01:41 +00:00
|
|
|
L->allowhook = 0; /* cannot call hooks inside a hook */
|
|
|
|
lua_unlock(L);
|
|
|
|
(*hook)(L, &ar);
|
|
|
|
lua_lock(L);
|
|
|
|
lua_assert(!L->allowhook);
|
|
|
|
L->allowhook = 1;
|
2014-04-02 18:46:06 +00:00
|
|
|
L->ci->top = restorestack(L, ci_top);
|
2009-05-21 19:01:41 +00:00
|
|
|
L->top = restorestack(L, top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
|
|
|
|
int i;
|
|
|
|
int nfixargs = p->numparams;
|
2019-07-12 10:23:52 +00:00
|
|
|
#if defined(LUA_COMPAT_VARARG)
|
2014-04-02 18:46:06 +00:00
|
|
|
Table *htab = NULL;
|
2019-07-12 10:23:52 +00:00
|
|
|
#endif
|
2009-05-21 19:01:41 +00:00
|
|
|
StkId base, fixed;
|
2014-04-02 18:46:06 +00:00
|
|
|
for (; actual < nfixargs; ++actual)
|
|
|
|
setnilvalue(L->top++);
|
|
|
|
#if defined(LUA_COMPAT_VARARG)
|
|
|
|
if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
|
|
|
|
int nvar = actual - nfixargs; /* number of extra arguments */
|
|
|
|
lua_assert(p->is_vararg & VARARG_HASARG);
|
|
|
|
luaC_checkGC(L);
|
2018-11-08 16:32:45 +00:00
|
|
|
luaD_checkstack(L, p->maxstacksize);
|
2014-04-02 18:46:06 +00:00
|
|
|
htab = luaH_new(L, nvar, 1); /* create `arg' table */
|
2019-07-12 10:23:52 +00:00
|
|
|
sethvalue2s(L, L->top, htab); /* put table on stack */
|
|
|
|
incr_top(L);
|
|
|
|
fixedstack(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
|
2019-07-12 10:23:52 +00:00
|
|
|
setobj2n(L, luaH_setnum(L, htab, i+1), L->top - 1 - nvar + i);
|
|
|
|
unfixedstack(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
/* store counter in field `n' */
|
|
|
|
setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
|
2019-07-12 10:23:52 +00:00
|
|
|
L->top--; /* remove table from stack */
|
2014-04-02 18:46:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-21 19:01:41 +00:00
|
|
|
/* move fixed parameters to final position */
|
|
|
|
fixed = L->top - actual; /* first fixed argument */
|
|
|
|
base = L->top; /* final position of first argument */
|
|
|
|
for (i=0; i<nfixargs; i++) {
|
2014-04-02 18:46:06 +00:00
|
|
|
setobjs2s(L, L->top++, fixed+i);
|
|
|
|
setnilvalue(fixed+i);
|
|
|
|
}
|
2019-07-12 10:23:52 +00:00
|
|
|
#if defined(LUA_COMPAT_VARARG)
|
2014-04-02 18:46:06 +00:00
|
|
|
/* add `arg' parameter */
|
|
|
|
if (htab) {
|
|
|
|
sethvalue(L, L->top++, htab);
|
|
|
|
lua_assert(iswhite(obj2gco(htab)));
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2019-07-12 10:23:52 +00:00
|
|
|
#endif
|
2009-05-21 19:01:41 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static StkId tryfuncTM (lua_State *L, StkId func) {
|
|
|
|
const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
|
|
|
|
StkId p;
|
|
|
|
ptrdiff_t funcr = savestack(L, func);
|
|
|
|
if (!ttisfunction(tm))
|
|
|
|
luaG_typeerror(L, func, "call");
|
|
|
|
/* Open a hole inside the stack at `func' */
|
|
|
|
for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
|
|
|
|
incr_top(L);
|
|
|
|
func = restorestack(L, funcr); /* previous call may change stack */
|
|
|
|
setobj2s(L, func, tm); /* tag method is the new function to be called */
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
#define inc_ci(L) \
|
|
|
|
((L->ci == L->end_ci) ? growCI(L) : \
|
|
|
|
(condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
|
2009-05-21 19:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
int luaD_precall (lua_State *L, StkId func, int nresults) {
|
2014-04-02 18:46:06 +00:00
|
|
|
LClosure *cl;
|
|
|
|
ptrdiff_t funcr;
|
|
|
|
if (!ttisfunction(func)) /* `func' is not a function? */
|
|
|
|
func = tryfuncTM(L, func); /* check the `function' tag method */
|
|
|
|
funcr = savestack(L, func);
|
|
|
|
cl = &clvalue(func)->l;
|
|
|
|
L->ci->savedpc = L->savedpc;
|
|
|
|
if (!cl->isC) { /* Lua function? prepare its call */
|
|
|
|
CallInfo *ci;
|
|
|
|
StkId st, base;
|
|
|
|
Proto *p = cl->p;
|
|
|
|
luaD_checkstack(L, p->maxstacksize);
|
|
|
|
func = restorestack(L, funcr);
|
|
|
|
if (!p->is_vararg) { /* no varargs? */
|
|
|
|
base = func + 1;
|
|
|
|
if (L->top > base + p->numparams)
|
|
|
|
L->top = base + p->numparams;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
else { /* vararg function */
|
|
|
|
int nargs = cast_int(L->top - func) - 1;
|
|
|
|
base = adjust_varargs(L, p, nargs);
|
|
|
|
func = restorestack(L, funcr); /* previous call may change the stack */
|
|
|
|
}
|
|
|
|
ci = inc_ci(L); /* now `enter' new function */
|
|
|
|
ci->func = func;
|
|
|
|
L->base = ci->base = base;
|
|
|
|
ci->top = L->base + p->maxstacksize;
|
|
|
|
lua_assert(ci->top <= L->stack_last);
|
|
|
|
L->savedpc = p->code; /* starting point */
|
|
|
|
ci->tailcalls = 0;
|
|
|
|
ci->nresults = nresults;
|
|
|
|
for (st = L->top; st < ci->top; st++)
|
|
|
|
setnilvalue(st);
|
|
|
|
L->top = ci->top;
|
|
|
|
if (L->hookmask & LUA_MASKCALL) {
|
|
|
|
L->savedpc++; /* hooks assume 'pc' is already incremented */
|
|
|
|
luaD_callhook(L, LUA_HOOKCALL, -1);
|
|
|
|
L->savedpc--; /* correct 'pc' */
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
return PCRLUA;
|
|
|
|
}
|
|
|
|
else { /* if is a C function, call it */
|
|
|
|
CallInfo *ci;
|
|
|
|
int n;
|
|
|
|
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
|
|
|
|
ci = inc_ci(L); /* now `enter' new function */
|
|
|
|
ci->func = restorestack(L, funcr);
|
|
|
|
L->base = ci->base = ci->func + 1;
|
|
|
|
ci->top = L->top + LUA_MINSTACK;
|
|
|
|
lua_assert(ci->top <= L->stack_last);
|
|
|
|
ci->nresults = nresults;
|
|
|
|
if (L->hookmask & LUA_MASKCALL)
|
|
|
|
luaD_callhook(L, LUA_HOOKCALL, -1);
|
|
|
|
lua_unlock(L);
|
|
|
|
n = (*curr_func(L)->c.f)(L); /* do the actual call */
|
|
|
|
lua_lock(L);
|
|
|
|
if (n < 0) /* yielding? */
|
|
|
|
return PCRYIELD;
|
|
|
|
else {
|
|
|
|
luaD_poscall(L, L->top - n);
|
|
|
|
return PCRC;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
static StkId callrethooks (lua_State *L, StkId firstResult) {
|
|
|
|
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
|
|
|
|
luaD_callhook(L, LUA_HOOKRET, -1);
|
|
|
|
if (f_isLua(L->ci)) { /* Lua function? */
|
|
|
|
while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
|
|
|
|
luaD_callhook(L, LUA_HOOKTAILRET, -1);
|
|
|
|
}
|
|
|
|
return restorestack(L, fr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-21 19:01:41 +00:00
|
|
|
int luaD_poscall (lua_State *L, StkId firstResult) {
|
|
|
|
StkId res;
|
|
|
|
int wanted, i;
|
2014-04-02 18:46:06 +00:00
|
|
|
CallInfo *ci;
|
|
|
|
if (L->hookmask & LUA_MASKRET)
|
|
|
|
firstResult = callrethooks(L, firstResult);
|
|
|
|
ci = L->ci--;
|
2009-05-21 19:01:41 +00:00
|
|
|
res = ci->func; /* res == final position of 1st result */
|
|
|
|
wanted = ci->nresults;
|
2014-04-02 18:46:06 +00:00
|
|
|
L->base = (ci - 1)->base; /* restore base */
|
|
|
|
L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
|
2009-05-21 19:01:41 +00:00
|
|
|
/* move results to correct place */
|
|
|
|
for (i = wanted; i != 0 && firstResult < L->top; i--)
|
|
|
|
setobjs2s(L, res++, firstResult++);
|
|
|
|
while (i-- > 0)
|
|
|
|
setnilvalue(res++);
|
|
|
|
L->top = res;
|
|
|
|
return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Call a function (C or Lua). The function to be called is at *func.
|
|
|
|
** The arguments are on the stack, right after the function.
|
|
|
|
** When returns, all the results are on the stack, starting at the original
|
|
|
|
** function position.
|
2014-04-02 18:46:06 +00:00
|
|
|
*/
|
|
|
|
void luaD_call (lua_State *L, StkId func, int nResults) {
|
2009-05-21 19:01:41 +00:00
|
|
|
if (++L->nCcalls >= LUAI_MAXCCALLS) {
|
|
|
|
if (L->nCcalls == LUAI_MAXCCALLS)
|
|
|
|
luaG_runerror(L, "C stack overflow");
|
|
|
|
else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
|
|
|
|
luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
|
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
|
|
|
|
luaV_execute(L, 1); /* call it */
|
2009-05-21 19:01:41 +00:00
|
|
|
L->nCcalls--;
|
2014-04-02 18:46:06 +00:00
|
|
|
luaC_checkGC(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
static void resume (lua_State *L, void *ud) {
|
|
|
|
StkId firstArg = cast(StkId, ud);
|
|
|
|
CallInfo *ci = L->ci;
|
2014-04-02 18:46:06 +00:00
|
|
|
if (L->status == 0) { /* start coroutine? */
|
|
|
|
lua_assert(ci == L->base_ci && firstArg > L->base);
|
|
|
|
if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
|
|
|
|
return;
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
|
|
|
else { /* resuming from previous yield */
|
2014-04-02 18:46:06 +00:00
|
|
|
lua_assert(L->status == LUA_YIELD);
|
|
|
|
L->status = 0;
|
|
|
|
if (!f_isLua(ci)) { /* `common' yield? */
|
|
|
|
/* finish interrupted execution of `OP_CALL' */
|
|
|
|
lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
|
|
|
|
GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
|
|
|
|
if (luaD_poscall(L, firstArg)) /* complete it... */
|
|
|
|
L->top = L->ci->top; /* and correct top if not multiple results */
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
else /* yielded inside a hook: just continue its execution */
|
|
|
|
L->base = L->ci->base;
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
luaV_execute(L, cast_int(L->ci - L->base_ci));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int resume_error (lua_State *L, const char *msg) {
|
|
|
|
L->top = L->ci->base;
|
|
|
|
setsvalue2s(L, L->top, luaS_new(L, msg));
|
|
|
|
incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
return LUA_ERRRUN;
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
LUA_API int lua_resume (lua_State *L, int nargs) {
|
2009-05-21 19:01:41 +00:00
|
|
|
int status;
|
|
|
|
lua_lock(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
|
|
|
|
return resume_error(L, "cannot resume non-suspended coroutine");
|
|
|
|
if (L->nCcalls >= LUAI_MAXCCALLS)
|
|
|
|
return resume_error(L, "C stack overflow");
|
2009-05-21 19:01:41 +00:00
|
|
|
luai_userstateresume(L, nargs);
|
2014-04-02 18:46:06 +00:00
|
|
|
lua_assert(L->errfunc == 0);
|
|
|
|
L->baseCcalls = ++L->nCcalls;
|
2009-05-21 19:01:41 +00:00
|
|
|
status = luaD_rawrunprotected(L, resume, L->top - nargs);
|
2014-04-02 18:46:06 +00:00
|
|
|
if (status != 0) { /* error? */
|
|
|
|
L->status = cast_byte(status); /* mark thread as `dead' */
|
|
|
|
luaD_seterrorobj(L, status, L->top);
|
|
|
|
L->ci->top = L->top;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
else {
|
|
|
|
lua_assert(L->nCcalls == L->baseCcalls);
|
|
|
|
status = L->status;
|
|
|
|
}
|
|
|
|
--L->nCcalls;
|
2009-05-21 19:01:41 +00:00
|
|
|
lua_unlock(L);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
LUA_API int lua_yield (lua_State *L, int nresults) {
|
2009-05-21 19:01:41 +00:00
|
|
|
luai_userstateyield(L, nresults);
|
|
|
|
lua_lock(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
if (L->nCcalls > L->baseCcalls)
|
|
|
|
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
|
|
|
|
L->base = L->top - nresults; /* protect stack slots below */
|
2009-05-21 19:01:41 +00:00
|
|
|
L->status = LUA_YIELD;
|
|
|
|
lua_unlock(L);
|
2014-04-02 18:46:06 +00:00
|
|
|
return -1;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
|
|
|
ptrdiff_t old_top, ptrdiff_t ef) {
|
|
|
|
int status;
|
2014-04-02 18:46:06 +00:00
|
|
|
unsigned short oldnCcalls = L->nCcalls;
|
|
|
|
ptrdiff_t old_ci = saveci(L, L->ci);
|
2009-05-21 19:01:41 +00:00
|
|
|
lu_byte old_allowhooks = L->allowhook;
|
|
|
|
ptrdiff_t old_errfunc = L->errfunc;
|
|
|
|
L->errfunc = ef;
|
|
|
|
status = luaD_rawrunprotected(L, func, u);
|
2014-04-02 18:46:06 +00:00
|
|
|
if (status != 0) { /* an error occurred? */
|
2009-05-21 19:01:41 +00:00
|
|
|
StkId oldtop = restorestack(L, old_top);
|
2014-04-02 18:46:06 +00:00
|
|
|
luaF_close(L, oldtop); /* close eventual pending closures */
|
|
|
|
luaD_seterrorobj(L, status, oldtop);
|
|
|
|
L->nCcalls = oldnCcalls;
|
|
|
|
L->ci = restoreci(L, old_ci);
|
|
|
|
L->base = L->ci->base;
|
|
|
|
L->savedpc = L->ci->savedpc;
|
2009-05-21 19:01:41 +00:00
|
|
|
L->allowhook = old_allowhooks;
|
2014-04-02 18:46:06 +00:00
|
|
|
restore_stack_limit(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
L->errfunc = old_errfunc;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Execute a protected parser.
|
|
|
|
*/
|
|
|
|
struct SParser { /* data to `f_parser' */
|
|
|
|
ZIO *z;
|
2014-04-02 18:46:06 +00:00
|
|
|
Mbuffer buff; /* buffer to be used by the scanner */
|
2009-05-21 19:01:41 +00:00
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void f_parser (lua_State *L, void *ud) {
|
|
|
|
int i;
|
2014-04-02 18:46:06 +00:00
|
|
|
Proto *tf;
|
2009-05-21 19:01:41 +00:00
|
|
|
Closure *cl;
|
|
|
|
struct SParser *p = cast(struct SParser *, ud);
|
2014-04-02 18:46:06 +00:00
|
|
|
int c = luaZ_lookahead(p->z);
|
|
|
|
luaC_checkGC(L);
|
2019-07-12 10:23:52 +00:00
|
|
|
set_block_gc(L); /* stop collector during parsing */
|
2014-04-02 18:46:06 +00:00
|
|
|
tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
|
|
|
|
&p->buff, p->name);
|
|
|
|
cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
|
|
|
|
cl->l.p = tf;
|
|
|
|
for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
|
|
|
|
cl->l.upvals[i] = luaF_newupval(L);
|
|
|
|
setclvalue(L, L->top, cl);
|
|
|
|
incr_top(L);
|
2019-07-12 10:23:52 +00:00
|
|
|
unset_block_gc(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
|
2009-05-21 19:01:41 +00:00
|
|
|
struct SParser p;
|
|
|
|
int status;
|
2014-04-02 18:46:06 +00:00
|
|
|
p.z = z; p.name = name;
|
2009-05-21 19:01:41 +00:00
|
|
|
luaZ_initbuffer(L, &p.buff);
|
|
|
|
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
|
|
|
|
luaZ_freebuffer(L, &p.buff);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|