lstate.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. ** $Id: lstate.c,v 1.24 2000/01/13 16:30:47 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #define LUA_REENTRANT
  8. #include "lauxlib.h"
  9. #include "lbuiltin.h"
  10. #include "ldo.h"
  11. #include "lgc.h"
  12. #include "llex.h"
  13. #include "lmem.h"
  14. #include "lref.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltm.h"
  18. lua_State *lua_state = NULL;
  19. static lua_State *newstate_aux (int stacksize, int put_builtin) {
  20. lua_State *L = luaM_new(NULL, lua_State);
  21. L->errorJmp = NULL;
  22. L->Mbuffer = NULL;
  23. L->Mbuffbase = 0;
  24. L->Mbuffsize = 0;
  25. L->Mbuffnext = 0;
  26. L->Cblocks = NULL;
  27. L->numCblocks = 0;
  28. L->rootproto = NULL;
  29. L->rootcl = NULL;
  30. L->rootglobal = NULL;
  31. L->roottable = NULL;
  32. L->IMtable = NULL;
  33. L->refArray = NULL;
  34. L->refSize = 0;
  35. L->refFree = NONEXT;
  36. L->nblocks = 0;
  37. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  38. L->debug = 0;
  39. L->callhook = NULL;
  40. L->linehook = NULL;
  41. L->allowhooks = 1;
  42. luaD_init(L, stacksize);
  43. luaS_init(L);
  44. luaX_init(L);
  45. luaT_init(L);
  46. if (put_builtin)
  47. luaB_predefine(L);
  48. L->GCthreshold = L->nblocks*4;
  49. return L;
  50. }
  51. lua_State *lua_newstate (const char *s, ...) {
  52. static const char *const ops[] = {"stack", "builtin", NULL};
  53. va_list ap;
  54. int stacksize = DEFAULT_STACK_SIZE;
  55. int put_builtin = 1;
  56. va_start(ap, s);
  57. while (s) {
  58. switch (luaL_findstring(s, ops)) {
  59. case 0: /* stack */
  60. stacksize = va_arg(ap, int);
  61. break;
  62. case 1: /* builtin */
  63. put_builtin = va_arg(ap, int);
  64. break;
  65. default: /* invalid argument */
  66. va_end(ap);
  67. return NULL;
  68. }
  69. s = va_arg(ap, const char *);
  70. }
  71. va_end(ap);
  72. return newstate_aux(stacksize, put_builtin);
  73. }
  74. void lua_close (lua_State *L) {
  75. luaC_collect(L, 1); /* collect all elements */
  76. LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
  77. LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
  78. LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
  79. LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
  80. luaS_freeall(L);
  81. luaM_free(L, L->stack);
  82. luaM_free(L, L->IMtable);
  83. luaM_free(L, L->refArray);
  84. luaM_free(L, L->Mbuffer);
  85. luaM_free(L, L->Cblocks);
  86. LUA_ASSERT(L, L->numCblocks == 0, "Cblocks still open");
  87. LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
  88. LUA_ASSERT(L, L->Cstack.base == L->top, "C2Lua not empty");
  89. luaM_free(L, L);
  90. if (L == lua_state) {
  91. LUA_ASSERT(L, memdebug_numblocks == 0, "memory leak!");
  92. LUA_ASSERT(L, memdebug_total == 0,"memory leak!");
  93. lua_state = NULL;
  94. }
  95. }