lstate.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** $Id: lstate.c,v 1.11 1999/05/11 14:19:32 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->Cblocks = NULL;
  30. L->numCblocks = 0;
  31. L->debug = 0;
  32. L->callhook = NULL;
  33. L->linehook = NULL;
  34. L->rootproto.next = NULL;
  35. L->rootproto.marked = 0;
  36. L->rootcl.next = NULL;
  37. L->rootcl.marked = 0;
  38. L->rootglobal.next = NULL;
  39. L->rootglobal.marked = 0;
  40. L->roottable.next = NULL;
  41. L->roottable.marked = 0;
  42. L->IMtable = NULL;
  43. L->refArray = NULL;
  44. L->refSize = 0;
  45. L->GCthreshold = GARBAGE_BLOCK;
  46. L->nblocks = 0;
  47. luaD_init();
  48. luaS_init();
  49. luaX_init();
  50. luaT_init();
  51. luaB_predefine();
  52. }
  53. void lua_close (void)
  54. {
  55. TaggedString *alludata = luaS_collectudata();
  56. L->GCthreshold = MAX_INT; /* to avoid GC during GC */
  57. luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
  58. luaC_strcallIM(alludata); /* GC tag methods for userdata */
  59. luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
  60. luaH_free((Hash *)L->roottable.next);
  61. luaF_freeproto((TProtoFunc *)L->rootproto.next);
  62. luaF_freeclosure((Closure *)L->rootcl.next);
  63. luaS_free(alludata);
  64. luaS_freeall();
  65. luaM_free(L->stack.stack);
  66. luaM_free(L->IMtable);
  67. luaM_free(L->refArray);
  68. luaM_free(L->Mbuffer);
  69. luaM_free(L->Cblocks);
  70. luaM_free(L);
  71. L = NULL;
  72. #ifdef DEBUG
  73. printf("total de blocos: %ld\n", numblocks);
  74. printf("total de memoria: %ld\n", totalmem);
  75. #endif
  76. }