lfunc.c 8.1 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 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 - 3, 0);
  85. }
  86. /*
  87. ** Prepare closing method plus its arguments 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. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  93. if (ttisnil(tm)) /* no metamethod? */
  94. return 0; /* nothing to call */
  95. setobj2s(L, top, tm); /* will call metamethod... */
  96. setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
  97. setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
  98. L->top = top + 3; /* add function and arguments */
  99. return 1;
  100. }
  101. /*
  102. ** Prepare and call a closing method. If status is OK, code is still
  103. ** inside the original protected call, and so any error will be handled
  104. ** there. Otherwise, a previous error already activated original
  105. ** protected call, and so the call to the closing method must be
  106. ** protected here. (A status = CLOSEPROTECT behaves like a previous
  107. ** error, to also run the closing method in protected mode).
  108. ** If status is OK, the call to the closing method will be pushed
  109. ** at the top of the stack. Otherwise, values are pushed after
  110. ** the 'level' of the upvalue being closed, as everything after
  111. ** that won't be used again.
  112. */
  113. static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) {
  114. if (likely(status == LUA_OK)) {
  115. if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
  116. callclose(L, NULL); /* call closing method */
  117. else if (!ttisnil(uv)) { /* non-closable non-nil value? */
  118. int idx = cast_int(level - L->ci->func);
  119. const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
  120. if (vname == NULL) vname = "?";
  121. luaG_runerror(L, "attempt to close non-closable variable '%s'", vname);
  122. }
  123. }
  124. else { /* must close the object in protected mode */
  125. ptrdiff_t oldtop = savestack(L, level + 1);
  126. /* save error message and set stack top to 'level + 1' */
  127. luaD_seterrorobj(L, status, level);
  128. if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
  129. int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0);
  130. if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
  131. status = newstatus; /* this will be the new error */
  132. else /* leave original error (or nil) on top */
  133. L->top = restorestack(L, oldtop);
  134. }
  135. /* else no metamethod; ignore this case and keep original error */
  136. }
  137. return status;
  138. }
  139. /*
  140. ** Try to create a to-be-closed upvalue
  141. ** (can raise a memory-allocation error)
  142. */
  143. static void trynewtbcupval (lua_State *L, void *ud) {
  144. StkId level = cast(StkId, ud);
  145. lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
  146. newupval(L, LUA_TUPVALTBC, level, &L->openupval);
  147. }
  148. /*
  149. ** Create a to-be-closed upvalue. If there is a memory error
  150. ** when creating the upvalue, the closing method must be called here,
  151. ** as there is no upvalue to call it later.
  152. */
  153. void luaF_newtbcupval (lua_State *L, StkId level) {
  154. int status = luaD_rawrunprotected(L, trynewtbcupval, level);
  155. if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
  156. lua_assert(status == LUA_ERRMEM);
  157. luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
  158. if (prepclosingmethod(L, s2v(level), s2v(level + 1)))
  159. callclose(L, NULL); /* call closing method */
  160. luaD_throw(L, LUA_ERRMEM); /* throw memory error */
  161. }
  162. }
  163. void luaF_unlinkupval (UpVal *uv) {
  164. lua_assert(upisopen(uv));
  165. *uv->u.open.previous = uv->u.open.next;
  166. if (uv->u.open.next)
  167. uv->u.open.next->u.open.previous = uv->u.open.previous;
  168. }
  169. int luaF_close (lua_State *L, StkId level, int status) {
  170. UpVal *uv;
  171. while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
  172. StkId upl = uplevel(uv);
  173. TValue *slot = &uv->u.value; /* new position for value */
  174. luaF_unlinkupval(uv);
  175. setobj(L, slot, uv->v); /* move value to upvalue slot */
  176. uv->v = slot; /* now current value lives here */
  177. if (!iswhite(uv))
  178. gray2black(uv); /* closed upvalues cannot be gray */
  179. luaC_barrier(L, uv, slot);
  180. if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) {
  181. /* must run closing method */
  182. ptrdiff_t levelrel = savestack(L, level);
  183. status = callclosemth(L, uv->v, upl, status); /* may change the stack */
  184. level = restorestack(L, levelrel);
  185. }
  186. }
  187. return status;
  188. }
  189. Proto *luaF_newproto (lua_State *L) {
  190. GCObject *o = luaC_newobj(L, LUA_TPROTO, 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. }