lfunc.c 3.4 KB

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