Browse Source

better names for auxiliar functions

v5-2
Roberto Ierusalimschy 22 years ago
parent
commit
097edd3884
  1. 51
      lauxlib.c
  2. 53
      lauxlib.h
  3. 84
      lbaselib.c
  4. 24
      ldblib.c
  5. 77
      liolib.c
  6. 67
      lmathlib.c
  7. 64
      lstrlib.c
  8. 34
      ltablib.c
  9. 64
      ltests.c

51
lauxlib.c

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.88 2002/10/16 20:41:35 roberto Exp roberto $
** $Id: lauxlib.c,v 1.89 2002/10/22 18:07:55 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -93,25 +93,25 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
}
LUALIB_API void luaL_check_stack (lua_State *L, int space, const char *mes) {
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
if (!lua_checkstack(L, space))
luaL_error(L, "stack overflow (%s)", mes);
}
LUALIB_API void luaL_check_type (lua_State *L, int narg, int t) {
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
if (lua_type(L, narg) != t)
tag_error(L, narg, t);
}
LUALIB_API void luaL_check_any (lua_State *L, int narg) {
LUALIB_API void luaL_checkany (lua_State *L, int narg) {
if (lua_type(L, narg) == LUA_TNONE)
luaL_argerror(L, narg, "value expected");
}
LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
const char *s = lua_tostring(L, narg);
if (!s) tag_error(L, narg, LUA_TSTRING);
if (len) *len = lua_strlen(L, narg);
@ -119,17 +119,18 @@ LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
}
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, size_t *len) {
LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
const char *def, size_t *len) {
if (lua_isnoneornil(L, narg)) {
if (len)
*len = (def ? strlen(def) : 0);
return def;
}
else return luaL_check_lstr(L, narg, len);
else return luaL_checklstring(L, narg, len);
}
LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
lua_Number d = lua_tonumber(L, narg);
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
tag_error(L, narg, LUA_TNUMBER);
@ -137,9 +138,9 @@ LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
}
LUALIB_API lua_Number luaL_opt_number (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;
else return luaL_check_number(L, narg);
else return luaL_checknumber(L, narg);
}
@ -165,7 +166,17 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
}
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup) {
if (libname) {
lua_pushstring(L, libname);
lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */
if (lua_isnil(L, -1)) { /* no? */
lua_pop(L, 1);
lua_newtable(L); /* create it */
}
lua_insert(L, -(nup+1)); /* move library table to below upvalues */
}
for (; l->name; l++) {
int i;
lua_pushstring(L, l->name);
@ -174,18 +185,12 @@ LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) {
lua_pushcclosure(L, l->func, nup);
lua_settable(L, -(nup+3));
}
lua_pop(L, nup);
}
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup) {
lua_pushstring(L, libname);
lua_insert(L, -(nup+1));
lua_newtable(L);
lua_insert(L, -(nup+1));
luaL_openlib(L, l, nup);
lua_settable(L, LUA_GLOBALSINDEX);
lua_pop(L, nup); /* remove upvalues */
if (libname) {
lua_pushstring(L, libname);
lua_pushvalue(L, -2);
lua_settable(L, LUA_GLOBALSINDEX);
}
}

53
lauxlib.h

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.h,v 1.53 2002/08/30 20:00:59 roberto Exp roberto $
** $Id: lauxlib.h,v 1.54 2002/09/16 19:49:45 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -27,22 +27,21 @@ typedef struct luaL_reg {
} luaL_reg;
LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup);
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup);
LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup);
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e);
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e);
LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname);
LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg);
LUALIB_API const char *luaL_check_lstr (lua_State *L, int numArg, size_t *l);
LUALIB_API const char *luaL_opt_lstr (lua_State *L, int numArg,
const char *def, size_t *l);
LUALIB_API lua_Number luaL_check_number (lua_State *L, int numArg);
LUALIB_API lua_Number luaL_opt_number (lua_State *L, int nArg, lua_Number def);
LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l);
LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg,
const char *def, size_t *l);
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg);
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def);
LUALIB_API void luaL_check_stack (lua_State *L, int sz, const char *msg);
LUALIB_API void luaL_check_type (lua_State *L, int narg, int t);
LUALIB_API void luaL_check_any (lua_State *L, int narg);
LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg);
LUALIB_API void luaL_checktype (lua_State *L, int narg, int t);
LUALIB_API void luaL_checkany (lua_State *L, int narg);
LUALIB_API void luaL_where (lua_State *L, int lvl);
LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
@ -64,14 +63,14 @@ LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz,
** ===============================================================
*/
#define luaL_arg_check(L, cond,numarg,extramsg) if (!(cond)) \
#define luaL_argcheck(L, cond,numarg,extramsg) if (!(cond)) \
luaL_argerror(L, numarg,extramsg)
#define luaL_check_string(L,n) (luaL_check_lstr(L, (n), NULL))
#define luaL_opt_string(L,n,d) (luaL_opt_lstr(L, (n), (d), NULL))
#define luaL_check_int(L,n) ((int)luaL_check_number(L, n))
#define luaL_check_long(L,n) ((long)luaL_check_number(L, n))
#define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d))
#define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d))
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
#define luaL_checkint(L,n) ((int)luaL_checknumber(L, n))
#define luaL_checklong(L,n) ((long)luaL_checknumber(L, n))
#define luaL_optint(L,n,d) ((int)luaL_optnumber(L, n,d))
#define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,d))
/*
@ -115,14 +114,24 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B);
** Compatibility macros
*/
#define luaL_checktype luaL_check_type
#define luaL_checkany luaL_check_any
LUALIB_API int lua_dofile (lua_State *L, const char *filename);
LUALIB_API int lua_dostring (lua_State *L, const char *str);
LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t sz,
const char *n);
/*
#define luaL_check_lstr luaL_checklstring
#define luaL_opt_lstr luaL_optlstring
#define luaL_check_number luaL_checknumber
#define luaL_opt_number luaL_optnumber
#define luaL_arg_check luaL_argcheck
#define luaL_check_string luaL_checkstring
#define luaL_opt_string luaL_optstring
#define luaL_check_int luaL_checkint
#define luaL_check_long luaL_checklong
#define luaL_opt_int luaL_optint
#define luaL_opt_long luaL_optlong
*/
#endif

