lfunc.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ** $Id: $
  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. TProtoFunc *luaF_root = NULL;
  10. Closure *luaF_rootcl = NULL;
  11. static void luaI_insertfunction (TProtoFunc *f)
  12. {
  13. ++luaO_nentities;
  14. f->head.next = (GCnode *)luaF_root;
  15. luaF_root = f;
  16. f->head.marked = 0;
  17. }
  18. Closure *luaF_newclosure (int nelems)
  19. {
  20. Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
  21. ++luaO_nentities;
  22. c->head.next = (GCnode *)luaF_rootcl;
  23. luaF_rootcl = c;
  24. c->head.marked = 0;
  25. return 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->nupvalues = 0;
  36. f->locvars = NULL;
  37. luaI_insertfunction(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. freefunc(l);
  52. l = next;
  53. }
  54. }
  55. void luaF_freeclosure (Closure *l)
  56. {
  57. while (l) {
  58. Closure *next = (Closure *)l->head.next;
  59. luaM_free(l);
  60. l = next;
  61. }
  62. }
  63. /*
  64. ** Look for n-esim local variable at line "line" in function "func".
  65. ** Returns NULL if not found.
  66. */
  67. char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
  68. {
  69. int count = 0;
  70. char *varname = NULL;
  71. LocVar *lv = func->locvars;
  72. if (lv == NULL)
  73. return NULL;
  74. for (; lv->line != -1 && lv->line < line; lv++) {
  75. if (lv->varname) { /* register */
  76. if (++count == local_number)
  77. varname = lv->varname->str;
  78. }
  79. else /* unregister */
  80. if (--count < local_number)
  81. varname = NULL;
  82. }
  83. return varname;
  84. }