lfunc.c 2.0 KB

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