lstate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** $Id: lstate.c,v 1.52 2001/01/22 18:01:38 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. luaS_init(L);
  76. luaX_init(L);
  77. luaT_init(L);
  78. G(L)->GCthreshold = 4*G(L)->nblocks;
  79. LUA_EXIT; /* temporary exit to use the API */
  80. lua_newtable(L);
  81. lua_ref(L, 1); /* create registry */
  82. lua_register(L, LUA_ERRORMESSAGE, errormessage);
  83. #ifdef LUA_DEBUG
  84. luaB_opentests(L);
  85. if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
  86. lua_assert(lua_gettop(L) == 0);
  87. #endif
  88. LUA_ENTRY; /* go back inside */
  89. }
  90. }
  91. LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
  92. struct Sopen so;
  93. lua_State *L;
  94. LUA_ENTRY;
  95. L = luaM_new(OL, lua_State);
  96. if (L) { /* allocation OK? */
  97. L->G = NULL;
  98. L->stack = NULL;
  99. L->stacksize = 0;
  100. L->errorJmp = NULL;
  101. L->callhook = NULL;
  102. L->linehook = NULL;
  103. L->allowhooks = 1;
  104. L->next = L->previous = L;
  105. so.stacksize = stacksize;
  106. so.L = OL;
  107. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  108. /* memory allocation error: free partial state */
  109. close_state(L);
  110. L = NULL;
  111. }
  112. }
  113. LUA_EXIT;
  114. return L;
  115. }
  116. static void close_state (lua_State *L) {
  117. lua_State *L1;
  118. L1 = L->next; /* any surviving thread (if there is one) */
  119. if (L1 == L) L1 = NULL; /* no surviving threads */
  120. if (L1 != NULL) { /* are there other threads? */
  121. lua_assert(L->previous != L);
  122. L->previous->next = L->next;
  123. L->next->previous = L->previous;
  124. }
  125. else if (G(L)) { /* last thread; close global state */
  126. luaC_collect(L, 1); /* collect all elements */
  127. lua_assert(G(L)->rootproto == NULL);
  128. lua_assert(G(L)->rootcl == NULL);
  129. lua_assert(G(L)->roottable == NULL);
  130. luaS_freeall(L);
  131. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  132. luaM_freearray(L, G(L)->refArray, G(L)->sizeref, struct Ref);
  133. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  134. luaM_freelem(NULL, L->G, global_State);
  135. }
  136. luaM_freearray(L1, L->stack, L->stacksize, TObject);
  137. luaM_freelem(L1, L, lua_State);
  138. }
  139. LUA_API void lua_close (lua_State *L) {
  140. lua_assert(L != lua_state || lua_gettop(L) == 0);
  141. LUA_ENTRY;
  142. close_state(L);
  143. LUA_EXIT;
  144. lua_assert(L != lua_state || memdebug_numblocks == 0);
  145. lua_assert(L != lua_state || memdebug_total == 0);
  146. }