diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 17:43:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 17:43:28 -0300 |
commit | a97f29f15487c5f584185f646f9cfc06a04b26ca (patch) | |
tree | e7ffd959fb80f9622d74a2efadfd7880037d64ca | |
parent | 4135f4f5862b57d9dbf3e9b7c6507f5c86516327 (diff) | |
download | lua-a97f29f15487c5f584185f646f9cfc06a04b26ca.tar.gz lua-a97f29f15487c5f584185f646f9cfc06a04b26ca.tar.bz2 lua-a97f29f15487c5f584185f646f9cfc06a04b26ca.zip |
explicit stack control in the API
-rw-r--r-- | lapi.c | 6 | ||||
-rw-r--r-- | lauxlib.c | 8 | ||||
-rw-r--r-- | lauxlib.h | 5 | ||||
-rw-r--r-- | ldo.c | 12 | ||||
-rw-r--r-- | liolib.c | 3 | ||||
-rw-r--r-- | lstrlib.c | 3 | ||||
-rw-r--r-- | lua.h | 6 |
7 files changed, 27 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.88 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.89 2000/08/29 14:52:27 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -45,6 +45,10 @@ void luaA_pushobject (lua_State *L, const TObject *o) { | |||
45 | incr_top; | 45 | incr_top; |
46 | } | 46 | } |
47 | 47 | ||
48 | int lua_stackspace (lua_State *L) { | ||
49 | return (L->stack_last - L->top); | ||
50 | } | ||
51 | |||
48 | 52 | ||
49 | 53 | ||
50 | /* | 54 | /* |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.31 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.32 2000/08/29 14:33:31 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -49,6 +49,12 @@ static void type_error (lua_State *L, int narg, const char *type_name) { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | 51 | ||
52 | void luaL_checkstack (lua_State *L, int space, const char *mes) { | ||
53 | if (space > lua_stackspace(L)) | ||
54 | luaL_verror(L, "stack overflow (%.30s)", mes); | ||
55 | } | ||
56 | |||
57 | |||
52 | /* | 58 | /* |
53 | ** use the 3rd letter of type names for testing: | 59 | ** use the 3rd letter of type names for testing: |
54 | ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY | 60 | ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.19 2000/08/09 19:16:57 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.20 2000/08/28 17:57:04 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -28,7 +28,8 @@ const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, | |||
28 | double luaL_check_number (lua_State *L, int numArg); | 28 | double luaL_check_number (lua_State *L, int numArg); |
29 | double luaL_opt_number (lua_State *L, int numArg, double def); | 29 | double luaL_opt_number (lua_State *L, int numArg, double def); |
30 | 30 | ||
31 | void luaL_checktype(lua_State *L, int narg, const char *tname); | 31 | void luaL_checkstack (lua_State *L, int space, const char *msg); |
32 | void luaL_checktype (lua_State *L, int narg, const char *tname); | ||
32 | 33 | ||
33 | void luaL_verror (lua_State *L, const char *fmt, ...); | 34 | void luaL_verror (lua_State *L, const char *fmt, ...); |
34 | int luaL_findstring (const char *name, const char *const list[]); | 35 | int luaL_findstring (const char *name, const char *const list[]); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.89 2000/08/29 14:57:23 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.90 2000/08/29 19:01:34 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 | */ |
@@ -53,14 +53,8 @@ void luaD_checkstack (lua_State *L, int n) { | |||
53 | lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!"); | 53 | lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!"); |
54 | } | 54 | } |
55 | else { | 55 | else { |
56 | lua_Debug dummy; | ||
57 | L->stack_last += EXTRA_STACK; /* to be used by error message */ | 56 | L->stack_last += EXTRA_STACK; /* to be used by error message */ |
58 | if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) { | 57 | lua_error(L, "stack overflow"); |
59 | /* too few funcs on stack: doesn't look like a recursion loop */ | ||
60 | lua_error(L, "Lua2C - C2Lua overflow"); | ||
61 | } | ||
62 | else | ||
63 | lua_error(L, "stack overflow; possible recursion loop"); | ||
64 | } | 58 | } |
65 | } | 59 | } |
66 | } | 60 | } |
@@ -140,7 +134,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | |||
140 | StkId old_Cbase = L->Cbase; | 134 | StkId old_Cbase = L->Cbase; |
141 | int n; | 135 | int n; |
142 | L->Cbase = base; /* new base for C function */ | 136 | L->Cbase = base; /* new base for C function */ |
143 | luaD_checkstack(L, nup); | 137 | luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */ |
144 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ | 138 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ |
145 | *(L->top++) = cl->upvalue[n]; | 139 | *(L->top++) = cl->upvalue[n]; |
146 | n = (*cl->f.c)(L); /* do the actual call */ | 140 | n = (*cl->f.c)(L); /* do the actual call */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.72 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.73 2000/08/29 14:33:31 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -372,6 +372,7 @@ static int io_read (lua_State *L) { | |||
372 | firstarg = lastarg = 1; /* correct indices */ | 372 | firstarg = lastarg = 1; /* correct indices */ |
373 | lua_pushstring(L, "*l"); /* push default argument */ | 373 | lua_pushstring(L, "*l"); /* push default argument */ |
374 | } | 374 | } |
375 | luaL_checkstack(L, lastarg-firstarg+1, "too many results"); | ||
375 | for (n = firstarg; n<=lastarg; n++) { | 376 | for (n = firstarg; n<=lastarg; n++) { |
376 | size_t l; | 377 | size_t l; |
377 | int success; | 378 | int success; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.46 2000/08/09 19:16:57 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.47 2000/08/28 17:57:04 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -400,6 +400,7 @@ static const char *lmemfind (const char *s1, size_t l1, | |||
400 | 400 | ||
401 | static int push_captures (lua_State *L, struct Capture *cap) { | 401 | static int push_captures (lua_State *L, struct Capture *cap) { |
402 | int i; | 402 | int i; |
403 | luaL_checkstack(L, cap->level, "too many captures"); | ||
403 | for (i=0; i<cap->level; i++) { | 404 | for (i=0; i<cap->level; i++) { |
404 | int l = cap->capture[i].len; | 405 | int l = cap->capture[i].len; |
405 | if (l == -1) lua_error(L, "unfinished capture"); | 406 | if (l == -1) lua_error(L, "unfinished capture"); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.60 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.61 2000/08/29 14:33:31 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 |
@@ -33,6 +33,9 @@ | |||
33 | #define LUA_MULTRET (-1) | 33 | #define LUA_MULTRET (-1) |
34 | 34 | ||
35 | 35 | ||
36 | #define LUA_MINSTACK 16 | ||
37 | |||
38 | |||
36 | /* error codes for lua_do* */ | 39 | /* error codes for lua_do* */ |
37 | #define LUA_ERRFILE 2 | 40 | #define LUA_ERRFILE 2 |
38 | #define LUA_ERRSYNTAX 3 | 41 | #define LUA_ERRSYNTAX 3 |
@@ -58,6 +61,7 @@ void lua_close (lua_State *L); | |||
58 | int lua_gettop (lua_State *L); | 61 | int lua_gettop (lua_State *L); |
59 | void lua_settop (lua_State *L, int index); | 62 | void lua_settop (lua_State *L, int index); |
60 | void lua_pushobject (lua_State *L, int index); | 63 | void lua_pushobject (lua_State *L, int index); |
64 | int lua_stackspace (lua_State *L); | ||
61 | 65 | ||
62 | 66 | ||
63 | /* | 67 | /* |