1997-11-19 20:31:19 +03:00
|
|
|
/*
|
2000-09-11 23:42:57 +04:00
|
|
|
** $Id: lstate.c,v 1.37 2000/09/11 17:38:42 roberto Exp roberto $
|
1997-11-19 20:31:19 +03:00
|
|
|
** Global State
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-12-06 14:41:28 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-11-19 20:31:19 +03:00
|
|
|
#include "ldo.h"
|
1997-12-01 23:31:25 +03:00
|
|
|
#include "lgc.h"
|
1997-11-19 20:31:19 +03:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
2000-05-08 23:32:53 +04:00
|
|
|
#include "ltable.h"
|
1997-11-19 20:31:19 +03:00
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
2000-09-05 23:33:32 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
extern lua_State *lua_state;
|
|
|
|
void luaB_opentests (lua_State *L);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-09-11 23:42:57 +04:00
|
|
|
lua_State *lua_open (int stacksize) {
|
2000-08-04 23:38:35 +04:00
|
|
|
struct lua_longjmp myErrorJmp;
|
1999-11-22 16:12:07 +03:00
|
|
|
lua_State *L = luaM_new(NULL, lua_State);
|
2000-08-04 23:38:35 +04:00
|
|
|
if (L == NULL) return NULL; /* memory allocation error */
|
|
|
|
L->stack = NULL;
|
|
|
|
L->strt.size = L->udt.size = 0;
|
|
|
|
L->strt.nuse = L->udt.nuse = 0;
|
|
|
|
L->strt.hash = NULL;
|
|
|
|
L->udt.hash = NULL;
|
1999-04-13 23:30:51 +04:00
|
|
|
L->Mbuffer = NULL;
|
|
|
|
L->Mbuffsize = 0;
|
1999-10-04 21:51:04 +04:00
|
|
|
L->rootproto = NULL;
|
|
|
|
L->rootcl = NULL;
|
|
|
|
L->roottable = NULL;
|
1999-04-13 23:30:51 +04:00
|
|
|
L->IMtable = NULL;
|
2000-08-04 23:38:35 +04:00
|
|
|
L->last_tag = -1;
|
1997-11-19 20:31:19 +03:00
|
|
|
L->refArray = NULL;
|
|
|
|
L->refSize = 0;
|
1999-11-10 18:39:35 +03:00
|
|
|
L->refFree = NONEXT;
|
1997-11-19 20:31:19 +03:00
|
|
|
L->nblocks = 0;
|
1999-11-10 18:39:35 +03:00
|
|
|
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
1999-12-21 21:04:41 +03:00
|
|
|
L->callhook = NULL;
|
|
|
|
L->linehook = NULL;
|
|
|
|
L->allowhooks = 1;
|
2000-08-04 23:38:35 +04:00
|
|
|
L->errorJmp = &myErrorJmp;
|
|
|
|
if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
|
|
|
|
L->gt = luaH_new(L, 10);
|
2000-08-31 17:30:39 +04:00
|
|
|
luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
|
|
|
|
stacksize+LUA_MINSTACK);
|
2000-08-04 23:38:35 +04:00
|
|
|
luaS_init(L);
|
|
|
|
luaX_init(L);
|
|
|
|
luaT_init(L);
|
2000-09-05 23:33:32 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
luaB_opentests(L);
|
|
|
|
#endif
|
2000-08-04 23:38:35 +04:00
|
|
|
L->GCthreshold = L->nblocks*4;
|
|
|
|
L->errorJmp = NULL;
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
else { /* memory allocation error: free partial state */
|
|
|
|
lua_close(L);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-11-19 20:31:19 +03:00
|
|
|
}
|
|
|
|
|
1997-12-01 23:31:25 +03:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void lua_close (lua_State *L) {
|
|
|
|
luaC_collect(L, 1); /* collect all elements */
|
2000-06-30 18:35:17 +04:00
|
|
|
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
|
|
|
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
|
|
|
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
1999-11-22 16:12:07 +03:00
|
|
|
luaS_freeall(L);
|
1999-12-01 22:50:08 +03:00
|
|
|
luaM_free(L, L->stack);
|
1999-11-22 16:12:07 +03:00
|
|
|
luaM_free(L, L->IMtable);
|
|
|
|
luaM_free(L, L->refArray);
|
|
|
|
luaM_free(L, L->Mbuffer);
|
2000-06-30 18:35:17 +04:00
|
|
|
LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
|
1999-11-22 16:12:07 +03:00
|
|
|
luaM_free(L, L);
|
2000-09-05 23:33:32 +04:00
|
|
|
LUA_ASSERT(L->Cbase == L->stack, "stack not empty");
|
2000-08-14 21:46:07 +04:00
|
|
|
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
|
|
|
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
1997-12-01 23:31:25 +03:00
|
|
|
}
|
1998-06-03 00:37:04 +04:00
|
|
|
|