Keep memory errors as memory errors · lua/lua@b57574d · GitHub
Skip to content

Commit b57574d

Browse files
committed
Keep memory errors as memory errors
Allow memory errors to be raised through the API (throwing the error with the memory error message); error in external allocations raises a memory error; memory errors in coroutines are re-raised as memory errors.
1 parent bfcf06d commit b57574d

4 files changed

Lines changed: 75 additions & 23 deletions

File tree

‎lapi.c‎

Lines changed: 7 additions & 1 deletion

‎lauxlib.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,10 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
475475
lua_Alloc allocf = lua_getallocf(L, &ud);
476476
UBox *box = (UBox *)lua_touserdata(L, idx);
477477
void *temp = allocf(ud, box->box, box->bsize, newsize);
478-
if (temp == NULL && newsize > 0) /* allocation error? */
479-
luaL_error(L, "not enough memory");
478+
if (temp == NULL && newsize > 0) { /* allocation error? */
479+
lua_pushliteral(L, "not enough memory");
480+
lua_error(L); /* raise a memory error */
481+
}
480482
box->box = temp;
481483
box->bsize = newsize;
482484
return temp;

‎lcorolib.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ static int luaB_coresume (lua_State *L) {
7373
static int luaB_auxwrap (lua_State *L) {
7474
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
7575
int r = auxresume(L, co, lua_gettop(L));
76-
if (r < 0) {
76+
if (r < 0) { /* error? */
7777
int stat = lua_status(co);
78-
if (stat != LUA_OK && stat != LUA_YIELD)
79-
lua_resetthread(co); /* close variables in case of errors */
80-
if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
78+
if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */
79+
lua_resetthread(co); /* close its tbc variables */
80+
if (stat != LUA_ERRMEM && /* not a memory error and ... */
81+
lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
8182
luaL_where(L, 1); /* add extra info, if available */
8283
lua_insert(L, -2);
8384
lua_concat(L, 2);

‎testes/api.lua‎

Lines changed: 59 additions & 16 deletions

0 commit comments

Comments
 (0)