lstate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ** $Id: lstate.c,v 1.47 2000/10/26 12:47:05 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include "lua.h"
  8. #include "ldo.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. #ifdef LUA_DEBUG
  17. static lua_State *lua_state = NULL;
  18. void luaB_opentests (lua_State *L);
  19. #endif
  20. /*
  21. ** built-in implementation for ERRORMESSAGE. In a "correct" environment
  22. ** ERRORMESSAGE should have an external definition, and so this function
  23. ** would not be used.
  24. */
  25. static int errormessage (lua_State *L) {
  26. const char *s = lua_tostring(L, 1);
  27. if (s == NULL) s = "(no message)";
  28. fprintf(stderr, "error: %s\n", s);
  29. return 0;
  30. }
  31. /*
  32. ** open parts that may cause memory-allocation errors
  33. */
  34. static void f_luaopen (lua_State *L, void *ud) {
  35. int stacksize = *(int *)ud;
  36. if (stacksize == 0)
  37. stacksize = DEFAULT_STACK_SIZE;
  38. else
  39. stacksize += LUA_MINSTACK;
  40. L->gt = luaH_new(L, 10); /* table of globals */
  41. luaD_init(L, stacksize);
  42. luaS_init(L);
  43. luaX_init(L);
  44. luaT_init(L);
  45. lua_newtable(L);
  46. lua_ref(L, 1); /* create registry */
  47. lua_register(L, LUA_ERRORMESSAGE, errormessage);
  48. #ifdef LUA_DEBUG
  49. luaB_opentests(L);
  50. if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
  51. #endif
  52. LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
  53. }
  54. LUA_API lua_State *lua_open (int stacksize) {
  55. lua_State *L = luaM_new(NULL, lua_State);
  56. if (L == NULL) return NULL; /* memory allocation error */
  57. L->stack = NULL;
  58. L->strt.size = L->udt.size = 0;
  59. L->strt.nuse = L->udt.nuse = 0;
  60. L->strt.hash = NULL;
  61. L->udt.hash = NULL;
  62. L->Mbuffer = NULL;
  63. L->Mbuffsize = 0;
  64. L->rootproto = NULL;
  65. L->rootcl = NULL;
  66. L->roottable = NULL;
  67. L->TMtable = NULL;
  68. L->last_tag = -1;
  69. L->refArray = NULL;
  70. L->refSize = 0;
  71. L->refFree = NONEXT;
  72. L->nblocks = sizeof(lua_State);
  73. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  74. L->callhook = NULL;
  75. L->linehook = NULL;
  76. L->allowhooks = 1;
  77. L->errorJmp = NULL;
  78. if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
  79. /* memory allocation error: free partial state */
  80. lua_close(L);
  81. return NULL;
  82. }
  83. L->GCthreshold = 2*L->nblocks;
  84. return L;
  85. }
  86. LUA_API void lua_close (lua_State *L) {
  87. LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
  88. luaC_collect(L, 1); /* collect all elements */
  89. LUA_ASSERT(L->rootproto == NULL, "list should be empty");
  90. LUA_ASSERT(L->rootcl == NULL, "list should be empty");
  91. LUA_ASSERT(L->roottable == NULL, "list should be empty");
  92. luaS_freeall(L);
  93. if (L->stack)
  94. L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
  95. luaM_free(L, L->stack);
  96. L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
  97. luaM_free(L, L->TMtable);
  98. L->nblocks -= (L->refSize)*sizeof(struct Ref);
  99. luaM_free(L, L->refArray);
  100. L->nblocks -= (L->Mbuffsize)*sizeof(char);
  101. luaM_free(L, L->Mbuffer);
  102. LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
  103. luaM_free(L, L);
  104. LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
  105. LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
  106. }