lfunc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. ** $Id: lfunc.c $
  3. ** Auxiliary functions to manipulate prototypes and closures
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lfunc_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lstate.h"
  18. CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
  19. GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
  20. CClosure *c = gco2ccl(o);
  21. c->nupvalues = cast_byte(nupvals);
  22. return c;
  23. }
  24. LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
  25. GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
  26. LClosure *c = gco2lcl(o);
  27. c->p = NULL;
  28. c->nupvalues = cast_byte(nupvals);
  29. while (nupvals--) c->upvals[nupvals] = NULL;
  30. return c;
  31. }
  32. /*
  33. ** fill a closure with new closed upvalues
  34. */
  35. void luaF_initupvals (lua_State *L, LClosure *cl) {
  36. int i;
  37. for (i = 0; i < cl->nupvalues; i++) {
  38. GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
  39. UpVal *uv = gco2upv(o);
  40. uv->v.p = &uv->u.value; /* make it closed */
  41. setnilvalue(uv->v.p);
  42. cl->upvals[i] = uv;
  43. luaC_objbarrier(L, cl, uv);
  44. }
  45. }
  46. /*
  47. ** Create a new upvalue at the given level, and link it to the list of
  48. ** open upvalues of 'L' after entry 'prev'.
  49. **/
  50. static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
  51. GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
  52. UpVal *uv = gco2upv(o);
  53. UpVal *next = *prev;
  54. uv->v.p = s2v(level); /* current value lives in the stack */
  55. uv->u.open.next = next; /* link it to list of open upvalues */
  56. uv->u.open.previous = prev;
  57. if (next)
  58. next->u.open.previous = &uv->u.open.next;
  59. *prev = uv;
  60. if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
  61. L->twups = G(L)->twups; /* link it to the list */
  62. G(L)->twups = L;
  63. }
  64. return uv;
  65. }
  66. /*
  67. ** Find and reuse, or create if it does not exist, an upvalue
  68. ** at the given level.
  69. */
  70. UpVal *luaF_findupval (lua_State *L, StkId level) {
  71. UpVal **pp = &L->openupval;
  72. UpVal *p;
  73. lua_assert(isintwups(L) || L->openupval == NULL);
  74. while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
  75. lua_assert(!isdead(G(L), p));
  76. if (uplevel(p) == level) /* corresponding upvalue? */
  77. return p; /* return it */
  78. pp = &p->u.open.next;
  79. }
  80. /* not found: create a new upvalue after 'pp' */
  81. return newupval(L, level, pp);
  82. }
  83. /*
  84. ** Call closing method for object 'obj' with error message 'err'. The
  85. ** boolean 'yy' controls whether the call is yieldable.
  86. ** (This function assumes EXTRA_STACK.)
  87. */
  88. static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
  89. StkId top = L->top.p;
  90. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  91. setobj2s(L, top, tm); /* will call metamethod... */
  92. setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
  93. setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
  94. L->top.p = top + 3; /* add function and arguments */
  95. if (yy)
  96. luaD_call(L, top, 0);
  97. else
  98. luaD_callnoyield(L, top, 0);
  99. }
  100. /*
  101. ** Check whether object at given level has a close metamethod and raise
  102. ** an error if not.
  103. */
  104. static void checkclosemth (lua_State *L, StkId level) {
  105. const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
  106. if (ttisnil(tm)) { /* no metamethod? */
  107. int idx = cast_int(level - L->ci->func.p); /* variable index */
  108. const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
  109. if (vname == NULL) vname = "?";
  110. luaG_runerror(L, "variable '%s' got a non-closable value", vname);
  111. }
  112. }
  113. /*
  114. ** Prepare and call a closing method.
  115. ** If status is CLOSEKTOP, the call to the closing method will be pushed
  116. ** at the top of the stack. Otherwise, values can be pushed right after
  117. ** the 'level' of the upvalue being closed, as everything after that
  118. ** won't be used again.
  119. */
  120. static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) {
  121. TValue *uv = s2v(level); /* value being closed */
  122. TValue *errobj;
  123. if (status == CLOSEKTOP)
  124. errobj = &G(L)->nilvalue; /* error object is nil */
  125. else { /* 'luaD_seterrorobj' will set top to level + 2 */
  126. errobj = s2v(level + 1); /* error object goes after 'uv' */
  127. luaD_seterrorobj(L, status, level + 1); /* set error object */
  128. }
  129. callclosemethod(L, uv, errobj, yy);
  130. }
  131. /*
  132. ** Maximum value for deltas in 'tbclist', dependent on the type
  133. ** of delta. (This macro assumes that an 'L' is in scope where it
  134. ** is used.)
  135. */
  136. #define MAXDELTA \
  137. ((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
  138. /*
  139. ** Insert a variable in the list of to-be-closed variables.
  140. */
  141. void luaF_newtbcupval (lua_State *L, StkId level) {
  142. lua_assert(level > L->tbclist.p);
  143. if (l_isfalse(s2v(level)))
  144. return; /* false doesn't need to be closed */
  145. checkclosemth(L, level); /* value must have a close method */
  146. while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
  147. L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
  148. L->tbclist.p->tbclist.delta = 0;
  149. }
  150. level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
  151. L->tbclist.p = level;
  152. }
  153. void luaF_unlinkupval (UpVal *uv) {
  154. lua_assert(upisopen(uv));
  155. *uv->u.open.previous = uv->u.open.next;
  156. if (uv->u.open.next)
  157. uv->u.open.next->u.open.previous = uv->u.open.previous;
  158. }
  159. /*
  160. ** Close all upvalues up to the given stack level.
  161. */
  162. void luaF_closeupval (lua_State *L, StkId level) {
  163. UpVal *uv;
  164. StkId upl; /* stack index pointed by 'uv' */
  165. while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
  166. TValue *slot = &uv->u.value; /* new position for value */
  167. lua_assert(uplevel(uv) < L->top.p);
  168. luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
  169. setobj(L, slot, uv->v.p); /* move value to upvalue slot */
  170. uv->v.p = slot; /* now current value lives here */
  171. if (!iswhite(uv)) { /* neither white nor dead? */
  172. nw2black(uv); /* closed upvalues cannot be gray */
  173. luaC_barrier(L, uv, slot);
  174. }
  175. }
  176. }
  177. /*
  178. ** Remove first element from the tbclist plus its dummy nodes.
  179. */
  180. static void poptbclist (lua_State *L) {
  181. StkId tbc = L->tbclist.p;
  182. lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
  183. tbc -= tbc->tbclist.delta;
  184. while (tbc > L->stack.p && tbc->tbclist.delta == 0)
  185. tbc -= MAXDELTA; /* remove dummy nodes */
  186. L->tbclist.p = tbc;
  187. }
  188. /*
  189. ** Close all upvalues and to-be-closed variables up to the given stack
  190. ** level. Return restored 'level'.
  191. */
  192. StkId luaF_close (lua_State *L, StkId level, int status, int yy) {
  193. ptrdiff_t levelrel = savestack(L, level);
  194. luaF_closeupval(L, level); /* first, close the upvalues */
  195. while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
  196. StkId tbc = L->tbclist.p; /* get variable index */
  197. poptbclist(L); /* remove it from list */
  198. prepcallclosemth(L, tbc, status, yy); /* close variable */
  199. level = restorestack(L, levelrel);
  200. }
  201. return level;
  202. }
  203. Proto *luaF_newproto (lua_State *L) {
  204. GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
  205. Proto *f = gco2p(o);
  206. f->k = NULL;
  207. f->sizek = 0;
  208. f->p = NULL;
  209. f->sizep = 0;
  210. f->code = NULL;
  211. f->sizecode = 0;
  212. f->lineinfo = NULL;
  213. f->sizelineinfo = 0;
  214. f->abslineinfo = NULL;
  215. f->sizeabslineinfo = 0;
  216. f->upvalues = NULL;
  217. f->sizeupvalues = 0;
  218. f->numparams = 0;
  219. f->is_vararg = 0;
  220. f->maxstacksize = 0;
  221. f->locvars = NULL;
  222. f->sizelocvars = 0;
  223. f->linedefined = 0;
  224. f->lastlinedefined = 0;
  225. f->source = NULL;
  226. return f;
  227. }
  228. void luaF_freeproto (lua_State *L, Proto *f) {
  229. luaM_freearray(L, f->code, f->sizecode);
  230. luaM_freearray(L, f->p, f->sizep);
  231. luaM_freearray(L, f->k, f->sizek);
  232. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  233. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  234. luaM_freearray(L, f->locvars, f->sizelocvars);
  235. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  236. luaM_free(L, f);
  237. }
  238. /*
  239. ** Look for n-th local variable at line 'line' in function 'func'.
  240. ** Returns NULL if not found.
  241. */
  242. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  243. int i;
  244. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  245. if (pc < f->locvars[i].endpc) { /* is variable active? */
  246. local_number--;
  247. if (local_number == 0)
  248. return getstr(f->locvars[i].varname);
  249. }
  250. }
  251. return NULL; /* not found */
  252. }