Browse Source

no more maximum stack size

v5-2
Roberto Ierusalimschy 23 years ago
parent
commit
b0a5e156b8
  1. 2
      lbaselib.c
  2. 26
      ldo.c
  3. 28
      lstate.c
  4. 6
      lstate.h
  5. 8
      ltests.c
  6. 14
      lua.h

2
lbaselib.c

@ -434,7 +434,7 @@ static int luaB_coroutine (lua_State *L) {
int n = lua_gettop(L);
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected");
NL = lua_newthread(L, 0);
NL = lua_newthread(L);
if (NL == NULL) lua_error(L, "unable to create new thread");
/* move function and arguments from L to NL */
for (i=0; i<n; i++) {

26
ldo.c

@ -71,26 +71,26 @@ void luaD_reallocstack (lua_State *L, int newsize) {
static void restore_stack_limit (lua_State *L) {
if (L->stacksize > L->maxstacksize) { /* there was an overflow? */
if (L->stacksize > LUA_MAXSTACK) { /* there was an overflow? */
int inuse = (L->top - L->stack);
if (inuse + MAXSTACK < L->maxstacksize) /* can `undo' overflow? */
luaD_reallocstack(L, L->maxstacksize);
if (inuse + MAXSTACK < LUA_MAXSTACK) /* can `undo' overflow? */
luaD_reallocstack(L, LUA_MAXSTACK);
}
}
void luaD_growstack (lua_State *L, int n) {
if (L->stacksize > L->maxstacksize) { /* overflow while handling overflow? */
if (L->stacksize > LUA_MAXSTACK) { /* overflow while handling overflow? */
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
}
else {
if (n <= L->stacksize && 2*L->stacksize < L->maxstacksize) /* can double? */
if (n <= L->stacksize && 2*L->stacksize < LUA_MAXSTACK) /* can double? */
luaD_reallocstack(L, 2*L->stacksize);
else if (L->stacksize+n <= L->maxstacksize) /* no overflow? */
luaD_reallocstack(L, L->maxstacksize);
else if (L->stacksize+n <= LUA_MAXSTACK) /* no overflow? */
luaD_reallocstack(L, LUA_MAXSTACK);
else {
/* resize to maximum + some extra space to handle error */
luaD_reallocstack(L, L->maxstacksize+4*LUA_MINSTACK);
luaD_reallocstack(L, LUA_MAXSTACK+4*LUA_MINSTACK);
luaD_error(L, "stack overflow");
}
}
@ -428,9 +428,13 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) {
f = fopen(filename, "rb"); /* reopen in binary mode */
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
}
lua_pushliteral(L, "@");
lua_pushstring(L, (filename == NULL) ? "=stdin" : filename);
lua_concat(L, 2);
if (filename == NULL)
lua_pushstring(L, "=stdin");
else {
lua_pushliteral(L, "@");
lua_pushstring(L, filename);
lua_concat(L, 2);
}
nlevel = lua_gettop(L);
filename = lua_tostring(L, -1); /* filename = `@'..filename */
luaZ_Fopen(&z, f, filename);

28
lstate.c

@ -21,22 +21,12 @@
struct Sopen {
lua_State *L;
int stacksize;
};
static void close_state (lua_State *L);
static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) {
if (maxstacksize == 0)
maxstacksize = DEFAULT_MAXSTACK;
else
maxstacksize += 2*LUA_MINSTACK;
static void stack_init (lua_State *L, lua_State *OL) {
L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
L->maxstacksize = maxstacksize;
L->stacksize = BASIC_STACK_SIZE;
L->top = L->stack + RESERVED_STACK_PREFIX;
L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1;
@ -53,7 +43,7 @@ static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) {
** open parts that may cause memory-allocation errors
*/
static void f_luaopen (lua_State *L, void *ud) {
struct Sopen *so = cast(struct Sopen *, ud);
UNUSED(ud);
/* create a new global state */
L->_G = luaM_new(L, global_State);
G(L)->strt.size = 0;
@ -68,7 +58,7 @@ static void f_luaopen (lua_State *L, void *ud) {
G(L)->rootudata = NULL;
G(L)->tmudata = NULL;
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
stack_init(L, L, so->stacksize); /* init stack */
stack_init(L, L); /* init stack */
/* create default meta table with a dummy table, and then close the loop */
sethvalue(defaultmeta(L), NULL);
sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
@ -85,7 +75,6 @@ static void f_luaopen (lua_State *L, void *ud) {
static void preinit_state (lua_State *L) {
L->stack = NULL;
L->stacksize = 0;
L->maxstacksize = 1;
L->errorJmp = NULL;
L->callhook = NULL;
L->linehook = NULL;
@ -96,7 +85,7 @@ static void preinit_state (lua_State *L) {
}
LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
LUA_API lua_State *lua_newthread (lua_State *OL) {
lua_State *L;
lua_lock(OL);
L = luaM_new(OL, lua_State);
@ -106,7 +95,7 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
L->next = OL->next;
OL->next = L;
L->previous = OL;
stack_init(L, OL, stacksize); /* init stack */
stack_init(L, OL); /* init stack */
setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
setobj(gt(L), gt(OL)); /* share table of globals */
setobj(registry(L), registry(OL)); /* share registry */
@ -116,17 +105,14 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
}
LUA_API lua_State *lua_open (int stacksize) {
struct Sopen so;
LUA_API lua_State *lua_open (void) {
lua_State *L;
L = luaM_new(NULL, lua_State);
if (L) { /* allocation OK? */
preinit_state(L);
L->_G = NULL;
L->next = L->previous = L;
so.stacksize = stacksize;
so.L = NULL;
if (luaD_runprotected(L, f_luaopen, &so) != 0) {
if (luaD_runprotected(L, f_luaopen, NULL) != 0) {
/* memory allocation error: free partial state */
close_state(L);
L = NULL;

6
lstate.h

@ -73,8 +73,9 @@ struct lua_longjmp; /* defined in ldo.c */
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
#define DEFAULT_MAXSTACK 12000
#ifndef LUA_MAXSTACK
#define LUA_MAXSTACK 14000
#endif
@ -141,7 +142,6 @@ struct lua_State {
lua_State *next; /* circular double linked list of states */
lua_State *previous;
int stacksize;
int maxstacksize;
int size_ci; /* size of array `base_ci' */
int allowhooks;
lua_Hook callhook;

8
ltests.c

@ -378,9 +378,9 @@ static int udataval (lua_State *L) {
static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
lua_dostring(L1, luaL_check_string(L, 2));
lua_pushnumber(L, 1);
lua_State *L1 = lua_newthread(L);
int status = lua_dostring(L1, luaL_check_string(L, 1));
lua_pushnumber(L, status);
lua_closethread(L, L1);
return 1;
}
@ -399,7 +399,7 @@ static int d2s (lua_State *L) {
static int newstate (lua_State *L) {
lua_State *L1 = lua_open(luaL_check_int(L, 1));
lua_State *L1 = lua_open();
if (L1) {
*cast(int **, L1) = &islocked; /* initialize the lock */
lua_pushnumber(L, (unsigned long)L1);

14
lua.h

@ -57,12 +57,12 @@ typedef int (*lua_CFunction) (lua_State *L);
*/
#define LUA_TNONE (-1)
#define LUA_TUSERDATA 0
#define LUA_TNIL 1
#define LUA_TNUMBER 2
#define LUA_TNIL 0
#define LUA_TNUMBER 1
#define LUA_TSTRING 2
#define LUA_TBOOLEAN 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TTABLE 4
#define LUA_TUSERDATA 5
#define LUA_TFUNCTION 6
@ -94,9 +94,9 @@ typedef LUA_NUMBER lua_Number;
/*
** state manipulation
*/
LUA_API lua_State *lua_open (int stacksize);
LUA_API lua_State *lua_open (void);
LUA_API void lua_close (lua_State *L);
LUA_API lua_State *lua_newthread (lua_State *L, int stacksize);
LUA_API lua_State *lua_newthread (lua_State *L);
LUA_API void lua_closethread (lua_State *L, lua_State *thread);

Loading…
Cancel
Save