func.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include "luadebug.h"
  3. #include "table.h"
  4. #include "mem.h"
  5. #include "func.h"
  6. #include "opcode.h"
  7. static TFunc *function_root = NULL;
  8. /*
  9. ** Insert function in list for GC
  10. */
  11. void luaI_insertfunction (TFunc *f)
  12. {
  13. lua_pack();
  14. f->next = function_root;
  15. function_root = f;
  16. f->marked = 0;
  17. }
  18. /*
  19. ** Free function
  20. */
  21. static void freefunc (TFunc *f)
  22. {
  23. luaI_free (f->code);
  24. luaI_free (f);
  25. }
  26. /*
  27. ** Garbage collection function.
  28. ** This function traverse the function list freeing unindexed functions
  29. */
  30. Long luaI_funccollector (void)
  31. {
  32. TFunc *curr = function_root;
  33. TFunc *prev = NULL;
  34. Long counter = 0;
  35. while (curr)
  36. {
  37. TFunc *next = curr->next;
  38. if (!curr->marked)
  39. {
  40. if (prev == NULL)
  41. function_root = next;
  42. else
  43. prev->next = next;
  44. freefunc (curr);
  45. ++counter;
  46. }
  47. else
  48. {
  49. curr->marked = 0;
  50. prev = curr;
  51. }
  52. curr = next;
  53. }
  54. return counter;
  55. }
  56. void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
  57. {
  58. Object *f = luaI_Address(func);
  59. if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
  60. {
  61. *filename = f->value.tf->fileName;
  62. *linedefined = f->value.tf->lineDefined;
  63. }
  64. else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)
  65. {
  66. *filename = "(C)";
  67. *linedefined = -1;
  68. }
  69. }