lfunc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ** $Id: lfunc.c,v 2.4 2004/04/30 20:13:38 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. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. Closure *luaF_newCclosure (lua_State *L, int nelems) {
  16. Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
  17. luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  18. c->c.isC = 1;
  19. c->c.nupvalues = cast(lu_byte, nelems);
  20. return c;
  21. }
  22. Closure *luaF_newLclosure (lua_State *L, int nelems, TValue *e) {
  23. Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
  24. luaC_link(L, obj2gco(c), LUA_TFUNCTION);
  25. c->l.isC = 0;
  26. c->l.g = *e;
  27. c->l.nupvalues = cast(lu_byte, nelems);
  28. while (nelems--) c->l.upvals[nelems] = NULL;
  29. return c;
  30. }
  31. UpVal *luaF_newupval (lua_State *L) {
  32. UpVal *uv = luaM_new(L, UpVal);
  33. luaC_link(L, obj2gco(uv), LUA_TUPVAL);
  34. uv->v = &uv->value;
  35. setnilvalue(uv->v);
  36. return uv;
  37. }
  38. UpVal *luaF_findupval (lua_State *L, StkId level) {
  39. GCObject **pp = &L->openupval;
  40. UpVal *p;
  41. UpVal *uv;
  42. while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
  43. if (p->v == level) return p;
  44. pp = &p->next;
  45. }
  46. uv = luaM_new(L, UpVal); /* not found: create a new one */
  47. uv->tt = LUA_TUPVAL;
  48. uv->marked = luaC_white(G(L));
  49. uv->v = level; /* current value lives in the stack */
  50. uv->next = *pp; /* chain it in the proper position */
  51. *pp = obj2gco(uv);
  52. return uv;
  53. }
  54. void luaF_close (lua_State *L, StkId level) {
  55. UpVal *uv;
  56. global_State *g = G(L);
  57. while ((uv = ngcotouv(L->openupval)) != NULL && uv->v >= level) {
  58. GCObject *o = obj2gco(uv);
  59. lua_assert(!isblack(o));
  60. L->openupval = uv->next; /* remove from `open' list */
  61. if (isdead(g, o))
  62. luaM_free(L, uv); /* free upvalue */
  63. else {
  64. setobj(L, &uv->value, uv->v);
  65. uv->v = &uv->value; /* now current value lives here */
  66. luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
  67. }
  68. }
  69. }
  70. Proto *luaF_newproto (lua_State *L) {
  71. Proto *f = luaM_new(L, Proto);
  72. luaC_link(L, obj2gco(f), LUA_TPROTO);
  73. f->k = NULL;
  74. f->sizek = 0;
  75. f->p = NULL;
  76. f->sizep = 0;
  77. f->code = NULL;
  78. f->sizecode = 0;
  79. f->sizelineinfo = 0;
  80. f->sizeupvalues = 0;
  81. f->nups = 0;
  82. f->upvalues = NULL;
  83. f->numparams = 0;
  84. f->is_vararg = 0;
  85. f->maxstacksize = 0;
  86. f->lineinfo = NULL;
  87. f->sizelocvars = 0;
  88. f->locvars = NULL;
  89. f->lineDefined = 0;
  90. f->source = NULL;
  91. return f;
  92. }
  93. void luaF_freeproto (lua_State *L, Proto *f) {
  94. luaM_freearray(L, f->code, f->sizecode, Instruction);
  95. luaM_freearray(L, f->p, f->sizep, Proto *);
  96. luaM_freearray(L, f->k, f->sizek, TValue);
  97. luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
  98. luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
  99. luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
  100. luaM_free(L, f);
  101. }
  102. void luaF_freeclosure (lua_State *L, Closure *c) {
  103. int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
  104. sizeLclosure(c->l.nupvalues);
  105. luaM_freemem(L, c, size);
  106. }
  107. /*
  108. ** Look for n-th local variable at line `line' in function `func'.
  109. ** Returns NULL if not found.
  110. */
  111. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  112. int i;
  113. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  114. if (pc < f->locvars[i].endpc) { /* is variable active? */
  115. local_number--;
  116. if (local_number == 0)
  117. return getstr(f->locvars[i].varname);
  118. }
  119. }
  120. return NULL; /* not found */
  121. }