From fd6e5bcef96ac5e769470823e24b984c9394bfd5 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Wed, 21 Oct 2020 20:24:05 -0500 Subject: refactor: move honey_lua function definitions to common.h --- CMakeLists.txt | 2 +- src/common.h | 42 +++++++++ src/honey_lua.c | 40 +++++++++ src/input/input.c | 242 ++++++++++++++++++++++++++-------------------------- src/input/input.h | 1 - src/lua/honey_lua.c | 40 --------- 6 files changed, 204 insertions(+), 163 deletions(-) create mode 100644 src/honey_lua.c delete mode 100644 src/lua/honey_lua.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d2e0ab1..c691224 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ set(SOURCE_FILES src/error/error.c src/honey.c src/input/input.c - src/lua/honey_lua.c + src/honey_lua.c src/light/light.c src/mesh/mesh.c src/model/model.c diff --git a/src/common.h b/src/common.h index ede67cd..1a56b56 100644 --- a/src/common.h +++ b/src/common.h @@ -74,4 +74,46 @@ void honey_error_set_string2(char* string); */ void honey_human_readable_error(char* error_string, honey_result error); +/* lua binding functions */ + +typedef enum { + HONEY_INT, + HONEY_NUM, + HONEY_STRING, + HONEY_FUNC +} honey_lua_type; + +/** @brief Wrap C objects for lua. */ +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 diff --git a/src/honey_lua.c b/src/honey_lua.c new file mode 100644 index 0000000..076fb65 --- /dev/null +++ b/src/honey_lua.c @@ -0,0 +1,40 @@ +#include "common.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