diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-02-14 19:47:29 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-02-14 19:47:29 -0200 |
commit | b0a5e156b8e6913c6eb8ed65ab28576b8b88f461 (patch) | |
tree | 11b32d6436e2c663171127990509d091c1ea847d | |
parent | ac178ee4788c13761ec0850b31ff17673a33f227 (diff) | |
download | lua-b0a5e156b8e6913c6eb8ed65ab28576b8b88f461.tar.gz lua-b0a5e156b8e6913c6eb8ed65ab28576b8b88f461.tar.bz2 lua-b0a5e156b8e6913c6eb8ed65ab28576b8b88f461.zip |
no more maximum stack size
-rw-r--r-- | lbaselib.c | 2 | ||||
-rw-r--r-- | ldo.c | 26 | ||||
-rw-r--r-- | lstate.c | 28 | ||||
-rw-r--r-- | lstate.h | 6 | ||||
-rw-r--r-- | ltests.c | 8 | ||||
-rw-r--r-- | lua.h | 14 |
6 files changed, 37 insertions, 47 deletions
@@ -434,7 +434,7 @@ static int luaB_coroutine (lua_State *L) { | |||
434 | int n = lua_gettop(L); | 434 | int n = lua_gettop(L); |
435 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | 435 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, |
436 | "Lua function expected"); | 436 | "Lua function expected"); |
437 | NL = lua_newthread(L, 0); | 437 | NL = lua_newthread(L); |
438 | if (NL == NULL) lua_error(L, "unable to create new thread"); | 438 | if (NL == NULL) lua_error(L, "unable to create new thread"); |
439 | /* move function and arguments from L to NL */ | 439 | /* move function and arguments from L to NL */ |
440 | for (i=0; i<n; i++) { | 440 | for (i=0; i<n; i++) { |
@@ -71,26 +71,26 @@ void luaD_reallocstack (lua_State *L, int newsize) { | |||
71 | 71 | ||
72 | 72 | ||
73 | static void restore_stack_limit (lua_State *L) { | 73 | static void restore_stack_limit (lua_State *L) { |
74 | if (L->stacksize > L->maxstacksize) { /* there was an overflow? */ | 74 | if (L->stacksize > LUA_MAXSTACK) { /* there was an overflow? */ |
75 | int inuse = (L->top - L->stack); | 75 | int inuse = (L->top - L->stack); |
76 | if (inuse + MAXSTACK < L->maxstacksize) /* can `undo' overflow? */ | 76 | if (inuse + MAXSTACK < LUA_MAXSTACK) /* can `undo' overflow? */ |
77 | luaD_reallocstack(L, L->maxstacksize); | 77 | luaD_reallocstack(L, LUA_MAXSTACK); |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | 81 | ||
82 | void luaD_growstack (lua_State *L, int n) { | 82 | void luaD_growstack (lua_State *L, int n) { |
83 | if (L->stacksize > L->maxstacksize) { /* overflow while handling overflow? */ | 83 | if (L->stacksize > LUA_MAXSTACK) { /* overflow while handling overflow? */ |
84 | luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ | 84 | luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ |
85 | } | 85 | } |
86 | else { | 86 | else { |
87 | if (n <= L->stacksize && 2*L->stacksize < L->maxstacksize) /* can double? */ | 87 | if (n <= L->stacksize && 2*L->stacksize < LUA_MAXSTACK) /* can double? */ |
88 | luaD_reallocstack(L, 2*L->stacksize); | 88 | luaD_reallocstack(L, 2*L->stacksize); |
89 | else if (L->stacksize+n <= L->maxstacksize) /* no overflow? */ | 89 | else if (L->stacksize+n <= LUA_MAXSTACK) /* no overflow? */ |
90 | luaD_reallocstack(L, L->maxstacksize); | 90 | luaD_reallocstack(L, LUA_MAXSTACK); |
91 | else { | 91 | else { |
92 | /* resize to maximum + some extra space to handle error */ | 92 | /* resize to maximum + some extra space to handle error */ |
93 | luaD_reallocstack(L, L->maxstacksize+4*LUA_MINSTACK); | 93 | luaD_reallocstack(L, LUA_MAXSTACK+4*LUA_MINSTACK); |
94 | luaD_error(L, "stack overflow"); | 94 | luaD_error(L, "stack overflow"); |
95 | } | 95 | } |
96 | } | 96 | } |
@@ -428,9 +428,13 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) { | |||
428 | f = fopen(filename, "rb"); /* reopen in binary mode */ | 428 | f = fopen(filename, "rb"); /* reopen in binary mode */ |
429 | if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */ | 429 | if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */ |
430 | } | 430 | } |
431 | lua_pushliteral(L, "@"); | 431 | if (filename == NULL) |
432 | lua_pushstring(L, (filename == NULL) ? "=stdin" : filename); | 432 | lua_pushstring(L, "=stdin"); |
433 | lua_concat(L, 2); | 433 | else { |
434 | lua_pushliteral(L, "@"); | ||
435 | lua_pushstring(L, filename); | ||
436 | lua_concat(L, 2); | ||
437 | } | ||
434 | nlevel = lua_gettop(L); | 438 | nlevel = lua_gettop(L); |
435 | filename = lua_tostring(L, -1); /* filename = `@'..filename */ | 439 | filename = lua_tostring(L, -1); /* filename = `@'..filename */ |
436 | luaZ_Fopen(&z, f, filename); | 440 | luaZ_Fopen(&z, f, filename); |
@@ -21,22 +21,12 @@ | |||
21 | 21 | ||
22 | 22 | ||
23 | 23 | ||
24 | struct Sopen { | ||
25 | lua_State *L; | ||
26 | int stacksize; | ||
27 | }; | ||
28 | |||
29 | 24 | ||
30 | static void close_state (lua_State *L); | 25 | static void close_state (lua_State *L); |
31 | 26 | ||
32 | 27 | ||
33 | static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) { | 28 | static void stack_init (lua_State *L, lua_State *OL) { |
34 | if (maxstacksize == 0) | ||
35 | maxstacksize = DEFAULT_MAXSTACK; | ||
36 | else | ||
37 | maxstacksize += 2*LUA_MINSTACK; | ||
38 | L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject); | 29 | L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject); |
39 | L->maxstacksize = maxstacksize; | ||
40 | L->stacksize = BASIC_STACK_SIZE; | 30 | L->stacksize = BASIC_STACK_SIZE; |
41 | L->top = L->stack + RESERVED_STACK_PREFIX; | 31 | L->top = L->stack + RESERVED_STACK_PREFIX; |
42 | L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1; | 32 | 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) { | |||
53 | ** open parts that may cause memory-allocation errors | 43 | ** open parts that may cause memory-allocation errors |
54 | */ | 44 | */ |
55 | static void f_luaopen (lua_State *L, void *ud) { | 45 | static void f_luaopen (lua_State *L, void *ud) { |
56 | struct Sopen *so = cast(struct Sopen *, ud); | 46 | UNUSED(ud); |
57 | /* create a new global state */ | 47 | /* create a new global state */ |
58 | L->_G = luaM_new(L, global_State); | 48 | L->_G = luaM_new(L, global_State); |
59 | G(L)->strt.size = 0; | 49 | G(L)->strt.size = 0; |
@@ -68,7 +58,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
68 | G(L)->rootudata = NULL; | 58 | G(L)->rootudata = NULL; |
69 | G(L)->tmudata = NULL; | 59 | G(L)->tmudata = NULL; |
70 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); | 60 | G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); |
71 | stack_init(L, L, so->stacksize); /* init stack */ | 61 | stack_init(L, L); /* init stack */ |
72 | /* create default meta table with a dummy table, and then close the loop */ | 62 | /* create default meta table with a dummy table, and then close the loop */ |
73 | sethvalue(defaultmeta(L), NULL); | 63 | sethvalue(defaultmeta(L), NULL); |
74 | sethvalue(defaultmeta(L), luaH_new(L, 0, 4)); | 64 | sethvalue(defaultmeta(L), luaH_new(L, 0, 4)); |
@@ -85,7 +75,6 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
85 | static void preinit_state (lua_State *L) { | 75 | static void preinit_state (lua_State *L) { |
86 | L->stack = NULL; | 76 | L->stack = NULL; |
87 | L->stacksize = 0; | 77 | L->stacksize = 0; |
88 | L->maxstacksize = 1; | ||
89 | L->errorJmp = NULL; | 78 | L->errorJmp = NULL; |
90 | L->callhook = NULL; | 79 | L->callhook = NULL; |
91 | L->linehook = NULL; | 80 | L->linehook = NULL; |
@@ -96,7 +85,7 @@ static void preinit_state (lua_State *L) { | |||
96 | } | 85 | } |
97 | 86 | ||
98 | 87 | ||
99 | LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { | 88 | LUA_API lua_State *lua_newthread (lua_State *OL) { |
100 | lua_State *L; | 89 | lua_State *L; |
101 | lua_lock(OL); | 90 | lua_lock(OL); |
102 | L = luaM_new(OL, lua_State); | 91 | L = luaM_new(OL, lua_State); |
@@ -106,7 +95,7 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { | |||
106 | L->next = OL->next; | 95 | L->next = OL->next; |
107 | OL->next = L; | 96 | OL->next = L; |
108 | L->previous = OL; | 97 | L->previous = OL; |
109 | stack_init(L, OL, stacksize); /* init stack */ | 98 | stack_init(L, OL); /* init stack */ |
110 | setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */ | 99 | setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */ |
111 | setobj(gt(L), gt(OL)); /* share table of globals */ | 100 | setobj(gt(L), gt(OL)); /* share table of globals */ |
112 | setobj(registry(L), registry(OL)); /* share registry */ | 101 | setobj(registry(L), registry(OL)); /* share registry */ |
@@ -116,17 +105,14 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { | |||
116 | } | 105 | } |
117 | 106 | ||
118 | 107 | ||
119 | LUA_API lua_State *lua_open (int stacksize) { | 108 | LUA_API lua_State *lua_open (void) { |
120 | struct Sopen so; | ||
121 | lua_State *L; | 109 | lua_State *L; |
122 | L = luaM_new(NULL, lua_State); | 110 | L = luaM_new(NULL, lua_State); |
123 | if (L) { /* allocation OK? */ | 111 | if (L) { /* allocation OK? */ |
124 | preinit_state(L); | 112 | preinit_state(L); |
125 | L->_G = NULL; | 113 | L->_G = NULL; |
126 | L->next = L->previous = L; | 114 | L->next = L->previous = L; |
127 | so.stacksize = stacksize; | 115 | if (luaD_runprotected(L, f_luaopen, NULL) != 0) { |
128 | so.L = NULL; | ||
129 | if (luaD_runprotected(L, f_luaopen, &so) != 0) { | ||
130 | /* memory allocation error: free partial state */ | 116 | /* memory allocation error: free partial state */ |
131 | close_state(L); | 117 | close_state(L); |
132 | L = NULL; | 118 | L = NULL; |
@@ -73,8 +73,9 @@ struct lua_longjmp; /* defined in ldo.c */ | |||
73 | 73 | ||
74 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) | 74 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) |
75 | 75 | ||
76 | #define DEFAULT_MAXSTACK 12000 | 76 | #ifndef LUA_MAXSTACK |
77 | 77 | #define LUA_MAXSTACK 14000 | |
78 | #endif | ||
78 | 79 | ||
79 | 80 | ||
80 | 81 | ||
@@ -141,7 +142,6 @@ struct lua_State { | |||
141 | lua_State *next; /* circular double linked list of states */ | 142 | lua_State *next; /* circular double linked list of states */ |
142 | lua_State *previous; | 143 | lua_State *previous; |
143 | int stacksize; | 144 | int stacksize; |
144 | int maxstacksize; | ||
145 | int size_ci; /* size of array `base_ci' */ | 145 | int size_ci; /* size of array `base_ci' */ |
146 | int allowhooks; | 146 | int allowhooks; |
147 | lua_Hook callhook; | 147 | lua_Hook callhook; |
@@ -378,9 +378,9 @@ static int udataval (lua_State *L) { | |||
378 | 378 | ||
379 | 379 | ||
380 | static int doonnewstack (lua_State *L) { | 380 | static int doonnewstack (lua_State *L) { |
381 | lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1)); | 381 | lua_State *L1 = lua_newthread(L); |
382 | lua_dostring(L1, luaL_check_string(L, 2)); | 382 | int status = lua_dostring(L1, luaL_check_string(L, 1)); |
383 | lua_pushnumber(L, 1); | 383 | lua_pushnumber(L, status); |
384 | lua_closethread(L, L1); | 384 | lua_closethread(L, L1); |
385 | return 1; | 385 | return 1; |
386 | } | 386 | } |
@@ -399,7 +399,7 @@ static int d2s (lua_State *L) { | |||
399 | 399 | ||
400 | 400 | ||
401 | static int newstate (lua_State *L) { | 401 | static int newstate (lua_State *L) { |
402 | lua_State *L1 = lua_open(luaL_check_int(L, 1)); | 402 | lua_State *L1 = lua_open(); |
403 | if (L1) { | 403 | if (L1) { |
404 | *cast(int **, L1) = &islocked; /* initialize the lock */ | 404 | *cast(int **, L1) = &islocked; /* initialize the lock */ |
405 | lua_pushnumber(L, (unsigned long)L1); | 405 | lua_pushnumber(L, (unsigned long)L1); |
@@ -57,12 +57,12 @@ typedef int (*lua_CFunction) (lua_State *L); | |||
57 | */ | 57 | */ |
58 | #define LUA_TNONE (-1) | 58 | #define LUA_TNONE (-1) |
59 | 59 | ||
60 | #define LUA_TUSERDATA 0 | 60 | #define LUA_TNIL 0 |
61 | #define LUA_TNIL 1 | 61 | #define LUA_TNUMBER 1 |
62 | #define LUA_TNUMBER 2 | 62 | #define LUA_TSTRING 2 |
63 | #define LUA_TBOOLEAN 3 | 63 | #define LUA_TBOOLEAN 3 |
64 | #define LUA_TSTRING 4 | 64 | #define LUA_TTABLE 4 |
65 | #define LUA_TTABLE 5 | 65 | #define LUA_TUSERDATA 5 |
66 | #define LUA_TFUNCTION 6 | 66 | #define LUA_TFUNCTION 6 |
67 | 67 | ||
68 | 68 | ||
@@ -94,9 +94,9 @@ typedef LUA_NUMBER lua_Number; | |||
94 | /* | 94 | /* |
95 | ** state manipulation | 95 | ** state manipulation |
96 | */ | 96 | */ |
97 | LUA_API lua_State *lua_open (int stacksize); | 97 | LUA_API lua_State *lua_open (void); |
98 | LUA_API void lua_close (lua_State *L); | 98 | LUA_API void lua_close (lua_State *L); |
99 | LUA_API lua_State *lua_newthread (lua_State *L, int stacksize); | 99 | LUA_API lua_State *lua_newthread (lua_State *L); |
100 | LUA_API void lua_closethread (lua_State *L, lua_State *thread); | 100 | LUA_API void lua_closethread (lua_State *L, lua_State *thread); |
101 | 101 | ||
102 | 102 | ||