lfunc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. CClosure *luaF_newCclosure (lua_State *L, int n) {
  18. GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
  19. CClosure *c = gco2ccl(o);
  20. c->nupvalues = cast_byte(n);
  21. return c;
  22. }
  23. LClosure *luaF_newLclosure (lua_State *L, int n) {
  24. GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
  25. LClosure *c = gco2lcl(o);
  26. c->p = NULL;
  27. c->nupvalues = cast_byte(n);
  28. while (n--) c->upvals[n] = NULL;
  29. return c;
  30. }
  31. /*
  32. ** fill a closure with new closed upvalues
  33. */
  34. void luaF_initupvals (lua_State *L, LClosure *cl) {
  35. int i;
  36. for (i = 0; i < cl->nupvalues; i++) {
  37. GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal));
  38. UpVal *uv = gco2upv(o);
  39. uv->v = &uv->u.value; /* make it closed */
  40. setnilvalue(uv->v);
  41. cl->upvals[i] = uv;
  42. luaC_objbarrier(L, cl, o);
  43. }
  44. }
  45. /*
  46. ** Create a new upvalue with the given tag at the given level,
  47. ** and link it to the list of open upvalues of 'L' after entry 'prev'.
  48. **/
  49. static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) {
  50. GCObject *o = luaC_newobj(L, tag, sizeof(UpVal));
  51. UpVal *uv = gco2upv(o);
  52. UpVal *next = *prev;
  53. uv->v = s2v(level); /* current value lives in the stack */
  54. uv->u.open.next = next; /* link it to list of open upvalues */
  55. uv->u.open.previous = prev;
  56. if (next)
  57. next->u.open.previous = &uv->u.open.next;
  58. *prev = uv;
  59. if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
  60. L->twups = G(L)->twups; /* link it to the list */
  61. G(L)->twups = L;
  62. }
  63. return uv;
  64. }
  65. /*
  66. ** Find and reuse, or create if it does not exist, a regular upvalue
  67. ** at the given level.
  68. */
  69. UpVal *luaF_findupval (lua_State *L, StkId level) {
  70. UpVal **pp = &L->openupval;
  71. UpVal *p;
  72. lua_assert(isintwups(L) || L->openupval == NULL);
  73. while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
  74. if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */
  75. return p; /* return it */
  76. pp = &p->u.open.next;
  77. }
  78. /* not found: create a new upvalue after 'pp' */
  79. return newupval(L, LUA_TUPVAL, level, pp);
  80. }
  81. static void callclose (lua_State *L, void *ud) {
  82. luaD_callnoyield(L, cast(StkId, ud), 0);
  83. }
  84. /*
  85. ** Prepare closing method with its argument for object at
  86. ** index 'func' in the stack. Assume there is an error message
  87. ** (or nil) just below the object.
  88. */
  89. static int prepclosingmethod (lua_State *L, StkId func) {
  90. if (ttisfunction(s2v(func))) { /* object to-be-closed is a function? */
  91. setobjs2s(L, func + 1, func - 1); /* push error msg. as argument */
  92. }
  93. else { /* try '__close' metamethod */
  94. const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CLOSE);
  95. if (ttisnil(tm)) /* no metamethod? */
  96. return 0; /* nothing to call */
  97. setobjs2s(L, func + 1, func); /* 'self' is the argument */
  98. setobj2s(L, func, tm); /* will call metamethod */
  99. }
  100. L->top = func + 2; /* add function and argument */
  101. return 1;
  102. }
  103. /*
  104. ** Prepare and call a closing method. If status is OK, code is
  105. ** still inside the original protected call, and so any error
  106. ** will be handled there. Otherwise, a previous error already
  107. ** activated original protected call, and so the call to the
  108. ** closing method must be protected here.
  109. */
  110. static int closeupval (lua_State *L, TValue *uv, StkId level, int status) {
  111. StkId func = level + 1; /* save slot for old error message */
  112. if (unlikely(status != LUA_OK)) /* was there an error? */
  113. luaD_seterrorobj(L, status, level); /* save error message */
  114. else
  115. setnilvalue(s2v(level)); /* no error message */
  116. setobj2s(L, func, uv); /* put object on top of error message */
  117. if (!prepclosingmethod(L, func))
  118. return status; /* nothing to call */
  119. if (likely(status == LUA_OK)) /* not in "error mode"? */
  120. callclose(L, func); /* call closing method */
  121. else { /* already inside error handler; cannot raise another error */
  122. int newstatus = luaD_pcall(L, callclose, func, savestack(L, level), 0);
  123. if (newstatus != LUA_OK) /* error when closing? */
  124. status = newstatus; /* this will be the new error */
  125. }
  126. return status;
  127. }
  128. /*
  129. ** Try to create a to-be-closed upvalue
  130. ** (can raise a memory-allocation error)
  131. */
  132. static void trynewtbcupval (lua_State *L, void *ud) {
  133. StkId level = cast(StkId, ud);
  134. lua_assert(L->openupval == NULL || uplevel(L->openupval) < level);
  135. newupval(L, LUA_TUPVALTBC, level, &L->openupval);
  136. }
  137. /*
  138. ** Create a to-be-closed upvalue. If there is a memory error
  139. ** when creating the upvalue, the closing method must be called here,
  140. ** as there is no upvalue to call it later.
  141. */
  142. void luaF_newtbcupval (lua_State *L, StkId level) {
  143. int status = luaD_rawrunprotected(L, trynewtbcupval, level);
  144. if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */
  145. StkId func = level + 1;
  146. lua_assert(status == LUA_ERRMEM);
  147. setobjs2s(L, func, level); /* open space for error message */
  148. luaD_seterrorobj(L, status, level); /* save error message */
  149. if (prepclosingmethod(L, func))
  150. callclose(L, func); /* call closing method */
  151. luaD_throw(L, LUA_ERRMEM); /* throw memory error */
  152. }
  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. int luaF_close (lua_State *L, StkId level, int status) {
  161. UpVal *uv;
  162. while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
  163. StkId upl = uplevel(uv);
  164. TValue *slot = &uv->u.value; /* new position for value */
  165. luaF_unlinkupval(uv);
  166. setobj(L, slot, uv->v); /* move value to upvalue slot */
  167. uv->v = slot; /* now current value lives here */
  168. if (!iswhite(uv))
  169. gray2black(uv); /* closed upvalues cannot be gray */
  170. luaC_barrier(L, uv, slot);
  171. if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */
  172. ptrdiff_t levelrel = savestack(L, level);
  173. status = closeupval(L, uv->v, upl, status); /* may reallocate the stack */
  174. level = restorestack(L, levelrel);
  175. }
  176. }
  177. return status;
  178. }
  179. Proto *luaF_newproto (lua_State *L) {
  180. GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
  181. Proto *f = gco2p(o);
  182. f->k = NULL;
  183. f->sizek = 0;
  184. f->p = NULL;
  185. f->sizep = 0;
  186. f->code = NULL;
  187. f->cache = NULL;
  188. f->cachemiss = 0;
  189. f->sizecode = 0;
  190. f->lineinfo = NULL;
  191. f->sizelineinfo = 0;
  192. f->abslineinfo = NULL;
  193. f->sizeabslineinfo = 0;
  194. f->upvalues = NULL;
  195. f->sizeupvalues = 0;
  196. f->numparams = 0;
  197. f->is_vararg = 0;
  198. f->maxstacksize = 0;
  199. f->locvars = NULL;
  200. f->sizelocvars = 0;
  201. f->linedefined = 0;
  202. f->lastlinedefined = 0;
  203. f->source = NULL;
  204. return f;
  205. }
  206. void luaF_freeproto (lua_State *L, Proto *f) {
  207. luaM_freearray(L, f->code, f->sizecode);
  208. luaM_freearray(L, f->p, f->sizep);
  209. luaM_freearray(L, f->k, f->sizek);
  210. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  211. luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
  212. luaM_freearray(L, f->locvars, f->sizelocvars);
  213. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  214. luaM_free(L, f);
  215. }
  216. /*
  217. ** Look for n-th local variable at line 'line' in function 'func'.
  218. ** Returns NULL if not found.
  219. */
  220. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  221. int i;
  222. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  223. if (pc < f->locvars[i].endpc) { /* is variable active? */
  224. local_number--;
  225. if (local_number == 0)
  226. return getstr(f->locvars[i].varname);
  227. }
  228. }
  229. return NULL; /* not found */
  230. }