terom@143: #ifndef LUA_FUNC_H terom@143: #define LUA_FUNC_H terom@143: terom@143: /** terom@143: * @file terom@143: * terom@143: * Convenience functions for working with lua C functions terom@143: */ terom@143: #include "lua_type.h" terom@143: #include terom@143: terom@143: /** terom@143: * Lua function argument types terom@143: */ terom@143: enum lua_arg_type { terom@143: LUA_ARG_INVALID, terom@201: terom@201: /** A `const char *` pointing to lua-GC'd memory */ terom@201: LUA_ARG_STRING, terom@143: terom@201: /** A c99 `bool` */ terom@143: LUA_ARG_BOOL, terom@201: terom@201: /** A `long signed int` */ terom@201: LUA_ARG_INT, terom@143: }; terom@143: terom@143: /** terom@143: * Function argument def terom@143: */ terom@143: struct lua_func_arg { terom@143: /** Argument name */ terom@143: const char *name; terom@143: terom@143: /** Expected type */ terom@143: enum lua_arg_type type; terom@143: terom@143: /** Default value */ terom@143: union { terom@143: const char *string; terom@143: int boolean; terom@201: long integer; terom@143: } def; terom@143: }; terom@143: terom@143: /** terom@143: * Function def terom@143: */ terom@143: struct lua_func { terom@145: /** Object type, or NULL */ terom@145: const struct lua_type *type; terom@143: terom@143: /** Function name */ terom@143: const char *name; terom@143: terom@143: /** Help string */ terom@143: const char *help; terom@143: terom@143: /** Arguments */ terom@143: const struct lua_func_arg args[]; terom@143: }; terom@143: terom@143: /** terom@143: * Used as the "invalid" default value terom@143: */ terom@143: #define LUA_ARG_REQUIRED (-1) terom@143: #define LUA_ARG_STRING_REQUIRED ((const char *) (-1)) terom@143: terom@143: /** terom@143: * Define a function argument terom@143: */ terom@143: #define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } } terom@143: #define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } } terom@201: #define LUA_FUNC_ARG_INT(name, def) { (name), LUA_ARG_INT, { .integer = (def) } } terom@145: #define LUA_FUNC_ARG_END { NULL, 0, { 0 } } terom@143: terom@143: /** terom@143: * Define a function terom@143: */ terom@145: #define LUA_FUNC(type, name, help, ...) { (type), (name), (help), { __VA_ARGS__, LUA_FUNC_ARG_END } } terom@143: terom@143: /** terom@143: * Parse and return a string argument terom@143: */ terom@143: const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def); terom@143: terom@143: /** terom@143: * Parse and return a boolean argument terom@143: */ terom@143: bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def); terom@143: terom@143: /** terom@201: * Parse and return an integer argument terom@201: */ terom@201: long lua_arg_int (lua_State *L, int nargs, int index, const char *name, long def); terom@201: terom@201: /** terom@199: * Return a userdata argument at the given fixed index terom@199: */ terom@201: void* lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional); terom@199: terom@199: /** terom@143: * Parse function arguments as defined terom@143: */ terom@143: void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...); terom@143: terom@143: #endif