lstate.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. ** $Id: lstate.c,v 1.39 2000/09/12 18:42:32 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 DEBUG
  17. extern lua_State *lua_state;
  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. lua_State *lua_open (int stacksize) {
  32. struct lua_longjmp myErrorJmp;
  33. lua_State *L = luaM_new(NULL, lua_State);
  34. if (L == NULL) return NULL; /* memory allocation error */
  35. L->stack = NULL;
  36. L->strt.size = L->udt.size = 0;
  37. L->strt.nuse = L->udt.nuse = 0;
  38. L->strt.hash = NULL;
  39. L->udt.hash = NULL;
  40. L->Mbuffer = NULL;
  41. L->Mbuffsize = 0;
  42. L->rootproto = NULL;
  43. L->rootcl = NULL;
  44. L->roottable = NULL;
  45. L->IMtable = NULL;
  46. L->last_tag = -1;
  47. L->refArray = NULL;
  48. L->refSize = 0;
  49. L->refFree = NONEXT;
  50. L->nblocks = 0;
  51. L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
  52. L->callhook = NULL;
  53. L->linehook = NULL;
  54. L->allowhooks = 1;
  55. L->errorJmp = &myErrorJmp;
  56. if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
  57. L->gt = luaH_new(L, 10);
  58. luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
  59. stacksize+LUA_MINSTACK);
  60. luaS_init(L);
  61. luaX_init(L);
  62. luaT_init(L);
  63. lua_register(L, LUA_ERRORMESSAGE, errormessage);
  64. #ifdef DEBUG
  65. luaB_opentests(L);
  66. #endif
  67. L->GCthreshold = L->nblocks*4;
  68. L->errorJmp = NULL;
  69. return L;
  70. }
  71. else { /* memory allocation error: free partial state */
  72. lua_close(L);
  73. return NULL;
  74. }
  75. }
  76. void lua_close (lua_State *L) {
  77. luaC_collect(L, 1); /* collect all elements */
  78. LUA_ASSERT(L->rootproto == NULL, "list should be empty");
  79. LUA_ASSERT(L->rootcl == NULL, "list should be empty");
  80. LUA_ASSERT(L->roottable == NULL, "list should be empty");
  81. luaS_freeall(L);
  82. luaM_free(L, L->stack);
  83. luaM_free(L, L->IMtable);
  84. luaM_free(L, L->refArray);
  85. luaM_free(L, L->Mbuffer);
  86. LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
  87. luaM_free(L, L);
  88. LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
  89. LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
  90. }