lfunc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ** $Id: lfunc.c,v 1.56 2002/05/02 13:06:20 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->v = level; /* current value lives in the stack */
  44. p->next = *pp; /* chain it in the proper position */
  45. *pp = p;
  46. return p;
  47. }
  48. void luaF_close (lua_State *L, StkId level) {
  49. UpVal *p;
  50. while ((p = L->openupval) != NULL && p->v >= level) {
  51. setobj(&p->value, p->v); /* save current value */
  52. p->v = &p->value; /* now current value lives here */
  53. L->openupval = p->next; /* remove from `open' list */
  54. p->next = G(L)->rootupval; /* chain in `closed' list */
  55. G(L)->rootupval = p;
  56. }
  57. }
  58. Proto *luaF_newproto (lua_State *L) {
  59. Proto *f = luaM_new(L, Proto);
  60. f->k = NULL;
  61. f->sizek = 0;
  62. f->p = NULL;
  63. f->sizep = 0;
  64. f->code = NULL;
  65. f->sizecode = 0;
  66. f->nupvalues = 0;
  67. f->numparams = 0;
  68. f->is_vararg = 0;
  69. f->maxstacksize = 0;
  70. f->marked = 0;
  71. f->lineinfo = NULL;
  72. f->sizelocvars = 0;
  73. f->locvars = NULL;
  74. f->lineDefined = 0;
  75. f->source = NULL;
  76. f->next = G(L)->rootproto; /* chain in list of protos */
  77. G(L)->rootproto = f;
  78. return f;
  79. }
  80. void luaF_freeproto (lua_State *L, Proto *f) {
  81. luaM_freearray(L, f->code, f->sizecode, Instruction);
  82. if (f->lineinfo)
  83. luaM_freearray(L, f->lineinfo, f->sizecode, int);
  84. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  85. luaM_freearray(L, f->k, f->sizek, TObject);
  86. luaM_freearray(L, f->p, f->sizep, Proto *);
  87. luaM_freelem(L, f);
  88. }
  89. void luaF_freeclosure (lua_State *L, Closure *c) {
  90. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  91. sizeLclosure(c->l.nupvalues);
  92. luaM_free(L, c, size);
  93. }
  94. /*
  95. ** Look for n-th local variable at line `line' in function `func'.
  96. ** Returns NULL if not found.
  97. */
  98. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  99. int i;
  100. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  101. if (pc < f->locvars[i].endpc) { /* is variable active? */
  102. local_number--;
  103. if (local_number == 0)
  104. return getstr(f->locvars[i].varname);
  105. }
  106. }
  107. return NULL; /* not found */
  108. }