From 053e87314596e48588a929791012def7818ec989 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Oct 2005 11:47:42 -0200 Subject: [PATCH] new macro luaL_opt to avoid evaluating defaults when no needed --- lauxlib.c | 10 ++++------ lauxlib.h | 3 ++- lbaselib.c | 10 ++++------ loslib.c | 6 +++--- ltablib.c | 9 ++++----- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index f68c3fba..34f1cc77 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.154 2005/10/19 13:05:11 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.155 2005/10/20 11:35:25 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -176,8 +176,7 @@ LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { - if (lua_isnoneornil(L, narg)) return def; - else return luaL_checknumber(L, narg); + return luaL_opt(L, luaL_checknumber, narg, def); } @@ -190,9 +189,8 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, - lua_Integer def) { - if (lua_isnoneornil(L, narg)) return def; - else return luaL_checkinteger(L, narg); + lua_Integer def) { + return luaL_opt(L, luaL_checkinteger, narg, def); } diff --git a/lauxlib.h b/lauxlib.h index f5ca0b44..7db3e95a 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.84 2005/08/26 17:36:32 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.85 2005/09/06 17:19:51 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -114,6 +114,7 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) /* ** {====================================================== diff --git a/lbaselib.c b/lbaselib.c index 35622402..1cd2e335 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.184 2005/10/03 14:36:45 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.185 2005/10/20 11:35:50 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -340,12 +340,10 @@ static int luaB_assert (lua_State *L) { static int luaB_unpack (lua_State *L) { - int i = luaL_optint(L, 2, 1); - int e = luaL_optint(L, 3, -1); - int n; + int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); - if (e == -1) - e = luaL_getn(L, 1); + i = luaL_optint(L, 2, 1); + e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); n = e - i + 1; /* number of elements */ if (n <= 0) return 0; /* empty range */ luaL_checkstack(L, n, "table too big to unpack"); diff --git a/loslib.c b/loslib.c index caf75543..d3cd31b8 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.12 2005/08/26 17:36:32 roberto Exp roberto $ +** $Id: loslib.c,v 1.13 2005/09/09 18:22:46 roberto Exp roberto $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -125,8 +125,8 @@ static int getfield (lua_State *L, const char *key, int d) { static int io_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); - lua_Number n = luaL_optnumber(L, 2, -1); - time_t t = (n == -1) ? time(NULL) : (time_t)n; + time_t t = lua_isnoneornil(L, 2) ? time(NULL) : + (time_t)luaL_checknumber(L, 2); struct tm *stm; if (*s == '!') { /* UTC? */ stm = gmtime(&t); diff --git a/ltablib.c b/ltablib.c index a8214d29..b8682b30 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.35 2005/08/26 17:36:32 roberto Exp roberto $ +** $Id: ltablib.c,v 1.36 2005/09/20 17:56:47 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -127,12 +127,11 @@ static int tremove (lua_State *L) { static int tconcat (lua_State *L) { luaL_Buffer b; size_t lsep; + int i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); - int i = luaL_optint(L, 3, 1); - int last = luaL_optint(L, 4, -2); luaL_checktype(L, 1, LUA_TTABLE); - if (last == -2) - last = luaL_getn(L, 1); + i = luaL_optint(L, 3, 1); + last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); luaL_buffinit(L, &b); for (; i <= last; i++) { lua_rawgeti(L, 1, i);