diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-21 19:53:22 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-21 19:53:22 -0500 |
commit | c407e5b904a69f3849831ba88074440707f0c6cd (patch) | |
tree | 4bfefd3a24d664c282289d3f3fd7242121de8b39 /src/lua | |
parent | 41fa908dc15b522e53946a716f4f6c00520bd46f (diff) |
add honey_lua.h and honey_lua.h
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/honey_lua.c | 40 | ||||
-rw-r--r-- | src/lua/honey_lua.h | 46 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/lua/honey_lua.c b/src/lua/honey_lua.c new file mode 100644 index 0000000..36ca335 --- /dev/null +++ b/src/lua/honey_lua.c @@ -0,0 +1,40 @@ +#include "honey_lua.h" + +void honey_lua_create_table(lua_State* L, + honey_lua_element* elements, + unsigned int n_elements) +{ + lua_createtable(L, 0, n_elements); + + for (int i=0; i<n_elements; i++) { + honey_lua_push_element(L, elements[i]); + lua_setfield(L, -2, elements[i].name); + } +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +void honey_lua_push_element(lua_State* L, honey_lua_element element) +{ + switch(element.type) { + case HONEY_INT: + lua_pushinteger(L, element.data.integer); + break; + + case HONEY_NUM: + lua_pushnumber(L, element.data.number); + break; + + case HONEY_STRING: + lua_pushstring(L, element.data.string); + break; + + case HONEY_FUNC: + lua_pushcfunction(L, element.data.function); + break; + + default: + // this should never happen + break; + } +} diff --git a/src/lua/honey_lua.h b/src/lua/honey_lua.h new file mode 100644 index 0000000..90fc2c7 --- /dev/null +++ b/src/lua/honey_lua.h @@ -0,0 +1,46 @@ +#ifndef HONEY_LUA_H +#define HONEY_LUA_H + +#include "../common.h" + +typedef enum { + HONEY_INT, + HONEY_NUM, + HONEY_STRING, + HONEY_FUNC +} honey_lua_type; + +typedef struct { + char* name; + honey_lua_type type; + union { + int integer; + double number; + char* string; + int (*function)(lua_State*); + } data; +} honey_lua_element; + +/** @brief Push an element to the lua stack. + * + * @param[in] L The lua state to push the element to. + * @param[in] element The honey_lua_element to push to the stack. + * + * @returns Nothing. + */ +void honey_lua_push_element(lua_State* L, + honey_lua_element element); + +/** @brief Create a lua table populated with various elements. + * + * @param[in] L The lua state to push the table to. + * @param[in] elements Array of elements to populate the table. + * @param[in] n_elements The number of elements in the array. + * + * @returns Nothing. + */ +void honey_lua_create_table(lua_State* L, + honey_lua_element* elements, + unsigned int n_elements); + +#endif |