lfunc.c 4.5 KB

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