lstate.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. ** $Id: lstate.c,v 1.9 1999/02/25 15:17:01 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include "lbuiltin.h"
  7. #include "ldo.h"
  8. #include "lfunc.h"
  9. #include "lgc.h"
  10. #include "llex.h"
  11. #include "lmem.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #include "ltable.h"
  15. #include "ltm.h"
  16. lua_State *lua_state = NULL;
  17. void lua_open (void)
  18. {
  19. if (lua_state) return;
  20. lua_state = luaM_new(lua_State);
  21. L->Cstack.base = 0;
  22. L->Cstack.lua2C = 0;
  23. L->Cstack.num = 0;
  24. L->errorJmp = NULL;
  25. L->Mbuffer = NULL;
  26. L->Mbuffbase = 0;
  27. L->Mbuffsize = 0;
  28. L->Mbuffnext = 0;
  29. L->numCblocks = 0;
  30. L->debug = 0;
  31. L->callhook = NULL;
  32. L->linehook = NULL;
  33. L->rootproto.next = NULL;
  34. L->rootproto.marked = 0;
  35. L->rootcl.next = NULL;
  36. L->rootcl.marked = 0;
  37. L->rootglobal.next = NULL;
  38. L->rootglobal.marked = 0;
  39. L->roottable.next = NULL;
  40. L->roottable.marked = 0;
  41. L->IMtable = NULL;
  42. L->refArray = NULL;
  43. L->refSize = 0;
  44. L->GCthreshold = GARBAGE_BLOCK;
  45. L->nblocks = 0;
  46. luaD_init();
  47. luaS_init();
  48. luaX_init();
  49. luaT_init();
  50. luaB_predefine();
  51. }
  52. void lua_close (void)
  53. {
  54. TaggedString *alludata = luaS_collectudata();
  55. L->GCthreshold = MAX_INT; /* to avoid GC during GC */
  56. luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
  57. luaC_strcallIM(alludata); /* GC tag methods for userdata */
  58. luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
  59. luaH_free((Hash *)L->roottable.next);
  60. luaF_freeproto((TProtoFunc *)L->rootproto.next);
  61. luaF_freeclosure((Closure *)L->rootcl.next);
  62. luaS_free(alludata);
  63. luaS_freeall();
  64. luaM_free(L->stack.stack);
  65. luaM_free(L->IMtable);
  66. luaM_free(L->refArray);
  67. luaM_free(L->Mbuffer);
  68. luaM_free(L);
  69. L = NULL;
  70. #ifdef DEBUG
  71. printf("total de blocos: %ld\n", numblocks);
  72. printf("total de memoria: %ld\n", totalmem);
  73. #endif
  74. }