lfunc.c 3.2 KB

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