1997-11-19 09:31:19 -08:00
|
|
|
/*
|
2000-10-24 12:12:06 -07:00
|
|
|
** $Id: lstate.c,v 1.45 2000/10/20 16:39:03 roberto Exp roberto $
|
1997-11-19 09:31:19 -08:00
|
|
|
** Global State
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-09-12 11:42:32 -07:00
|
|
|
#include <stdio.h>
|
1999-12-06 03:41:28 -08:00
|
|
|
|
2000-06-12 06:52:05 -07:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-11-19 09:31:19 -08:00
|
|
|
#include "ldo.h"
|
1997-12-01 12:31:25 -08:00
|
|
|
#include "lgc.h"
|
1997-11-19 09:31:19 -08:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
2000-05-08 12:32:53 -07:00
|
|
|
#include "ltable.h"
|
1997-11-19 09:31:19 -08:00
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
2000-09-05 12:33:32 -07:00
|
|
|
#ifdef DEBUG
|
2000-09-25 09:22:42 -07:00
|
|
|
static lua_State *lua_state = NULL;
|
2000-09-05 12:33:32 -07:00
|
|
|
void luaB_opentests (lua_State *L);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-09-12 11:42:32 -07: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 09:22:42 -07: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;
|
2000-10-24 12:12:06 -07:00
|
|
|
L->gt = luaH_new(L, 10); /* table of globals */
|
2000-09-25 09:22:42 -07:00
|
|
|
luaD_init(L, stacksize);
|
|
|
|
luaS_init(L);
|
|
|
|
luaX_init(L);
|
|
|
|
luaT_init(L);
|
2000-10-24 12:12:06 -07:00
|
|
|
lua_newtable(L);
|
|
|
|
lua_ref(L, 1); /* create registry */
|
2000-09-25 09:22:42 -07:00
|
|
|
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-24 12:12:06 -07:00
|
|
|
LUA_ASSERT(lua_gettop(L) == 0, "wrong API stack");
|
2000-09-25 09:22:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-20 09:39:03 -07:00
|
|
|
LUA_API lua_State *lua_open (int stacksize) {
|
1999-11-22 05:12:07 -08:00
|
|
|
lua_State *L = luaM_new(NULL, lua_State);
|
2000-08-04 12:38:35 -07: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 12:30:51 -07:00
|
|
|
L->Mbuffer = NULL;
|
|
|
|
L->Mbuffsize = 0;
|
1999-10-04 10:51:04 -07:00
|
|
|
L->rootproto = NULL;
|
|
|
|
L->rootcl = NULL;
|
|
|
|
L->roottable = NULL;
|
2000-10-05 06:00:17 -07:00
|
|
|
L->TMtable = NULL;
|
2000-08-04 12:38:35 -07:00
|
|
|
L->last_tag = -1;
|
1997-11-19 09:31:19 -08:00
|
|
|
L->refArray = NULL;
|
|
|
|
L->refSize = 0;
|
1999-11-10 07:39:35 -08:00
|
|
|
L->refFree = NONEXT;
|
2000-09-29 05:42:13 -07:00
|
|
|
L->nblocks = sizeof(lua_State);
|
1999-11-10 07:39:35 -08:00
|
|
|
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
1999-12-21 10:04:41 -08:00
|
|
|
L->callhook = NULL;
|
|
|
|
L->linehook = NULL;
|
|
|
|
L->allowhooks = 1;
|
2000-09-25 09:22:42 -07:00
|
|
|
L->errorJmp = NULL;
|
|
|
|
if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
|
|
|
|
/* memory allocation error: free partial state */
|
2000-08-04 12:38:35 -07:00
|
|
|
lua_close(L);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-06 12:28:47 -07:00
|
|
|
L->GCthreshold = 2*L->nblocks;
|
2000-09-25 09:22:42 -07:00
|
|
|
return L;
|
1997-11-19 09:31:19 -08:00
|
|
|
}
|
|
|
|
|
1997-12-01 12:31:25 -08:00
|
|
|
|
2000-10-20 09:39:03 -07:00
|
|
|
LUA_API void lua_close (lua_State *L) {
|
1999-11-22 05:12:07 -08:00
|
|
|
luaC_collect(L, 1); /* collect all elements */
|
2000-06-30 07:35:17 -07: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 05:12:07 -08:00
|
|
|
luaS_freeall(L);
|
2000-09-29 05:42:13 -07:00
|
|
|
if (L->stack)
|
|
|
|
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
|
1999-12-01 11:50:08 -08:00
|
|
|
luaM_free(L, L->stack);
|
2000-10-05 06:00:17 -07:00
|
|
|
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
|
|
|
|
luaM_free(L, L->TMtable);
|
2000-09-29 05:42:13 -07:00
|
|
|
L->nblocks -= (L->refSize)*sizeof(struct Ref);
|
1999-11-22 05:12:07 -08:00
|
|
|
luaM_free(L, L->refArray);
|
2000-09-29 05:42:13 -07:00
|
|
|
L->nblocks -= (L->Mbuffsize)*sizeof(char);
|
1999-11-22 05:12:07 -08:00
|
|
|
luaM_free(L, L->Mbuffer);
|
2000-09-29 05:42:13 -07:00
|
|
|
LUA_ASSERT(L->nblocks == sizeof(lua_State), "wrong count for nblocks");
|
1999-11-22 05:12:07 -08:00
|
|
|
luaM_free(L, L);
|
2000-08-14 10:46:07 -07:00
|
|
|
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
|
|
|
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
1997-12-01 12:31:25 -08:00
|
|
|
}
|
1998-06-02 13:37:04 -07:00
|
|
|
|