lfunc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static void callclose (lua_State *L, void *ud) {
  85. UNUSED(ud);
  86. luaD_callnoyield(L, L->top - 3, 0);
  87. }
  88. /*
  89. ** Prepare closing method plus its arguments for object 'obj' with
  90. ** error message 'err'. (This function assumes EXTRA_STACK.)
  91. */
  92. static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) {
  93. StkId top = L->top;
  94. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  95. if (ttisnil(tm)) /* no metamethod? */
  96. return 0; /* nothing to call */
  97. setobj2s(L, top, tm); /* will call metamethod... */
  98. setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */
  99. setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */
  100. L->top = top + 3; /* add function and arguments */
  101. return 1;
  102. }
  103. /*
  104. ** Raise an error with message 'msg', inserting the name of the
  105. ** local variable at position 'level' in the stack.
  106. */
  107. static void varerror (lua_State *L, StkId level, const char *msg) {
  108. int idx = cast_int(level - L->ci->func);
  109. const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
  110. if (vname == NULL) vname = "?";
  111. luaG_runerror(L, msg, vname);
  112. }
  113. /*
  114. ** Prepare and call a closing method. If status is OK, code is still
  115. ** inside the original protected call, and so any error will be handled
  116. ** there. Otherwise, a previous error already activated the original
  117. ** protected call, and so the call to the closing method must be
  118. ** protected here. (A status == CLOSEPROTECT behaves like a previous
  119. ** error, to also run the closing method in protected mode).
  120. ** If status is OK, the call to the closing method will be pushed
  121. ** at the top of the stack. Otherwise, values are pushed after
  122. ** the 'level' of the upvalue being closed, as everything after
  123. ** that won't be used again.
  124. */
  125. static int callclosemth (lua_State *L, StkId level, int status) {
  126. TValue *uv = s2v(level); /* value being closed */
  127. if (likely(status == LUA_OK)) {
  128. if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */
  129. callclose(L, NULL); /* call closing method */
  130. else if (!l_isfalse(uv)) /* non-closable non-false value? */
  131. varerror(L, level, "attempt to close non-closable variable '%s'");
  132. }
  133. else { /* must close the object in protected mode */
  134. ptrdiff_t oldtop;
  135. level++; /* space for error message */
  136. oldtop = savestack(L, level + 1); /* top will be after that */
  137. luaD_seterrorobj(L, status, level); /* set error message */
  138. if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */
  139. int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0);
  140. if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */
  141. status = newstatus; /* this will be the new error */
  142. else {
  143. if (newstatus != LUA_OK) /* suppressed error? */
  144. luaE_warnerror(L, "__close metamethod");
  145. /* leave original error (or nil) on top */
  146. L->top = restorestack(L, oldtop);
  147. }
  148. }
  149. /* else no metamethod; ignore this case and keep original error */
  150. }
  151. return status;
  152. }
  153. /*
  154. ** Try to create a to-be-closed upvalue
  155. ** (can raise a memory-allocation error)
  156. */
  157. static void trynewtbcupval (lua_State *L, void *ud) {
  158. newupval(L, 1, cast(StkId, ud), &L->openupval);
  159. }
  160. /*
  161. ** Create a to-be-closed upvalue. If there is a memory error
  162. ** when creating the upvalue, the closing method must be called here,
  163. ** as there is no upvalue to call it later.
  164. */
  165. void luaF_newtbcupval (lua_State *L, StkId level) {
  166. TValue *obj = s2v(level);
  167. lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
  168. if (!l_isfalse(obj)) { /* false doesn't need to be closed */
  169. int status;
  170. const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
  171. if (ttisnil(tm)) /* no metamethod? */
  172. varerror(L, level, "variable '%s' got a non-closable value");
  173. status = luaD_rawrunprotected(L, trynewtbcupval, level);
  174. if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
  175. lua_assert(status == LUA_ERRMEM);
  176. luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */
  177. /* next call must succeed, as object is closable */
  178. prepclosingmethod(L, s2v(level), s2v(level + 1));
  179. callclose(L, NULL); /* call closing method */
  180. luaD_throw(L, LUA_ERRMEM); /* throw memory error */
  181. }
  182. }
  183. }
  184. void luaF_unlinkupval (UpVal *uv) {
  185. lua_assert(upisopen(uv));
  186. *uv->u.open.previous = uv->u.open.next;
  187. if (uv->u.open.next)
  188. uv->u.open.next->u.open.previous = uv->u.open.previous;
  189. }
  190. int luaF_close (lua_State *L, StkId level, int status) {
  191. UpVal *uv;
  192. while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
  193. TValue *slot = &uv->u.value; /* new position for value */
  194. lua_assert(uplevel(uv) < L->top);
  195. if (uv->tbc && status != NOCLOSINGMETH) {
  196. /* must run closing method, which may change the stack */
  197. ptrdiff_t levelrel = savestack(L, level);
  198. status = callclosemth(L, uplevel(uv), status);
  199. level = restorestack(L, levelrel);
  200. }
  201. luaF_unlinkupval(uv);
  202. setobj(L, slot, uv->v); /* move value to upvalue slot */
  203. uv->v = slot; /* now current value lives here */
  204. if (!iswhite(uv)) { /* neither white nor dead? */
  205. nw2black(uv); /* closed upvalues cannot be gray */
  206. luaC_barrier(L, uv, slot);
  207. }
  208. }
  209. return status;
  210. }
  211. Proto *luaF_newproto (lua_State *L) {
  212. GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
  213. Proto *f = gco2p(o);
  214. f->k = NULL;
  215. f->sizek = 0;
  216. f->p = NULL;
  217. f->sizep = 0;
  218. f->code = NULL;
  219. f->sizecode = 0;
  220. f->lineinfo = NULL;
  221. f->sizelineinfo = 0;
  222. f->abslineinfo = NULL;
  223. f->sizeabslineinfo = 0;
  224. f->upvalues = NULL;
  225. f->sizeupvalues = 0;
  226. f->numparams = 0;
  227. f->is_vararg = 0;
  228. f->maxstacksize = 0;
  229. f->locvars = NULL;
  230. f->sizelocvars = 0;
  231. f->linedefined = 0;
  232. f->lastlinedefined = 0;
  233. f->source = NULL;
  234. return f;
  235. }
  236. void luaF_freeproto (lua_State *L, Proto *f) {
  237. luaM_freearray(L, f->code, f->sizecode);
  238. luaM_freearray(L, f->p, f->sizep);
  239. luaM_freearray(L, f->k, f->sizek);
  240. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  241. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  242. luaM_freearray(L, f->locvars, f->sizelocvars);
  243. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  244. luaM_free(L, f);
  245. }
  246. /*
  247. ** Look for n-th local variable at line 'line' in function 'func'.
  248. ** Returns NULL if not found.
  249. */
  250. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  251. int i;
  252. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  253. if (pc < f->locvars[i].endpc) { /* is variable active? */
  254. local_number--;
  255. if (local_number == 0)
  256. return getstr(f->locvars[i].varname);
  257. }
  258. }
  259. return NULL; /* not found */
  260. }