[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [PROPOSAL] more luaL_check* in lauxlib.h (for Lua 5.2)
- From: François Perrad <francois.perrad@...>
- Date: Tue, 31 May 2011 10:55:36 +0200
I propose the addition of the following macros in lauxlib.h :
#define luaL_checkboolean(L,n) (luaL_checktype(L, (n), LUA_TBOOLEAN))
#define luaL_checkfunction(L,n) (luaL_checktype(L, (n), LUA_TFUNCTION))
#define luaL_checklightuserdata(L,n) (luaL_checktype(L, (n), LUA_TLIGHTUSERDATA))
#define luaL_checknil(L,n) (luaL_checktype(L, (n), LUA_TNIL))
#define luaL_checktable(L,n) (luaL_checktype(L, (n), LUA_TTABLE))
#define luaL_checkthread(L,n) (luaL_checktype(L, (n), LUA_TTHREAD))
#define luaL_checkuserdata(L,n) (luaL_checktype(L, (n), LUA_TUSERDATA))
It is more consistent with the lua_is* functions/macros in lua.h
LUA_API int (lua_isnumber) (lua_State *L, int idx);
LUA_API int (lua_isstring) (lua_State *L, int idx);
LUA_API int (lua_iscfunction) (lua_State *L, int idx);
LUA_API int (lua_isuserdata) (lua_State *L, int idx);
...
#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
And the Lua internals are more hidden in libraries or extensions.
The attached patch (against Lua 5.2.0 alpha) shows the use of these macros in the standard libraries.
François Perrad.
From 600bcc5996707112a28d97cfaf5b947c6c958db8 Mon Sep 17 00:00:00 2001
From: Francois Perrad <francois.perrad@gadz.org>
Date: Mon, 30 May 2011 16:41:28 +0200
Subject: [PATCH] more luaL_check*
---
src/lauxlib.h | 7 +++++++
src/lbaselib.c | 14 +++++++-------
src/lcorolib.c | 2 +-
src/ldblib.c | 10 +++++-----
src/loadlib.c | 2 +-
src/loslib.c | 2 +-
src/lstrlib.c | 4 ++--
src/ltablib.c | 10 +++++-----
8 files changed, 29 insertions(+), 22 deletions(-)
diff --git a/src/lauxlib.h b/src/lauxlib.h
index 3950645..c43b457 100644
--- a/src/lauxlib.h
+++ b/src/lauxlib.h
@@ -111,6 +111,13 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
+#define luaL_checkboolean(L,n) (luaL_checktype(L, (n), LUA_TBOOLEAN))
+#define luaL_checkfunction(L,n) (luaL_checktype(L, (n), LUA_TFUNCTION))
+#define luaL_checklightuserdata(L,n) (luaL_checktype(L, (n), LUA_TLIGHTUSERDATA))
+#define luaL_checknil(L,n) (luaL_checktype(L, (n), LUA_TNIL))
+#define luaL_checktable(L,n) (luaL_checktype(L, (n), LUA_TTABLE))
+#define luaL_checkthread(L,n) (luaL_checktype(L, (n), LUA_TTHREAD))
+#define luaL_checkuserdata(L,n) (luaL_checktype(L, (n), LUA_TUSERDATA))
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
diff --git a/src/lbaselib.c b/src/lbaselib.c
index 1553c1d..69dbb93 100644
--- a/src/lbaselib.c
+++ b/src/lbaselib.c
@@ -99,7 +99,7 @@ static int luaB_getmetatable (lua_State *L) {
static int luaB_setmetatable (lua_State *L) {
int t = lua_type(L, 2);
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected");
if (luaL_getmetafield(L, 1, "__metatable"))
@@ -126,7 +126,7 @@ static int luaB_rawequal (lua_State *L) {
static int luaB_rawget (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
luaL_checkany(L, 2);
lua_settop(L, 2);
lua_rawget(L, 1);
@@ -134,7 +134,7 @@ static int luaB_rawget (lua_State *L) {
}
static int luaB_rawset (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_settop(L, 3);
@@ -182,7 +182,7 @@ static int luaB_type (lua_State *L) {
static int pairsmeta (lua_State *L, const char *method, int iszero,
lua_CFunction iter) {
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
- luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
+ luaL_checktable(L, 1); /* argument must be a table */
lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
if (iszero) lua_pushinteger(L, 0); /* and initial value */
@@ -197,7 +197,7 @@ static int pairsmeta (lua_State *L, const char *method, int iszero,
static int luaB_next (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
@@ -215,7 +215,7 @@ static int luaB_pairs (lua_State *L) {
static int ipairsaux (lua_State *L) {
int i = luaL_checkint(L, 2);
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
i++; /* next value */
lua_pushinteger(L, i);
lua_rawgeti(L, 1, i);
@@ -317,7 +317,7 @@ static int luaB_load_aux (lua_State *L, int farg) {
}
else { /* loading from a reader function */
const char *chunkname = luaL_optstring(L, farg + 1, "=(load)");
- luaL_checktype(L, farg, LUA_TFUNCTION);
+ luaL_checkfunction(L, farg);
stat.f = farg;
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
status = lua_load(L, generic_reader, &stat, chunkname);
diff --git a/src/lcorolib.c b/src/lcorolib.c
index 981ca38..bc6f6b3 100644
--- a/src/lcorolib.c
+++ b/src/lcorolib.c
@@ -81,7 +81,7 @@ static int luaB_auxwrap (lua_State *L) {
static int luaB_cocreate (lua_State *L) {
lua_State *NL = lua_newthread(L);
- luaL_checktype(L, 1, LUA_TFUNCTION);
+ luaL_checkfunction(L, 1);
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
return 1;
diff --git a/src/ldblib.c b/src/ldblib.c
index d631dcb..9bd9eaf 100644
--- a/src/ldblib.c
+++ b/src/ldblib.c
@@ -56,9 +56,9 @@ static int db_getuservalue (lua_State *L) {
static int db_setuservalue (lua_State *L) {
if (lua_type(L, 1) == LUA_TLIGHTUSERDATA)
luaL_argerror(L, 1, "full userdata expected, got light userdata");
- luaL_checktype(L, 1, LUA_TUSERDATA);
+ luaL_checkuserdata(L, 1);
if (!lua_isnoneornil(L, 2))
- luaL_checktype(L, 2, LUA_TTABLE);
+ luaL_checktable(L, 2);
lua_settop(L, 2);
lua_setuservalue(L, 1);
return 1;
@@ -202,7 +202,7 @@ static int db_setlocal (lua_State *L) {
static int auxupvalue (lua_State *L, int get) {
const char *name;
int n = luaL_checkint(L, 2);
- luaL_checktype(L, 1, LUA_TFUNCTION);
+ luaL_checkfunction(L, 1);
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
if (name == NULL) return 0;
lua_pushstring(L, name);
@@ -225,7 +225,7 @@ static int db_setupvalue (lua_State *L) {
static int checkupval (lua_State *L, int argf, int argnup) {
lua_Debug ar;
int nup = luaL_checkint(L, argnup);
- luaL_checktype(L, argf, LUA_TFUNCTION);
+ luaL_checkfunction(L, argf);
lua_pushvalue(L, argf);
lua_getinfo(L, ">u", &ar);
luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
@@ -314,7 +314,7 @@ static int db_sethook (lua_State *L) {
}
else {
const char *smask = luaL_checkstring(L, arg+2);
- luaL_checktype(L, arg+1, LUA_TFUNCTION);
+ luaL_checkfunction(L, arg+1);
count = luaL_optint(L, arg+3, 0);
func = hookf; mask = makemask(smask, count);
}
diff --git a/src/loadlib.c b/src/loadlib.c
index 3e5eaec..8501811 100644
--- a/src/loadlib.c
+++ b/src/loadlib.c
@@ -545,7 +545,7 @@ static int ll_module (lua_State *L) {
static int ll_seeall (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_createtable(L, 0, 1); /* create new metatable */
lua_pushvalue(L, -1);
diff --git a/src/loslib.c b/src/loslib.c
index 0278eb5..e88a165 100644
--- a/src/loslib.c
+++ b/src/loslib.c
@@ -235,7 +235,7 @@ static int os_time (lua_State *L) {
t = time(NULL); /* get current time */
else {
struct tm ts;
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
lua_settop(L, 1); /* make sure table is at the top */
ts.tm_sec = getfield(L, "sec", 0);
ts.tm_min = getfield(L, "min", 0);
diff --git a/src/lstrlib.c b/src/lstrlib.c
index 2491bbb..5c14423 100644
--- a/src/lstrlib.c
+++ b/src/lstrlib.c
@@ -157,7 +157,7 @@ static int writer (lua_State *L, const void* b, size_t size, void* B) {
static int str_dump (lua_State *L) {
luaL_Buffer b;
- luaL_checktype(L, 1, LUA_TFUNCTION);
+ luaL_checkfunction(L, 1);
lua_settop(L, 1);
luaL_buffinit(L,&b);
if (lua_dump(L, writer, &b) != 0)
@@ -510,7 +510,7 @@ static int nospecials (const char *p, size_t l) {
size_t upto = 0;
do {
if (strpbrk(p + upto, SPECIALS))
- return 0; /* pattern has a special character */
+ return 0; /* pattern has a special character */
upto += strlen(p + upto) + 1; /* may have more after \0 */
} while (upto <= l);
return 1; /* no special chars found */
diff --git a/src/ltablib.c b/src/ltablib.c
index faabfe5..5fbaf1b 100644
--- a/src/ltablib.c
+++ b/src/ltablib.c
@@ -17,7 +17,7 @@
#define aux_getn(L,n) \
- (luaL_checktype(L, n, LUA_TTABLE), (int)lua_rawlen(L, n))
+ (luaL_checktable(L, n), (int)lua_rawlen(L, n))
static int deprecatedfunc (lua_State *L) {
@@ -28,7 +28,7 @@ static int deprecatedfunc (lua_State *L) {
#if defined(LUA_COMPAT_MAXN)
static int maxn (lua_State *L) {
lua_Number max = 0;
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
lua_pushnil(L); /* first key */
while (lua_next(L, 1)) {
lua_pop(L, 1); /* remove value */
@@ -102,7 +102,7 @@ static int tconcat (lua_State *L) {
size_t lsep;
int i, last;
const char *sep = luaL_optlstring(L, 2, "", &lsep);
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
i = luaL_optint(L, 3, 1);
last = luaL_opt(L, luaL_checkint, 4, (int)lua_rawlen(L, 1));
luaL_buffinit(L, &b);
@@ -141,7 +141,7 @@ static int pack (lua_State *L) {
static int unpack (lua_State *L) {
int i, e, n;
- luaL_checktype(L, 1, LUA_TTABLE);
+ luaL_checktable(L, 1);
i = luaL_optint(L, 2, 1);
e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
if (i > e) return 0; /* empty range */
@@ -254,7 +254,7 @@ static int sort (lua_State *L) {
int n = aux_getn(L, 1);
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ luaL_checkfunction(L, 2);
lua_settop(L, 2); /* make sure there is two arguments */
auxsort(L, 1, n);
return 0;
--
1.7.1