xref: /freebsd-src/contrib/lua/src/lfunc.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lfunc.c $
38e3e3a7aSWarner Losh ** Auxiliary functions to manipulate prototypes and closures
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lfunc_c
88e3e3a7aSWarner Losh #define LUA_CORE
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh 
138e3e3a7aSWarner Losh #include <stddef.h>
148e3e3a7aSWarner Losh 
158e3e3a7aSWarner Losh #include "lua.h"
168e3e3a7aSWarner Losh 
170495ed39SKyle Evans #include "ldebug.h"
180495ed39SKyle Evans #include "ldo.h"
198e3e3a7aSWarner Losh #include "lfunc.h"
208e3e3a7aSWarner Losh #include "lgc.h"
218e3e3a7aSWarner Losh #include "lmem.h"
228e3e3a7aSWarner Losh #include "lobject.h"
238e3e3a7aSWarner Losh #include "lstate.h"
248e3e3a7aSWarner Losh 
258e3e3a7aSWarner Losh 
268e3e3a7aSWarner Losh 
luaF_newCclosure(lua_State * L,int nupvals)270495ed39SKyle Evans CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
280495ed39SKyle Evans   GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
298e3e3a7aSWarner Losh   CClosure *c = gco2ccl(o);
300495ed39SKyle Evans   c->nupvalues = cast_byte(nupvals);
318e3e3a7aSWarner Losh   return c;
328e3e3a7aSWarner Losh }
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh 
luaF_newLclosure(lua_State * L,int nupvals)350495ed39SKyle Evans LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
360495ed39SKyle Evans   GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
378e3e3a7aSWarner Losh   LClosure *c = gco2lcl(o);
388e3e3a7aSWarner Losh   c->p = NULL;
390495ed39SKyle Evans   c->nupvalues = cast_byte(nupvals);
400495ed39SKyle Evans   while (nupvals--) c->upvals[nupvals] = NULL;
418e3e3a7aSWarner Losh   return c;
428e3e3a7aSWarner Losh }
438e3e3a7aSWarner Losh 
440495ed39SKyle Evans 
458e3e3a7aSWarner Losh /*
468e3e3a7aSWarner Losh ** fill a closure with new closed upvalues
478e3e3a7aSWarner Losh */
luaF_initupvals(lua_State * L,LClosure * cl)488e3e3a7aSWarner Losh void luaF_initupvals (lua_State *L, LClosure *cl) {
498e3e3a7aSWarner Losh   int i;
508e3e3a7aSWarner Losh   for (i = 0; i < cl->nupvalues; i++) {
510495ed39SKyle Evans     GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
520495ed39SKyle Evans     UpVal *uv = gco2upv(o);
53*a9490b81SWarner Losh     uv->v.p = &uv->u.value;  /* make it closed */
54*a9490b81SWarner Losh     setnilvalue(uv->v.p);
558e3e3a7aSWarner Losh     cl->upvals[i] = uv;
560495ed39SKyle Evans     luaC_objbarrier(L, cl, uv);
578e3e3a7aSWarner Losh   }
588e3e3a7aSWarner Losh }
598e3e3a7aSWarner Losh 
608e3e3a7aSWarner Losh 
610495ed39SKyle Evans /*
620495ed39SKyle Evans ** Create a new upvalue at the given level, and link it to the list of
630495ed39SKyle Evans ** open upvalues of 'L' after entry 'prev'.
640495ed39SKyle Evans **/
newupval(lua_State * L,StkId level,UpVal ** prev)65*a9490b81SWarner Losh static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
660495ed39SKyle Evans   GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
670495ed39SKyle Evans   UpVal *uv = gco2upv(o);
680495ed39SKyle Evans   UpVal *next = *prev;
69*a9490b81SWarner Losh   uv->v.p = s2v(level);  /* current value lives in the stack */
700495ed39SKyle Evans   uv->u.open.next = next;  /* link it to list of open upvalues */
710495ed39SKyle Evans   uv->u.open.previous = prev;
720495ed39SKyle Evans   if (next)
730495ed39SKyle Evans     next->u.open.previous = &uv->u.open.next;
740495ed39SKyle Evans   *prev = uv;
758e3e3a7aSWarner Losh   if (!isintwups(L)) {  /* thread not in list of threads with upvalues? */
768e3e3a7aSWarner Losh     L->twups = G(L)->twups;  /* link it to the list */
778e3e3a7aSWarner Losh     G(L)->twups = L;
788e3e3a7aSWarner Losh   }
798e3e3a7aSWarner Losh   return uv;
808e3e3a7aSWarner Losh }
818e3e3a7aSWarner Losh 
828e3e3a7aSWarner Losh 
830495ed39SKyle Evans /*
840495ed39SKyle Evans ** Find and reuse, or create if it does not exist, an upvalue
850495ed39SKyle Evans ** at the given level.
860495ed39SKyle Evans */
luaF_findupval(lua_State * L,StkId level)870495ed39SKyle Evans UpVal *luaF_findupval (lua_State *L, StkId level) {
880495ed39SKyle Evans   UpVal **pp = &L->openupval;
890495ed39SKyle Evans   UpVal *p;
900495ed39SKyle Evans   lua_assert(isintwups(L) || L->openupval == NULL);
910495ed39SKyle Evans   while ((p = *pp) != NULL && uplevel(p) >= level) {  /* search for it */
920495ed39SKyle Evans     lua_assert(!isdead(G(L), p));
930495ed39SKyle Evans     if (uplevel(p) == level)  /* corresponding upvalue? */
940495ed39SKyle Evans       return p;  /* return it */
950495ed39SKyle Evans     pp = &p->u.open.next;
960495ed39SKyle Evans   }
970495ed39SKyle Evans   /* not found: create a new upvalue after 'pp' */
98*a9490b81SWarner Losh   return newupval(L, level, pp);
990495ed39SKyle Evans }
1000495ed39SKyle Evans 
1010495ed39SKyle Evans 
1020495ed39SKyle Evans /*
1038c784bb8SWarner Losh ** Call closing method for object 'obj' with error message 'err'. The
1048c784bb8SWarner Losh ** boolean 'yy' controls whether the call is yieldable.
1058c784bb8SWarner Losh ** (This function assumes EXTRA_STACK.)
1060495ed39SKyle Evans */
callclosemethod(lua_State * L,TValue * obj,TValue * err,int yy)1078c784bb8SWarner Losh static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
108*a9490b81SWarner Losh   StkId top = L->top.p;
1090495ed39SKyle Evans   const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
1100495ed39SKyle Evans   setobj2s(L, top, tm);  /* will call metamethod... */
1110495ed39SKyle Evans   setobj2s(L, top + 1, obj);  /* with 'self' as the 1st argument */
1120495ed39SKyle Evans   setobj2s(L, top + 2, err);  /* and error msg. as 2nd argument */
113*a9490b81SWarner Losh   L->top.p = top + 3;  /* add function and arguments */
1148c784bb8SWarner Losh   if (yy)
1158c784bb8SWarner Losh     luaD_call(L, top, 0);
1168c784bb8SWarner Losh   else
1178c784bb8SWarner Losh     luaD_callnoyield(L, top, 0);
1180495ed39SKyle Evans }
1190495ed39SKyle Evans 
1200495ed39SKyle Evans 
1210495ed39SKyle Evans /*
1228c784bb8SWarner Losh ** Check whether object at given level has a close metamethod and raise
1238c784bb8SWarner Losh ** an error if not.
1240495ed39SKyle Evans */
checkclosemth(lua_State * L,StkId level)1258c784bb8SWarner Losh static void checkclosemth (lua_State *L, StkId level) {
1268c784bb8SWarner Losh   const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
1278c784bb8SWarner Losh   if (ttisnil(tm)) {  /* no metamethod? */
128*a9490b81SWarner Losh     int idx = cast_int(level - L->ci->func.p);  /* variable index */
1290495ed39SKyle Evans     const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
1300495ed39SKyle Evans     if (vname == NULL) vname = "?";
1318c784bb8SWarner Losh     luaG_runerror(L, "variable '%s' got a non-closable value", vname);
1328c784bb8SWarner Losh   }
1330495ed39SKyle Evans }
1340495ed39SKyle Evans 
1350495ed39SKyle Evans 
1360495ed39SKyle Evans /*
1378c784bb8SWarner Losh ** Prepare and call a closing method.
1388c784bb8SWarner Losh ** If status is CLOSEKTOP, the call to the closing method will be pushed
1398c784bb8SWarner Losh ** at the top of the stack. Otherwise, values can be pushed right after
1408c784bb8SWarner Losh ** the 'level' of the upvalue being closed, as everything after that
1418c784bb8SWarner Losh ** won't be used again.
1420495ed39SKyle Evans */
prepcallclosemth(lua_State * L,StkId level,int status,int yy)1438c784bb8SWarner Losh static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
1440495ed39SKyle Evans   TValue *uv = s2v(level);  /* value being closed */
1458c784bb8SWarner Losh   TValue *errobj;
1468c784bb8SWarner Losh   if (status == CLOSEKTOP)
1478c784bb8SWarner Losh     errobj = &G(L)->nilvalue;  /* error object is nil */
1488c784bb8SWarner Losh   else {  /* 'luaD_seterrorobj' will set top to level + 2 */
1498c784bb8SWarner Losh     errobj = s2v(level + 1);  /* error object goes after 'uv' */
1508c784bb8SWarner Losh     luaD_seterrorobj(L, status, level + 1);  /* set error object */
1510495ed39SKyle Evans   }
1528c784bb8SWarner Losh   callclosemethod(L, uv, errobj, yy);
1530495ed39SKyle Evans }
1540495ed39SKyle Evans 
1550495ed39SKyle Evans 
1560495ed39SKyle Evans /*
1578c784bb8SWarner Losh ** Maximum value for deltas in 'tbclist', dependent on the type
1588c784bb8SWarner Losh ** of delta. (This macro assumes that an 'L' is in scope where it
1598c784bb8SWarner Losh ** is used.)
1600495ed39SKyle Evans */
1618c784bb8SWarner Losh #define MAXDELTA  \
162*a9490b81SWarner Losh 	((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
1630495ed39SKyle Evans 
1640495ed39SKyle Evans 
1650495ed39SKyle Evans /*
1668c784bb8SWarner Losh ** Insert a variable in the list of to-be-closed variables.
1670495ed39SKyle Evans */
luaF_newtbcupval(lua_State * L,StkId level)1680495ed39SKyle Evans void luaF_newtbcupval (lua_State *L, StkId level) {
169*a9490b81SWarner Losh   lua_assert(level > L->tbclist.p);
1708c784bb8SWarner Losh   if (l_isfalse(s2v(level)))
1718c784bb8SWarner Losh     return;  /* false doesn't need to be closed */
1728c784bb8SWarner Losh   checkclosemth(L, level);  /* value must have a close method */
173*a9490b81SWarner Losh   while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
174*a9490b81SWarner Losh     L->tbclist.p += MAXDELTA;  /* create a dummy node at maximum delta */
175*a9490b81SWarner Losh     L->tbclist.p->tbclist.delta = 0;
1760495ed39SKyle Evans   }
177*a9490b81SWarner Losh   level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
178*a9490b81SWarner Losh   L->tbclist.p = level;
1790495ed39SKyle Evans }
1800495ed39SKyle Evans 
1810495ed39SKyle Evans 
luaF_unlinkupval(UpVal * uv)1820495ed39SKyle Evans void luaF_unlinkupval (UpVal *uv) {
1830495ed39SKyle Evans   lua_assert(upisopen(uv));
1840495ed39SKyle Evans   *uv->u.open.previous = uv->u.open.next;
1850495ed39SKyle Evans   if (uv->u.open.next)
1860495ed39SKyle Evans     uv->u.open.next->u.open.previous = uv->u.open.previous;
1870495ed39SKyle Evans }
1880495ed39SKyle Evans 
1890495ed39SKyle Evans 
1908c784bb8SWarner Losh /*
1918c784bb8SWarner Losh ** Close all upvalues up to the given stack level.
1928c784bb8SWarner Losh */
luaF_closeupval(lua_State * L,StkId level)1938c784bb8SWarner Losh void luaF_closeupval (lua_State *L, StkId level) {
1940495ed39SKyle Evans   UpVal *uv;
1958c784bb8SWarner Losh   StkId upl;  /* stack index pointed by 'uv' */
1968c784bb8SWarner Losh   while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
1970495ed39SKyle Evans     TValue *slot = &uv->u.value;  /* new position for value */
198*a9490b81SWarner Losh     lua_assert(uplevel(uv) < L->top.p);
1998c784bb8SWarner Losh     luaF_unlinkupval(uv);  /* remove upvalue from 'openupval' list */
200*a9490b81SWarner Losh     setobj(L, slot, uv->v.p);  /* move value to upvalue slot */
201*a9490b81SWarner Losh     uv->v.p = slot;  /* now current value lives here */
2020495ed39SKyle Evans     if (!iswhite(uv)) {  /* neither white nor dead? */
2030495ed39SKyle Evans       nw2black(uv);  /* closed upvalues cannot be gray */
2040495ed39SKyle Evans       luaC_barrier(L, uv, slot);
2050495ed39SKyle Evans     }
2060495ed39SKyle Evans   }
2078c784bb8SWarner Losh }
2088c784bb8SWarner Losh 
2098c784bb8SWarner Losh 
2108c784bb8SWarner Losh /*
211*a9490b81SWarner Losh ** Remove first element from the tbclist plus its dummy nodes.
2128c784bb8SWarner Losh */
poptbclist(lua_State * L)2138c784bb8SWarner Losh static void poptbclist (lua_State *L) {
214*a9490b81SWarner Losh   StkId tbc = L->tbclist.p;
2158c784bb8SWarner Losh   lua_assert(tbc->tbclist.delta > 0);  /* first element cannot be dummy */
2168c784bb8SWarner Losh   tbc -= tbc->tbclist.delta;
217*a9490b81SWarner Losh   while (tbc > L->stack.p && tbc->tbclist.delta == 0)
2188c784bb8SWarner Losh     tbc -= MAXDELTA;  /* remove dummy nodes */
219*a9490b81SWarner Losh   L->tbclist.p = tbc;
2208c784bb8SWarner Losh }
2218c784bb8SWarner Losh 
2228c784bb8SWarner Losh 
2238c784bb8SWarner Losh /*
2248c784bb8SWarner Losh ** Close all upvalues and to-be-closed variables up to the given stack
225*a9490b81SWarner Losh ** level. Return restored 'level'.
2268c784bb8SWarner Losh */
luaF_close(lua_State * L,StkId level,int status,int yy)227*a9490b81SWarner Losh StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
2288c784bb8SWarner Losh   ptrdiff_t levelrel = savestack(L, level);
2298c784bb8SWarner Losh   luaF_closeupval(L, level);  /* first, close the upvalues */
230*a9490b81SWarner Losh   while (L->tbclist.p >= level) {  /* traverse tbc's down to that level */
231*a9490b81SWarner Losh     StkId tbc = L->tbclist.p;  /* get variable index */
2328c784bb8SWarner Losh     poptbclist(L);  /* remove it from list */
2338c784bb8SWarner Losh     prepcallclosemth(L, tbc, status, yy);  /* close variable */
2348c784bb8SWarner Losh     level = restorestack(L, levelrel);
2358c784bb8SWarner Losh   }
236*a9490b81SWarner Losh   return level;
2378e3e3a7aSWarner Losh }
2388e3e3a7aSWarner Losh 
2398e3e3a7aSWarner Losh 
luaF_newproto(lua_State * L)2408e3e3a7aSWarner Losh Proto *luaF_newproto (lua_State *L) {
2410495ed39SKyle Evans   GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
2428e3e3a7aSWarner Losh   Proto *f = gco2p(o);
2438e3e3a7aSWarner Losh   f->k = NULL;
2448e3e3a7aSWarner Losh   f->sizek = 0;
2458e3e3a7aSWarner Losh   f->p = NULL;
2468e3e3a7aSWarner Losh   f->sizep = 0;
2478e3e3a7aSWarner Losh   f->code = NULL;
2488e3e3a7aSWarner Losh   f->sizecode = 0;
2498e3e3a7aSWarner Losh   f->lineinfo = NULL;
2508e3e3a7aSWarner Losh   f->sizelineinfo = 0;
2510495ed39SKyle Evans   f->abslineinfo = NULL;
2520495ed39SKyle Evans   f->sizeabslineinfo = 0;
2538e3e3a7aSWarner Losh   f->upvalues = NULL;
2548e3e3a7aSWarner Losh   f->sizeupvalues = 0;
2558e3e3a7aSWarner Losh   f->numparams = 0;
2568e3e3a7aSWarner Losh   f->is_vararg = 0;
2578e3e3a7aSWarner Losh   f->maxstacksize = 0;
2588e3e3a7aSWarner Losh   f->locvars = NULL;
2598e3e3a7aSWarner Losh   f->sizelocvars = 0;
2608e3e3a7aSWarner Losh   f->linedefined = 0;
2618e3e3a7aSWarner Losh   f->lastlinedefined = 0;
2628e3e3a7aSWarner Losh   f->source = NULL;
2638e3e3a7aSWarner Losh   return f;
2648e3e3a7aSWarner Losh }
2658e3e3a7aSWarner Losh 
2668e3e3a7aSWarner Losh 
luaF_freeproto(lua_State * L,Proto * f)2678e3e3a7aSWarner Losh void luaF_freeproto (lua_State *L, Proto *f) {
2688e3e3a7aSWarner Losh   luaM_freearray(L, f->code, f->sizecode);
2698e3e3a7aSWarner Losh   luaM_freearray(L, f->p, f->sizep);
2708e3e3a7aSWarner Losh   luaM_freearray(L, f->k, f->sizek);
2718e3e3a7aSWarner Losh   luaM_freearray(L, f->lineinfo, f->sizelineinfo);
2720495ed39SKyle Evans   luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
2738e3e3a7aSWarner Losh   luaM_freearray(L, f->locvars, f->sizelocvars);
2748e3e3a7aSWarner Losh   luaM_freearray(L, f->upvalues, f->sizeupvalues);
2758e3e3a7aSWarner Losh   luaM_free(L, f);
2768e3e3a7aSWarner Losh }
2778e3e3a7aSWarner Losh 
2788e3e3a7aSWarner Losh 
2798e3e3a7aSWarner Losh /*
2808e3e3a7aSWarner Losh ** Look for n-th local variable at line 'line' in function 'func'.
2818e3e3a7aSWarner Losh ** Returns NULL if not found.
2828e3e3a7aSWarner Losh */
luaF_getlocalname(const Proto * f,int local_number,int pc)2838e3e3a7aSWarner Losh const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
2848e3e3a7aSWarner Losh   int i;
2858e3e3a7aSWarner Losh   for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
2868e3e3a7aSWarner Losh     if (pc < f->locvars[i].endpc) {  /* is variable active? */
2878e3e3a7aSWarner Losh       local_number--;
2888e3e3a7aSWarner Losh       if (local_number == 0)
2898e3e3a7aSWarner Losh         return getstr(f->locvars[i].varname);
2908e3e3a7aSWarner Losh     }
2918e3e3a7aSWarner Losh   }
2928e3e3a7aSWarner Losh   return NULL;  /* not found */
2938e3e3a7aSWarner Losh }
2948e3e3a7aSWarner Losh 
295