new macro luaL_opt to avoid evaluating defaults when no needed

This commit is contained in:
Roberto Ierusalimschy 2005-10-21 11:47:42 -02:00
parent 9f4211310f
commit 053e873145
5 changed files with 17 additions and 21 deletions

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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) { LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
if (lua_isnoneornil(L, narg)) return def; return luaL_opt(L, luaL_checknumber, narg, def);
else return luaL_checknumber(L, narg);
} }
@ -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, LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
lua_Integer def) { lua_Integer def) {
if (lua_isnoneornil(L, narg)) return def; return luaL_opt(L, luaL_checkinteger, narg, def);
else return luaL_checkinteger(L, narg);
} }

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
/* /*
** {====================================================== ** {======================================================

View File

@ -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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -340,12 +340,10 @@ static int luaB_assert (lua_State *L) {
static int luaB_unpack (lua_State *L) { static int luaB_unpack (lua_State *L) {
int i = luaL_optint(L, 2, 1); int i, e, n;
int e = luaL_optint(L, 3, -1);
int n;
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
if (e == -1) i = luaL_optint(L, 2, 1);
e = luaL_getn(L, 1); e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
n = e - i + 1; /* number of elements */ n = e - i + 1; /* number of elements */
if (n <= 0) return 0; /* empty range */ if (n <= 0) return 0; /* empty range */
luaL_checkstack(L, n, "table too big to unpack"); luaL_checkstack(L, n, "table too big to unpack");

View File

@ -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 ** Standard Operating System library
** See Copyright Notice in lua.h ** 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) { static int io_date (lua_State *L) {
const char *s = luaL_optstring(L, 1, "%c"); const char *s = luaL_optstring(L, 1, "%c");
lua_Number n = luaL_optnumber(L, 2, -1); time_t t = lua_isnoneornil(L, 2) ? time(NULL) :
time_t t = (n == -1) ? time(NULL) : (time_t)n; (time_t)luaL_checknumber(L, 2);
struct tm *stm; struct tm *stm;
if (*s == '!') { /* UTC? */ if (*s == '!') { /* UTC? */
stm = gmtime(&t); stm = gmtime(&t);

View File

@ -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 ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -127,12 +127,11 @@ static int tremove (lua_State *L) {
static int tconcat (lua_State *L) { static int tconcat (lua_State *L) {
luaL_Buffer b; luaL_Buffer b;
size_t lsep; size_t lsep;
int i, last;
const char *sep = luaL_optlstring(L, 2, "", &lsep); 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); luaL_checktype(L, 1, LUA_TTABLE);
if (last == -2) i = luaL_optint(L, 3, 1);
last = luaL_getn(L, 1); last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
for (; i <= last; i++) { for (; i <= last; i++) {
lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i);