lfunc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 n) {
  19. GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
  20. CClosure *c = gco2ccl(o);
  21. c->nupvalues = cast_byte(n);
  22. return c;
  23. }
  24. LClosure *luaF_newLclosure (lua_State *L, int n) {
  25. GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
  26. LClosure *c = gco2lcl(o);
  27. c->p = NULL;
  28. c->nupvalues = cast_byte(n);
  29. while (n--) c->upvals[n] = 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_TUPVAL, 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, o);
  44. }
  45. }
  46. /*
  47. ** Create a new upvalue with the given tag at the given level,
  48. ** and link it to the list of open upvalues of 'L' after entry 'prev'.
  49. **/
  50. static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
  51. GCObject *o = luaC_newobj(L, tag, sizeof(UpVal));
  52. UpVal *uv = gco2upv(o);
  53. UpVal *next = *prev;
  54. uv->v = 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, a regular 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. if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */
  76. return p; /* return it */
  77. pp = &p->u.open.next;
  78. }
  79. /* not found: create a new upvalue after 'pp' */
  80. return newupval(L, LUA_TUPVAL, level, pp);
  81. }
  82. static void callclose (lua_State *L, void *ud) {
  83. UNUSED(ud);
  84. luaD_callnoyield(L, L->top - 2, 0);
  85. }
  86. /*
  87. ** Prepare closing method plus its argument for object 'obj' with
  88. ** error message 'err'. (This function assumes EXTRA_STACK.)
  89. */
  90. static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) {
  91. StkId top = L->top;
  92. if (ttisfunction(obj)) { /* object to-be-closed is a function? */
  93. setobj2s(L, top, obj); /* push function */
  94. setobj2s(L, top + 1, err); /* push error msg. as argument */
  95. }
  96. else { /* try '__close' metamethod */
  97. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  98. if (ttisnil(tm)) /* no metamethod? */
  99. return 0; /* nothing to call */
  100. setobj2s(L, top, tm); /* will call metamethod... */
  101. setobj2s(L, top + 1, obj); /* with 'self' as the argument */
  102. }
  103. L->top = top + 2; /* add function and argument */
  104. return 1;
  105. }
  106. /*
  107. ** Prepare and call a closing method. If status is OK, code is
  108. ** still inside the original protected call, and so any error
  109. ** will be handled there. Otherwise, a previous error already
  110. ** activated original protected call, and so the call to the
  111. ** closing method must be protected here.
  112. ** If status is OK, the call to the closing method will be pushed
  113. ** at the top of the stack. Otherwise, values are pushed after
  114. ** the 'level' of the upvalue being closed, as everything after
  115. ** that won't be used again.
  116. */
  117. static int closeupval (lua_State *L, TValue *uv, StkId level, int status) {
  118. if (likely(status == LUA_OK)) {
  119. if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
  120. callclose(L, NULL); /* call closing method */
  121. else if (!ttisnil(uv)) { /* non-closable non-nil value? */
  122. const char *vname = luaG_findlocal(L, L->ci, level - L->ci->func, NULL);
  123. if (vname == NULL) vname = "?";
  124. luaG_runerror(L, "attempt to close non-closable variable '%s'", vname);
  125. }
  126. }
  127. else { /* there was an error */
  128. /* save error message and set stack top to 'level + 1' */
  129. luaD_seterrorobj(L, status, level);
  130. if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
  131. int newstatus = luaD_pcall(L, callclose, NULL, savestack(L, level), 0);
  132. if (newstatus != LUA_OK) /* another error when closing? */
  133. status = newstatus; /* this will be the new error */
  134. }
  135. }
  136. return status;
  137. }
  138. /*
  139. ** Try to create a to-be-closed upvalue
  140. ** (can raise a memory-allocation error)
  141. */
  142. static void trynewtbcupval (lua_State *L, void *ud) {
  143. StkId level = cast(StkId, ud);
  144. lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
  145. newupval(L, LUA_TUPVALTBC, level, &L->openupval);
  146. }
  147. /*
  148. ** Create a to-be-closed upvalue. If there is a memory error
  149. ** when creating the upvalue, the closing method must be called here,
  150. ** as there is no upvalue to call it later.
  151. */
  152. void luaF_newtbcupval (lua_State *L, StkId level) {
  153. int status = luaD_rawrunprotected(L, trynewtbcupval, level);
  154. if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
  155. lua_assert(status == LUA_ERRMEM);
  156. luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
  157. if (prepclosingmethod(L, s2v(level), s2v(level + 1)))
  158. callclose(L, NULL); /* call closing method */
  159. luaD_throw(L, LUA_ERRMEM); /* throw memory error */
  160. }
  161. }
  162. void luaF_unlinkupval (UpVal *uv) {
  163. lua_assert(upisopen(uv));
  164. *uv->u.open.previous = uv->u.open.next;
  165. if (uv->u.open.next)
  166. uv->u.open.next->u.open.previous = uv->u.open.previous;
  167. }
  168. int luaF_close (lua_State *L, StkId level, int status) {
  169. UpVal *uv;
  170. while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
  171. StkId upl = uplevel(uv);
  172. TValue *slot = &uv->u.value; /* new position for value */
  173. luaF_unlinkupval(uv);
  174. setobj(L, slot, uv->v); /* move value to upvalue slot */
  175. uv->v = slot; /* now current value lives here */
  176. if (!iswhite(uv))
  177. gray2black(uv); /* closed upvalues cannot be gray */
  178. luaC_barrier(L, uv, slot);
  179. if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */
  180. ptrdiff_t levelrel = savestack(L, level);
  181. status = closeupval(L, uv->v, upl, status); /* may realloc. the stack */
  182. level = restorestack(L, levelrel);
  183. }
  184. }
  185. return status;
  186. }
  187. Proto *luaF_newproto (lua_State *L) {
  188. GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
  189. Proto *f = gco2p(o);
  190. f->k = NULL;
  191. f->sizek = 0;
  192. f->p = NULL;
  193. f->sizep = 0;
  194. f->code = NULL;
  195. f->sizecode = 0;
  196. f->lineinfo = NULL;
  197. f->sizelineinfo = 0;
  198. f->abslineinfo = NULL;
  199. f->sizeabslineinfo = 0;
  200. f->upvalues = NULL;
  201. f->sizeupvalues = 0;
  202. f->numparams = 0;
  203. f->is_vararg = 0;
  204. f->maxstacksize = 0;
  205. f->locvars = NULL;
  206. f->sizelocvars = 0;
  207. f->linedefined = 0;
  208. f->lastlinedefined = 0;
  209. f->source = NULL;
  210. return f;
  211. }
  212. void luaF_freeproto (lua_State *L, Proto *f) {
  213. luaM_freearray(L, f->code, f->sizecode);
  214. luaM_freearray(L, f->p, f->sizep);
  215. luaM_freearray(L, f->k, f->sizek);
  216. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  217. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  218. luaM_freearray(L, f->locvars, f->sizelocvars);
  219. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  220. luaM_free(L, f);
  221. }
  222. /*
  223. ** Look for n-th local variable at line 'line' in function 'func'.
  224. ** Returns NULL if not found.
  225. */
  226. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  227. int i;
  228. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  229. if (pc < f->locvars[i].endpc) { /* is variable active? */
  230. local_number--;
  231. if (local_number == 0)
  232. return getstr(f->locvars[i].varname);
  233. }
  234. }
  235. return NULL; /* not found */
  236. }