lfunc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. ** $Id: lfunc.c,v 1.24 2000/06/12 13:52:05 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 "lua.h"
  9. #include "lfunc.h"
  10. #include "lmem.h"
  11. #include "lstate.h"
  12. #define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
  13. #define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure))
  14. Closure *luaF_newclosure (lua_State *L, int nelems) {
  15. Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
  16. (lint32)sizeof(TObject)*(nelems-1));
  17. c->next = L->rootcl;
  18. L->rootcl = c;
  19. c->marked = 0;
  20. c->nupvalues = nelems;
  21. L->nblocks += gcsizeclosure(L, c);
  22. return c;
  23. }
  24. Proto *luaF_newproto (lua_State *L) {
  25. Proto *f = luaM_new(L, Proto);
  26. f->code = NULL;
  27. f->lines = NULL;
  28. f->lineDefined = 0;
  29. f->source = NULL;
  30. f->kstr = NULL;
  31. f->nkstr = 0;
  32. f->knum = NULL;
  33. f->nknum = 0;
  34. f->kproto = NULL;
  35. f->nkproto = 0;
  36. f->locvars = NULL;
  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->lines);
  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 *func, int local_number, int pc) {
  62. int count = 0;
  63. const char *varname = NULL;
  64. LocVar *lv = func->locvars;
  65. if (lv == NULL)
  66. return NULL;
  67. for (; lv->pc != -1 && lv->pc <= pc; lv++) {
  68. if (lv->varname) { /* register */
  69. if (++count == local_number)
  70. varname = lv->varname->str;
  71. }
  72. else /* unregister */
  73. if (--count < local_number)
  74. varname = NULL;
  75. }
  76. return varname;
  77. }