1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1997-12-23 22:24:36 +03:00
|
|
|
** $Id: lauxlib.c,v 1.6 1997/12/17 20:48:58 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Auxiliar functions for building Lua libraries
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
1997-03-17 20:02:29 +03:00
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
#include <stdarg.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#include <stdio.h>
|
1997-03-17 20:02:29 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lauxlib.h"
|
1997-03-17 20:02:29 +03:00
|
|
|
#include "lua.h"
|
1997-04-06 18:08:08 +04:00
|
|
|
#include "luadebug.h"
|
1997-03-17 20:02:29 +03:00
|
|
|
|
|
|
|
|
1997-04-07 18:48:53 +04:00
|
|
|
|
1997-11-21 22:00:46 +03:00
|
|
|
void luaL_arg_check (int cond, int numarg, char *extramsg)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
if (!cond) {
|
1997-04-14 19:30:03 +04:00
|
|
|
char *funcname;
|
|
|
|
lua_getobjname(lua_stackedfunction(0), &funcname);
|
|
|
|
if (funcname == NULL)
|
|
|
|
funcname = "???";
|
1997-03-17 20:02:29 +03:00
|
|
|
if (extramsg == NULL)
|
1997-09-16 23:25:59 +04:00
|
|
|
luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
|
1997-03-17 20:02:29 +03:00
|
|
|
else
|
1997-09-16 23:25:59 +04:00
|
|
|
luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
|
1997-03-17 20:02:29 +03:00
|
|
|
numarg, funcname, extramsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
char *luaL_check_string (int numArg)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
1997-03-17 20:02:29 +03:00
|
|
|
return lua_getstring(o);
|
|
|
|
}
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
char *luaL_opt_string (int numArg, char *def)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_check_string(numArg);
|
1997-03-17 20:02:29 +03:00
|
|
|
}
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
double luaL_check_number (int numArg)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_arg_check(lua_isnumber(o), numArg, "number expected");
|
1997-03-17 20:02:29 +03:00
|
|
|
return lua_getnumber(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
double luaL_opt_number (int numArg, double def)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_check_number(numArg);
|
1997-12-09 16:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object luaL_tablearg (int arg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(arg);
|
|
|
|
luaL_arg_check(lua_istable(o), arg, "table expected");
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_Object luaL_functionarg (int arg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(arg);
|
|
|
|
luaL_arg_check(lua_isfunction(o), arg, "function expected");
|
|
|
|
return o;
|
1997-03-17 20:02:29 +03:00
|
|
|
}
|
|
|
|
|
1997-09-26 19:02:26 +04:00
|
|
|
lua_Object luaL_nonnullarg (int numArg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
|
|
|
luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
void luaL_openlib (struct luaL_reg *l, int n)
|
|
|
|
{
|
|
|
|
int i;
|
1997-11-04 18:27:53 +03:00
|
|
|
lua_open(); /* make sure lua is already open */
|
1997-03-18 18:30:50 +03:00
|
|
|
for (i=0; i<n; i++)
|
|
|
|
lua_register(l[i].name, l[i].func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaL_verror (char *fmt, ...)
|
|
|
|
{
|
1997-09-16 23:25:59 +04:00
|
|
|
char buff[500];
|
1997-03-18 18:30:50 +03:00
|
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
|
|
|
vsprintf(buff, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
lua_error(buff);
|
|
|
|
}
|
1997-09-26 19:02:26 +04:00
|
|
|
|