diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-18 12:19:27 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-18 12:19:27 -0300 |
| commit | 1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b (patch) | |
| tree | e845de9ee24b94362146e38410ae0670df18526b /lbaselib.c | |
| parent | 8f080fd683d63b0cd4b38380f6a5bdae5d6e2584 (diff) | |
| download | lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.tar.gz lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.tar.bz2 lua-1dbe708aa84f3a1e51daf8d7e2f714e2b02f554b.zip | |
new protocol for error handling
Diffstat (limited to 'lbaselib.c')
| -rw-r--r-- | lbaselib.c | 54 |
1 files changed, 24 insertions, 30 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.80 2002/06/13 13:39:55 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.81 2002/06/13 13:44:50 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 | */ |
| @@ -37,7 +37,7 @@ static int luaB_print (lua_State *L) { | |||
| 37 | lua_upcall(L, 1, 1); | 37 | lua_upcall(L, 1, 1); |
| 38 | s = lua_tostring(L, -1); /* get result */ | 38 | s = lua_tostring(L, -1); /* get result */ |
| 39 | if (s == NULL) | 39 | if (s == NULL) |
| 40 | return luaL_verror(L, "`tostring' must return a string to `print'"); | 40 | return luaL_error(L, "`tostring' must return a string to `print'"); |
| 41 | if (i>1) fputs("\t", stdout); | 41 | if (i>1) fputs("\t", stdout); |
| 42 | fputs(s, stdout); | 42 | fputs(s, stdout); |
| 43 | lua_pop(L, 1); /* pop result */ | 43 | lua_pop(L, 1); /* pop result */ |
| @@ -76,8 +76,8 @@ static int luaB_tonumber (lua_State *L) { | |||
| 76 | 76 | ||
| 77 | 77 | ||
| 78 | static int luaB_error (lua_State *L) { | 78 | static int luaB_error (lua_State *L) { |
| 79 | lua_settop(L, 1); | 79 | luaL_check_any(L, 1); |
| 80 | return lua_errorobj(L); | 80 | return lua_error(L); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | 83 | ||
| @@ -193,9 +193,10 @@ static int luaB_nexti (lua_State *L) { | |||
| 193 | static int passresults (lua_State *L, int status) { | 193 | static int passresults (lua_State *L, int status) { |
| 194 | if (status == 0) return 1; | 194 | if (status == 0) return 1; |
| 195 | else { | 195 | else { |
| 196 | int numres = (status == LUA_ERRRUN) ? 3 : 2; | ||
| 196 | lua_pushnil(L); | 197 | lua_pushnil(L); |
| 197 | lua_insert(L, -2); | 198 | lua_insert(L, -numres); |
| 198 | return 2; | 199 | return numres; |
| 199 | } | 200 | } |
| 200 | } | 201 | } |
| 201 | 202 | ||
| @@ -217,7 +218,7 @@ static int luaB_loadfile (lua_State *L) { | |||
| 217 | static int luaB_assert (lua_State *L) { | 218 | static int luaB_assert (lua_State *L) { |
| 218 | luaL_check_any(L, 1); | 219 | luaL_check_any(L, 1); |
| 219 | if (!lua_toboolean(L, 1)) | 220 | if (!lua_toboolean(L, 1)) |
| 220 | return luaL_verror(L, "%s", luaL_opt_string(L, 2, "assertion failed!")); | 221 | return luaL_error(L, "%s", luaL_opt_string(L, 2, "assertion failed!")); |
| 221 | lua_settop(L, 1); | 222 | lua_settop(L, 1); |
| 222 | return 1; | 223 | return 1; |
| 223 | } | 224 | } |
| @@ -234,25 +235,10 @@ static int luaB_unpack (lua_State *L) { | |||
| 234 | } | 235 | } |
| 235 | 236 | ||
| 236 | 237 | ||
| 237 | static int luaB_xpcall (lua_State *L) { | ||
| 238 | int status; | ||
| 239 | luaL_check_any(L, 1); | ||
| 240 | luaL_check_any(L, 2); | ||
| 241 | status = lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET, 1); | ||
| 242 | if (status != 0) | ||
| 243 | return passresults(L, status); | ||
| 244 | else { | ||
| 245 | lua_pushboolean(L, 1); | ||
| 246 | lua_replace(L, 1); | ||
| 247 | return lua_gettop(L); /* return `true' + all results */ | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | static int luaB_pcall (lua_State *L) { | 238 | static int luaB_pcall (lua_State *L) { |
| 253 | int status; | 239 | int status; |
| 254 | luaL_check_any(L, 1); | 240 | luaL_check_any(L, 1); |
| 255 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); | 241 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET); |
| 256 | if (status != 0) | 242 | if (status != 0) |
| 257 | return passresults(L, status); | 243 | return passresults(L, status); |
| 258 | else { | 244 | else { |
| @@ -362,7 +348,7 @@ static int luaB_require (lua_State *L) { | |||
| 362 | lua_pushvalue(L, 1); | 348 | lua_pushvalue(L, 1); |
| 363 | lua_setglobal(L, "_REQUIREDNAME"); | 349 | lua_setglobal(L, "_REQUIREDNAME"); |
| 364 | lua_getglobal(L, REQTAB); | 350 | lua_getglobal(L, REQTAB); |
| 365 | if (!lua_istable(L, 2)) return luaL_verror(L, REQTAB " is not a table"); | 351 | if (!lua_istable(L, 2)) return luaL_error(L, REQTAB " is not a table"); |
| 366 | path = getpath(L); | 352 | path = getpath(L); |
| 367 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ | 353 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ |
| 368 | lua_gettable(L, 2); | 354 | lua_gettable(L, 2); |
| @@ -385,11 +371,11 @@ static int luaB_require (lua_State *L) { | |||
| 385 | return 0; | 371 | return 0; |
| 386 | } | 372 | } |
| 387 | case LUA_ERRFILE: { /* file not found */ | 373 | case LUA_ERRFILE: { /* file not found */ |
| 388 | return luaL_verror(L, "could not load package `%s' from path `%s'", | 374 | return luaL_error(L, "could not load package `%s' from path `%s'", |
| 389 | lua_tostring(L, 1), getpath(L)); | 375 | lua_tostring(L, 1), getpath(L)); |
| 390 | } | 376 | } |
| 391 | default: { | 377 | default: { |
| 392 | return luaL_verror(L, "error loading package\n%s", lua_tostring(L, -1)); | 378 | return luaL_error(L, "error loading package\n%s", lua_tostring(L, -1)); |
| 393 | } | 379 | } |
| 394 | } | 380 | } |
| 395 | } | 381 | } |
| @@ -413,7 +399,6 @@ static const luaL_reg base_funcs[] = { | |||
| 413 | {"rawget", luaB_rawget}, | 399 | {"rawget", luaB_rawget}, |
| 414 | {"rawset", luaB_rawset}, | 400 | {"rawset", luaB_rawset}, |
| 415 | {"pcall", luaB_pcall}, | 401 | {"pcall", luaB_pcall}, |
| 416 | {"xpcall", luaB_xpcall}, | ||
| 417 | {"collectgarbage", luaB_collectgarbage}, | 402 | {"collectgarbage", luaB_collectgarbage}, |
| 418 | {"gcinfo", luaB_gcinfo}, | 403 | {"gcinfo", luaB_gcinfo}, |
| 419 | {"loadfile", luaB_loadfile}, | 404 | {"loadfile", luaB_loadfile}, |
| @@ -432,9 +417,18 @@ static const luaL_reg base_funcs[] = { | |||
| 432 | 417 | ||
| 433 | static int luaB_resume (lua_State *L) { | 418 | static int luaB_resume (lua_State *L) { |
| 434 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); | 419 | lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1)); |
| 420 | int status; | ||
| 435 | lua_settop(L, 0); | 421 | lua_settop(L, 0); |
| 436 | if (lua_resume(L, co) != 0) | 422 | status = lua_resume(L, co); |
| 437 | return lua_errorobj(L); | 423 | if (status != 0) { |
| 424 | if (status == LUA_ERRRUN) { | ||
| 425 | if (lua_isstring(L, -1) && lua_isstring(L, -2)) | ||
| 426 | lua_concat(L, 2); | ||
| 427 | else | ||
| 428 | lua_pop(L, 1); | ||
| 429 | } | ||
| 430 | return lua_error(L); | ||
| 431 | } | ||
| 438 | return lua_gettop(L); | 432 | return lua_gettop(L); |
| 439 | } | 433 | } |
| 440 | 434 | ||
| @@ -455,7 +449,7 @@ static int luaB_coroutine (lua_State *L) { | |||
| 455 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, | 449 | luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, |
| 456 | "Lua function expected"); | 450 | "Lua function expected"); |
| 457 | NL = lua_newthread(L); | 451 | NL = lua_newthread(L); |
| 458 | if (NL == NULL) return luaL_verror(L, "unable to create new thread"); | 452 | if (NL == NULL) return luaL_error(L, "unable to create new thread"); |
| 459 | /* move function and arguments from L to NL */ | 453 | /* move function and arguments from L to NL */ |
| 460 | for (i=0; i<n; i++) { | 454 | for (i=0; i<n; i++) { |
| 461 | ref = lua_ref(L, 1); | 455 | ref = lua_ref(L, 1); |
