2
** $Id: lstate.c,v 1.1 2003/02/09 21:30:32 dheck Exp $
4
** See Copyright Notice in lua.h
23
static lua_State *lua_state = NULL;
24
void luaB_opentests (lua_State *L);
29
** built-in implementation for ERRORMESSAGE. In a "correct" environment
30
** ERRORMESSAGE should have an external definition, and so this function
33
static int errormessage (lua_State *L) {
34
const char *s = lua_tostring(L, 1);
35
if (s == NULL) s = "(no message)";
36
fprintf(stderr, "error: %s\n", s);
42
** open parts that may cause memory-allocation errors
44
static void f_luaopen (lua_State *L, void *ud) {
45
int stacksize = *(int *)ud;
47
stacksize = DEFAULT_STACK_SIZE;
49
stacksize += LUA_MINSTACK;
50
L->gt = luaH_new(L, 10); /* table of globals */
51
luaD_init(L, stacksize);
56
lua_ref(L, 1); /* create registry */
57
lua_register(L, LUA_ERRORMESSAGE, errormessage);
60
if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
62
LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
66
LUA_API lua_State *lua_open (int stacksize) {
67
lua_State *L = luaM_new(NULL, lua_State);
68
if (L == NULL) return NULL; /* memory allocation error */
70
L->strt.size = L->udt.size = 0;
71
L->strt.nuse = L->udt.nuse = 0;
84
L->nblocks = sizeof(lua_State);
85
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
90
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
91
/* memory allocation error: free partial state */
95
L->GCthreshold = 2*L->nblocks;
100
LUA_API void lua_close (lua_State *L) {
101
LUA_ASSERT(L != lua_state || lua_gettop(L) == 0, "garbage in C stack");
102
luaC_collect(L, 1); /* collect all elements */
103
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
104
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
105
LUA_ASSERT(L->roottable == NULL, "list should be empty");
108
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
109
luaM_free(L, L->stack);
110
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
111
luaM_free(L, L->TMtable);
112
L->nblocks -= (L->refSize)*sizeof(struct Ref);
113
luaM_free(L, L->refArray);
114
L->nblocks -= (L->Mbuffsize)*sizeof(char);
115
luaM_free(L, L->Mbuffer);
116
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
118
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
119
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");