lstate.c 4.8 KB

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