lfunc.c 3.3 KB

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