lfunc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ** $Id: lfunc.c,v 1.57 2002/06/20 20:41:46 roberto Exp roberto $
  3. ** Auxiliary functions to manipulate prototypes and closures
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "lfunc.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
  13. cast(int, sizeof(TObject)*((n)-1)))
  14. #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
  15. cast(int, sizeof(TObject *)*((n)-1)))
  16. Closure *luaF_newCclosure (lua_State *L, int nelems) {
  17. Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
  18. c->c.isC = 1;
  19. c->c.next = G(L)->rootcl;
  20. G(L)->rootcl = c;
  21. c->c.marked = 0;
  22. c->c.nupvalues = cast(lu_byte, nelems);
  23. return c;
  24. }
  25. Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
  26. Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  27. c->l.isC = 0;
  28. c->c.next = G(L)->rootcl;
  29. G(L)->rootcl = c;
  30. c->l.marked = 0;
  31. c->l.g = *gt;
  32. c->l.nupvalues = cast(lu_byte, nelems);
  33. return c;
  34. }
  35. UpVal *luaF_findupval (lua_State *L, StkId level) {
  36. UpVal **pp = &L->openupval;
  37. UpVal *p;
  38. while ((p = *pp) != NULL && p->v >= level) {
  39. if (p->v == level) return p;
  40. pp = &p->next;
  41. }
  42. p = luaM_new(L, UpVal); /* not found: create a new one */
  43. p->marked = 1; /* open upvalues should not be collected */
  44. p->v = level; /* current value lives in the stack */
  45. p->next = *pp; /* chain it in the proper position */
  46. *pp = p;
  47. return p;
  48. }
  49. void luaF_close (lua_State *L, StkId level) {
  50. UpVal *p;
  51. while ((p = L->openupval) != NULL && p->v >= level) {
  52. lua_assert(p->marked);
  53. p->marked = 0;
  54. setobj(&p->value, p->v); /* save current value */
  55. p->v = &p->value; /* now current value lives here */
  56. L->openupval = p->next; /* remove from `open' list */
  57. p->next = G(L)->rootupval; /* chain in `closed' list */
  58. G(L)->rootupval = p;
  59. }
  60. }
  61. Proto *luaF_newproto (lua_State *L) {
  62. Proto *f = luaM_new(L, Proto);
  63. f->k = NULL;
  64. f->sizek = 0;
  65. f->p = NULL;
  66. f->sizep = 0;
  67. f->code = NULL;
  68. f->sizecode = 0;
  69. f->nupvalues = 0;
  70. f->numparams = 0;
  71. f->is_vararg = 0;
  72. f->maxstacksize = 0;
  73. f->marked = 0;
  74. f->lineinfo = NULL;
  75. f->sizelocvars = 0;
  76. f->locvars = NULL;
  77. f->lineDefined = 0;
  78. f->source = NULL;
  79. f->next = G(L)->rootproto; /* chain in list of protos */
  80. G(L)->rootproto = f;
  81. return f;
  82. }
  83. void luaF_freeproto (lua_State *L, Proto *f) {
  84. luaM_freearray(L, f->code, f->sizecode, Instruction);
  85. if (f->lineinfo)
  86. luaM_freearray(L, f->lineinfo, f->sizecode, int);
  87. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  88. luaM_freearray(L, f->k, f->sizek, TObject);
  89. luaM_freearray(L, f->p, f->sizep, Proto *);
  90. luaM_freelem(L, f);
  91. }
  92. void luaF_freeclosure (lua_State *L, Closure *c) {
  93. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  94. sizeLclosure(c->l.nupvalues);
  95. luaM_free(L, c, size);
  96. }
  97. /*
  98. ** Look for n-th local variable at line `line' in function `func'.
  99. ** Returns NULL if not found.
  100. */
  101. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  102. int i;
  103. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  104. if (pc < f->locvars[i].endpc) { /* is variable active? */
  105. local_number--;
  106. if (local_number == 0)
  107. return getstr(f->locvars[i].varname);
  108. }
  109. }
  110. return NULL; /* not found */
  111. }