lfunc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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, TStatus status,
  121. int yy) {
  122. TValue *uv = s2v(level); /* value being closed */
  123. TValue *errobj;
  124. if (status == CLOSEKTOP)
  125. errobj = &G(L)->nilvalue; /* error object is nil */
  126. else { /* 'luaD_seterrorobj' will set top to level + 2 */
  127. errobj = s2v(level + 1); /* error object goes after 'uv' */
  128. luaD_seterrorobj(L, status, level + 1); /* set error object */
  129. }
  130. callclosemethod(L, uv, errobj, yy);
  131. }
  132. /*
  133. ** Maximum value for deltas in 'tbclist', dependent on the type
  134. ** of delta. (This macro assumes that an 'L' is in scope where it
  135. ** is used.)
  136. */
  137. #define MAXDELTA \
  138. ((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1)
  139. /*
  140. ** Insert a variable in the list of to-be-closed variables.
  141. */
  142. void luaF_newtbcupval (lua_State *L, StkId level) {
  143. lua_assert(level > L->tbclist.p);
  144. if (l_isfalse(s2v(level)))
  145. return; /* false doesn't need to be closed */
  146. checkclosemth(L, level); /* value must have a close method */
  147. while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
  148. L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
  149. L->tbclist.p->tbclist.delta = 0;
  150. }
  151. level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
  152. L->tbclist.p = level;
  153. }
  154. void luaF_unlinkupval (UpVal *uv) {
  155. lua_assert(upisopen(uv));
  156. *uv->u.open.previous = uv->u.open.next;
  157. if (uv->u.open.next)
  158. uv->u.open.next->u.open.previous = uv->u.open.previous;
  159. }
  160. /*
  161. ** Close all upvalues up to the given stack level.
  162. */
  163. void luaF_closeupval (lua_State *L, StkId level) {
  164. UpVal *uv;
  165. StkId upl; /* stack index pointed by 'uv' */
  166. while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
  167. TValue *slot = &uv->u.value; /* new position for value */
  168. lua_assert(uplevel(uv) < L->top.p);
  169. luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
  170. setobj(L, slot, uv->v.p); /* move value to upvalue slot */
  171. uv->v.p = slot; /* now current value lives here */
  172. if (!iswhite(uv)) { /* neither white nor dead? */
  173. nw2black(uv); /* closed upvalues cannot be gray */
  174. luaC_barrier(L, uv, slot);
  175. }
  176. }
  177. }
  178. /*
  179. ** Remove first element from the tbclist plus its dummy nodes.
  180. */
  181. static void poptbclist (lua_State *L) {
  182. StkId tbc = L->tbclist.p;
  183. lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
  184. tbc -= tbc->tbclist.delta;
  185. while (tbc > L->stack.p && tbc->tbclist.delta == 0)
  186. tbc -= MAXDELTA; /* remove dummy nodes */
  187. L->tbclist.p = tbc;
  188. }
  189. /*
  190. ** Close all upvalues and to-be-closed variables up to the given stack
  191. ** level. Return restored 'level'.
  192. */
  193. StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
  194. ptrdiff_t levelrel = savestack(L, level);
  195. luaF_closeupval(L, level); /* first, close the upvalues */
  196. while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
  197. StkId tbc = L->tbclist.p; /* get variable index */
  198. poptbclist(L); /* remove it from list */
  199. prepcallclosemth(L, tbc, status, yy); /* close variable */
  200. level = restorestack(L, levelrel);
  201. }
  202. return level;
  203. }
  204. Proto *luaF_newproto (lua_State *L) {
  205. GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
  206. Proto *f = gco2p(o);
  207. f->k = NULL;
  208. f->sizek = 0;
  209. f->p = NULL;
  210. f->sizep = 0;
  211. f->code = NULL;
  212. f->sizecode = 0;
  213. f->lineinfo = NULL;
  214. f->sizelineinfo = 0;
  215. f->abslineinfo = NULL;
  216. f->sizeabslineinfo = 0;
  217. f->upvalues = NULL;
  218. f->sizeupvalues = 0;
  219. f->numparams = 0;
  220. f->flag = 0;
  221. f->maxstacksize = 0;
  222. f->locvars = NULL;
  223. f->sizelocvars = 0;
  224. f->linedefined = 0;
  225. f->lastlinedefined = 0;
  226. f->source = NULL;
  227. return f;
  228. }
  229. lu_mem luaF_protosize (Proto *p) {
  230. lu_mem sz = cast(lu_mem, sizeof(Proto))
  231. + cast_uint(p->sizep) * sizeof(Proto*)
  232. + cast_uint(p->sizek) * sizeof(TValue)
  233. + cast_uint(p->sizelocvars) * sizeof(LocVar)
  234. + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
  235. if (!(p->flag & PF_FIXED)) {
  236. sz += cast_uint(p->sizecode) * sizeof(Instruction);
  237. sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
  238. sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
  239. }
  240. return sz;
  241. }
  242. void luaF_freeproto (lua_State *L, Proto *f) {
  243. if (!(f->flag & PF_FIXED)) {
  244. luaM_freearray(L, f->code, cast_sizet(f->sizecode));
  245. luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
  246. luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
  247. }
  248. luaM_freearray(L, f->p, cast_sizet(f->sizep));
  249. luaM_freearray(L, f->k, cast_sizet(f->sizek));
  250. luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
  251. luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
  252. luaM_free(L, f);
  253. }
  254. /*
  255. ** Look for n-th local variable at line 'line' in function 'func'.
  256. ** Returns NULL if not found.
  257. */
  258. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  259. int i;
  260. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  261. if (pc < f->locvars[i].endpc) { /* is variable active? */
  262. local_number--;
  263. if (local_number == 0)
  264. return getstr(f->locvars[i].varname);
  265. }
  266. }
  267. return NULL; /* not found */
  268. }