diff options
| -rw-r--r-- | lbaselib.c | 49 |
1 files changed, 31 insertions, 18 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.90 2002/07/04 17:58:02 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.91 2002/07/17 16:25:13 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -214,10 +214,9 @@ static int luaB_ipairs (lua_State *L) { | |||
| 214 | static int passresults (lua_State *L, int status) { | 214 | static int passresults (lua_State *L, int status) { |
| 215 | if (status == 0) return 1; | 215 | if (status == 0) return 1; |
| 216 | else { | 216 | else { |
| 217 | int numres = (status == LUA_ERRRUN) ? 3 : 2; | ||
| 218 | lua_pushnil(L); | 217 | lua_pushnil(L); |
| 219 | lua_insert(L, -numres); | 218 | lua_insert(L, -2); |
| 220 | return numres; | 219 | return 2; |
| 221 | } | 220 | } |
| 222 | } | 221 | } |
| 223 | 222 | ||
| @@ -278,13 +277,33 @@ static int luaB_pcall (lua_State *L) { | |||
| 278 | int status; | 277 | int status; |
| 279 | luaL_check_any(L, 1); | 278 | luaL_check_any(L, 1); |
| 280 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET); | 279 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET); |
| 281 | if (status != 0) | 280 | if (status) /* error? */ |
| 282 | return passresults(L, status); | 281 | lua_pcallreset(L); /* reset error handler */ |
| 283 | else { | 282 | lua_pushboolean(L, (status == 0)); |
| 284 | lua_pushboolean(L, 1); | 283 | lua_insert(L, 1); |
| 285 | lua_insert(L, 1); | 284 | return lua_gettop(L); /* return status + all results */ |
| 286 | return lua_gettop(L); /* return `true' + all results */ | 285 | } |
| 286 | |||
| 287 | |||
| 288 | static int luaB_xpcall (lua_State *L) { | ||
| 289 | int status; | ||
| 290 | int ref; | ||
| 291 | luaL_check_any(L, 2); | ||
| 292 | lua_settop(L, 2); | ||
| 293 | ref = lua_ref(L, 1); /* save error function */ | ||
| 294 | status = lua_pcall(L, 0, LUA_MULTRET); | ||
| 295 | if (status) { /* error? */ | ||
| 296 | if (status == LUA_ERRRUN) { /* run-time error? */ | ||
| 297 | lua_getref(L, ref); /* get error function */ | ||
| 298 | lua_pushvalue(L, -2); /* error message */ | ||
| 299 | lua_call(L, 1, 1); /* call error function */ | ||
| 300 | } | ||
| 301 | lua_pcallreset(L); /* reset error handler */ | ||
| 287 | } | 302 | } |
| 303 | lua_unref(L, ref); /* free reference */ | ||
| 304 | lua_pushboolean(L, (status == 0)); | ||
| 305 | lua_insert(L, 1); | ||
| 306 | return lua_gettop(L); /* return status + all results */ | ||
| 288 | } | 307 | } |
| 289 | 308 | ||
| 290 | 309 | ||
| @@ -466,6 +485,7 @@ static const luaL_reg base_funcs[] = { | |||
| 466 | {"rawget", luaB_rawget}, | 485 | {"rawget", luaB_rawget}, |
| 467 | {"rawset", luaB_rawset}, | 486 | {"rawset", luaB_rawset}, |
| 468 | {"pcall", luaB_pcall}, | 487 | {"pcall", luaB_pcall}, |
| 488 | {"xpcall", luaB_xpcall}, | ||
| 469 | {"collectgarbage", luaB_collectgarbage}, | 489 | {"collectgarbage", luaB_collectgarbage}, |
| 470 | {"gcinfo", luaB_gcinfo}, | 490 | {"gcinfo", luaB_gcinfo}, |
| 471 | {"loadfile", luaB_loadfile}, | 491 | {"loadfile", luaB_loadfile}, |
| @@ -488,15 +508,8 @@ static int luaB_resume (lua_State *L) { | |||
| 488 | int status; | 508 | int status; |
| 489 | lua_settop(L, 0); | 509 | lua_settop(L, 0); |
| 490 | status = lua_resume(L, co); | 510 | status = lua_resume(L, co); |
| 491 | if (status != 0) { | 511 | if (status != 0) |
| 492 | if (status == LUA_ERRRUN) { | ||
| 493 | if (lua_isstring(L, -1) && lua_isstring(L, -2)) | ||
| 494 | lua_concat(L, 2); | ||
| 495 | else | ||
| 496 | lua_pop(L, 1); | ||
| 497 | } | ||
| 498 | return lua_error(L); | 512 | return lua_error(L); |
| 499 | } | ||
| 500 | return lua_gettop(L); | 513 | return lua_gettop(L); |
| 501 | } | 514 | } |
| 502 | 515 | ||
