lfunc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ** $Id: lfunc.c,v 2.19 2009/12/16 16:42:58 roberto Exp roberto $
  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 "lopcodes.h"
  15. #include "lstate.h"
  16. Closure *luaF_newCclosure (lua_State *L, int n, Table *e) {
  17. Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeCclosure(n), NULL, 0)->cl;
  18. c->c.isC = 1;
  19. c->c.env = e;
  20. c->c.nupvalues = cast_byte(n);
  21. return c;
  22. }
  23. Closure *luaF_newLclosure (lua_State *L, int n, Table *e) {
  24. Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl;
  25. c->l.isC = 0;
  26. c->l.env = e;
  27. c->l.nupvalues = cast_byte(n);
  28. while (n--) c->l.upvals[n] = NULL;
  29. return c;
  30. }
  31. UpVal *luaF_newupval (lua_State *L) {
  32. UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
  33. uv->v = &uv->u.value;
  34. setnilvalue(uv->v);
  35. return uv;
  36. }
  37. UpVal *luaF_findupval (lua_State *L, StkId level) {
  38. global_State *g = G(L);
  39. GCObject **pp = &L->openupval;
  40. UpVal *p;
  41. UpVal *uv;
  42. while (*pp != NULL && (p = gco2uv(*pp))->v >= level) {
  43. lua_assert(p->v != &p->u.value);
  44. if (p->v == level) { /* found a corresponding upvalue? */
  45. if (isdead(g, obj2gco(p))) /* is it dead? */
  46. changewhite(obj2gco(p)); /* ressurrect it */
  47. return p;
  48. }
  49. pp = &p->next;
  50. }
  51. /* not found: create a new one */
  52. uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv;
  53. uv->v = level; /* current value lives in the stack */
  54. uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
  55. uv->u.l.next = g->uvhead.u.l.next;
  56. uv->u.l.next->u.l.prev = uv;
  57. g->uvhead.u.l.next = uv;
  58. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  59. return uv;
  60. }
  61. static void unlinkupval (UpVal *uv) {
  62. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  63. uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
  64. uv->u.l.prev->u.l.next = uv->u.l.next;
  65. }
  66. void luaF_freeupval (lua_State *L, UpVal *uv) {
  67. if (uv->v != &uv->u.value) /* is it open? */
  68. unlinkupval(uv); /* remove from open list */
  69. luaM_free(L, uv); /* free upvalue */
  70. }
  71. void luaF_close (lua_State *L, StkId level) {
  72. UpVal *uv;
  73. global_State *g = G(L);
  74. while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) {
  75. GCObject *o = obj2gco(uv);
  76. lua_assert(!isblack(o) && uv->v != &uv->u.value);
  77. L->openupval = uv->next; /* remove from `open' list */
  78. if (isdead(g, o))
  79. luaF_freeupval(L, uv); /* free upvalue */
  80. else {
  81. unlinkupval(uv);
  82. setobj(L, &uv->u.value, uv->v);
  83. uv->v = &uv->u.value; /* now current value lives here */
  84. luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
  85. }
  86. }
  87. }
  88. Proto *luaF_newproto (lua_State *L) {
  89. Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
  90. f->k = NULL;
  91. f->sizek = 0;
  92. f->p = NULL;
  93. f->sizep = 0;
  94. f->code = NULL;
  95. f->sizecode = 0;
  96. f->lineinfo = NULL;
  97. f->sizelineinfo = 0;
  98. f->upvalues = NULL;
  99. f->sizeupvalues = 0;
  100. f->numparams = 0;
  101. f->is_vararg = 0;
  102. f->maxstacksize = 0;
  103. f->locvars = NULL;
  104. f->sizelocvars = 0;
  105. f->linedefined = 0;
  106. f->lastlinedefined = 0;
  107. f->source = NULL;
  108. return f;
  109. }
  110. void luaF_freeproto (lua_State *L, Proto *f) {
  111. luaM_freearray(L, f->code, f->sizecode);
  112. luaM_freearray(L, f->p, f->sizep);
  113. luaM_freearray(L, f->k, f->sizek);
  114. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  115. luaM_freearray(L, f->locvars, f->sizelocvars);
  116. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  117. luaM_free(L, f);
  118. }
  119. void luaF_freeclosure (lua_State *L, Closure *c) {
  120. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  121. sizeLclosure(c->l.nupvalues);
  122. luaM_freemem(L, c, size);
  123. }
  124. /*
  125. ** Look for n-th local variable at line `line' in function `func'.
  126. ** Returns NULL if not found.
  127. */
  128. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  129. int i;
  130. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  131. if (pc < f->locvars[i].endpc) { /* is variable active? */
  132. local_number--;
  133. if (local_number == 0)
  134. return getstr(f->locvars[i].varname);
  135. }
  136. }
  137. return NULL; /* not found */
  138. }