84
lbaselib.c

@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.105 2002/11/07 15:39:23 roberto Exp roberto $
** $Id: lbaselib.c,v 1.106 2002/11/14 12:01:35 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@ -47,19 +47,19 @@ static int luaB_print (lua_State *L) {
static int luaB_tonumber (lua_State *L) {
int base = luaL_opt_int(L, 2, 10);
int base = luaL_optint(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_check_any(L, 1);
luaL_checkany(L, 1);
if (lua_isnumber(L, 1)) {
lua_pushnumber(L, lua_tonumber(L, 1));
return 1;
}
}
else {
const char *s1 = luaL_check_string(L, 1);
const char *s1 = luaL_checkstring(L, 1);
char *s2;
unsigned long n;
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
n = strtoul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
@ -75,8 +75,8 @@ static int luaB_tonumber (lua_State *L) {
static int luaB_error (lua_State *L) {
int level = luaL_opt_int(L, 2, 1);
luaL_check_any(L, 1);
int level = luaL_optint(L, 2, 1);
luaL_checkany(L, 1);
if (!lua_isstring(L, 1) || level == 0)
lua_pushvalue(L, 1); /* propagate error mesage without changes */
else { /* add extra information */
@ -89,7 +89,7 @@ static int luaB_error (lua_State *L) {
static int luaB_getmetatable (lua_State *L) {
luaL_check_any(L, 1);
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L);
return 1; /* no metatable */
@ -101,8 +101,8 @@ static int luaB_getmetatable (lua_State *L) {
static int luaB_setmetatable (lua_State *L) {
int t = lua_type(L, 2);
luaL_check_type(L, 1, LUA_TTABLE);
luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected");
if (luaL_getmetafield(L, 1, "__metatable"))
luaL_error(L, "cannot change a protected metatable");
@ -116,8 +116,8 @@ static void getfunc (lua_State *L) {
if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
else {
lua_Debug ar;
int level = luaL_opt_int(L, 1, 1);
luaL_arg_check(L, level >= 0, 1, "level must be non-negative");
int level = luaL_optint(L, 1, 1);
luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
if (lua_getstack(L, level, &ar) == 0)
luaL_argerror(L, 1, "invalid level");
lua_getinfo(L, "f", &ar);
@ -142,7 +142,7 @@ static int luaB_getglobals (lua_State *L) {
static int luaB_setglobals (lua_State *L) {
luaL_check_type(L, 2, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
getfunc(L);
if (aux_getglobals(L)) /* __globals defined? */
luaL_error(L, "cannot change a protected global table");
@ -156,24 +156,24 @@ static int luaB_setglobals (lua_State *L) {
static int luaB_rawequal (lua_State *L) {
luaL_check_any(L, 1);
luaL_check_any(L, 2);
luaL_checkany(L, 1);
luaL_checkany(L, 2);
lua_pushboolean(L, lua_rawequal(L, 1, 2));
return 1;
}
static int luaB_rawget (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
luaL_check_any(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
lua_rawget(L, 1);
return 1;
}
static int luaB_rawset (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
luaL_check_any(L, 2);
luaL_check_any(L, 3);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checkany(L, 2);
luaL_checkany(L, 3);
lua_rawset(L, 1);
return 1;
}
@ -187,20 +187,20 @@ static int luaB_gcinfo (lua_State *L) {
static int luaB_collectgarbage (lua_State *L) {
lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
lua_setgcthreshold(L, luaL_optint(L, 1, 0));
return 0;
}
static int luaB_type (lua_State *L) {
luaL_check_any(L, 1);
luaL_checkany(L, 1);
lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
return 1;
}
static int luaB_next (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
@ -212,7 +212,7 @@ static int luaB_next (lua_State *L) {
static int luaB_pairs (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
lua_getglobal(L, "next"); /* return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
@ -222,7 +222,7 @@ static int luaB_pairs (lua_State *L) {
static int luaB_ipairs (lua_State *L) {
lua_Number i = lua_tonumber(L, 2);
luaL_check_type(L, 1, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
if (i == 0 && lua_isnull(L, 2)) { /* `for' start? */
lua_getglobal(L, "ipairs"); /* return generator, */
lua_pushvalue(L, 1); /* state, */
@ -250,8 +250,8 @@ static int passresults (lua_State *L, int status) {
static int luaB_loadstring (lua_State *L) {
size_t l;
const char *s = luaL_check_lstr(L, 1, &l);
const char *chunkname = luaL_opt_string(L, 2, s);
const char *s = luaL_checklstring(L, 1, &l);
const char *chunkname = luaL_optstring(L, 2, s);
return passresults(L, luaL_loadbuffer(L, s, l, chunkname));
}
@ -265,7 +265,7 @@ static int writer (lua_State *L, const void* b, size_t size, void* B) {
static int luaB_stringdump (lua_State *L) {
luaL_Buffer b;
luaL_check_type(L, 1, LUA_TFUNCTION);
luaL_checktype(L, 1, LUA_TFUNCTION);
luaL_buffinit(L,&b);
if (!lua_dump(L, writer, &b))
luaL_error(L, "unable to dump given function");
@ -276,13 +276,13 @@ static int luaB_stringdump (lua_State *L) {
static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_opt_string(L, 1, NULL);
const char *fname = luaL_optstring(L, 1, NULL);
return passresults(L, luaL_loadfile(L, fname));
}
static int luaB_dofile (lua_State *L) {
const char *fname = luaL_opt_string(L, 1, NULL);
const char *fname = luaL_optstring(L, 1, NULL);
int status = luaL_loadfile(L, fname);
if (status != 0) lua_error(L);
lua_call(L, 0, LUA_MULTRET);
@ -291,9 +291,9 @@ static int luaB_dofile (lua_State *L) {
static int luaB_assert (lua_State *L) {
luaL_check_any(L, 1);
luaL_checkany(L, 1);
if (!lua_toboolean(L, 1))
return luaL_error(L, "%s", luaL_opt_string(L, 2, "assertion failed!"));
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
lua_settop(L, 1);
return 1;
}
@ -301,12 +301,12 @@ static int luaB_assert (lua_State *L) {
static int luaB_unpack (lua_State *L) {
int n, i;
luaL_check_type(L, 1, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushliteral(L, "n");
lua_rawget(L, 1);
n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
luaL_check_stack(L, LUA_MINSTACK, "table too big to unpack");
luaL_checkstack(L, LUA_MINSTACK, "table too big to unpack");
lua_rawgeti(L, 1, i+1);
if (n == -1) { /* no explicit limit? */
if (lua_isnil(L, -1)) { /* stop at first `nil' element */
@ -321,7 +321,7 @@ static int luaB_unpack (lua_State *L) {
static int luaB_pcall (lua_State *L) {
int status;
luaL_check_any(L, 1);
luaL_checkany(L, 1);
status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
lua_pushboolean(L, (status == 0));
lua_insert(L, 1);
@ -331,7 +331,7 @@ static int luaB_pcall (lua_State *L) {
static int luaB_xpcall (lua_State *L) {
int status;
luaL_check_any(L, 2);
luaL_checkany(L, 2);
lua_settop(L, 2);
lua_insert(L, 1); /* put error function under function to be called */
status = lua_pcall(L, 0, LUA_MULTRET, 1);
@ -396,7 +396,7 @@ static int luaB_newproxy (lua_State *L) {
validproxy = lua_toboolean(L, -1);
lua_pop(L, 1); /* remove value */
}
luaL_arg_check(L, validproxy, 1, "boolean/proxy expected");
luaL_argcheck(L, validproxy, 1, "boolean/proxy expected");
lua_getmetatable(L, 1); /* metatable is valid; get it */
}
lua_setmetatable(L, 2);
@ -463,7 +463,7 @@ static void pushcomposename (lua_State *L) {
static int luaB_require (lua_State *L) {
const char *path;
int status = LUA_ERRFILE; /* not found (yet) */
luaL_check_string(L, 1);
luaL_checkstring(L, 1);
lua_settop(L, 1);
lua_pushvalue(L, 1);
lua_setglobal(L, "_REQUIREDNAME");
@ -563,7 +563,7 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
static int luaB_coresume (lua_State *L) {
lua_State *co = lua_tothread(L, 1);
int r;
luaL_arg_check(L, co, 1, "coroutine/thread expected");
luaL_argcheck(L, co, 1, "coroutine/thread expected");
r = auxresume(L, co, lua_gettop(L) - 1);
if (r < 0) {
lua_pushboolean(L, 0);
@ -588,7 +588,7 @@ static int luaB_auxwrap (lua_State *L) {
static int luaB_cocreate (lua_State *L) {
lua_State *NL = lua_newthread(L);
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected");
lua_pushvalue(L, 1); /* move function to top */
lua_xmove(L, NL, 1); /* move function from L to NL */
@ -622,7 +622,7 @@ static const luaL_reg co_funcs[] = {
static void base_open (lua_State *L) {
lua_pushliteral(L, "_G");
lua_pushvalue(L, LUA_GLOBALSINDEX);
luaL_openlib(L, base_funcs, 0); /* open lib into global table */
luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */
lua_pushliteral(L, "_VERSION");
lua_pushliteral(L, LUA_VERSION);
lua_rawset(L, -3); /* set global _VERSION */
@ -642,7 +642,7 @@ static void base_open (lua_State *L) {
LUALIB_API int lua_baselibopen (lua_State *L) {
base_open(L);
luaL_opennamedlib(L, LUA_COLIBNAME, co_funcs, 0);
luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0);
lua_newtable(L);
lua_setglobal(L, REQTAB);
return 0;

24
ldblib.c

@ -1,5 +1,5 @@
/*
** $Id: ldblib.c,v 1.69 2002/09/05 19:45:42 roberto Exp roberto $
** $Id: ldblib.c,v 1.70 2002/09/16 19:18:01 roberto Exp roberto $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@ -32,7 +32,7 @@ static void settabsi (lua_State *L, const char *i, int v) {
static int getinfo (lua_State *L) {
lua_Debug ar;
const char *options = luaL_opt_string(L, 2, "flnSu");
const char *options = luaL_optstring(L, 2, "flnSu");
if (lua_isnumber(L, 1)) {
if (!lua_getstack(L, (int)(lua_tonumber(L, 1)), &ar)) {
lua_pushnil(L); /* level out of range */
@ -81,9 +81,9 @@ static int getinfo (lua_State *L) {
static int getlocal (lua_State *L) {
lua_Debug ar;
const char *name;
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
return luaL_argerror(L, 1, "level out of range");
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
name = lua_getlocal(L, &ar, luaL_checkint(L, 2));
if (name) {
lua_pushstring(L, name);
lua_pushvalue(L, -2);
@ -98,10 +98,10 @@ static int getlocal (lua_State *L) {
static int setlocal (lua_State *L) {
lua_Debug ar;
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
if (!lua_getstack(L, luaL_checkint(L, 1), &ar)) /* level out of range? */
return luaL_argerror(L, 1, "level out of range");
luaL_check_any(L, 3);
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
luaL_checkany(L, 3);
lua_pushstring(L, lua_setlocal(L, &ar, luaL_checkint(L, 2)));
return 1;
}
@ -151,10 +151,10 @@ static int sethook (lua_State *L) {
lua_sethook(L, NULL, 0); /* turn off hooks */
}
else {
const char *smask = luaL_check_string(L, 2);
lua_Number count = luaL_opt_number(L, 3, 0);
luaL_check_type(L, 1, LUA_TFUNCTION);
luaL_arg_check(L, count <= LUA_MAXCOUNT, 2, "count too large (>= 2^24)");
const char *smask = luaL_checkstring(L, 2);
lua_Number count = luaL_optnumber(L, 3, 0);
luaL_checktype(L, 1, LUA_TFUNCTION);
luaL_argcheck(L, count <= LUA_MAXCOUNT, 2, "count too large (>= 2^24)");
lua_sethook(L, hookf, makemask(smask, (int)count));
}
lua_pushlightuserdata(L, (void *)&KEY_HOOK);
@ -255,7 +255,7 @@ static const luaL_reg dblib[] = {
LUALIB_API int lua_dblibopen (lua_State *L) {
luaL_opennamedlib(L, LUA_DBLIBNAME, dblib, 0);
luaL_openlib(L, LUA_DBLIBNAME, dblib, 0);
lua_pushliteral(L, "_TRACEBACK");
lua_pushcfunction(L, errorfb);
lua_settable(L, LUA_GLOBALSINDEX);

77
liolib.c

@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 2.21 2002/10/16 20:41:35 roberto Exp roberto $
** $Id: liolib.c,v 2.22 2002/10/21 20:41:24 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@ -83,6 +83,10 @@ static FILE **newfile (lua_State *L) {
}
/*
** assumes that top of the stack is the `io' library, and next is
** the `io' metatable
*/
static void registerfile (lua_State *L, FILE *f, const char *name,
const char *impname) {
lua_pushstring(L, name);
@ -90,9 +94,9 @@ static void registerfile (lua_State *L, FILE *f, const char *name,
if (impname) {
lua_pushstring(L, impname);
lua_pushvalue(L, -2);
lua_settable(L, -6);
lua_settable(L, -6); /* metatable[impname] = file */
}
lua_settable(L, -3);
lua_settable(L, -3); /* io[name] = file */
}
@ -127,8 +131,8 @@ static int io_gc (lua_State *L) {
static int io_open (lua_State *L) {
const char *filename = luaL_check_string(L, 1);
const char *mode = luaL_opt_string(L, 2, "r");
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
FILE **pf = newfile(L);
*pf = fopen(filename, mode);
return (*pf == NULL) ? pushresult(L, 0) : 1;
@ -140,8 +144,8 @@ static int io_popen (lua_State *L) {
luaL_error(L, "`popen' not supported");
return 0;
#else
const char *filename = luaL_check_string(L, 1);
const char *mode = luaL_opt_string(L, 2, "r");
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
FILE **pf = newfile(L);
*pf = popen(filename, mode);
return (*pf == NULL) ? pushresult(L, 0) : 1;
@ -179,7 +183,7 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) {
if (filename) {
FILE **pf = newfile(L);
*pf = fopen(filename, mode);
luaL_arg_check(L, *pf, 1, strerror(errno));
luaL_argcheck(L, *pf, 1, strerror(errno));
}
else {
tofile(L, 1); /* check that it's a valid file handle */
@ -227,10 +231,10 @@ static int io_lines (lua_State *L) {
return f_lines(L);
}
else {
const char *filename = luaL_check_string(L, 1);
const char *filename = luaL_checkstring(L, 1);
FILE **pf = newfile(L);
*pf = fopen(filename, "r");
luaL_arg_check(L, *pf, 1, strerror(errno));
luaL_argcheck(L, *pf, 1, strerror(errno));
aux_lines(L, lua_gettop(L), 1);
return 1;
}
@ -311,7 +315,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
n = first+1; /* to return 1 result */
}
else { /* ensure stack space for all results and for auxlib's buffer */
luaL_check_stack(L, nargs+LUA_MINSTACK, "too many arguments");
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
success = 1;
for (n = first; nargs-- && success; n++) {
if (lua_type(L, n) == LUA_TNUMBER) {
@ -388,7 +392,7 @@ static int g_write (lua_State *L, FILE *f, int arg) {
}
else {
size_t l;
const char *s = luaL_check_lstr(L, arg, &l);
const char *s = luaL_checklstring(L, arg, &l);
status = status && (fwrite(s, sizeof(char), l, f) == l);
}
}
@ -411,9 +415,9 @@ static int f_seek (lua_State *L) {
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
static const char *const modenames[] = {"set", "cur", "end", NULL};
FILE *f = tofile(L, 1);
int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
long offset = luaL_opt_long(L, 3, 0);
luaL_arg_check(L, op != -1, 2, "invalid mode");
int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames);
long offset = luaL_optlong(L, 3, 0);
luaL_argcheck(L, op != -1, 2, "invalid mode");
op = fseek(f, offset, mode[op]);
if (op)
return pushresult(L, 0); /* error */
@ -473,7 +477,7 @@ static void createmeta (lua_State *L) {
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
lua_pushvalue(L, -1); /* push metatable (will be upvalue for library) */
luaL_openlib(L, flib, 1);
luaL_openlib(L, NULL, flib, 1);
lua_rawset(L, LUA_REGISTRYINDEX); /* registry.FILEHANDLE = metatable */
}
@ -487,19 +491,19 @@ static void createmeta (lua_State *L) {
*/
static int io_execute (lua_State *L) {
lua_pushnumber(L, system(luaL_check_string(L, 1)));
lua_pushnumber(L, system(luaL_checkstring(L, 1)));
return 1;
}
static int io_remove (lua_State *L) {
return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
return pushresult(L, remove(luaL_checkstring(L, 1)) == 0);
}
static int io_rename (lua_State *L) {
return pushresult(L, rename(luaL_check_string(L, 1),
luaL_check_string(L, 2)) == 0);
return pushresult(L, rename(luaL_checkstring(L, 1),
luaL_checkstring(L, 2)) == 0);
}
@ -513,7 +517,7 @@ static int io_tmpname (lua_State *L) {
static int io_getenv (lua_State *L) {
lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
return 1;
}
@ -571,8 +575,8 @@ static int getfield (lua_State *L, const char *key, int d) {
static int io_date (lua_State *L) {
const char *s = luaL_opt_string(L, 1, "%c");
time_t t = (time_t)(luaL_opt_number(L, 2, -1));
const char *s = luaL_optstring(L, 1, "%c");
time_t t = (time_t)(luaL_optnumber(L, 2, -1));
struct tm *stm;
if (t == (time_t)(-1)) /* no time given? */
t = time(NULL); /* use current time */
@ -613,7 +617,7 @@ static int io_time (lua_State *L) {
else {
time_t t;
struct tm ts;
luaL_check_type(L, 1, LUA_TTABLE);
luaL_checktype(L, 1, LUA_TTABLE);
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);
@ -633,8 +637,8 @@ static int io_time (lua_State *L) {
static int io_difftime (lua_State *L) {
lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)),
(time_t)(luaL_opt_number(L, 2, 0))));
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
(time_t)(luaL_optnumber(L, 2, 0))));
return 1;
}
@ -647,16 +651,16 @@ static int io_setloc (lua_State *L) {
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
"numeric", "time", NULL};
const char *l = lua_tostring(L, 1);
int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
luaL_arg_check(L, l || lua_isnoneornil(L, 1), 1, "string expected");
luaL_arg_check(L, op != -1, 2, "invalid option");
int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames);
luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected");
luaL_argcheck(L, op != -1, 2, "invalid option");
lua_pushstring(L, setlocale(cat[op], l));
return 1;
}
static int io_exit (lua_State *L) {
exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
exit(luaL_optint(L, 1, EXIT_SUCCESS));
return 0; /* to avoid warnings */
}
@ -681,18 +685,15 @@ static const luaL_reg syslib[] = {
LUALIB_API int lua_iolibopen (lua_State *L) {
createmeta(L);
luaL_opennamedlib(L, LUA_OSLIBNAME, syslib, 0);
lua_pushliteral(L, FILEHANDLE); /* S: FH */
lua_rawget(L, LUA_REGISTRYINDEX); /* S: mt */
lua_pushvalue(L, -1); /* S: mt mt */
luaL_opennamedlib(L, LUA_IOLIBNAME, iolib, 1); /* S: mt */
lua_pushliteral(L, LUA_IOLIBNAME); /* S: `io' mt */
lua_gettable(L, LUA_GLOBALSINDEX); /* S: io mt */
luaL_openlib(L, LUA_OSLIBNAME, syslib, 0);
lua_pushliteral(L, FILEHANDLE);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, -1);
luaL_openlib(L, LUA_IOLIBNAME, iolib, 1);
/* put predefined file handles into `io' table */
registerfile(L, stdin, "stdin", IO_INPUT);
registerfile(L, stdout, "stdout", IO_OUTPUT);
registerfile(L, stderr, "stderr", NULL);
lua_pop(L, 2); /* S: empty */
return 0;
}

67
lmathlib.c

@ -1,5 +1,5 @@
/*
** $Id: lmathlib.c,v 1.50 2002/08/14 20:07:43 roberto Exp roberto $
** $Id: lmathlib.c,v 1.51 2002/08/14 20:10:33 roberto Exp roberto $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
@ -34,104 +34,104 @@
static int math_abs (lua_State *L) {
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
return 1;
}
static int math_sin (lua_State *L) {
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
return 1;
}
static int math_cos (lua_State *L) {
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
return 1;
}
static int math_tan (lua_State *L) {
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
return 1;
}
static int math_asin (lua_State *L) {
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
return 1;
}
static int math_acos (lua_State *L) {
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
return 1;
}
static int math_atan (lua_State *L) {
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1))));
return 1;
}
static int math_atan2 (lua_State *L) {
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
return 1;
}
static int math_ceil (lua_State *L) {
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
return 1;
}
static int math_floor (lua_State *L) {
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
return 1;
}
static int math_mod (lua_State *L) {
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
return 1;
}
static int math_sqrt (lua_State *L) {
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
return 1;
}
static int math_pow (lua_State *L) {
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
return 1;
}
static int math_log (lua_State *L) {
lua_pushnumber(L, log(luaL_check_number(L, 1)));
lua_pushnumber(L, log(luaL_checknumber(L, 1)));
return 1;
}
static int math_log10 (lua_State *L) {
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
return 1;
}
static int math_exp (lua_State *L) {
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
return 1;
}
static int math_deg (lua_State *L) {
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
return 1;
}
static int math_rad (lua_State *L) {
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
return 1;
}
static int math_frexp (lua_State *L) {
int e;
lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
lua_pushnumber(L, e);
return 2;
}
static int math_ldexp (lua_State *L) {
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
return 1;
}
@ -139,10 +139,10 @@ static int math_ldexp (lua_State *L) {
static int math_min (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
lua_Number dmin = luaL_check_number(L, 1);
lua_Number dmin = luaL_checknumber(L, 1);
int i;
for (i=2; i<=n; i++) {
lua_Number d = luaL_check_number(L, i);
lua_Number d = luaL_checknumber(L, i);
if (d < dmin)
dmin = d;
}
@ -153,10 +153,10 @@ static int math_min (lua_State *L) {
static int math_max (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
lua_Number dmax = luaL_check_number(L, 1);
lua_Number dmax = luaL_checknumber(L, 1);
int i;
for (i=2; i<=n; i++) {
lua_Number d = luaL_check_number(L, i);
lua_Number d = luaL_checknumber(L, i);
if (d > dmax)
dmax = d;
}
@ -175,15 +175,15 @@ static int math_random (lua_State *L) {
break;
}
case 1: { /* only upper limit */
int u = luaL_check_int(L, 1);
luaL_arg_check(L, 1<=u, 1, "interval is empty");
int u = luaL_checkint(L, 1);
luaL_argcheck(L, 1<=u, 1, "interval is empty");
lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */
break;
}
case 2: { /* lower and upper limits */
int l = luaL_check_int(L, 1);
int u = luaL_check_int(L, 2);
luaL_arg_check(L, l<=u, 2, "interval is empty");
int l = luaL_checkint(L, 1);
int u = luaL_checkint(L, 2);
luaL_argcheck(L, l<=u, 2, "interval is empty");
lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
break;
}
@ -194,7 +194,7 @@ static int math_random (lua_State *L) {
static int math_randomseed (lua_State *L) {
srand(luaL_check_int(L, 1));
srand(luaL_checkint(L, 1));
return 0;
}
@ -232,13 +232,10 @@ static const luaL_reg mathlib[] = {
** Open math library
*/
LUALIB_API int lua_mathlibopen (lua_State *L) {
lua_pushliteral(L, LUA_MATHLIBNAME);
lua_newtable(L);
luaL_openlib(L, mathlib, 0);
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
lua_pushliteral(L, "pi");
lua_pushnumber(L, PI);
lua_settable(L, -3);
lua_settable(L, LUA_GLOBALSINDEX);
lua_pushliteral(L, "__pow");
lua_pushcfunction(L, math_pow);
lua_settable(L, LUA_REGISTRYINDEX);

64
lstrlib.c

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.88 2002/08/21 19:39:31 roberto Exp roberto $
** $Id: lstrlib.c,v 1.89 2002/08/23 19:45:24 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -28,7 +28,7 @@ typedef long sint32; /* a signed version for size_t */
static int str_len (lua_State *L) {
size_t l;
luaL_check_lstr(L, 1, &l);
luaL_checklstring(L, 1, &l);
lua_pushnumber(L, l);
return 1;
}
@ -42,9 +42,9 @@ static sint32 posrelat (sint32 pos, size_t len) {
static int str_sub (lua_State *L) {
size_t l;
const char *s = luaL_check_lstr(L, 1, &l);
sint32 start = posrelat(luaL_check_long(L, 2), l);
sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
const char *s = luaL_checklstring(L, 1, &l);
sint32 start = posrelat(luaL_checklong(L, 2), l);
sint32 end = posrelat(luaL_optlong(L, 3, -1), l);
if (start < 1) start = 1;
if (end > (sint32)l) end = l;
if (start <= end)
@ -58,7 +58,7 @@ static int str_lower (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const char *s = luaL_check_lstr(L, 1, &l);
const char *s = luaL_checklstring(L, 1, &l);
luaL_buffinit(L, &b);
for (i=0; i<l; i++)
luaL_putchar(&b, tolower(uchar(s[i])));
@ -71,7 +71,7 @@ static int str_upper (lua_State *L) {
size_t l;
size_t i;
luaL_Buffer b;
const char *s = luaL_check_lstr(L, 1, &l);
const char *s = luaL_checklstring(L, 1, &l);
luaL_buffinit(L, &b);
for (i=0; i<l; i++)
luaL_putchar(&b, toupper(uchar(s[i])));
@ -82,8 +82,8 @@ static int str_upper (lua_State *L) {
static int str_rep (lua_State *L) {
size_t l;
luaL_Buffer b;
const char *s = luaL_check_lstr(L, 1, &l);
int n = luaL_check_int(L, 2);
const char *s = luaL_checklstring(L, 1, &l);
int n = luaL_checkint(L, 2);
luaL_buffinit(L, &b);
while (n-- > 0)
luaL_addlstring(&b, s, l);
@ -94,9 +94,9 @@ static int str_rep (lua_State *L) {
static int str_byte (lua_State *L) {
size_t l;
const char *s = luaL_check_lstr(L, 1, &l);
sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
const char *s = luaL_checklstring(L, 1, &l);
sint32 pos = posrelat(luaL_optlong(L, 2, 1), l);
luaL_argcheck(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
lua_pushnumber(L, uchar(s[pos-1]));
return 1;
}
@ -108,8 +108,8 @@ static int str_char (lua_State *L) {
luaL_Buffer b;
luaL_buffinit(L, &b);
for (i=1; i<=n; i++) {
int c = luaL_check_int(L, i);
luaL_arg_check(L, uchar(c) == c, i, "invalid value");
int c = luaL_checkint(L, i);
luaL_argcheck(L, uchar(c) == c, i, "invalid value");
luaL_putchar(&b, uchar(c));
}
luaL_pushresult(&b);
@ -439,7 +439,7 @@ static void push_onecapture (MatchState *ms, int i) {
static int push_captures (MatchState *ms, const char *s, const char *e) {
int i;
luaL_check_stack(ms->L, ms->level, "too many captures");
luaL_checkstack(ms->L, ms->level, "too many captures");
if (ms->level == 0 && s) { /* no explicit captures? */
lua_pushlstring(ms->L, s, e-s); /* return whole match */
return 1;
@ -454,10 +454,10 @@ static int push_captures (MatchState *ms, const char *s, const char *e) {
static int str_find (lua_State *L) {
size_t l1, l2;
const char *s = luaL_check_lstr(L, 1, &l1);
const char *p = luaL_check_lstr(L, 2, &l2);
sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
const char *s = luaL_checklstring(L, 1, &l1);
const char *p = luaL_checklstring(L, 2, &l2);
sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1;
luaL_argcheck(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
if (lua_toboolean(L, 4) || /* explicit request? */
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
/* do a plain search */
@ -517,8 +517,8 @@ static int gfind_aux (lua_State *L) {
static int gfind (lua_State *L) {
luaL_check_string(L, 1);
luaL_check_string(L, 2);
luaL_checkstring(L, 1);
luaL_checkstring(L, 2);
lua_settop(L, 2);
lua_pushnumber(L, 0);
lua_pushcclosure(L, gfind_aux, 3);
@ -563,14 +563,14 @@ static void add_s (MatchState *ms, luaL_Buffer *b,
static int str_gsub (lua_State *L) {
size_t srcl;
const char *src = luaL_check_lstr(L, 1, &srcl);
const char *p = luaL_check_string(L, 2);
int max_s = luaL_opt_int(L, 4, srcl+1);
const char *src = luaL_checklstring(L, 1, &srcl);
const char *p = luaL_checkstring(L, 2);
int max_s = luaL_optint(L, 4, srcl+1);
int anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0;
MatchState ms;
luaL_Buffer b;
luaL_arg_check(L,
luaL_argcheck(L,
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
3, "string or function expected");
luaL_buffinit(L, &b);
@ -609,7 +609,7 @@ static int str_gsub (lua_State *L) {
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
size_t l;
const char *s = luaL_check_lstr(L, arg, &l);
const char *s = luaL_checklstring(L, arg, &l);
luaL_putchar(b, '"');
while (l--) {
switch (*s) {
@ -659,7 +659,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt,
static int str_format (lua_State *L) {
int arg = 1;
size_t sfl;
const char *strfrmt = luaL_check_lstr(L, arg, &sfl);
const char *strfrmt = luaL_checklstring(L, arg, &sfl);
const char *strfrmt_end = strfrmt+sfl;
luaL_Buffer b;
luaL_buffinit(L, &b);
@ -678,16 +678,16 @@ static int str_format (lua_State *L) {
strfrmt = scanformat(L, strfrmt, form, &hasprecision);
switch (*strfrmt++) {
case 'c': case 'd': case 'i': {
sprintf(buff, form, luaL_check_int(L, arg));
sprintf(buff, form, luaL_checkint(L, arg));
break;
}
case 'o': case 'u': case 'x': case 'X': {
sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg)));
sprintf(buff, form, (unsigned int)(luaL_checknumber(L, arg)));
break;
}
case 'e': case 'E': case 'f':
case 'g': case 'G': {
sprintf(buff, form, luaL_check_number(L, arg));
sprintf(buff, form, luaL_checknumber(L, arg));
break;
}
case 'q': {
@ -696,7 +696,7 @@ static int str_format (lua_State *L) {
}
case 's': {
size_t l;
const char *s = luaL_check_lstr(L, arg, &l);
const char *s = luaL_checklstring(L, arg, &l);
if (!hasprecision && l >= 100) {
/* no precision and string is too long to be formatted;
keep original string */
@ -741,7 +741,7 @@ static const luaL_reg strlib[] = {
** Open string library
*/
LUALIB_API int lua_strlibopen (lua_State *L) {
luaL_opennamedlib(L, LUA_STRLIBNAME, strlib, 0);
luaL_openlib(L, LUA_STRLIBNAME, strlib, 0);
return 0;
}

34
ltablib.c

@ -1,5 +1,5 @@
/*
** $Id: ltablib.c,v 1.14 2002/10/23 19:08:23 roberto Exp roberto $
** $Id: ltablib.c,v 1.15 2002/11/14 11:51:50 roberto Exp roberto $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@ -40,7 +40,7 @@ static void aux_setn (lua_State *L, int t, int n) {
static int aux_getn (lua_State *L, int t) {
int n;
luaL_check_type(L, t, LUA_TTABLE);
luaL_checktype(L, t, LUA_TTABLE);
lua_pushliteral(L, "n"); /* try t.n */
lua_rawget(L, t);
if ((n = checkint(L)) >= 0) return n;
@ -64,7 +64,7 @@ static int aux_getn (lua_State *L, int t) {
static int luaB_foreachi (lua_State *L) {
int i;
int n = aux_getn(L, 1);
luaL_check_type(L, 2, LUA_TFUNCTION);
luaL_checktype(L, 2, LUA_TFUNCTION);
for (i=1; i<=n; i++) {
lua_pushvalue(L, 2); /* function */
lua_pushnumber(L, i); /* 1st argument */
@ -79,8 +79,8 @@ static int luaB_foreachi (lua_State *L) {
static int luaB_foreach (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
luaL_check_type(L, 2, LUA_TFUNCTION);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushnil(L); /* first key */
for (;;) {
if (lua_next(L, 1) == 0)
@ -103,8 +103,8 @@ static int luaB_getn (lua_State *L) {
static int luaB_setn (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
aux_setn(L, 1, luaL_check_int(L, 2));
luaL_checktype(L, 1, LUA_TTABLE);
aux_setn(L, 1, luaL_checkint(L, 2));
return 0;
}
@ -116,7 +116,7 @@ static int luaB_tinsert (lua_State *L) {
if (v == 2) /* called with only 2 arguments */
pos = n; /* insert new element at the end */
else {
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
pos = luaL_checkint(L, 2); /* 2nd argument is the position */
if (pos > n) n = pos; /* `grow' array if necessary */
v = 3; /* function may be called with more than 3 args */
}
@ -133,7 +133,7 @@ static int luaB_tinsert (lua_State *L) {
static int luaB_tremove (lua_State *L) {
int n = aux_getn(L, 1);
int pos = luaL_opt_int(L, 2, n);
int pos = luaL_optint(L, 2, n);
if (n <= 0) return 0; /* table is `empty' */
aux_setn(L, 1, n-1); /* t.n = n-1 */
lua_rawgeti(L, 1, pos); /* result = t[pos] */
@ -150,15 +150,15 @@ static int luaB_tremove (lua_State *L) {
static int str_concat (lua_State *L) {
luaL_Buffer b;
size_t lsep;
const char *sep = luaL_opt_lstr(L, 2, "", &lsep);
int i = luaL_opt_int(L, 3, 1);
int n = luaL_opt_int(L, 4, 0);
luaL_check_type(L, 1, LUA_TTABLE);
const char *sep = luaL_optlstring(L, 2, "", &lsep);
int i = luaL_optint(L, 3, 1);
int n = luaL_optint(L, 4, 0);
luaL_checktype(L, 1, LUA_TTABLE);
if (n == 0) n = aux_getn(L, 1);
luaL_buffinit(L, &b);
for (; i <= n; i++) {
lua_rawgeti(L, 1, i);
luaL_arg_check(L, lua_isstring(L, -1), 1, "table contains non-strings");
luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
luaL_addvalue(&b);
if (i != n)
luaL_addlstring(&b, sep, lsep);
@ -262,9 +262,9 @@ static void auxsort (lua_State *L, int l, int u) {
static int luaB_sort (lua_State *L) {
int n = aux_getn(L, 1);
luaL_check_stack(L, 40, ""); /* assume array is smaller than 2^40 */
luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
luaL_check_type(L, 2, LUA_TFUNCTION);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2); /* make sure there is two arguments */
auxsort(L, 1, n);
return 0;
@ -293,7 +293,7 @@ LUALIB_API int lua_tablibopen (lua_State *L) {
lua_pushliteral(L, "__mode");
lua_pushliteral(L, "k");
lua_rawset(L, -3); /* metatable(N).__mode = "k" */
luaL_opennamedlib(L, LUA_TABLIBNAME, tab_funcs, 1);
luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 1);
return 0;
}

64
ltests.c

@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 1.141 2002/11/13 11:31:39 roberto Exp roberto $
** $Id: ltests.c,v 1.142 2002/11/14 11:51:23 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@ -178,7 +178,7 @@ void luaI_printcode (Proto *pt, int size) {
static int listcode (lua_State *L) {
int pc;
Proto *p;
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(index(L, 1))->l.p;
lua_newtable(L);
@ -197,7 +197,7 @@ static int listcode (lua_State *L) {
static int listk (lua_State *L) {
Proto *p;
int i;
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(index(L, 1))->l.p;
lua_newtable(L);
@ -212,10 +212,10 @@ static int listk (lua_State *L) {
static int listlocals (lua_State *L) {
Proto *p;
int pc = luaL_check_int(L, 2) - 1;
int pc = luaL_checkint(L, 2) - 1;
int i = 0;
const char *name;
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = clvalue(index(L, 1))->l.p;
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
@ -248,7 +248,7 @@ static int mem_query (lua_State *L) {
return 3;
}
else {
memdebug_memlimit = luaL_check_int(L, 1);
memdebug_memlimit = luaL_checkint(L, 1);
return 0;
}
}
@ -256,13 +256,13 @@ static int mem_query (lua_State *L) {
static int hash_query (lua_State *L) {
if (lua_isnone(L, 2)) {
luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
lua_pushnumber(L, tsvalue(index(L, 1))->tsv.hash);
}
else {
TObject *o = index(L, 1);
Table *t;
luaL_check_type(L, 2, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
t = hvalue(index(L, 2));
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
}
@ -283,8 +283,8 @@ static int stacklevel (lua_State *L) {
static int table_query (lua_State *L) {
const Table *t;
int i = luaL_opt_int(L, 2, -1);
luaL_check_type(L, 1, LUA_TTABLE);
int i = luaL_optint(L, 2, -1);
luaL_checktype(L, 1, LUA_TTABLE);
t = hvalue(index(L, 1));
if (i == -1) {
lua_pushnumber(L, t->sizearray);
@ -316,7 +316,7 @@ static int table_query (lua_State *L) {
static int string_query (lua_State *L) {
stringtable *tb = &G(L)->strt;
int s = luaL_opt_int(L, 2, 0) - 1;
int s = luaL_optint(L, 2, 0) - 1;
if (s==-1) {
lua_pushnumber(L ,tb->nuse);
lua_pushnumber(L ,tb->size);
@ -338,8 +338,8 @@ static int string_query (lua_State *L) {
static int tref (lua_State *L) {
int level = lua_gettop(L);
int lock = luaL_opt_int(L, 2, 1);
luaL_check_any(L, 1);
int lock = luaL_optint(L, 2, 1);
luaL_checkany(L, 1);
lua_pushvalue(L, 1);
lua_pushnumber(L, lua_ref(L, lock));
assert(lua_gettop(L) == level+1); /* +1 for result */
@ -348,34 +348,34 @@ static int tref (lua_State *L) {
static int getref (lua_State *L) {
int level = lua_gettop(L);
lua_getref(L, luaL_check_int(L, 1));
lua_getref(L, luaL_checkint(L, 1));
assert(lua_gettop(L) == level+1);
return 1;
}
static int unref (lua_State *L) {
int level = lua_gettop(L);
lua_unref(L, luaL_check_int(L, 1));
lua_unref(L, luaL_checkint(L, 1));
assert(lua_gettop(L) == level);
return 0;
}
static int metatable (lua_State *L) {
luaL_check_any(L, 1);
luaL_checkany(L, 1);
if (lua_isnone(L, 2)) {
if (lua_getmetatable(L, 1) == 0)
lua_pushnil(L);
}
else {
lua_settop(L, 2);
luaL_check_type(L, 2, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
lua_setmetatable(L, 1);
}
return 1;
}
static int newuserdata (lua_State *L) {
size_t size = luaL_check_int(L, 1);
size_t size = luaL_checkint(L, 1);
char *p = cast(char *, lua_newuserdata(L, size));
while (size--) *p++ = '\0';
return 1;
@ -383,7 +383,7 @@ static int newuserdata (lua_State *L) {
static int pushuserdata (lua_State *L) {
lua_pushlightuserdata(L, cast(void *, luaL_check_int(L, 1)));
lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
return 1;
}
@ -397,7 +397,7 @@ static int udataval (lua_State *L) {
static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_newthread(L);
size_t l;
const char *s = luaL_check_lstr(L, 1, &l);
const char *s = luaL_checklstring(L, 1, &l);
int status = luaL_loadbuffer(L1, s, l, s);
if (status == 0)
status = lua_pcall(L1, 0, 0, 0);
@ -407,12 +407,12 @@ static int doonnewstack (lua_State *L) {
static int s2d (lua_State *L) {
lua_pushnumber(L, *cast(const double *, luaL_check_string(L, 1)));
lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
return 1;
}
static int d2s (lua_State *L) {
double d = luaL_check_number(L, 1);
double d = luaL_checknumber(L, 1);
lua_pushlstring(L, cast(char *, &d), sizeof(d));
return 1;
}
@ -430,7 +430,7 @@ static int newstate (lua_State *L) {
}
static int loadlib (lua_State *L) {
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
lua_register(L1, "mathlibopen", lua_mathlibopen);
lua_register(L1, "strlibopen", lua_strlibopen);
lua_register(L1, "iolibopen", lua_iolibopen);
@ -440,16 +440,16 @@ static int loadlib (lua_State *L) {
}
static int closestate (lua_State *L) {
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
lua_close(L1);
lua_unlock(L); /* close cannot unlock that */
return 0;
}
static int doremote (lua_State *L) {
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_check_number(L, 1)));
lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
size_t lcode;
const char *code = luaL_check_lstr(L, 2, &lcode);
const char *code = luaL_checklstring(L, 2, &lcode);
int status;
lua_settop(L1, 0);
status = luaL_loadbuffer(L1, code, lcode, code);
@ -471,13 +471,13 @@ static int doremote (lua_State *L) {
static int log2_aux (lua_State *L) {
lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
lua_pushnumber(L, luaO_log2(luaL_checkint(L, 1)));
return 1;
}
static int test_do (lua_State *L) {
const char *p = luaL_check_string(L, 1);
const char *p = luaL_checkstring(L, 1);
if (*p == '@')
lua_dofile(L, p+1);
else
@ -536,7 +536,7 @@ static const char *getname_aux (char *buff, const char **pc) {
static int testC (lua_State *L) {
char buff[30];
const char *pc = luaL_check_string(L, 1);
const char *pc = luaL_checkstring(L, 1);
for (;;) {
const char *inst = getname;
if EQ("") return 0;
@ -657,11 +657,11 @@ static int testC (lua_State *L) {
}
else if EQ("loadstring") {
size_t sl;
const char *s = luaL_check_lstr(L, getnum, &sl);
const char *s = luaL_checklstring(L, getnum, &sl);
luaL_loadbuffer(L, s, sl, s);
}
else if EQ("loadfile") {
luaL_loadfile(L, luaL_check_string(L, getnum));
luaL_loadfile(L, luaL_checkstring(L, getnum));
}
else if EQ("setmetatable") {
lua_setmetatable(L, getnum);
@ -724,7 +724,7 @@ static void fim (void) {
int luaB_opentests (lua_State *L) {
lua_userstateopen(L); /* init lock */
lua_state = L; /* keep first state to be opened */
luaL_opennamedlib(L, "T", tests_funcs, 0);
luaL_openlib(L, "T", tests_funcs, 0);
atexit(fim);
return 0;
}

Loading…
Cancel
Save