浏览代码

`lua_newstate' gets the inital stack size and other arguments

Roberto Ierusalimschy 26 年之前
父节点
当前提交
968ad49da6
共有 5 个文件被更改,包括 55 次插入15 次删除
  1. 2 2
      ldo.h
  2. 44 7
      lstate.c
  3. 2 1
      lstate.h
  4. 4 2
      lua.c
  5. 3 3
      lua.h

+ 2 - 2
ldo.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.h,v 1.12 1999/12/01 19:50:08 roberto Exp roberto $
+** $Id: ldo.h,v 1.13 1999/12/02 16:24:45 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -23,7 +23,7 @@
 #define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
 
 
-void luaD_init (lua_State *L);
+void luaD_init (lua_State *L, int stacksize);
 void luaD_adjusttop (lua_State *L, StkId base, int extra);
 void luaD_openstack (lua_State *L, StkId pos);
 void luaD_lineHook (lua_State *L, int line);

+ 44 - 7
lstate.c

@@ -1,12 +1,15 @@
 /*
-** $Id: lstate.c,v 1.18 1999/11/29 19:12:07 roberto Exp roberto $
+** $Id: lstate.c,v 1.19 1999/12/01 19:50:08 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
 
 
+#include <stdarg.h>
+
 #define LUA_REENTRANT
 
+#include "lauxlib.h"
 #include "lbuiltin.h"
 #include "ldo.h"
 #include "lgc.h"
@@ -18,10 +21,15 @@
 #include "ltm.h"
 
 
+#ifndef DEFAULT_STACK_SIZE
+#define DEFAULT_STACK_SIZE      1024
+#endif
+
+
 lua_State *lua_state = NULL;
 
 
-lua_State *lua_newstate (void) {
+static lua_State *newstate_aux (int stacksize, int put_builtin) {
   lua_State *L = luaM_new(NULL, lua_State);
   L->errorJmp = NULL;
   L->Mbuffer = NULL;
@@ -43,16 +51,42 @@ lua_State *lua_newstate (void) {
   L->refFree = NONEXT;
   L->nblocks = 0;
   L->GCthreshold = MAX_INT;  /* to avoid GC during pre-definitions */
-  luaD_init(L);
+  luaD_init(L, stacksize);
   luaS_init(L);
   luaX_init(L);
   luaT_init(L);
-  luaB_predefine(L);
+  if (put_builtin)
+    luaB_predefine(L);
   L->GCthreshold = L->nblocks*4;
   return L;
 }
 
 
+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);
+}
+
+
 void lua_close (lua_State *L) {
   luaC_collect(L, 1);  /* collect all elements */
   LUA_ASSERT(L, L->rootproto == NULL, "list should be empty");
@@ -66,9 +100,12 @@ void lua_close (lua_State *L) {
   luaM_free(L, L->Mbuffer);
   luaM_free(L, L->Cblocks);
   LUA_ASSERT(L, L->nblocks == 0, "wrong count for nblocks");
+  LUA_ASSERT(L, L != lua_state || L->Cstack.lua2C == L->stack, "bad stack");
+  LUA_ASSERT(L, L != lua_state || L->Cstack.base == L->stack, "bad stack");
   luaM_free(L, L);
-  LUA_ASSERT(L, numblocks == 0, "memory leak!");
-  LUA_ASSERT(L, totalmem == 0,"memory leak!");
-  L = NULL;
+  LUA_ASSERT(L, L != lua_state || numblocks == 0, "memory leak!");
+  LUA_ASSERT(L, L != lua_state || totalmem == 0,"memory leak!");
+  if (L == lua_state)
+    lua_state = NULL;
 }
 

+ 2 - 1
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 1.23 1999/11/22 13:12:07 roberto Exp roberto $
+** $Id: lstate.h,v 1.24 1999/12/01 19:50:08 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -53,6 +53,7 @@ struct lua_State {
   StkId top;  /* first free slot in the stack */
   StkId stack;  /* stack base */
   StkId stack_last;  /* last free slot in the stack */
+  int stacksize;
   struct C_Lua_Stack Cstack;  /* C2lua struct */
   struct lua_longjmp *errorJmp;  /* current error recover point */
   char *Mbuffer;  /* global buffer */

+ 4 - 2
lua.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.c,v 1.26 1999/11/16 12:50:48 roberto Exp roberto $
+** $Id: lua.c,v 1.27 1999/11/22 13:12:07 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 */
@@ -91,6 +91,7 @@ static void assign (char *arg) {
 
 
 static void getargs (int argc, char *argv[]) {
+  lua_beginblock(); {
   int i, j;
   lua_Object args = lua_createtable();
   lua_pushobject(args);
@@ -108,6 +109,7 @@ static void getargs (int argc, char *argv[]) {
   /* arg.nn = minimum index in table `arg' */
   lua_pushobject(args); lua_pushstring("nn");
   lua_pushnumber(-i); lua_settable();
+  } lua_endblock();
 }
 
 
@@ -146,7 +148,7 @@ static void manual_input (int prompt) {
 
 int main (int argc, char *argv[]) {
   int i;
-  lua_open();
+  lua_state = lua_newstate("stack", 1024, "builtin", 1, NULL);
   lua_pushstring("> "); lua_setglobal("_PROMPT");
   lua_userinit();
   getargs(argc, argv);

+ 3 - 3
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.41 1999/11/29 19:31:29 roberto Exp roberto $
+** $Id: lua.h,v 1.42 1999/12/02 16:24:45 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
@@ -30,7 +30,7 @@ typedef struct TObject *lua_Object;
 #define LUA_NOOBJECT	((lua_Object)0)
 
 
-lua_State     *lua_newstate (void);
+lua_State     *lua_newstate (const char *s, ...);
 void           lua_close (lua_State *L);
 
 lua_Object     lua_settagmethod (lua_State *L, int tag, const char *event);
@@ -156,7 +156,7 @@ lua_State     *lua_setstate (lua_State *st);
 
 extern lua_State *lua_state;
 
-#define lua_open()		((void)(lua_state?0:(lua_state=lua_newstate())))
+#define lua_open()	((void)(lua_state?0:(lua_state=lua_newstate(NULL))))
 
 #define lua_close()		(lua_close)(lua_state)
 #define lua_setstate(st)	(lua_setstate)(lua_state, st)