lfunc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = &uv->u.value; /* make it closed */
  41. setnilvalue(uv->v);
  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, int tbc, 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 = s2v(level); /* current value lives in the stack */
  55. uv->tbc = tbc;
  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, 0, 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;
  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 = 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); /* 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. ** Insert a variable in the list of to-be-closed variables.
  134. */
  135. void luaF_newtbcupval (lua_State *L, StkId level) {
  136. lua_assert(level > L->tbclist);
  137. if (l_isfalse(s2v(level)))
  138. return; /* false doesn't need to be closed */
  139. checkclosemth(L, level); /* value must have a close method */
  140. while (level - L->tbclist > USHRT_MAX) { /* is delta too large? */
  141. L->tbclist += USHRT_MAX; /* create a dummy node at maximum delta */
  142. L->tbclist->tbclist.delta = USHRT_MAX;
  143. L->tbclist->tbclist.isdummy = 1;
  144. }
  145. level->tbclist.delta = level - L->tbclist;
  146. level->tbclist.isdummy = 0;
  147. L->tbclist = level;
  148. }
  149. void luaF_unlinkupval (UpVal *uv) {
  150. lua_assert(upisopen(uv));
  151. *uv->u.open.previous = uv->u.open.next;
  152. if (uv->u.open.next)
  153. uv->u.open.next->u.open.previous = uv->u.open.previous;
  154. }
  155. /*
  156. ** Close all upvalues up to the given stack level.
  157. */
  158. void luaF_closeupval (lua_State *L, StkId level) {
  159. UpVal *uv;
  160. StkId upl; /* stack index pointed by 'uv' */
  161. while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) {
  162. TValue *slot = &uv->u.value; /* new position for value */
  163. lua_assert(uplevel(uv) < L->top);
  164. luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
  165. setobj(L, slot, uv->v); /* move value to upvalue slot */
  166. uv->v = slot; /* now current value lives here */
  167. if (!iswhite(uv)) { /* neither white nor dead? */
  168. nw2black(uv); /* closed upvalues cannot be gray */
  169. luaC_barrier(L, uv, slot);
  170. }
  171. }
  172. }
  173. /*
  174. ** Close all upvalues and to-be-closed variables up to the given stack
  175. ** level.
  176. */
  177. void luaF_close (lua_State *L, StkId level, int status, int yy) {
  178. ptrdiff_t levelrel = savestack(L, level);
  179. luaF_closeupval(L, level); /* first, close the upvalues */
  180. while (L->tbclist >= level) { /* traverse tbc's down to that level */
  181. StkId tbc = L->tbclist; /* get variable index */
  182. L->tbclist -= tbc->tbclist.delta; /* remove it from list */
  183. if (!tbc->tbclist.isdummy) { /* not a dummy entry? */
  184. prepcallclosemth(L, tbc, status, yy); /* close variable */
  185. level = restorestack(L, levelrel);
  186. }
  187. }
  188. }
  189. Proto *luaF_newproto (lua_State *L) {
  190. GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
  191. Proto *f = gco2p(o);
  192. f->k = NULL;
  193. f->sizek = 0;
  194. f->p = NULL;
  195. f->sizep = 0;
  196. f->code = NULL;
  197. f->sizecode = 0;
  198. f->lineinfo = NULL;
  199. f->sizelineinfo = 0;
  200. f->abslineinfo = NULL;
  201. f->sizeabslineinfo = 0;
  202. f->upvalues = NULL;
  203. f->sizeupvalues = 0;
  204. f->numparams = 0;
  205. f->is_vararg = 0;
  206. f->maxstacksize = 0;
  207. f->locvars = NULL;
  208. f->sizelocvars = 0;
  209. f->linedefined = 0;
  210. f->lastlinedefined = 0;
  211. f->source = NULL;
  212. return f;
  213. }
  214. void luaF_freeproto (lua_State *L, Proto *f) {
  215. luaM_freearray(L, f->code, f->sizecode);
  216. luaM_freearray(L, f->p, f->sizep);
  217. luaM_freearray(L, f->k, f->sizek);
  218. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  219. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  220. luaM_freearray(L, f->locvars, f->sizelocvars);
  221. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  222. luaM_free(L, f);
  223. }
  224. /*
  225. ** Look for n-th local variable at line 'line' in function 'func'.
  226. ** Returns NULL if not found.
  227. */
  228. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  229. int i;
  230. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  231. if (pc < f->locvars[i].endpc) { /* is variable active? */
  232. local_number--;
  233. if (local_number == 0)
  234. return getstr(f->locvars[i].varname);
  235. }
  236. }
  237. return NULL; /* not found */
  238. }