2
0

lfunc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 object '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. StkId func = top;
  91. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  92. setobj2s(L, top++, tm); /* will call metamethod... */
  93. setobj2s(L, top++, obj); /* with 'self' as the 1st argument */
  94. if (err != NULL) /* if there was an error... */
  95. setobj2s(L, top++, err); /* then error object will be 2nd argument */
  96. L->top.p = top; /* add function and arguments */
  97. if (yy)
  98. luaD_call(L, func, 0);
  99. else
  100. luaD_callnoyield(L, func, 0);
  101. }
  102. /*
  103. ** Check whether object at given level has a close metamethod and raise
  104. ** an error if not.
  105. */
  106. static void checkclosemth (lua_State *L, StkId level) {
  107. const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
  108. if (ttisnil(tm)) { /* no metamethod? */
  109. int idx = cast_int(level - L->ci->func.p); /* variable index */
  110. const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
  111. if (vname == NULL) vname = "?";
  112. luaG_runerror(L, "variable '%s' got a non-closable value", vname);
  113. }
  114. }
  115. /*
  116. ** Prepare and call a closing method.
  117. ** If status is CLOSEKTOP, the call to the closing method will be pushed
  118. ** at the top of the stack. Otherwise, values can be pushed right after
  119. ** the 'level' of the upvalue being closed, as everything after that
  120. ** won't be used again.
  121. */
  122. static void prepcallclosemth (lua_State *L, StkId level, TStatus status,
  123. int yy) {
  124. TValue *uv = s2v(level); /* value being closed */
  125. TValue *errobj;
  126. switch (status) {
  127. case LUA_OK:
  128. L->top.p = level + 1; /* call will be at this level */
  129. /* FALLTHROUGH */
  130. case CLOSEKTOP: /* don't need to change top */
  131. errobj = NULL; /* no error object */
  132. break;
  133. default: /* 'luaD_seterrorobj' will set top to level + 2 */
  134. errobj = s2v(level + 1); /* error object goes after 'uv' */
  135. luaD_seterrorobj(L, status, level + 1); /* set error object */
  136. break;
  137. }
  138. callclosemethod(L, uv, errobj, yy);
  139. }
  140. /* Maximum value for deltas in 'tbclist' */
  141. #define MAXDELTA USHRT_MAX
  142. /*
  143. ** Insert a variable in the list of to-be-closed variables.
  144. */
  145. void luaF_newtbcupval (lua_State *L, StkId level) {
  146. lua_assert(level > L->tbclist.p);
  147. if (l_isfalse(s2v(level)))
  148. return; /* false doesn't need to be closed */
  149. checkclosemth(L, level); /* value must have a close method */
  150. while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
  151. L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
  152. L->tbclist.p->tbclist.delta = 0;
  153. }
  154. level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
  155. L->tbclist.p = level;
  156. }
  157. void luaF_unlinkupval (UpVal *uv) {
  158. lua_assert(upisopen(uv));
  159. *uv->u.open.previous = uv->u.open.next;
  160. if (uv->u.open.next)
  161. uv->u.open.next->u.open.previous = uv->u.open.previous;
  162. }
  163. /*
  164. ** Close all upvalues up to the given stack level.
  165. */
  166. void luaF_closeupval (lua_State *L, StkId level) {
  167. UpVal *uv;
  168. StkId upl; /* stack index pointed by 'uv' */
  169. while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
  170. TValue *slot = &uv->u.value; /* new position for value */
  171. lua_assert(uplevel(uv) < L->top.p);
  172. luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
  173. setobj(L, slot, uv->v.p); /* move value to upvalue slot */
  174. uv->v.p = slot; /* now current value lives here */
  175. if (!iswhite(uv)) { /* neither white nor dead? */
  176. nw2black(uv); /* closed upvalues cannot be gray */
  177. luaC_barrier(L, uv, slot);
  178. }
  179. }
  180. }
  181. /*
  182. ** Remove first element from the tbclist plus its dummy nodes.
  183. */
  184. static void poptbclist (lua_State *L) {
  185. StkId tbc = L->tbclist.p;
  186. lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
  187. tbc -= tbc->tbclist.delta;
  188. while (tbc > L->stack.p && tbc->tbclist.delta == 0)
  189. tbc -= MAXDELTA; /* remove dummy nodes */
  190. L->tbclist.p = tbc;
  191. }
  192. /*
  193. ** Close all upvalues and to-be-closed variables up to the given stack
  194. ** level. Return restored 'level'.
  195. */
  196. StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
  197. ptrdiff_t levelrel = savestack(L, level);
  198. luaF_closeupval(L, level); /* first, close the upvalues */
  199. while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
  200. StkId tbc = L->tbclist.p; /* get variable index */
  201. poptbclist(L); /* remove it from list */
  202. prepcallclosemth(L, tbc, status, yy); /* close variable */
  203. level = restorestack(L, levelrel);
  204. }
  205. return level;
  206. }
  207. Proto *luaF_newproto (lua_State *L) {
  208. GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
  209. Proto *f = gco2p(o);
  210. f->k = NULL;
  211. f->sizek = 0;
  212. f->p = NULL;
  213. f->sizep = 0;
  214. f->code = NULL;
  215. f->sizecode = 0;
  216. f->lineinfo = NULL;
  217. f->sizelineinfo = 0;
  218. f->abslineinfo = NULL;
  219. f->sizeabslineinfo = 0;
  220. f->upvalues = NULL;
  221. f->sizeupvalues = 0;
  222. f->numparams = 0;
  223. f->flag = 0;
  224. f->maxstacksize = 0;
  225. f->locvars = NULL;
  226. f->sizelocvars = 0;
  227. f->linedefined = 0;
  228. f->lastlinedefined = 0;
  229. f->source = NULL;
  230. return f;
  231. }
  232. lu_mem luaF_protosize (Proto *p) {
  233. lu_mem sz = cast(lu_mem, sizeof(Proto))
  234. + cast_uint(p->sizep) * sizeof(Proto*)
  235. + cast_uint(p->sizek) * sizeof(TValue)
  236. + cast_uint(p->sizelocvars) * sizeof(LocVar)
  237. + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
  238. if (!(p->flag & PF_FIXED)) {
  239. sz += cast_uint(p->sizecode) * sizeof(Instruction);
  240. sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
  241. sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
  242. }
  243. return sz;
  244. }
  245. void luaF_freeproto (lua_State *L, Proto *f) {
  246. if (!(f->flag & PF_FIXED)) {
  247. luaM_freearray(L, f->code, cast_sizet(f->sizecode));
  248. luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
  249. luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
  250. }
  251. luaM_freearray(L, f->p, cast_sizet(f->sizep));
  252. luaM_freearray(L, f->k, cast_sizet(f->sizek));
  253. luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
  254. luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
  255. luaM_free(L, f);
  256. }
  257. /*
  258. ** Look for n-th local variable at line 'line' in function 'func'.
  259. ** Returns NULL if not found.
  260. */
  261. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  262. int i;
  263. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  264. if (pc < f->locvars[i].endpc) { /* is variable active? */
  265. local_number--;
  266. if (local_number == 0)
  267. return getstr(f->locvars[i].varname);
  268. }
  269. }
  270. return NULL; /* not found */
  271. }