1 #ifndef LUA_FUNC_H |
|
2 #define LUA_FUNC_H |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * |
|
7 * Convenience functions for working with lua C functions |
|
8 */ |
|
9 #include "lua_type.h" |
|
10 #include <stdbool.h> |
|
11 |
|
12 /** |
|
13 * Lua function argument types |
|
14 */ |
|
15 enum lua_arg_type { |
|
16 LUA_ARG_INVALID, |
|
17 |
|
18 /** A `const char *` pointing to lua-GC'd memory */ |
|
19 LUA_ARG_STRING, |
|
20 |
|
21 /** A c99 `bool` */ |
|
22 LUA_ARG_BOOL, |
|
23 |
|
24 /** A `long signed int` */ |
|
25 LUA_ARG_INT, |
|
26 }; |
|
27 |
|
28 /** |
|
29 * Function argument def |
|
30 */ |
|
31 struct lua_func_arg { |
|
32 /** Argument name */ |
|
33 const char *name; |
|
34 |
|
35 /** Expected type */ |
|
36 enum lua_arg_type type; |
|
37 |
|
38 /** Default value */ |
|
39 union { |
|
40 const char *string; |
|
41 int boolean; |
|
42 long integer; |
|
43 } def; |
|
44 }; |
|
45 |
|
46 /** |
|
47 * Function def |
|
48 */ |
|
49 struct lua_func { |
|
50 /** Object type, or NULL */ |
|
51 const struct lua_type *type; |
|
52 |
|
53 /** Function name */ |
|
54 const char *name; |
|
55 |
|
56 /** Help string */ |
|
57 const char *help; |
|
58 |
|
59 /** Arguments */ |
|
60 const struct lua_func_arg args[]; |
|
61 }; |
|
62 |
|
63 /** |
|
64 * Used as the "invalid" default value |
|
65 */ |
|
66 #define LUA_ARG_REQUIRED (-1) |
|
67 #define LUA_ARG_STRING_REQUIRED ((const char *) (-1)) |
|
68 |
|
69 /** |
|
70 * Define a function argument |
|
71 */ |
|
72 #define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } } |
|
73 #define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } } |
|
74 #define LUA_FUNC_ARG_INT(name, def) { (name), LUA_ARG_INT, { .integer = (def) } } |
|
75 #define LUA_FUNC_ARG_END { NULL, 0, { 0 } } |
|
76 |
|
77 /** |
|
78 * Define a function |
|
79 */ |
|
80 #define LUA_FUNC(type, name, help, ...) { (type), (name), (help), { __VA_ARGS__, LUA_FUNC_ARG_END } } |
|
81 |
|
82 /** |
|
83 * Parse and return a string argument |
|
84 */ |
|
85 const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def); |
|
86 |
|
87 /** |
|
88 * Parse and return a boolean argument |
|
89 */ |
|
90 bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def); |
|
91 |
|
92 /** |
|
93 * Parse and return an integer argument |
|
94 */ |
|
95 long lua_arg_int (lua_State *L, int nargs, int index, const char *name, long def); |
|
96 |
|
97 /** |
|
98 * Return a userdata argument at the given fixed index |
|
99 */ |
|
100 void* lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional); |
|
101 |
|
102 /** |
|
103 * Parse function arguments as defined |
|
104 */ |
|
105 void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...); |
|
106 |
|
107 #endif |
|