diff options
Diffstat (limited to 'lstate.c')
| -rw-r--r-- | lstate.c | 77 |
1 files changed, 41 insertions, 36 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 1.49 2000/12/26 18:46:09 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.50 2000/12/28 12:55:41 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 | */ |
| @@ -47,6 +47,24 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 47 | stacksize = DEFAULT_STACK_SIZE; | 47 | stacksize = DEFAULT_STACK_SIZE; |
| 48 | else | 48 | else |
| 49 | stacksize += LUA_MINSTACK; | 49 | stacksize += LUA_MINSTACK; |
| 50 | L->G = luaM_new(L, global_State); | ||
| 51 | G(L)->strt.size = G(L)->udt.size = 0; | ||
| 52 | G(L)->strt.nuse = G(L)->udt.nuse = 0; | ||
| 53 | G(L)->strt.hash = G(L)->udt.hash = NULL; | ||
| 54 | G(L)->Mbuffer = NULL; | ||
| 55 | G(L)->Mbuffsize = 0; | ||
| 56 | G(L)->rootproto = NULL; | ||
| 57 | G(L)->rootcl = NULL; | ||
| 58 | G(L)->roottable = NULL; | ||
| 59 | G(L)->TMtable = NULL; | ||
| 60 | G(L)->sizeTM = 0; | ||
| 61 | G(L)->ntag = 0; | ||
| 62 | G(L)->refArray = NULL; | ||
| 63 | G(L)->nref = 0; | ||
| 64 | G(L)->sizeref = 0; | ||
| 65 | G(L)->refFree = NONEXT; | ||
| 66 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | ||
| 67 | G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | ||
| 50 | L->gt = luaH_new(L, 10); /* table of globals */ | 68 | L->gt = luaH_new(L, 10); /* table of globals */ |
| 51 | luaD_init(L, stacksize); | 69 | luaD_init(L, stacksize); |
| 52 | luaS_init(L); | 70 | luaS_init(L); |
| @@ -58,61 +76,48 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 58 | #ifdef LUA_DEBUG | 76 | #ifdef LUA_DEBUG |
| 59 | luaB_opentests(L); | 77 | luaB_opentests(L); |
| 60 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ | 78 | if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ |
| 61 | LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack"); | 79 | lua_assert(lua_gettop(L) == 0); |
| 62 | #endif | 80 | #endif |
| 63 | } | 81 | } |
| 64 | 82 | ||
| 65 | 83 | ||
| 66 | LUA_API lua_State *lua_open (int stacksize) { | 84 | LUA_API lua_State *lua_open (int stacksize) { |
| 67 | lua_State *L = luaM_new(NULL, lua_State); | 85 | lua_State *L; |
| 86 | L = luaM_new(NULL, lua_State); | ||
| 68 | if (L == NULL) return NULL; /* memory allocation error */ | 87 | if (L == NULL) return NULL; /* memory allocation error */ |
| 88 | L->G = NULL; | ||
| 69 | L->stack = NULL; | 89 | L->stack = NULL; |
| 70 | L->stacksize = 0; | 90 | L->stacksize = 0; |
| 71 | L->strt.size = L->udt.size = 0; | 91 | L->errorJmp = NULL; |
| 72 | L->strt.nuse = L->udt.nuse = 0; | ||
| 73 | L->strt.hash = 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->sizeTM = 0; | ||
| 81 | L->ntag = 0; | ||
| 82 | L->refArray = NULL; | ||
| 83 | L->nref = 0; | ||
| 84 | L->sizeref = 0; | ||
| 85 | L->refFree = NONEXT; | ||
| 86 | L->nblocks = sizeof(lua_State); | ||
| 87 | L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | ||
| 88 | L->callhook = NULL; | 92 | L->callhook = NULL; |
| 89 | L->linehook = NULL; | 93 | L->linehook = NULL; |
| 90 | L->allowhooks = 1; | 94 | L->allowhooks = 1; |
| 91 | L->errorJmp = NULL; | ||
| 92 | if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) { | 95 | if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) { |
| 93 | /* memory allocation error: free partial state */ | 96 | /* memory allocation error: free partial state */ |
| 94 | lua_close(L); | 97 | lua_close(L); |
| 95 | return NULL; | 98 | return NULL; |
| 96 | } | 99 | } |
| 97 | L->GCthreshold = 2*L->nblocks; | 100 | G(L)->GCthreshold = 2*G(L)->nblocks; |
| 98 | return L; | 101 | return L; |
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | 104 | ||
| 102 | LUA_API void lua_close (lua_State *L) { | 105 | LUA_API void lua_close (lua_State *L) { |
| 103 | LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack"); | 106 | lua_assert(L != lua_state || lua_gettop(L) == 0); |
| 104 | luaC_collect(L, 1); /* collect all elements */ | 107 | if (G(L)) { /* close global state */ |
| 105 | LUA_ASSERT(L->rootproto == NULL, "list should be empty"); | 108 | luaC_collect(L, 1); /* collect all elements */ |
| 106 | LUA_ASSERT(L->rootcl == NULL, "list should be empty"); | 109 | lua_assert(G(L)->rootproto == NULL); |
| 107 | LUA_ASSERT(L->roottable == NULL, "list should be empty"); | 110 | lua_assert(G(L)->rootcl == NULL); |
| 108 | luaS_freeall(L); | 111 | lua_assert(G(L)->roottable == NULL); |
| 109 | luaM_freearray(L, L->stack, L->stacksize, TObject); | 112 | luaS_freeall(L); |
| 110 | luaM_freearray(L, L->TMtable, L->sizeTM, struct TM); | 113 | luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM); |
| 111 | luaM_freearray(L, L->refArray, L->sizeref, struct Ref); | 114 | luaM_freearray(L, G(L)->refArray, G(L)->sizeref, struct Ref); |
| 112 | luaM_freearray(L, L->Mbuffer, L->Mbuffsize, char); | 115 | luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); |
| 113 | LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks"); | 116 | luaM_freelem(NULL, L->G, global_State); |
| 114 | luaM_freelem(L, L, lua_State); | 117 | } |
| 115 | LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!"); | 118 | luaM_freearray(NULL, L->stack, L->stacksize, TObject); |
| 116 | LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!"); | 119 | luaM_freelem(NULL, L, lua_State); |
| 120 | lua_assert(L != lua_state || memdebug_numblocks == 0); | ||
| 121 | lua_assert(L != lua_state || memdebug_total == 0); | ||
| 117 | } | 122 | } |
| 118 | 123 | ||
