lfunc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. ** $Id: lfunc.c,v 1.20 2000/03/10 18:37:44 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 LUA_REENTRANT
  8. #include "lfunc.h"
  9. #include "lmem.h"
  10. #include "lstate.h"
  11. #define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
  12. #define gcsizeclosure(L, c) numblocks(L, c->nelems, sizeof(Closure))
  13. Closure *luaF_newclosure (lua_State *L, int nelems) {
  14. Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
  15. sizeof(TObject)*(nelems-1));
  16. c->next = L->rootcl;
  17. L->rootcl = c;
  18. c->marked = 0;
  19. c->nelems = nelems;
  20. L->nblocks += gcsizeclosure(L, c);
  21. return c;
  22. }
  23. Proto *luaF_newproto (lua_State *L) {
  24. Proto *f = luaM_new(L, Proto);
  25. f->code = NULL;
  26. f->lineDefined = 0;
  27. f->source = NULL;
  28. f->kstr = NULL;
  29. f->nkstr = 0;
  30. f->knum = NULL;
  31. f->nknum = 0;
  32. f->kproto = NULL;
  33. f->nkproto = 0;
  34. f->locvars = NULL;
  35. f->next = L->rootproto;
  36. L->rootproto = f;
  37. f->marked = 0;
  38. L->nblocks += gcsizeproto(L, f);
  39. return f;
  40. }
  41. void luaF_freeproto (lua_State *L, Proto *f) {
  42. L->nblocks -= gcsizeproto(L, f);
  43. luaM_free(L, f->code);
  44. luaM_free(L, f->locvars);
  45. luaM_free(L, f->kstr);
  46. luaM_free(L, f->knum);
  47. luaM_free(L, f->kproto);
  48. luaM_free(L, f);
  49. }
  50. void luaF_freeclosure (lua_State *L, Closure *c) {
  51. L->nblocks -= gcsizeclosure(L, c);
  52. luaM_free(L, c);
  53. }
  54. /*
  55. ** Look for n-th local variable at line `line' in function `func'.
  56. ** Returns NULL if not found.
  57. */
  58. const char *luaF_getlocalname (const Proto *func,
  59. int local_number, int line) {
  60. int count = 0;
  61. const char *varname = NULL;
  62. LocVar *lv = func->locvars;
  63. if (lv == NULL)
  64. return NULL;
  65. for (; lv->line != -1 && lv->line < line; lv++) {
  66. if (lv->varname) { /* register */
  67. if (++count == local_number)
  68. varname = lv->varname->str;
  69. }
  70. else /* unregister */
  71. if (--count < local_number)
  72. varname = NULL;
  73. }
  74. return varname;
  75. }