Browse Source

new macro luaL_opt to avoid evaluating defaults when no needed

Roberto Ierusalimschy 20 years ago
parent
commit
053e873145
5 changed files with 17 additions and 21 deletions
  1. 4 6
      lauxlib.c
  2. 2 1
      lauxlib.h
  3. 4 6
      lbaselib.c
  4. 3 3
      loslib.c
  5. 4 5
      ltablib.c

+ 4 - 6
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);
 }
 
 

+ 2 - 1
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)))
 
 /*
 ** {======================================================

+ 4 - 6
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");

+ 3 - 3
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);

+ 4 - 5
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);