lfunc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ** $Id: lfunc.c,v 1.60 2002/10/16 20:40:58 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 "lgc.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
  14. cast(int, sizeof(TObject)*((n)-1)))
  15. #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
  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. luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
  20. c->c.isC = 1;
  21. c->c.nupvalues = cast(lu_byte, nelems);
  22. return c;
  23. }
  24. Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
  25. Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  26. luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
  27. c->l.isC = 0;
  28. c->l.g = *gt;
  29. c->l.nupvalues = cast(lu_byte, nelems);
  30. return c;
  31. }
  32. UpVal *luaF_findupval (lua_State *L, StkId level) {
  33. GCObject **pp = &L->openupval;
  34. GCObject *p;
  35. UpVal *v;
  36. while ((p = *pp) != NULL && (&p->uv)->v >= level) {
  37. if ((&p->uv)->v == level) return &p->uv;
  38. pp = &p->gch.next;
  39. }
  40. v = luaM_new(L, UpVal); /* not found: create a new one */
  41. v->marked = 1; /* open upvalues should not be collected */
  42. v->v = level; /* current value lives in the stack */
  43. v->next = *pp; /* chain it in the proper position */
  44. *pp = cast(GCObject *, v);
  45. return v;
  46. }
  47. void luaF_close (lua_State *L, StkId level) {
  48. UpVal *p;
  49. while ((p = &(L->openupval)->uv) != 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. luaC_link(L, cast(GCObject *, p), LUA_TUPVAL);
  54. }
  55. }
  56. Proto *luaF_newproto (lua_State *L) {
  57. Proto *f = luaM_new(L, Proto);
  58. luaC_link(L, cast(GCObject *, f), LUA_TPROTO);
  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->sizelineinfo = 0;
  66. f->nupvalues = 0;
  67. f->numparams = 0;
  68. f->is_vararg = 0;
  69. f->maxstacksize = 0;
  70. f->lineinfo = NULL;
  71. f->sizelocvars = 0;
  72. f->locvars = NULL;
  73. f->lineDefined = 0;
  74. f->source = NULL;
  75. return f;
  76. }
  77. void luaF_freeproto (lua_State *L, Proto *f) {
  78. luaM_freearray(L, f->code, f->sizecode, Instruction);
  79. luaM_freearray(L, f->p, f->sizep, Proto *);
  80. luaM_freearray(L, f->k, f->sizek, TObject);
  81. luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
  82. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  83. luaM_freelem(L, f);
  84. }
  85. void luaF_freeclosure (lua_State *L, Closure *c) {
  86. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  87. sizeLclosure(c->l.nupvalues);
  88. luaM_free(L, c, size);
  89. }
  90. /*
  91. ** Look for n-th local variable at line `line' in function `func'.
  92. ** Returns NULL if not found.
  93. */
  94. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  95. int i;
  96. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  97. if (pc < f->locvars[i].endpc) { /* is variable active? */
  98. local_number--;
  99. if (local_number == 0)
  100. return getstr(f->locvars[i].varname);
  101. }
  102. }
  103. return NULL; /* not found */
  104. }