1 /*
2 ** $Id$
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #include <stdio.h>
9 
10 #include "lua.h"
11 
12 #include "ldo.h"
13 #include "lgc.h"
14 #include "llex.h"
15 #include "lmem.h"
16 #include "lstate.h"
17 #include "lstring.h"
18 #include "ltable.h"
19 #include "ltm.h"
20 
21 
22 #ifdef LUA_DEBUG
23 static lua_State *lua_state = NULL;
24 void luaB_opentests (lua_State *L);
25 #endif
26 
27 
28 /*
29 ** built-in implementation for ERRORMESSAGE. In a "correct" environment
30 ** ERRORMESSAGE should have an external definition, and so this function
31 ** would not be used.
32 */
errormessage(lua_State * L)33 static int errormessage (lua_State *L) {
34   const char *s = lua_tostring(L, 1);
35   if (s == NULL) s = "(no message)";
36   fprintf(stderr, "error: %s\n", s);
37   return 0;
38 }
39 
40 
41 /*
42 ** open parts that may cause memory-allocation errors
43 */
f_luaopen(lua_State * L,void * ud)44 static void f_luaopen (lua_State *L, void *ud) {
45   int stacksize = *(int *)ud;
46   if (stacksize == 0)
47     stacksize = DEFAULT_STACK_SIZE;
48   else
49     stacksize += LUA_MINSTACK;
50   L->gt = luaH_new(L, 10);  /* table of globals */
51   luaD_init(L, stacksize);
52   luaS_init(L);
53   luaX_init(L);
54   luaT_init(L);
55   lua_newtable(L);
56   lua_ref(L, 1);  /* create registry */
57   lua_register(L, LUA_ERRORMESSAGE, errormessage);
58 #ifdef LUA_DEBUG
59   luaB_opentests(L);
60   if (lua_state == NULL) lua_state = L;  /* keep first state to be opened */
61 #endif
62   LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
63 }
64 
65 
lua_open(int stacksize)66 LUA_API lua_State *lua_open (int stacksize) {
67   lua_State *L = luaM_new(NULL, lua_State);
68   if (L == NULL) return NULL;  /* memory allocation error */
69   L->stack = NULL;
70   L->strt.size = L->udt.size = 0;
71   L->strt.nuse = L->udt.nuse = 0;
72   L->strt.hash = NULL;
73   L->udt.hash = NULL;
74   L->Mbuffer = NULL;
75   L->Mbuffsize = 0;
76   L->rootproto = NULL;
77   L->rootcl = NULL;
78   L->roottable = NULL;
79   L->TMtable = NULL;
80   L->last_tag = -1;
81   L->refArray = NULL;
82   L->refSize = 0;
83   L->refFree = NONEXT;
84   L->nblocks = sizeof(lua_State);
85   L->GCthreshold = MAX_INT;  /* to avoid GC during pre-definitions */
86   L->callhook = NULL;
87   L->linehook = NULL;
88   L->allowhooks = 1;
89   L->errorJmp = NULL;
90   if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
91     /* memory allocation error: free partial state */
92     lua_close(L);
93     return NULL;
94   }
95   L->GCthreshold = 2*L->nblocks;
96   return L;
97 }
98 
99 
lua_close(lua_State * L)100 LUA_API void lua_close (lua_State *L) {
101   LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
102   luaC_collect(L, 1);  /* collect all elements */
103   LUA_ASSERT(L->rootproto == NULL, "list should be empty");
104   LUA_ASSERT(L->rootcl == NULL, "list should be empty");
105   LUA_ASSERT(L->roottable == NULL, "list should be empty");
106   luaS_freeall(L);
107   if (L->stack)
108     L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
109   luaM_free(L, L->stack);
110   L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
111   luaM_free(L, L->TMtable);
112   L->nblocks -= (L->refSize)*sizeof(struct Ref);
113   luaM_free(L, L->refArray);
114   L->nblocks -= (L->Mbuffsize)*sizeof(char);
115   luaM_free(L, L->Mbuffer);
116   LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
117   luaM_free(L, L);
118   LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
119   LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
120 }
121 
122