lfunc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "lstring.h"
  19. CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
  20. GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
  21. CClosure *c = gco2ccl(o);
  22. c->nupvalues = cast_byte(nupvals);
  23. return c;
  24. }
  25. LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
  26. GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
  27. LClosure *c = gco2lcl(o);
  28. c->p = NULL;
  29. c->nupvalues = cast_byte(nupvals);
  30. while (nupvals--) c->upvals[nupvals] = NULL;
  31. return c;
  32. }
  33. /*
  34. ** fill a closure with new closed upvalues
  35. */
  36. void luaF_initupvals (lua_State *L, LClosure *cl) {
  37. int i;
  38. for (i = 0; i < cl->nupvalues; i++) {
  39. GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
  40. UpVal *uv = gco2upv(o);
  41. uv->v.p = &uv->u.value; /* make it closed */
  42. setnilvalue(uv->v.p);
  43. cl->upvals[i] = uv;
  44. luaC_objbarrier(L, cl, uv);
  45. }
  46. }
  47. /*
  48. ** Create a new upvalue at the given level, and link it to the list of
  49. ** open upvalues of 'L' after entry 'prev'.
  50. **/
  51. static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
  52. GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
  53. UpVal *uv = gco2upv(o);
  54. UpVal *next = *prev;
  55. uv->v.p = s2v(level); /* current value lives in the stack */
  56. uv->u.open.next = next; /* link it to list of open upvalues */
  57. uv->u.open.previous = prev;
  58. if (next)
  59. next->u.open.previous = &uv->u.open.next;
  60. *prev = uv;
  61. if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
  62. L->twups = G(L)->twups; /* link it to the list */
  63. G(L)->twups = L;
  64. }
  65. return uv;
  66. }
  67. /*
  68. ** Find and reuse, or create if it does not exist, an upvalue
  69. ** at the given level.
  70. */
  71. UpVal *luaF_findupval (lua_State *L, StkId level) {
  72. UpVal **pp = &L->openupval;
  73. UpVal *p;
  74. lua_assert(isintwups(L) || L->openupval == NULL);
  75. while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
  76. lua_assert(!isdead(G(L), p));
  77. if (uplevel(p) == level) /* corresponding upvalue? */
  78. return p; /* return it */
  79. pp = &p->u.open.next;
  80. }
  81. /* not found: create a new upvalue after 'pp' */
  82. return newupval(L, level, pp);
  83. }
  84. /*
  85. ** Call closing method for object 'obj' with error message 'err'. The
  86. ** boolean 'yy' controls whether the call is yieldable.
  87. ** (This function assumes EXTRA_STACK.)
  88. */
  89. static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
  90. StkId top = L->top.p;
  91. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  92. setobj2s(L, top, tm); /* will call metamethod... */
  93. setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
  94. setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
  95. L->top.p = top + 3; /* add function and arguments */
  96. if (yy)
  97. luaD_call(L, top, 0);
  98. else
  99. luaD_callnoyield(L, top, 0);
  100. }
  101. /*
  102. ** Check whether object at given level has a close metamethod and raise
  103. ** an error if not.
  104. */
  105. static void checkclosemth (lua_State *L, StkId level) {
  106. const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
  107. if (ttisnil(tm)) { /* no metamethod? */
  108. int idx = cast_int(level - L->ci->func.p); /* variable index */
  109. const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
  110. if (vname == NULL) vname = "?";
  111. luaG_runerror(L, "variable '%s' got a non-closable value", vname);
  112. }
  113. }
  114. /*
  115. ** Prepare and call a closing method.
  116. ** If status is CLOSEKTOP, the call to the closing method will be pushed
  117. ** at the top of the stack. Otherwise, values can be pushed right after
  118. ** the 'level' of the upvalue being closed, as everything after that
  119. ** won't be used again.
  120. */
  121. static void prepcallclosemth (lua_State *L, StkId level, int status, 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, int 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->is_vararg = 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. void luaF_freeproto (lua_State *L, Proto *f) {
  230. luaM_freearray(L, f->code, f->sizecode);
  231. luaM_freearray(L, f->p, f->sizep);
  232. luaM_freearray(L, f->k, f->sizek);
  233. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  234. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  235. luaM_freearray(L, f->locvars, f->sizelocvars);
  236. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  237. luaM_free(L, f);
  238. }
  239. /*
  240. ** Look for n-th local variable at line 'line' in function 'func'.
  241. ** Returns NULL if not found.
  242. */
  243. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  244. int i;
  245. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  246. if (pc < f->locvars[i].endpc) { /* is variable active? */
  247. local_number--;
  248. if (local_number == 0)
  249. return getstr(f->locvars[i].varname);
  250. }
  251. }
  252. return NULL; /* not found */
  253. }
  254. void luaF_shareproto (Proto *f) {
  255. int i;
  256. if (f == NULL || isshared(f))
  257. return;
  258. makeshared(f);
  259. luaS_share(f->source);
  260. for (i = 0; i < f->sizek; i++) {
  261. if (ttype(&f->k[i]) == LUA_TSTRING)
  262. luaS_share(tsvalue(&f->k[i]));
  263. }
  264. for (i = 0; i < f->sizeupvalues; i++)
  265. luaS_share(f->upvalues[i].name);
  266. for (i = 0; i < f->sizelocvars; i++)
  267. luaS_share(f->locvars[i].varname);
  268. for (i = 0; i < f->sizep; i++)
  269. luaF_shareproto(f->p[i]);
  270. }