lfunc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. ** $Id: lfunc.c,v 1.45 2001/06/28 14:57:17 roberto Exp $
  3. ** Auxiliary functions to manipulate prototypes and closures
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define LUA_PRIVATE
  8. #include "lua.h"
  9. #include "lfunc.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #define sizeCclosure(n) (cast(int, sizeof(Closure)) + \
  14. cast(int, sizeof(TObject)*((n)-1)))
  15. #define sizeLclosure(n) (cast(int, sizeof(Closure)) + \
  16. cast(int, sizeof(TObject *)*((n)-1)))
  17. Closure *luaF_newCclosure (lua_State *L, int nelems) {
  18. Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
  19. c->isC = 1;
  20. c->next = G(L)->rootcl;
  21. G(L)->rootcl = c;
  22. c->mark = c;
  23. c->nupvalues = nelems;
  24. return c;
  25. }
  26. Closure *luaF_newLclosure (lua_State *L, int nelems) {
  27. Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  28. c->isC = 0;
  29. c->mark = c;
  30. c->u.l.isopen = 0;
  31. c->nupvalues = nelems;
  32. return c;
  33. }
  34. /*
  35. ** returns the open pointer in a closure that points higher into the stack
  36. */
  37. static StkId uppoint (Closure *cl) {
  38. StkId lp = NULL;
  39. int i;
  40. lua_assert(cl->u.l.isopen);
  41. for (i=0; i<cl->nupvalues; i++) {
  42. if (!luaF_isclosed(cl, i))
  43. if (lp == NULL || cl->u.l.upvals[i] > lp)
  44. lp = cl->u.l.upvals[i];
  45. }
  46. lua_assert(lp != NULL);
  47. return lp;
  48. }
  49. void luaF_LConlist (lua_State *L, Closure *cl) {
  50. lua_assert(!cl->isC);
  51. if (cl->u.l.isopen == 0) { /* no more open entries? */
  52. cl->next = G(L)->rootcl; /* insert in final list */
  53. G(L)->rootcl = cl;
  54. }
  55. else { /* insert in list of open closures, ordered by decreasing uppoints */
  56. StkId cli = uppoint(cl);
  57. Closure **p = &L->opencl;
  58. while (*p != NULL && uppoint(*p) > cli) p = &(*p)->next;
  59. cl->next = *p;
  60. *p = cl;
  61. }
  62. }
  63. static int closeCl (lua_State *L, Closure *cl, StkId level) {
  64. int got = 0; /* flag: 1 if some pointer in the closure was corrected */
  65. int i;
  66. for (i=0; i<cl->nupvalues; i++) {
  67. StkId var;
  68. if (!luaF_isclosed(cl, i) && (var=cl->u.l.upvals[i]) >= level) {
  69. if (ttype(var) != LUA_TUPVAL) {
  70. UpVal *v = luaM_new(L, UpVal);
  71. v->val = *var;
  72. v->marked = 0;
  73. v->next = G(L)->rootupval;
  74. G(L)->rootupval = v;
  75. setupvalue(var, v);
  76. }
  77. cl->u.l.upvals[i] = cast(TObject *, vvalue(var));
  78. luaF_closeentry(cl, i);
  79. got = 1;
  80. }
  81. }
  82. return got;
  83. }
  84. void luaF_close (lua_State *L, StkId level) {
  85. Closure *affected = NULL; /* closures with open pointers >= level */
  86. Closure *cl;
  87. while ((cl=L->opencl) != NULL) {
  88. if (!closeCl(L, cl, level)) break;
  89. /* some pointer in `cl' changed; will re-insert it in original list */
  90. L->opencl = cl->next; /* remove from original list */
  91. cl->next = affected;
  92. affected = cl; /* insert in affected list */
  93. }
  94. /* re-insert all affected closures in original list */
  95. while ((cl=affected) != NULL) {
  96. affected = cl->next;
  97. luaF_LConlist(L, cl);
  98. }
  99. }
  100. Proto *luaF_newproto (lua_State *L) {
  101. Proto *f = luaM_new(L, Proto);
  102. f->k = NULL;
  103. f->sizek = 0;
  104. f->p = NULL;
  105. f->sizep = 0;
  106. f->code = NULL;
  107. f->sizecode = 0;
  108. f->nupvalues = 0;
  109. f->numparams = 0;
  110. f->is_vararg = 0;
  111. f->maxstacksize = 0;
  112. f->marked = 0;
  113. f->lineinfo = NULL;
  114. f->sizelocvars = 0;
  115. f->sizelineinfo = 0;
  116. f->locvars = NULL;
  117. f->lineDefined = 0;
  118. f->source = NULL;
  119. f->next = G(L)->rootproto; /* chain in list of protos */
  120. G(L)->rootproto = f;
  121. return f;
  122. }
  123. void luaF_freeproto (lua_State *L, Proto *f) {
  124. luaM_freearray(L, f->code, f->sizecode, Instruction);
  125. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  126. luaM_freearray(L, f->k, f->sizek, TObject);
  127. luaM_freearray(L, f->p, f->sizep, Proto *);
  128. luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
  129. luaM_freelem(L, f);
  130. }
  131. void luaF_freeclosure (lua_State *L, Closure *c) {
  132. int size = (c->isC) ? sizeCclosure(c->nupvalues) : sizeLclosure(c->nupvalues);
  133. luaM_free(L, c, size);
  134. }
  135. /*
  136. ** Look for n-th local variable at line `line' in function `func'.
  137. ** Returns NULL if not found.
  138. */
  139. const l_char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  140. int i;
  141. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  142. if (pc < f->locvars[i].endpc) { /* is variable active? */
  143. local_number--;
  144. if (local_number == 0)
  145. return getstr(f->locvars[i].varname);
  146. }
  147. }
  148. return NULL; /* not found */
  149. }