aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-06 09:41:28 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-06 09:41:28 -0200
commit968ad49da6522c1cf413bd86323a613176d12c07 (patch)
tree6961b75297c9533963f3b2d7db4f06900ed4f9f7
parent1fdb445e7dae6d08b67e46e1979f285b38bbc18d (diff)
downloadlua-968ad49da6522c1cf413bd86323a613176d12c07.tar.gz
lua-968ad49da6522c1cf413bd86323a613176d12c07.tar.bz2
lua-968ad49da6522c1cf413bd86323a613176d12c07.zip
`lua_newstate' gets the inital stack size and other arguments
-rw-r--r--ldo.h4
-rw-r--r--lstate.c51
-rw-r--r--lstate.h3
-rw-r--r--lua.c6
-rw-r--r--lua.h6
5 files changed, 55 insertions, 15 deletions
diff --git a/ldo.h b/ldo.h
index bc64e845..f03bbd0a 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.12 1999/12/01 19:50:08 roberto Exp roberto $ 2** $Id: ldo.h,v 1.13 1999/12/02 16:24:45 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -23,7 +23,7 @@
23#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;} 23#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
24 24
25 25
26void luaD_init (lua_State *L); 26void luaD_init (lua_State *L, int stacksize);
27void luaD_adjusttop (lua_State *L, StkId base, int extra); 27void luaD_adjusttop (lua_State *L, StkId base, int extra);
28void luaD_openstack (lua_State *L, StkId pos); 28void luaD_openstack (lua_State *L, StkId pos);
29void luaD_lineHook (lua_State *L, int line); 29void luaD_lineHook (lua_State *L, int line);
diff --git a/lstate.c b/lstate.c
index fe2b4830..3306c780 100644
--- a/lstate.c
+++ b/lstate.c
@@ -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
21lua_State *lua_state = NULL; 29lua_State *lua_state = NULL;
22 30
23 31
24lua_State *lua_newstate (void) { 32static 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
65lua_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
56void lua_close (lua_State *L) { 90void 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
diff --git a/lstate.h b/lstate.h
index 1b946312..cd4020be 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.23 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lstate.h,v 1.24 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*/
@@ -53,6 +53,7 @@ struct lua_State {
53 StkId top; /* first free slot in the stack */ 53 StkId top; /* first free slot in the stack */
54 StkId stack; /* stack base */ 54 StkId stack; /* stack base */
55 StkId stack_last; /* last free slot in the stack */ 55 StkId stack_last; /* last free slot in the stack */
56 int stacksize;
56 struct C_Lua_Stack Cstack; /* C2lua struct */ 57 struct C_Lua_Stack Cstack; /* C2lua struct */
57 struct lua_longjmp *errorJmp; /* current error recover point */ 58 struct lua_longjmp *errorJmp; /* current error recover point */
58 char *Mbuffer; /* global buffer */ 59 char *Mbuffer; /* global buffer */
diff --git a/lua.c b/lua.c
index d3d716f2..7956ede9 100644
--- a/lua.c
+++ b/lua.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.c,v 1.26 1999/11/16 12:50:48 roberto Exp roberto $ 2** $Id: lua.c,v 1.27 1999/11/22 13:12:07 roberto Exp roberto $
3** Lua stand-alone interpreter 3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -91,6 +91,7 @@ static void assign (char *arg) {
91 91
92 92
93static void getargs (int argc, char *argv[]) { 93static void getargs (int argc, char *argv[]) {
94 lua_beginblock(); {
94 int i, j; 95 int i, j;
95 lua_Object args = lua_createtable(); 96 lua_Object args = lua_createtable();
96 lua_pushobject(args); 97 lua_pushobject(args);
@@ -108,6 +109,7 @@ static void getargs (int argc, char *argv[]) {
108 /* arg.nn = minimum index in table `arg' */ 109 /* arg.nn = minimum index in table `arg' */
109 lua_pushobject(args); lua_pushstring("nn"); 110 lua_pushobject(args); lua_pushstring("nn");
110 lua_pushnumber(-i); lua_settable(); 111 lua_pushnumber(-i); lua_settable();
112 } lua_endblock();
111} 113}
112 114
113 115
@@ -146,7 +148,7 @@ static void manual_input (int prompt) {
146 148
147int main (int argc, char *argv[]) { 149int main (int argc, char *argv[]) {
148 int i; 150 int i;
149 lua_open(); 151 lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
150 lua_pushstring("> "); lua_setglobal("_PROMPT"); 152 lua_pushstring("> "); lua_setglobal("_PROMPT");
151 lua_userinit(); 153 lua_userinit();
152 getargs(argc, argv); 154 getargs(argc, argv);
diff --git a/lua.h b/lua.h
index db251834..f57b0e3d 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.41 1999/11/29 19:31:29 roberto Exp roberto $ 2** $Id: lua.h,v 1.42 1999/12/02 16:24:45 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -30,7 +30,7 @@ typedef struct TObject *lua_Object;
30#define LUA_NOOBJECT ((lua_Object)0) 30#define LUA_NOOBJECT ((lua_Object)0)
31 31
32 32
33lua_State *lua_newstate (void); 33lua_State *lua_newstate (const char *s, ...);
34void lua_close (lua_State *L); 34void lua_close (lua_State *L);
35 35
36lua_Object lua_settagmethod (lua_State *L, int tag, const char *event); 36lua_Object lua_settagmethod (lua_State *L, int tag, const char *event);
@@ -156,7 +156,7 @@ lua_State *lua_setstate (lua_State *st);
156 156
157extern lua_State *lua_state; 157extern lua_State *lua_state;
158 158
159#define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate()))) 159#define lua_open() ((void)(lua_state?0:(lua_state=lua_newstate(NULL))))
160 160
161#define lua_close() (lua_close)(lua_state) 161#define lua_close() (lua_close)(lua_state)
162#define lua_setstate(st) (lua_setstate)(lua_state, st) 162#define lua_setstate(st) (lua_setstate)(lua_state, st)