lfunc.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. ** $Id: lfunc.c,v 1.29 2000/08/09 19:16:57 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 "lmem.h"
  10. #include "lstate.h"
  11. #define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
  12. #define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure))
  13. Closure *luaF_newclosure (lua_State *L, int nelems) {
  14. Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
  15. (lint32)sizeof(TObject)*(nelems-1));
  16. c->next = L->rootcl;
  17. L->rootcl = c;
  18. c->mark = c;
  19. c->nupvalues = 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->lineinfo = NULL;
  27. f->lineDefined = 0;
  28. f->source = NULL;
  29. f->kstr = NULL;
  30. f->nkstr = 0;
  31. f->knum = NULL;
  32. f->nknum = 0;
  33. f->kproto = NULL;
  34. f->nkproto = 0;
  35. f->locvars = NULL;
  36. f->nlocvars = 0;
  37. f->next = L->rootproto;
  38. L->rootproto = f;
  39. f->marked = 0;
  40. L->nblocks += gcsizeproto(L, f);
  41. return f;
  42. }
  43. void luaF_freeproto (lua_State *L, Proto *f) {
  44. L->nblocks -= gcsizeproto(L, f);
  45. luaM_free(L, f->code);
  46. luaM_free(L, f->locvars);
  47. luaM_free(L, f->kstr);
  48. luaM_free(L, f->knum);
  49. luaM_free(L, f->kproto);
  50. luaM_free(L, f->lineinfo);
  51. luaM_free(L, f);
  52. }
  53. void luaF_freeclosure (lua_State *L, Closure *c) {
  54. L->nblocks -= gcsizeclosure(L, c);
  55. luaM_free(L, c);
  56. }
  57. /*
  58. ** Look for n-th local variable at line `line' in function `func'.
  59. ** Returns NULL if not found.
  60. */
  61. const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
  62. int i;
  63. for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) {
  64. if (pc < f->locvars[i].endpc) { /* is variable active? */
  65. local_number--;
  66. if (local_number == 0)
  67. return f->locvars[i].varname->str;
  68. }
  69. }
  70. return NULL; /* not found */
  71. }