diff options
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 51 |
1 files changed, 44 insertions, 7 deletions
@@ -1,12 +1,15 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 1.18 1999/11/29 19:12:07 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.19 1999/12/01 19:50:08 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <stdarg.h> | ||
9 | |||
8 | #define LUA_REENTRANT | 10 | #define LUA_REENTRANT |
9 | 11 | ||
12 | #include "lauxlib.h" | ||
10 | #include "lbuiltin.h" | 13 | #include "lbuiltin.h" |
11 | #include "ldo.h" | 14 | #include "ldo.h" |
12 | #include "lgc.h" | 15 | #include "lgc.h" |
@@ -18,10 +21,15 @@ | |||
18 | #include "ltm.h" | 21 | #include "ltm.h" |
19 | 22 | ||
20 | 23 | ||
24 | #ifndef DEFAULT_STACK_SIZE | ||
25 | #define DEFAULT_STACK_SIZE 1024 | ||
26 | #endif | ||
27 | |||
28 | |||
21 | lua_State *lua_state = NULL; | 29 | lua_State *lua_state = NULL; |
22 | 30 | ||
23 | 31 | ||
24 | lua_State *lua_newstate (void) { | 32 | static lua_State *newstate_aux (int stacksize, int put_builtin) { |
25 | lua_State *L = luaM_new(NULL, lua_State); | 33 | lua_State *L = luaM_new(NULL, lua_State); |
26 | L->errorJmp = NULL; | 34 | L->errorJmp = NULL; |
27 | L->Mbuffer = NULL; | 35 | L->Mbuffer = NULL; |
@@ -43,16 +51,42 @@ lua_State *lua_newstate (void) { | |||
43 | L->refFree = NONEXT; | 51 | L->refFree = NONEXT; |
44 | L->nblocks = 0; | 52 | L->nblocks = 0; |
45 | L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ | 53 | L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ |
46 | luaD_init(L); | 54 | luaD_init(L, stacksize); |
47 | luaS_init(L); | 55 | luaS_init(L); |
48 | luaX_init(L); | 56 | luaX_init(L); |
49 | luaT_init(L); | 57 | luaT_init(L); |
50 | luaB_predefine(L); | 58 | if (put_builtin) |
59 | luaB_predefine(L); | ||
51 | L->GCthreshold = L->nblocks*4; | 60 | L->GCthreshold = L->nblocks*4; |
52 | return L; | 61 | return L; |
53 | } | 62 | } |
54 | 63 | ||
55 | 64 | ||
65 | lua_State *lua_newstate (const char *s, ...) { | ||
66 | static const char *const ops[] = {"stack", "builtin", NULL}; | ||
67 | va_list ap; | ||
68 | int stacksize = DEFAULT_STACK_SIZE; | ||
69 | int put_builtin = 1; | ||
70 | va_start(ap, s); | ||
71 | while (s) { | ||
72 | switch (luaL_findstring(s, ops)) { | ||
73 | case 0: /* stack */ | ||
74 | stacksize = va_arg(ap, int); | ||
75 | break; | ||
76 | case 1: /* builtin */ | ||
77 | put_builtin = va_arg(ap, int); | ||
78 | break; | ||
79 | default: /* invalid argument */ | ||
80 | va_end(ap); | ||
81 | return NULL; | ||
82 | } | ||
83 | s = va_arg(ap, const char *); | ||
84 | } | ||
85 | va_end(ap); | ||
86 | return newstate_aux(stacksize, put_builtin); | ||
87 | } | ||
88 | |||
89 | |||
56 | void lua_close (lua_State *L) { | 90 | void lua_close (lua_State *L) { |
57 | luaC_collect(L, 1); /* collect all elements */ | 91 | luaC_collect(L, 1); /* collect all elements */ |
58 | LUA_ASSERT(L, L->rootproto == NULL, "list should be empty"); | 92 | LUA_ASSERT(L, L->rootproto == NULL, "list should be empty"); |
@@ -66,9 +100,12 @@ void lua_close (lua_State *L) { | |||
66 | luaM_free(L, L->Mbuffer); | 100 | luaM_free(L, L->Mbuffer); |
67 | luaM_free(L, L->Cblocks); | 101 | luaM_free(L, L->Cblocks); |
68 | LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks"); | 102 | LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks"); |
103 | LUA_ASSERT(L, L != lua_state || L->Cstack.lua2C == L->stack, "bad stack"); | ||
104 | LUA_ASSERT(L, L != lua_state || L->Cstack.base == L->stack, "bad stack"); | ||
69 | luaM_free(L, L); | 105 | luaM_free(L, L); |
70 | LUA_ASSERT(L, numblocks == 0, "memory leak!"); | 106 | LUA_ASSERT(L, L != lua_state || numblocks == 0, "memory leak!"); |
71 | LUA_ASSERT(L, totalmem == 0,"memory leak!"); | 107 | LUA_ASSERT(L, L != lua_state || totalmem == 0,"memory leak!"); |
72 | L = NULL; | 108 | if (L == lua_state) |
109 | lua_state = NULL; | ||
73 | } | 110 | } |
74 | 111 | ||