lua/lstate.c

118 lines
2.9 KiB
C
Raw Normal View History

1997-11-19 20:31:19 +03:00
/*
2000-10-20 20:39:03 +04:00
** $Id: lstate.c,v 1.44 2000/10/06 19:28:47 roberto Exp roberto $
1997-11-19 20:31:19 +03:00
** Global State
** See Copyright Notice in lua.h
*/
2000-09-12 22:42:32 +04:00
#include <stdio.h>
#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"
#include "ltable.h"
1997-11-19 20:31:19 +03:00
#include "ltm.h"
2000-09-05 23:33:32 +04:00
#ifdef DEBUG
2000-09-25 20:22:42 +04:00
static lua_State *lua_state = NULL;
2000-09-05 23:33:32 +04:00
void luaB_opentests (lua_State *L);
#endif
2000-09-12 22:42:32 +04:00
/*
** built-in implementation for ERRORMESSAGE. In a "correct" environment
** ERRORMESSAGE should have an external definition, and so this function
** would not be used.
*/
static int errormessage (lua_State *L) {
const char *s = lua_tostring(L, 1);
if (s == NULL) s = "(no message)";
fprintf(stderr, "error: %s\n", s);
return 0;
}
2000-09-25 20:22:42 +04:00
/*
** open parts that may cause memory-allocation errors
*/
static void f_luaopen (lua_State *L, void *ud) {
int stacksize = *(int *)ud;
if (stacksize == 0)
stacksize = DEFAULT_STACK_SIZE;
else
stacksize += LUA_MINSTACK;
L->gt = luaH_new(L, 10);
luaD_init(L, stacksize);
luaS_init(L);
luaX_init(L);
luaT_init(L);
lua_register(L, LUA_ERRORMESSAGE, errormessage);
#ifdef DEBUG
luaB_opentests(L);
if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
#endif
}
2000-10-20 20:39:03 +04:00
LUA_API lua_State *lua_open (int stacksize) {
lua_State *L = luaM_new(NULL, lua_State);
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;
L->Mbuffer = NULL;
L->Mbuffsize = 0;
L->rootproto = NULL;
L->rootcl = NULL;
L->roottable = NULL;
L->TMtable = NULL;
L->last_tag = -1;
1997-11-19 20:31:19 +03:00
L->refArray = NULL;
L->refSize = 0;
L->refFree = NONEXT;
L->nblocks = sizeof(lua_State);
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
L->callhook = NULL;
L->linehook = NULL;
L->allowhooks = 1;
2000-09-25 20:22:42 +04:00
L->errorJmp = NULL;
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
/* memory allocation error: free partial state */
lua_close(L);
return NULL;
}
2000-10-06 23:28:47 +04:00
L->GCthreshold = 2*L->nblocks;
2000-09-25 20:22:42 +04:00
return L;
1997-11-19 20:31:19 +03:00
}
1997-12-01 23:31:25 +03:00
2000-10-20 20:39:03 +04:00
LUA_API 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);
if (L->stack)
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
1999-12-01 22:50:08 +03:00
luaM_free(L, L->stack);
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
luaM_free(L, L->TMtable);
L->nblocks -= (L->refSize)*sizeof(struct Ref);
luaM_free(L, L->refArray);
L->nblocks -= (L->Mbuffsize)*sizeof(char);
luaM_free(L, L->Mbuffer);
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
luaM_free(L, L);
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
}