lstate.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** $Id: lstate.c,v 1.28 2000/06/30 14:35:17 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #define LUA_REENTRANT
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lbuiltin.h"
  11. #include "ldo.h"
  12. #include "lgc.h"
  13. #include "llex.h"
  14. #include "lmem.h"
  15. #include "lref.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. lua_State *lua_state = NULL;
  21. lua_State *lua_newstate (int stacksize, int put_builtin) {
  22. lua_State *L = luaM_new(NULL, lua_State);
  23. L->errorJmp = NULL;
  24. L->Mbuffer = NULL;
  25. L->Mbuffbase = 0;
  26. L->Mbuffsize = 0;
  27. L->Mbuffnext = 0;
  28. L->Cblocks = NULL;
  29. L->numCblocks = 0;
  30. L->rootproto = NULL;
  31. L->rootcl = NULL;
  32. L->roottable = NULL;
  33. L->IMtable = NULL;
  34. L->refArray = NULL;
  35. L->refSize = 0;
  36. L->refFree = NONEXT;
  37. L->nblocks = 0;
  38. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  39. L->debug = 0;
  40. L->callhook = NULL;
  41. L->linehook = NULL;
  42. L->allowhooks = 1;
  43. L->gt = luaH_new(L, 10);
  44. if (stacksize == 0) stacksize = DEFAULT_STACK_SIZE;
  45. luaD_init(L, stacksize);
  46. luaS_init(L);
  47. luaX_init(L);
  48. luaT_init(L);
  49. if (put_builtin)
  50. luaB_predefine(L);
  51. L->GCthreshold = L->nblocks*4;
  52. return L;
  53. }
  54. void lua_close (lua_State *L) {
  55. luaC_collect(L, 1); /* collect all elements */
  56. LUA_ASSERT(L->rootproto == NULL, "list should be empty");
  57. LUA_ASSERT(L->rootcl == NULL, "list should be empty");
  58. LUA_ASSERT(L->roottable == NULL, "list should be empty");
  59. luaS_freeall(L);
  60. luaM_free(L, L->stack);
  61. luaM_free(L, L->IMtable);
  62. luaM_free(L, L->refArray);
  63. luaM_free(L, L->Mbuffer);
  64. luaM_free(L, L->Cblocks);
  65. LUA_ASSERT(L->numCblocks == 0, "Cblocks still open");
  66. LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
  67. LUA_ASSERT(L->Cstack.base == L->top, "C2Lua not empty");
  68. luaM_free(L, L);
  69. if (L == lua_state) {
  70. LUA_ASSERT(memdebug_numblocks == 0, "memory leak!");
  71. LUA_ASSERT(memdebug_total == 0,"memory leak!");
  72. lua_state = NULL;
  73. }
  74. }