aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-10-21 11:47:42 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-10-21 11:47:42 -0200
commit053e87314596e48588a929791012def7818ec989 (patch)
treee5ce2b8bdf03da3dbdac087bf62824b49e7d40f0
parent9f4211310fcea9ebf08f4884f8665520c1b8d85f (diff)
downloadlua-053e87314596e48588a929791012def7818ec989.tar.gz
lua-053e87314596e48588a929791012def7818ec989.tar.bz2
lua-053e87314596e48588a929791012def7818ec989.zip
new macro luaL_opt to avoid evaluating defaults when no needed
-rw-r--r--lauxlib.c10
-rw-r--r--lauxlib.h3
-rw-r--r--lbaselib.c10
-rw-r--r--loslib.c6
-rw-r--r--ltablib.c9
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 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.154 2005/10/19 13:05:11 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.155 2005/10/20 11:35:25 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -176,8 +176,7 @@ LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
176 176
177 177
178LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { 178LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
179 if (lua_isnoneornil(L, narg)) return def; 179 return luaL_opt(L, luaL_checknumber, narg, def);
180 else return luaL_checknumber(L, narg);
181} 180}
182 181
183 182
@@ -190,9 +189,8 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
190 189
191 190
192LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, 191LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
193 lua_Integer def) { 192 lua_Integer def) {
194 if (lua_isnoneornil(L, narg)) return def; 193 return luaL_opt(L, luaL_checkinteger, narg, def);
195 else return luaL_checkinteger(L, narg);
196} 194}
197 195
198 196
diff --git a/lauxlib.h b/lauxlib.h
index f5ca0b44..7db3e95a 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.84 2005/08/26 17:36:32 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.85 2005/09/06 17:19:51 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -114,6 +114,7 @@ LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
114 114
115#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 115#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
116 116
117#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
117 118
118/* 119/*
119** {====================================================== 120** {======================================================
diff --git a/lbaselib.c b/lbaselib.c
index 35622402..1cd2e335 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.184 2005/10/03 14:36:45 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.185 2005/10/20 11:35: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*/
@@ -340,12 +340,10 @@ static int luaB_assert (lua_State *L) {
340 340
341 341
342static int luaB_unpack (lua_State *L) { 342static int luaB_unpack (lua_State *L) {
343 int i = luaL_optint(L, 2, 1); 343 int i, e, n;
344 int e = luaL_optint(L, 3, -1);
345 int n;
346 luaL_checktype(L, 1, LUA_TTABLE); 344 luaL_checktype(L, 1, LUA_TTABLE);
347 if (e == -1) 345 i = luaL_optint(L, 2, 1);
348 e = luaL_getn(L, 1); 346 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
349 n = e - i + 1; /* number of elements */ 347 n = e - i + 1; /* number of elements */
350 if (n <= 0) return 0; /* empty range */ 348 if (n <= 0) return 0; /* empty range */
351 luaL_checkstack(L, n, "table too big to unpack"); 349 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 @@
1/* 1/*
2** $Id: loslib.c,v 1.12 2005/08/26 17:36:32 roberto Exp roberto $ 2** $Id: loslib.c,v 1.13 2005/09/09 18:22:46 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -125,8 +125,8 @@ static int getfield (lua_State *L, const char *key, int d) {
125 125
126static int io_date (lua_State *L) { 126static int io_date (lua_State *L) {
127 const char *s = luaL_optstring(L, 1, "%c"); 127 const char *s = luaL_optstring(L, 1, "%c");
128 lua_Number n = luaL_optnumber(L, 2, -1); 128 time_t t = lua_isnoneornil(L, 2) ? time(NULL) :
129 time_t t = (n == -1) ? time(NULL) : (time_t)n; 129 (time_t)luaL_checknumber(L, 2);
130 struct tm *stm; 130 struct tm *stm;
131 if (*s == '!') { /* UTC? */ 131 if (*s == '!') { /* UTC? */
132 stm = gmtime(&t); 132 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 @@
1/* 1/*
2** $Id: ltablib.c,v 1.35 2005/08/26 17:36:32 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.36 2005/09/20 17:56:47 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -127,12 +127,11 @@ static int tremove (lua_State *L) {
127static int tconcat (lua_State *L) { 127static int tconcat (lua_State *L) {
128 luaL_Buffer b; 128 luaL_Buffer b;
129 size_t lsep; 129 size_t lsep;
130 int i, last;
130 const char *sep = luaL_optlstring(L, 2, "", &lsep); 131 const char *sep = luaL_optlstring(L, 2, "", &lsep);
131 int i = luaL_optint(L, 3, 1);
132 int last = luaL_optint(L, 4, -2);
133 luaL_checktype(L, 1, LUA_TTABLE); 132 luaL_checktype(L, 1, LUA_TTABLE);
134 if (last == -2) 133 i = luaL_optint(L, 3, 1);
135 last = luaL_getn(L, 1); 134 last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
136 luaL_buffinit(L, &b); 135 luaL_buffinit(L, &b);
137 for (; i <= last; i++) { 136 for (; i <= last; i++) {
138 lua_rawgeti(L, 1, i); 137 lua_rawgeti(L, 1, i);