lfunc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $
  3. ** Auxiliary functions to manipulate prototypes and closures
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define lfunc_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. Closure *luaF_newCclosure(lua_State *L, int nelems, Table *e)
  16. {
  17. Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
  18. luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  19. c->c.isC = 1;
  20. c->c.env = e;
  21. c->c.nupvalues = cast_byte(nelems);
  22. return c;
  23. }
  24. Closure *luaF_newLclosure(lua_State *L, int nelems, Table *e)
  25. {
  26. Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  27. luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  28. c->l.isC = 0;
  29. c->l.env = e;
  30. c->l.nupvalues = cast_byte(nelems);
  31. while (nelems--) c->l.upvals[nelems] = NULL;
  32. return c;
  33. }
  34. UpVal *luaF_newupval(lua_State *L)
  35. {
  36. UpVal *uv = luaM_new(L, UpVal);
  37. luaC_link(L, obj2gco(uv), LUA_TUPVAL);
  38. uv->v = &uv->u.value;
  39. setnilvalue(uv->v);
  40. return uv;
  41. }
  42. UpVal *luaF_findupval(lua_State *L, StkId level)
  43. {
  44. global_State *g = G(L);
  45. GCObject **pp = &L->openupval;
  46. UpVal *p;
  47. UpVal *uv;
  48. while (*pp != NULL && (p = ngcotouv(*pp))->v >= level)
  49. {
  50. lua_assert(p->v != &p->u.value);
  51. if (p->v == level) /* found a corresponding upvalue? */
  52. {
  53. if (isdead(g, obj2gco(p))) /* is it dead? */
  54. changewhite(obj2gco(p)); /* ressurect it */
  55. return p;
  56. }
  57. pp = &p->next;
  58. }
  59. uv = luaM_new(L, UpVal); /* not found: create a new one */
  60. uv->tt = LUA_TUPVAL;
  61. uv->v = level; /* current value lives in the stack */
  62. uv->next = *pp; /* chain it in the proper position */
  63. *pp = obj2gco(uv);
  64. uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
  65. uv->u.l.next = g->uvhead.u.l.next;
  66. uv->u.l.next->u.l.prev = uv;
  67. g->uvhead.u.l.next = uv;
  68. luaC_marknew(L, obj2gco(uv));
  69. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  70. return uv;
  71. }
  72. static void unlinkupval(UpVal *uv)
  73. {
  74. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  75. uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
  76. uv->u.l.prev->u.l.next = uv->u.l.next;
  77. }
  78. void luaF_freeupval(lua_State *L, UpVal *uv)
  79. {
  80. if (uv->v != &uv->u.value) /* is it open? */
  81. unlinkupval(uv); /* remove from open list */
  82. luaM_free(L, uv); /* free upvalue */
  83. }
  84. void luaF_close(lua_State *L, StkId level)
  85. {
  86. UpVal *uv;
  87. global_State *g = G(L);
  88. while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level)
  89. {
  90. GCObject *o = obj2gco(uv);
  91. lua_assert(!isblack(o) && uv->v != &uv->u.value);
  92. L->openupval = uv->next; /* remove from `open' list */
  93. if (isdead(g, o))
  94. luaF_freeupval(L, uv); /* free upvalue */
  95. else
  96. {
  97. unlinkupval(uv);
  98. setobj(L, &uv->u.value, uv->v);
  99. uv->v = &uv->u.value; /* now current value lives here */
  100. luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
  101. }
  102. }
  103. }
  104. Proto *luaF_newproto(lua_State *L)
  105. {
  106. Proto *f = luaM_new(L, Proto);
  107. luaC_link(L, obj2gco(f), LUA_TPROTO);
  108. f->k = NULL;
  109. f->sizek = 0;
  110. f->p = NULL;
  111. f->sizep = 0;
  112. f->code = NULL;
  113. f->sizecode = 0;
  114. f->sizelineinfo = 0;
  115. f->sizeupvalues = 0;
  116. f->nups = 0;
  117. f->upvalues = NULL;
  118. f->numparams = 0;
  119. f->is_vararg = 0;
  120. f->maxstacksize = 0;
  121. f->lineinfo = NULL;
  122. f->sizelocvars = 0;
  123. f->locvars = NULL;
  124. f->linedefined = 0;
  125. f->lastlinedefined = 0;
  126. f->source = NULL;
  127. return f;
  128. }
  129. void luaF_freeproto(lua_State *L, Proto *f)
  130. {
  131. luaM_freearray(L, f->p, f->sizep, Proto *);
  132. luaM_freearray(L, f->k, f->sizek, TValue);
  133. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  134. luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
  135. if (!proto_is_readonly(f))
  136. {
  137. luaM_freearray(L, f->code, f->sizecode, Instruction);
  138. luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
  139. }
  140. luaM_free(L, f);
  141. }
  142. void luaF_freeclosure(lua_State *L, Closure *c)
  143. {
  144. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  145. sizeLclosure(c->l.nupvalues);
  146. luaM_freemem(L, c, size);
  147. }
  148. /*
  149. ** Look for n-th local variable at line `line' in function `func'.
  150. ** Returns NULL if not found.
  151. */
  152. const char *luaF_getlocalname(const Proto *f, int local_number, int pc)
  153. {
  154. int i;
  155. for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++)
  156. {
  157. if (pc < f->locvars[i].endpc) /* is variable active? */
  158. {
  159. local_number--;
  160. if (local_number == 0)
  161. return getstr(f->locvars[i].varname);
  162. }
  163. }
  164. return NULL; /* not found */
  165. }