lua/lstate.c

87 lines
2.0 KiB
C
Raw Normal View History

1997-11-19 20:31:19 +03:00
/*
2000-06-30 23:17:08 +04:00
** $Id: lstate.c,v 1.28 2000/06/30 14:35:17 roberto Exp roberto $
1997-11-19 20:31:19 +03:00
** Global State
** See Copyright Notice in lua.h
*/
#include <stdarg.h>
#define LUA_REENTRANT
#include "lua.h"
#include "lauxlib.h"
1997-11-19 20:31:19 +03:00
#include "lbuiltin.h"
#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 "lref.h"
1997-11-19 20:31:19 +03:00
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
1997-11-19 20:31:19 +03:00
#include "ltm.h"
lua_State *lua_state = NULL;
1997-11-19 20:31:19 +03:00
2000-06-30 23:17:08 +04:00
lua_State *lua_newstate (int stacksize, int put_builtin) {
lua_State *L = luaM_new(NULL, lua_State);
1997-11-19 20:31:19 +03:00
L->errorJmp = NULL;
L->Mbuffer = NULL;
L->Mbuffbase = 0;
L->Mbuffsize = 0;
L->Mbuffnext = 0;
1999-05-11 18:19:32 +04:00
L->Cblocks = NULL;
L->numCblocks = 0;
L->rootproto = NULL;
L->rootcl = NULL;
L->roottable = NULL;
L->IMtable = NULL;
1997-11-19 20:31:19 +03:00
L->refArray = NULL;
L->refSize = 0;
L->refFree = NONEXT;
1997-11-19 20:31:19 +03:00
L->nblocks = 0;
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
L->debug = 0;
L->callhook = NULL;
L->linehook = NULL;
L->allowhooks = 1;
L->gt = luaH_new(L, 10);
2000-06-30 23:17:08 +04:00
if (stacksize == 0) stacksize = DEFAULT_STACK_SIZE;
luaD_init(L, stacksize);
luaS_init(L);
luaX_init(L);
luaT_init(L);
if (put_builtin)
luaB_predefine(L);
L->GCthreshold = L->nblocks*4;
return L;
1997-11-19 20:31:19 +03:00
}
1997-12-01 23:31:25 +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");
luaS_freeall(L);
1999-12-01 22:50:08 +03:00
luaM_free(L, L->stack);
luaM_free(L, L->IMtable);
luaM_free(L, L->refArray);
luaM_free(L, L->Mbuffer);
luaM_free(L, L->Cblocks);
2000-06-30 18:35:17 +04:00
LUA_ASSERT(L->numCblocks == 0, "Cblocks still open");
LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
LUA_ASSERT(L->Cstack.base == L->top, "C2Lua not empty");
luaM_free(L, L);
if (L == lua_state) {
2000-06-30 18:35:17 +04:00
LUA_ASSERT(memdebug_numblocks == 0, "memory leak!");
LUA_ASSERT(memdebug_total == 0,"memory leak!");
lua_state = NULL;
}
1997-12-01 23:31:25 +03:00
}