lstate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** $Id: lstate.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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 "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. static void stack_init (lua_State *L, lua_State *OL) {
  19. L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
  20. L->stacksize = BASIC_STACK_SIZE;
  21. L->top = L->stack + RESERVED_STACK_PREFIX;
  22. L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1;
  23. L->base_ci = luaM_newvector(OL, BASIC_CI_SIZE, CallInfo);
  24. L->ci = L->base_ci;
  25. L->ci->base = L->top;
  26. L->ci->pc = NULL;
  27. L->size_ci = BASIC_CI_SIZE;
  28. L->end_ci = L->base_ci + L->size_ci;
  29. }
  30. /*
  31. ** open parts that may cause memory-allocation errors
  32. */
  33. static void f_luaopen (lua_State *L, void *ud) {
  34. UNUSED(ud);
  35. /* create a new global state */
  36. L->_G = luaM_new(L, global_State);
  37. G(L)->strt.size = 0;
  38. G(L)->strt.nuse = 0;
  39. G(L)->strt.hash = NULL;
  40. G(L)->Mbuffer = NULL;
  41. G(L)->Mbuffsize = 0;
  42. G(L)->rootproto = NULL;
  43. G(L)->rootcl = NULL;
  44. G(L)->roottable = NULL;
  45. G(L)->rootupval = NULL;
  46. G(L)->rootudata = NULL;
  47. G(L)->tmudata = NULL;
  48. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  49. stack_init(L, L); /* init stack */
  50. /* create default meta table with a dummy table, and then close the loop */
  51. sethvalue(defaultmeta(L), NULL);
  52. sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
  53. hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
  54. sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
  55. sethvalue(registry(L), luaH_new(L, 0, 0)); /* registry */
  56. luaS_resize(L, 4); /* initial size of string table */
  57. luaT_init(L);
  58. luaX_init(L);
  59. G(L)->GCthreshold = 4*G(L)->nblocks;
  60. }
  61. static void preinit_state (lua_State *L) {
  62. L->stack = NULL;
  63. L->stacksize = 0;
  64. L->errorJmp = NULL;
  65. L->callhook = NULL;
  66. L->linehook = NULL;
  67. L->openupval = NULL;
  68. L->size_ci = 0;
  69. L->base_ci = NULL;
  70. L->allowhooks = 1;
  71. }
  72. LUA_API lua_State *lua_newthread (lua_State *OL) {
  73. lua_State *L;
  74. lua_lock(OL);
  75. L = luaM_new(OL, lua_State);
  76. preinit_state(L);
  77. L->_G = OL->_G;
  78. OL->next->previous = L; /* insert L into linked list */
  79. L->next = OL->next;
  80. OL->next = L;
  81. L->previous = OL;
  82. stack_init(L, OL); /* init stack */
  83. setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
  84. setobj(gt(L), gt(OL)); /* share table of globals */
  85. setobj(registry(L), registry(OL)); /* share registry */
  86. lua_unlock(OL);
  87. lua_userstateopen(L);
  88. return L;
  89. }
  90. LUA_API lua_State *lua_open (void) {
  91. lua_State *L;
  92. L = luaM_new(NULL, lua_State);
  93. if (L) { /* allocation OK? */
  94. preinit_state(L);
  95. L->_G = NULL;
  96. L->next = L->previous = L;
  97. if (luaD_runprotected(L, f_luaopen, NULL) != 0) {
  98. /* memory allocation error: free partial state */
  99. close_state(L);
  100. L = NULL;
  101. }
  102. }
  103. lua_userstateopen(L);
  104. return L;
  105. }
  106. void luaE_closethread (lua_State *OL, lua_State *L) {
  107. luaF_close(L, L->stack); /* close all upvalues for this thread */
  108. lua_assert(L->openupval == NULL);
  109. L->previous->next = L->next;
  110. L->next->previous = L->previous;
  111. luaM_freearray(OL, L->base_ci, L->size_ci, CallInfo);
  112. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  113. luaM_freelem(OL, L);
  114. }
  115. static void close_state (lua_State *L) {
  116. luaF_close(L, L->stack); /* close all upvalues for this thread */
  117. if (G(L)) { /* close global state */
  118. luaC_collect(L, 1); /* collect all elements */
  119. lua_assert(G(L)->rootproto == NULL);
  120. lua_assert(G(L)->rootudata == NULL);
  121. lua_assert(G(L)->rootcl == NULL);
  122. lua_assert(G(L)->rootupval == NULL);
  123. lua_assert(G(L)->roottable == NULL);
  124. luaS_freeall(L);
  125. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
  126. luaM_freelem(NULL, L->_G);
  127. }
  128. luaE_closethread(NULL, L);
  129. }
  130. LUA_API void lua_closethread (lua_State *L, lua_State *thread) {
  131. if (L == thread) lua_error(L, "cannot close only thread of a state");
  132. lua_lock(L);
  133. luaE_closethread(L, thread);
  134. lua_unlock(L);
  135. }
  136. LUA_API void lua_close (lua_State *L) {
  137. lua_lock(L);
  138. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  139. lua_assert(G(L)->tmudata == NULL);
  140. while (L->next != L) /* then, close all other threads */
  141. luaE_closethread(L, L->next);
  142. close_state(L);
  143. }