lstate.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. ** $Id: lstate.c,v 1.23 1999/12/21 18:04:41 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. #ifndef DEFAULT_STACK_SIZE
  19. #define DEFAULT_STACK_SIZE 1024
  20. #endif
  21. lua_State *lua_state = NULL;
  22. static lua_State *newstate_aux (int stacksize, int put_builtin) {
  23. lua_State *L = luaM_new(NULL, lua_State);
  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->rootproto = NULL;
  32. L->rootcl = NULL;
  33. L->rootglobal = NULL;
  34. L->roottable = NULL;
  35. L->IMtable = NULL;
  36. L->refArray = NULL;
  37. L->refSize = 0;
  38. L->refFree = NONEXT;
  39. L->nblocks = 0;
  40. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  41. L->debug = 0;
  42. L->callhook = NULL;
  43. L->linehook = NULL;
  44. L->allowhooks = 1;
  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. lua_State *lua_newstate (const char *s, ...) {
  55. static const char *const ops[] = {"stack", "builtin", NULL};
  56. va_list ap;
  57. int stacksize = DEFAULT_STACK_SIZE;
  58. int put_builtin = 1;
  59. va_start(ap, s);
  60. while (s) {
  61. switch (luaL_findstring(s, ops)) {
  62. case 0: /* stack */
  63. stacksize = va_arg(ap, int);
  64. break;
  65. case 1: /* builtin */
  66. put_builtin = va_arg(ap, int);
  67. break;
  68. default: /* invalid argument */
  69. va_end(ap);
  70. return NULL;
  71. }
  72. s = va_arg(ap, const char *);
  73. }
  74. va_end(ap);
  75. return newstate_aux(stacksize, put_builtin);
  76. }
  77. void lua_close (lua_State *L) {
  78. luaC_collect(L, 1); /* collect all elements */
  79. LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
  80. LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
  81. LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
  82. LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
  83. luaS_freeall(L);
  84. luaM_free(L, L->stack);
  85. luaM_free(L, L->IMtable);
  86. luaM_free(L, L->refArray);
  87. luaM_free(L, L->Mbuffer);
  88. luaM_free(L, L->Cblocks);
  89. LUA_ASSERT(L, L->numCblocks == 0, "Cblocks still open");
  90. LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
  91. LUA_ASSERT(L, L->Cstack.base == L->top, "C2Lua not empty");
  92. luaM_free(L, L);
  93. if (L == lua_state) {
  94. LUA_ASSERT(L, memdebug_numblocks == 0, "memory leak!");
  95. LUA_ASSERT(L, memdebug_total == 0,"memory leak!");
  96. lua_state = NULL;
  97. }
  98. }