lstate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. ** $Id: lstate.c,v 1.102 2002/08/06 15:32:22 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include "lua.h"
  7. #include "ldebug.h"
  8. #include "ldo.h"
  9. #include "lfunc.h"
  10. #include "lgc.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "ltable.h"
  16. #include "ltm.h"
  17. static void close_state (lua_State *L);
  18. /*
  19. ** you can change this function through the official API
  20. ** call `lua_setpanicf'
  21. */
  22. static int default_panic (lua_State *L) {
  23. UNUSED(L);
  24. return 0;
  25. }
  26. static void stack_init (lua_State *L, lua_State *OL) {
  27. L->stack = luaM_newvector(OL, BASIC_STACK_SIZE + EXTRA_STACK, TObject);
  28. L->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  29. L->top = L->stack;
  30. L->stack_last = L->stack+(L->stacksize - EXTRA_STACK)-1;
  31. L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo);
  32. L->ci = L->base_ci;
  33. L->ci->state = CI_C; /* not a Lua function */
  34. setnilvalue(L->top++); /* `function' entry for this `ci' */
  35. L->ci->base = L->top;
  36. L->ci->top = L->top + LUA_MINSTACK;
  37. L->size_ci = BASIC_CI_SIZE;
  38. L->end_ci = L->base_ci + L->size_ci;
  39. }
  40. /*
  41. ** open parts that may cause memory-allocation errors
  42. */
  43. static void f_luaopen (lua_State *L, void *ud) {
  44. UNUSED(ud);
  45. /* create a new global state */
  46. L->l_G = luaM_new(L, global_State);
  47. G(L)->GCthreshold = 0; /* mark it as unfinished state */
  48. G(L)->strt.size = 0;
  49. G(L)->strt.nuse = 0;
  50. G(L)->strt.hash = NULL;
  51. G(L)->Mbuffer = NULL;
  52. G(L)->Mbuffsize = 0;
  53. G(L)->panic = &default_panic;
  54. G(L)->rootproto = NULL;
  55. G(L)->rootcl = NULL;
  56. G(L)->roottable = NULL;
  57. G(L)->rootupval = NULL;
  58. G(L)->rootudata = NULL;
  59. G(L)->tmudata = NULL;
  60. setnilvalue(key(G(L)->dummynode));
  61. setnilvalue(val(G(L)->dummynode));
  62. G(L)->dummynode->next = NULL;
  63. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  64. stack_init(L, L); /* init stack */
  65. /* create default meta table with a dummy table, and then close the loop */
  66. sethvalue(defaultmeta(L), NULL);
  67. sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
  68. hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
  69. sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
  70. sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */
  71. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  72. luaT_init(L);
  73. luaX_init(L);
  74. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  75. G(L)->GCthreshold = 4*G(L)->nblocks;
  76. }
  77. static void preinit_state (lua_State *L) {
  78. L->stack = NULL;
  79. L->stacksize = 0;
  80. L->errorJmp = NULL;
  81. L->hook = NULL;
  82. L->hookmask = 0;
  83. setallowhook(L, 1);
  84. resethookcount(L);
  85. L->openupval = NULL;
  86. L->size_ci = 0;
  87. L->base_ci = L->ci = NULL;
  88. L->errfunc = 0;
  89. setnilvalue(defaultmeta(L));
  90. setnilvalue(gt(L));
  91. setnilvalue(registry(L));
  92. }
  93. LUA_API lua_State *lua_newthread (lua_State *OL) {
  94. lua_State *L;
  95. lua_lock(OL);
  96. L = luaM_new(OL, lua_State);
  97. preinit_state(L);
  98. L->l_G = OL->l_G;
  99. OL->next->previous = L; /* insert L into linked list */
  100. L->next = OL->next;
  101. OL->next = L;
  102. L->previous = OL;
  103. stack_init(L, OL); /* init stack */
  104. setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
  105. setobj(gt(L), gt(OL)); /* share table of globals */
  106. setobj(registry(L), registry(OL)); /* share registry */
  107. lua_unlock(OL);
  108. lua_userstateopen(L);
  109. return L;
  110. }
  111. LUA_API lua_State *lua_open (void) {
  112. lua_State *L;
  113. L = luaM_new(NULL, lua_State);
  114. if (L) { /* allocation OK? */
  115. preinit_state(L);
  116. L->l_G = NULL;
  117. L->next = L->previous = L;
  118. if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  119. /* memory allocation error: free partial state */
  120. close_state(L);
  121. L = NULL;
  122. }
  123. }
  124. lua_userstateopen(L);
  125. return L;
  126. }
  127. void luaE_closethread (lua_State *OL, lua_State *L) {
  128. luaF_close(L, L->stack); /* close all upvalues for this thread */
  129. lua_assert(L->openupval == NULL);
  130. L->previous->next = L->next;
  131. L->next->previous = L->previous;
  132. luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
  133. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  134. luaM_freelem(OL, L);
  135. }
  136. static void close_state (lua_State *L) {
  137. luaF_close(L, L->stack); /* close all upvalues for this thread */
  138. if (G(L)) { /* close global state */
  139. luaC_collect(L, 1); /* collect all elements */
  140. lua_assert(G(L)->rootproto == NULL);
  141. lua_assert(G(L)->rootudata == NULL);
  142. lua_assert(G(L)->rootcl == NULL);
  143. lua_assert(G(L)->rootupval == NULL);
  144. lua_assert(G(L)->roottable == NULL);
  145. luaS_freeall(L);
  146. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  147. luaM_freelem(NULL, L->l_G);
  148. }
  149. luaE_closethread(NULL, L);
  150. }
  151. LUA_API void lua_closethread (lua_State *L, lua_State *thread) {
  152. lua_lock(L);
  153. if (L == thread) luaG_runerror(L, "cannot close only thread of a state");
  154. luaE_closethread(L, thread);
  155. lua_unlock(L);
  156. }
  157. LUA_API void lua_close (lua_State *L) {
  158. lua_lock(L);
  159. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  160. lua_assert(G(L)->tmudata == NULL);
  161. while (L->next != L) /* then, close all other threads */
  162. luaE_closethread(L, L->next);
  163. close_state(L);
  164. }