lfunc.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. ** $Id: lfunc.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $
  3. ** Lua Funcion auxiliar
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lfunc.h"
  8. #include "lmem.h"
  9. #include "lstate.h"
  10. #define gcsizeproto(p) 5
  11. #define gcsizeclosure(c) 1
  12. Closure *luaF_newclosure (int nelems)
  13. {
  14. Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
  15. luaO_insertlist(&(L->rootcl), (GCnode *)c);
  16. L->nblocks += gcsizeclosure(c);
  17. c->nelems = nelems;
  18. return c;
  19. }
  20. void luaF_simpleclosure (TObject *o)
  21. {
  22. Closure *c = luaF_newclosure(0);
  23. c->consts[0] = *o;
  24. ttype(o) = LUA_T_FUNCTION;
  25. clvalue(o) = c;
  26. }
  27. TProtoFunc *luaF_newproto (void)
  28. {
  29. TProtoFunc *f = luaM_new(TProtoFunc);
  30. f->code = NULL;
  31. f->lineDefined = 0;
  32. f->fileName = NULL;
  33. f->consts = NULL;
  34. f->nconsts = 0;
  35. f->locvars = NULL;
  36. luaO_insertlist(&(L->rootproto), (GCnode *)f);
  37. L->nblocks += gcsizeproto(f);
  38. return f;
  39. }
  40. static void freefunc (TProtoFunc *f)
  41. {
  42. luaM_free(f->code);
  43. luaM_free(f->locvars);
  44. luaM_free(f->consts);
  45. luaM_free(f);
  46. }
  47. void luaF_freeproto (TProtoFunc *l)
  48. {
  49. while (l) {
  50. TProtoFunc *next = (TProtoFunc *)l->head.next;
  51. L->nblocks -= gcsizeproto(l);
  52. freefunc(l);
  53. l = next;
  54. }
  55. }
  56. void luaF_freeclosure (Closure *l)
  57. {
  58. while (l) {
  59. Closure *next = (Closure *)l->head.next;
  60. L->nblocks -= gcsizeclosure(l);
  61. luaM_free(l);
  62. l = next;
  63. }
  64. }
  65. /*
  66. ** Look for n-esim local variable at line "line" in function "func".
  67. ** Returns NULL if not found.
  68. */
  69. char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
  70. {
  71. int count = 0;
  72. char *varname = NULL;
  73. LocVar *lv = func->locvars;
  74. if (lv == NULL)
  75. return NULL;
  76. for (; lv->line != -1 && lv->line < line; lv++) {
  77. if (lv->varname) { /* register */
  78. if (++count == local_number)
  79. varname = lv->varname->str;
  80. }
  81. else /* unregister */
  82. if (--count < local_number)
  83. varname = NULL;
  84. }
  85. return varname;
  86. }