1997-11-20 01:31:19 +08:00
|
|
|
/*
|
2000-01-14 00:30:47 +08:00
|
|
|
** $Id: lstate.c,v 1.23 1999/12/21 18:04:41 roberto Exp roberto $
|
1997-11-20 01:31:19 +08:00
|
|
|
** Global State
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-12-06 19:41:28 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
1999-12-06 19:41:28 +08:00
|
|
|
#include "lauxlib.h"
|
1997-11-20 01:31:19 +08:00
|
|
|
#include "lbuiltin.h"
|
|
|
|
#include "ldo.h"
|
1997-12-02 04:31:25 +08:00
|
|
|
#include "lgc.h"
|
1997-11-20 01:31:19 +08:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
1999-11-10 23:39:35 +08:00
|
|
|
#include "lref.h"
|
1997-11-20 01:31:19 +08:00
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltm.h"
|
|
|
|
|
|
|
|
|
1999-12-06 19:41:28 +08:00
|
|
|
#ifndef DEFAULT_STACK_SIZE
|
|
|
|
#define DEFAULT_STACK_SIZE 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1998-06-03 04:37:04 +08:00
|
|
|
lua_State *lua_state = NULL;
|
1997-11-20 01:31:19 +08:00
|
|
|
|
|
|
|
|
1999-12-06 19:41:28 +08:00
|
|
|
static lua_State *newstate_aux (int stacksize, int put_builtin) {
|
1999-11-22 21:12:07 +08:00
|
|
|
lua_State *L = luaM_new(NULL, lua_State);
|
1997-11-20 01:31:19 +08:00
|
|
|
L->errorJmp = NULL;
|
1999-04-14 03:30:51 +08:00
|
|
|
L->Mbuffer = NULL;
|
|
|
|
L->Mbuffbase = 0;
|
|
|
|
L->Mbuffsize = 0;
|
|
|
|
L->Mbuffnext = 0;
|
1999-05-11 22:19:32 +08:00
|
|
|
L->Cblocks = NULL;
|
1999-04-14 03:30:51 +08:00
|
|
|
L->numCblocks = 0;
|
1999-10-05 01:51:04 +08:00
|
|
|
L->rootproto = NULL;
|
|
|
|
L->rootcl = NULL;
|
|
|
|
L->rootglobal = NULL;
|
|
|
|
L->roottable = NULL;
|
1999-04-14 03:30:51 +08:00
|
|
|
L->IMtable = NULL;
|
1997-11-20 01:31:19 +08:00
|
|
|
L->refArray = NULL;
|
|
|
|
L->refSize = 0;
|
1999-11-10 23:39:35 +08:00
|
|
|
L->refFree = NONEXT;
|
1997-11-20 01:31:19 +08:00
|
|
|
L->nblocks = 0;
|
1999-11-10 23:39:35 +08:00
|
|
|
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
1999-12-22 02:04:41 +08:00
|
|
|
L->debug = 0;
|
|
|
|
L->callhook = NULL;
|
|
|
|
L->linehook = NULL;
|
|
|
|
L->allowhooks = 1;
|
1999-12-06 19:41:28 +08:00
|
|
|
luaD_init(L, stacksize);
|
1999-11-22 21:12:07 +08:00
|
|
|
luaS_init(L);
|
|
|
|
luaX_init(L);
|
|
|
|
luaT_init(L);
|
1999-12-06 19:41:28 +08:00
|
|
|
if (put_builtin)
|
|
|
|
luaB_predefine(L);
|
1999-11-10 23:39:35 +08:00
|
|
|
L->GCthreshold = L->nblocks*4;
|
1999-11-30 03:12:07 +08:00
|
|
|
return L;
|
1997-11-20 01:31:19 +08:00
|
|
|
}
|
|
|
|
|
1997-12-02 04:31:25 +08:00
|
|
|
|
1999-12-06 19:41:28 +08:00
|
|
|
lua_State *lua_newstate (const char *s, ...) {
|
|
|
|
static const char *const ops[] = {"stack", "builtin", NULL};
|
|
|
|
va_list ap;
|
|
|
|
int stacksize = DEFAULT_STACK_SIZE;
|
|
|
|
int put_builtin = 1;
|
|
|
|
va_start(ap, s);
|
|
|
|
while (s) {
|
|
|
|
switch (luaL_findstring(s, ops)) {
|
|
|
|
case 0: /* stack */
|
|
|
|
stacksize = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
case 1: /* builtin */
|
|
|
|
put_builtin = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
default: /* invalid argument */
|
|
|
|
va_end(ap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s = va_arg(ap, const char *);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return newstate_aux(stacksize, put_builtin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
void lua_close (lua_State *L) {
|
|
|
|
luaC_collect(L, 1); /* collect all elements */
|
|
|
|
LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
|
|
|
|
LUA_ASSERT(L, L->rootcl == NULL, "list should be empty");
|
|
|
|
LUA_ASSERT(L, L->rootglobal == NULL, "list should be empty");
|
|
|
|
LUA_ASSERT(L, L->roottable == NULL, "list should be empty");
|
|
|
|
luaS_freeall(L);
|
1999-12-02 03:50:08 +08:00
|
|
|
luaM_free(L, L->stack);
|
1999-11-22 21:12:07 +08:00
|
|
|
luaM_free(L, L->IMtable);
|
|
|
|
luaM_free(L, L->refArray);
|
|
|
|
luaM_free(L, L->Mbuffer);
|
|
|
|
luaM_free(L, L->Cblocks);
|
1999-12-06 20:03:45 +08:00
|
|
|
LUA_ASSERT(L, L->numCblocks == 0, "Cblocks still open");
|
1999-11-22 21:12:07 +08:00
|
|
|
LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
|
1999-12-07 19:42:54 +08:00
|
|
|
LUA_ASSERT(L, L->Cstack.base == L->top, "C2Lua not empty");
|
1999-11-22 21:12:07 +08:00
|
|
|
luaM_free(L, L);
|
1999-12-07 19:42:54 +08:00
|
|
|
if (L == lua_state) {
|
2000-01-14 00:30:47 +08:00
|
|
|
LUA_ASSERT(L, memdebug_numblocks == 0, "memory leak!");
|
|
|
|
LUA_ASSERT(L, memdebug_total == 0,"memory leak!");
|
1999-12-06 19:41:28 +08:00
|
|
|
lua_state = NULL;
|
1999-12-07 19:42:54 +08:00
|
|
|
}
|
1997-12-02 04:31:25 +08:00
|
|
|
}
|
1998-06-03 04:37:04 +08:00
|
|
|
|