lstate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ** $Id: lstate.c,v 1.54 2001/01/25 16:45:36 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. int islocked = 0;
  20. #endif
  21. /*
  22. ** built-in implementation for ERRORMESSAGE. In a "correct" environment
  23. ** ERRORMESSAGE should have an external definition, and so this function
  24. ** would not be used.
  25. */
  26. static int errormessage (lua_State *L) {
  27. const char *s = lua_tostring(L, 1);
  28. if (s == NULL) s = "(no message)";
  29. fprintf(stderr, "error: %s\n", s);
  30. return 0;
  31. }
  32. struct Sopen {
  33. int stacksize;
  34. lua_State *L;
  35. };
  36. static void close_state (lua_State *L);
  37. /*
  38. ** open parts that may cause memory-allocation errors
  39. */
  40. static void f_luaopen (lua_State *L, void *ud) {
  41. struct Sopen *so = (struct Sopen *)ud;
  42. if (so->stacksize == 0)
  43. so->stacksize = DEFAULT_STACK_SIZE;
  44. else
  45. so->stacksize += LUA_MINSTACK;
  46. if (so->L != NULL) { /* shared global state? */
  47. L->G = G(so->L);
  48. L->gt = so->L->gt; /* share table of globals */
  49. so->L->next->previous = L; /* insert L into linked list */
  50. L->next = so->L->next;
  51. so->L->next = L;
  52. L->previous = so->L;
  53. luaD_init(L, so->stacksize); /* init stack */
  54. }
  55. else { /* create a new global state */
  56. L->G = luaM_new(L, global_State);
  57. G(L)->strt.size = G(L)->udt.size = 0;
  58. G(L)->strt.nuse = G(L)->udt.nuse = 0;
  59. G(L)->strt.hash = G(L)->udt.hash = NULL;
  60. G(L)->Mbuffer = NULL;
  61. G(L)->Mbuffsize = 0;
  62. G(L)->rootproto = NULL;
  63. G(L)->rootcl = NULL;
  64. G(L)->roottable = NULL;
  65. G(L)->TMtable = NULL;
  66. G(L)->sizeTM = 0;
  67. G(L)->ntag = 0;
  68. G(L)->refArray = NULL;
  69. G(L)->nref = 0;
  70. G(L)->sizeref = 0;
  71. G(L)->refFree = NONEXT;
  72. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  73. luaD_init(L, so->stacksize); /* init stack */
  74. L->gt = luaH_new(L, 10); /* table of globals */
  75. G(L)->type2tag = luaH_new(L, 10);
  76. luaS_init(L);
  77. luaX_init(L);
  78. luaT_init(L);
  79. G(L)->GCthreshold = 4*G(L)->nblocks;
  80. LUA_UNLOCK; /* temporary exit to use the API */
  81. lua_newtable(L);
  82. lua_ref(L, 1); /* create registry */
  83. lua_register(L, LUA_ERRORMESSAGE, errormessage);
  84. #ifdef LUA_DEBUG
  85. luaB_opentests(L);
  86. if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
  87. lua_assert(lua_gettop(L) == 0);
  88. #endif
  89. LUA_LOCK; /* go back inside */
  90. }
  91. }
  92. LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
  93. struct Sopen so;
  94. lua_State *L;
  95. LUA_LOCK;
  96. L = luaM_new(OL, lua_State);
  97. if (L) { /* allocation OK? */
  98. L->G = NULL;
  99. L->stack = NULL;
  100. L->stacksize = 0;
  101. L->errorJmp = NULL;
  102. L->callhook = NULL;
  103. L->linehook = NULL;
  104. L->allowhooks = 1;
  105. L->next = L->previous = L;
  106. so.stacksize = stacksize;
  107. so.L = OL;
  108. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  109. /* memory allocation error: free partial state */
  110. close_state(L);
  111. L = NULL;
  112. }
  113. }
  114. LUA_UNLOCK;
  115. return L;
  116. }
  117. static void close_state (lua_State *L) {
  118. lua_State *L1;
  119. L1 = L->next; /* any surviving thread (if there is one) */
  120. if (L1 == L) L1 = NULL; /* no surviving threads */
  121. if (L1 != NULL) { /* are there other threads? */
  122. lua_assert(L->previous != L);
  123. L->previous->next = L->next;
  124. L->next->previous = L->previous;
  125. }
  126. else if (G(L)) { /* last thread; close global state */
  127. luaC_collect(L, 1); /* collect all elements */
  128. lua_assert(G(L)->rootproto == NULL);
  129. lua_assert(G(L)->rootcl == NULL);
  130. lua_assert(G(L)->roottable == NULL);
  131. luaS_freeall(L);
  132. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  133. luaM_freearray(L, G(L)->refArray, G(L)->sizeref, struct Ref);
  134. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  135. luaM_freelem(NULL, L->G, global_State);
  136. }
  137. luaM_freearray(L1, L->stack, L->stacksize, TObject);
  138. luaM_freelem(L1, L, lua_State);
  139. }
  140. LUA_API void lua_close (lua_State *L) {
  141. lua_assert(L != lua_state || lua_gettop(L) == 0);
  142. LUA_LOCK;
  143. close_state(L);
  144. LUA_UNLOCK;
  145. lua_assert(L != lua_state || memdebug_numblocks == 0);
  146. lua_assert(L != lua_state || memdebug_total == 0);
  147. }