diff options
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 60 |
1 files changed, 38 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 1.51 2001/01/19 13:20:30 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.52 2001/01/22 18:01:38 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,6 +22,7 @@ | |||
22 | #ifdef LUA_DEBUG | 22 | #ifdef LUA_DEBUG |
23 | static lua_State *lua_state = NULL; | 23 | static lua_State *lua_state = NULL; |
24 | void luaB_opentests (lua_State *L); | 24 | void luaB_opentests (lua_State *L); |
25 | int islocked = 0; | ||
25 | #endif | 26 | #endif |
26 | 27 | ||
27 | 28 | ||
@@ -44,6 +45,9 @@ struct Sopen { | |||
44 | }; | 45 | }; |
45 | 46 | ||
46 | 47 | ||
48 | static void close_state (lua_State *L); | ||
49 | |||
50 | |||
47 | /* | 51 | /* |
48 | ** open parts that may cause memory-allocation errors | 52 | ** open parts that may cause memory-allocation errors |
49 | */ | 53 | */ |
@@ -80,12 +84,13 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
80 | G(L)->sizeref = 0; | 84 | G(L)->sizeref = 0; |
81 | G(L)->refFree = NONEXT; | 85 | G(L)->refFree = NONEXT; |
82 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | 86 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); |
83 | G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | ||
84 | luaD_init(L, so->stacksize); /* init stack */ | 87 | luaD_init(L, so->stacksize); /* init stack */ |
85 | L->gt = luaH_new(L, 10); /* table of globals */ | 88 | L->gt = luaH_new(L, 10); /* table of globals */ |
86 | luaS_init(L); | 89 | luaS_init(L); |
87 | luaX_init(L); | 90 | luaX_init(L); |
88 | luaT_init(L); | 91 | luaT_init(L); |
92 | G(L)->GCthreshold = 4*G(L)->nblocks; | ||
93 | LUA_EXIT; /* temporary exit to use the API */ | ||
89 | lua_newtable(L); | 94 | lua_newtable(L); |
90 | lua_ref(L, 1); /* create registry */ | 95 | lua_ref(L, 1); /* create registry */ |
91 | lua_register(L, LUA_ERRORMESSAGE, errormessage); | 96 | lua_register(L, LUA_ERRORMESSAGE, errormessage); |
@@ -94,37 +99,41 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
94 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ | 99 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ |
95 | lua_assert(lua_gettop(L) == 0); | 100 | lua_assert(lua_gettop(L) == 0); |
96 | #endif | 101 | #endif |
97 | G(L)->GCthreshold = 2*G(L)->nblocks; | 102 | LUA_ENTRY; /* go back inside */ |
98 | } | 103 | } |
99 | } | 104 | } |
100 | 105 | ||
101 | 106 | ||
102 | LUA_API lua_State *lua_open (lua_State *OL, int stacksize) { | 107 | LUA_API lua_State *lua_open (lua_State *OL, int stacksize) { |
103 | struct Sopen so; | 108 | struct Sopen so; |
104 | lua_State *L = luaM_new(OL, lua_State); | 109 | lua_State *L; |
105 | if (L == NULL) return NULL; /* memory allocation error */ | 110 | LUA_ENTRY; |
106 | L->G = NULL; | 111 | L = luaM_new(OL, lua_State); |
107 | L->stack = NULL; | 112 | if (L) { /* allocation OK? */ |
108 | L->stacksize = 0; | 113 | L->G = NULL; |
109 | L->errorJmp = NULL; | 114 | L->stack = NULL; |
110 | L->callhook = NULL; | 115 | L->stacksize = 0; |
111 | L->linehook = NULL; | 116 | L->errorJmp = NULL; |
112 | L->allowhooks = 1; | 117 | L->callhook = NULL; |
113 | L->next = L->previous = L; | 118 | L->linehook = NULL; |
114 | so.stacksize = stacksize; | 119 | L->allowhooks = 1; |
115 | so.L = OL; | 120 | L->next = L->previous = L; |
116 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { | 121 | so.stacksize = stacksize; |
117 | /* memory allocation error: free partial state */ | 122 | so.L = OL; |
118 | lua_close(L); | 123 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { |
119 | return NULL; | 124 | /* memory allocation error: free partial state */ |
125 | close_state(L); | ||
126 | L = NULL; | ||
127 | } | ||
120 | } | 128 | } |
129 | LUA_EXIT; | ||
121 | return L; | 130 | return L; |
122 | } | 131 | } |
123 | 132 | ||
124 | 133 | ||
125 | LUA_API void lua_close (lua_State *L) { | 134 | static void close_state (lua_State *L) { |
126 | lua_State *L1 = L->next; /* any surviving thread (if there is one) */ | 135 | lua_State *L1; |
127 | lua_assert(L != lua_state || lua_gettop(L) == 0); | 136 | L1 = L->next; /* any surviving thread (if there is one) */ |
128 | if (L1 == L) L1 = NULL; /* no surviving threads */ | 137 | if (L1 == L) L1 = NULL; /* no surviving threads */ |
129 | if (L1 != NULL) { /* are there other threads? */ | 138 | if (L1 != NULL) { /* are there other threads? */ |
130 | lua_assert(L->previous != L); | 139 | lua_assert(L->previous != L); |
@@ -144,6 +153,13 @@ LUA_API void lua_close (lua_State *L) { | |||
144 | } | 153 | } |
145 | luaM_freearray(L1, L->stack, L->stacksize, TObject); | 154 | luaM_freearray(L1, L->stack, L->stacksize, TObject); |
146 | luaM_freelem(L1, L, lua_State); | 155 | luaM_freelem(L1, L, lua_State); |
156 | } | ||
157 | |||
158 | LUA_API void lua_close (lua_State *L) { | ||
159 | lua_assert(L != lua_state || lua_gettop(L) == 0); | ||
160 | LUA_ENTRY; | ||
161 | close_state(L); | ||
162 | LUA_EXIT; | ||
147 | lua_assert(L != lua_state || memdebug_numblocks == 0); | 163 | lua_assert(L != lua_state || memdebug_numblocks == 0); |
148 | lua_assert(L != lua_state || memdebug_total == 0); | 164 | lua_assert(L != lua_state || memdebug_total == 0); |
149 | } | 165 | } |