diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-08 14:15:43 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-08 14:15:43 -0200 |
| commit | ed117bb42afc67c83c677347842b35f1ebd51877 (patch) | |
| tree | 37c13ba6af472040453f6390dc15d4603aea22c9 | |
| parent | a4472490bc4b2c5802b98d0b27b77a4353eff867 (diff) | |
| download | lua-ed117bb42afc67c83c677347842b35f1ebd51877.tar.gz lua-ed117bb42afc67c83c677347842b35f1ebd51877.tar.bz2 lua-ed117bb42afc67c83c677347842b35f1ebd51877.zip | |
lua_checkstack does not raise memory errors (instead it signals them)
| -rw-r--r-- | lapi.c | 22 |
1 files changed, 17 insertions, 5 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.101 2009/11/27 15:37:59 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.102 2009/12/07 15:49:47 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 | */ |
| @@ -81,16 +81,28 @@ static Table *getcurrenv (lua_State *L) { | |||
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | 83 | ||
| 84 | /* | ||
| 85 | ** to be caled by 'lua_checkstack' in protected mode, to grow stack | ||
| 86 | ** capturing memory errors | ||
| 87 | */ | ||
| 88 | static void growstack (lua_State *L, void *ud) { | ||
| 89 | int size = *(int *)ud; | ||
| 90 | luaD_growstack(L, size); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 84 | LUA_API int lua_checkstack (lua_State *L, int size) { | 94 | LUA_API int lua_checkstack (lua_State *L, int size) { |
| 85 | int res = 1; | 95 | int res; |
| 86 | CallInfo *ci = L->ci; | 96 | CallInfo *ci = L->ci; |
| 87 | lua_lock(L); | 97 | lua_lock(L); |
| 88 | if (L->stack_last - L->top <= size) { /* need to grow stack? */ | 98 | if (L->stack_last - L->top > size) /* stack large enough? */ |
| 99 | res = 1; /* yes; check is OK */ | ||
| 100 | else { /* no; need to grow stack */ | ||
| 89 | int inuse = L->top - L->stack + EXTRA_STACK; | 101 | int inuse = L->top - L->stack + EXTRA_STACK; |
| 90 | if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */ | 102 | if (inuse > LUAI_MAXSTACK - size) /* can grow without overflow? */ |
| 91 | res = 0; /* no */ | 103 | res = 0; /* no */ |
| 92 | else | 104 | else /* try to grow stack */ |
| 93 | luaD_growstack(L, size); | 105 | res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK); |
| 94 | } | 106 | } |
| 95 | if (res && ci->top < L->top + size) | 107 | if (res && ci->top < L->top + size) |
| 96 | ci->top = L->top + size; /* adjust frame top */ | 108 | ci->top = L->top + size; /* adjust frame top */ |
