9
3

lfunc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $
  3. ** Auxiliary functions to manipulate prototypes and closures
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lfunc_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include "lua.h"
  11. #include "lfunc.h"
  12. #include "lgc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. CClosure *luaF_newCclosure (lua_State *L, int n) {
  17. GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
  18. CClosure *c = gco2ccl(o);
  19. c->nupvalues = cast_byte(n);
  20. return c;
  21. }
  22. LClosure *luaF_newLclosure (lua_State *L, int n) {
  23. GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
  24. LClosure *c = gco2lcl(o);
  25. c->p = NULL;
  26. c->nupvalues = cast_byte(n);
  27. while (n--) c->upvals[n] = NULL;
  28. return c;
  29. }
  30. /*
  31. ** fill a closure with new closed upvalues
  32. */
  33. void luaF_initupvals (lua_State *L, LClosure *cl) {
  34. int i;
  35. for (i = 0; i < cl->nupvalues; i++) {
  36. UpVal *uv = luaM_new(L, UpVal);
  37. uv->refcount = 1;
  38. uv->v = &uv->u.value; /* make it closed */
  39. setnilvalue(uv->v);
  40. cl->upvals[i] = uv;
  41. }
  42. }
  43. UpVal *luaF_findupval (lua_State *L, StkId level) {
  44. UpVal **pp = &L->openupval;
  45. UpVal *p;
  46. UpVal *uv;
  47. lua_assert(isintwups(L) || L->openupval == NULL);
  48. while (*pp != NULL && (p = *pp)->v >= level) {
  49. lua_assert(upisopen(p));
  50. if (p->v == level) /* found a corresponding upvalue? */
  51. return p; /* return it */
  52. pp = &p->u.open.next;
  53. }
  54. /* not found: create a new upvalue */
  55. uv = luaM_new(L, UpVal);
  56. uv->refcount = 0;
  57. uv->u.open.next = *pp; /* link it to list of open upvalues */
  58. uv->u.open.touched = 1;
  59. *pp = uv;
  60. uv->v = level; /* current value lives in the stack */
  61. if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
  62. L->twups = G(L)->twups; /* link it to the list */
  63. G(L)->twups = L;
  64. }
  65. return uv;
  66. }
  67. void luaF_close (lua_State *L, StkId level) {
  68. UpVal *uv;
  69. while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
  70. lua_assert(upisopen(uv));
  71. L->openupval = uv->u.open.next; /* remove from 'open' list */
  72. if (uv->refcount == 0) /* no references? */
  73. luaM_free(L, uv); /* free upvalue */
  74. else {
  75. setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
  76. uv->v = &uv->u.value; /* now current value lives here */
  77. luaC_upvalbarrier(L, uv);
  78. }
  79. }
  80. }
  81. Proto *luaF_newproto (lua_State *L) {
  82. GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
  83. Proto *f = gco2p(o);
  84. f->k = NULL;
  85. f->sizek = 0;
  86. f->p = NULL;
  87. f->sizep = 0;
  88. f->code = NULL;
  89. f->cache = NULL;
  90. f->sizecode = 0;
  91. f->lineinfo = NULL;
  92. f->sizelineinfo = 0;
  93. f->upvalues = NULL;
  94. f->sizeupvalues = 0;
  95. f->numparams = 0;
  96. f->is_vararg = 0;
  97. f->maxstacksize = 0;
  98. f->locvars = NULL;
  99. f->sizelocvars = 0;
  100. f->linedefined = 0;
  101. f->lastlinedefined = 0;
  102. f->source = NULL;
  103. return f;
  104. }
  105. void luaF_freeproto (lua_State *L, Proto *f) {
  106. luaM_freearray(L, f->code, f->sizecode);
  107. luaM_freearray(L, f->p, f->sizep);
  108. luaM_freearray(L, f->k, f->sizek);
  109. luaM_freearray(L, f->lineinfo, f->sizelineinfo);
  110. luaM_freearray(L, f->locvars, f->sizelocvars);
  111. luaM_freearray(L, f->upvalues, f->sizeupvalues);
  112. luaM_free(L, f);
  113. }
  114. /*
  115. ** Look for n-th local variable at line 'line' in function 'func'.
  116. ** Returns NULL if not found.
  117. */
  118. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  119. int i;
  120. for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
  121. if (pc < f->locvars[i].endpc) { /* is variable active? */
  122. local_number--;
  123. if (local_number == 0)
  124. return getstr(f->locvars[i].varname);
  125. }
  126. }
  127. return NULL; /* not found */
  128. }