From c31aa863ac29fad5bb3f44c4e08640f877b65f25 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Sep 1997 16:25:59 -0300 Subject: Auxiliar functions for building Lua libraries --- lauxlib.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lauxlib.c (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c new file mode 100644 index 00000000..37a6e41a --- /dev/null +++ b/lauxlib.c @@ -0,0 +1,76 @@ +/* +** $Id: $ +** Auxiliar functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#include "lauxlib.h" +#include "lua.h" +#include "luadebug.h" + + + +void luaL_arg_check(int cond, int numarg, char *extramsg) +{ + if (!cond) { + char *funcname; + lua_getobjname(lua_stackedfunction(0), &funcname); + if (funcname == NULL) + funcname = "???"; + if (extramsg == NULL) + luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname); + else + luaL_verror("bad argument #%d to function `%.50s' (%.100s)", + numarg, funcname, extramsg); + } +} + +char *luaL_check_string (int numArg) +{ + lua_Object o = lua_getparam(numArg); + luaL_arg_check(lua_isstring(o), numArg, "string expected"); + return lua_getstring(o); +} + +char *luaL_opt_string (int numArg, char *def) +{ + return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : + luaL_check_string(numArg); +} + +double luaL_check_number (int numArg) +{ + lua_Object o = lua_getparam(numArg); + luaL_arg_check(lua_isnumber(o), numArg, "number expected"); + return lua_getnumber(o); +} + + +double luaL_opt_number (int numArg, double def) +{ + return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : + luaL_check_number(numArg); +} + +void luaL_openlib (struct luaL_reg *l, int n) +{ + int i; + for (i=0; i