func.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <string.h>
  2. #include "luadebug.h"
  3. #include "table.h"
  4. #include "luamem.h"
  5. #include "func.h"
  6. #include "opcode.h"
  7. #include "inout.h"
  8. static TFunc *function_root = NULL;
  9. static void luaI_insertfunction (TFunc *f)
  10. {
  11. lua_pack();
  12. f->next = function_root;
  13. function_root = f;
  14. f->marked = 0;
  15. }
  16. /*
  17. ** Initialize TFunc struct
  18. */
  19. void luaI_initTFunc (TFunc *f)
  20. {
  21. f->next = NULL;
  22. f->marked = 0;
  23. f->code = NULL;
  24. f->lineDefined = 0;
  25. f->fileName = lua_parsedfile;
  26. f->consts = NULL;
  27. f->nconsts = 0;
  28. f->locvars = NULL;
  29. luaI_insertfunction(f);
  30. }
  31. /*
  32. ** Free function
  33. */
  34. static void luaI_freefunc (TFunc *f)
  35. {
  36. luaI_free(f->code);
  37. luaI_free(f->locvars);
  38. luaI_free(f->consts);
  39. luaI_free(f);
  40. }
  41. void luaI_funcfree (TFunc *l)
  42. {
  43. while (l) {
  44. TFunc *next = l->next;
  45. luaI_freefunc(l);
  46. l = next;
  47. }
  48. }
  49. void luaI_funcmark (TFunc *f)
  50. {
  51. f->marked = 1;
  52. if (!f->fileName->marked)
  53. f->fileName->marked = 1;
  54. if (f->consts) {
  55. int i;
  56. for (i=0; i<f->nconsts; i++)
  57. lua_markobject(&f->consts[i]);
  58. }
  59. }
  60. /*
  61. ** Garbage collection function.
  62. */
  63. TFunc *luaI_funccollector (long *acum)
  64. {
  65. TFunc *curr = function_root;
  66. TFunc *prev = NULL;
  67. TFunc *frees = NULL;
  68. long counter = 0;
  69. while (curr) {
  70. TFunc *next = curr->next;
  71. if (!curr->marked) {
  72. if (prev == NULL)
  73. function_root = next;
  74. else
  75. prev->next = next;
  76. curr->next = frees;
  77. frees = curr;
  78. ++counter;
  79. }
  80. else {
  81. curr->marked = 0;
  82. prev = curr;
  83. }
  84. curr = next;
  85. }
  86. *acum += counter;
  87. return frees;
  88. }
  89. void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
  90. {
  91. TObject *f = luaI_Address(func);
  92. if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
  93. {
  94. *filename = f->value.tf->fileName->str;
  95. *linedefined = f->value.tf->lineDefined;
  96. }
  97. else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
  98. {
  99. *filename = "(C)";
  100. *linedefined = -1;
  101. }
  102. }
  103. /*
  104. ** Look for n-esim local variable at line "line" in function "func".
  105. ** Returns NULL if not found.
  106. */
  107. char *luaI_getlocalname (TFunc *func, int local_number, int line)
  108. {
  109. int count = 0;
  110. char *varname = NULL;
  111. LocVar *lv = func->locvars;
  112. if (lv == NULL)
  113. return NULL;
  114. for (; lv->line != -1 && lv->line < line; lv++)
  115. {
  116. if (lv->varname) /* register */
  117. {
  118. if (++count == local_number)
  119. varname = lv->varname->str;
  120. }
  121. else /* unregister */
  122. if (--count < local_number)
  123. varname = NULL;
  124. }
  125. return varname;
  126. }