summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2021-09-04 01:40:27 -0500
committersanine <sanine.not@pm.me>2021-09-04 01:40:27 -0500
commit16c30cbbeb77cf5607a3212b4e7039169b9b268e (patch)
treef719ec8abba9ee936777c353bb13efe0563a083e
parentc5befe281c1ce96471d391bff9708e0cd8eadb4b (diff)
fix minor bugs in hs_process_table tests and implement hs_process_table
-rw-r--r--src/hs_process_table.c81
-rw-r--r--src/tests/hs_process_table_tests.c4
2 files changed, 78 insertions, 7 deletions
diff --git a/src/hs_process_table.c b/src/hs_process_table.c
index 9d72253..d07d815 100644
--- a/src/hs_process_table.c
+++ b/src/hs_process_table.c
@@ -1,15 +1,86 @@
#include "honeysuckle.h"
-void hs_pt_set_boolean(bool value, void *data) {}
-void hs_pt_set_integer(lua_Integer value, void *data) {}
-void hs_pt_set_number(lua_Number value, void *data) {}
-void hs_pt_set_string(const char *value, void *data) {}
+void hs_pt_set_boolean(bool value, void *data)
+{
+ *(bool *) data = value;
+}
+
+void hs_pt_set_integer(lua_Integer value, void *data)
+{
+ *(lua_Integer *) data = value;
+}
+
+void hs_pt_set_number(lua_Number value, void *data)
+{
+ *(lua_Number *) data = value;
+}
+
+void hs_pt_set_string(const char *value, void *data)
+{
+ *(const char **) data = value;
+}
+
+
+static bool process_key(lua_State *L, struct hs_table_processor *p)
+{
+ switch (p->type) {
+ case HS_BOOL:
+ if (!lua_isboolean(L, -1))
+ hs_throw_error(L,
+ "expected key '%s' to be of type boolean, "\
+ "but got type %s instead",
+ p->key,
+ lua_typename(L, lua_type(L, -1)));
+ p->func.boolean(lua_toboolean(L, -1), p->data);
+ break;
+ case HS_INT:
+ if (!lua_isnumber(L, -1))
+ hs_throw_error(L,
+ "expected key '%s' to be of type integer, "\
+ "but got type %s instead",
+ p->key,
+ lua_typename(L, lua_type(L, -1)));
+ p->func.integer(lua_tointeger(L, -1), p->data);
+ break;
+
+ case HS_NUM:
+ if (!lua_isnumber(L, -1))
+ hs_throw_error(L,
+ "expected key '%s' to be of type number, "\
+ "but got type %s instead",
+ p->key,
+ lua_typename(L, lua_type(L, -1)));
+ p->func.number(lua_tonumber(L, -1), p->data);
+ break;
+
+ case HS_STR:
+ if (!lua_isstring(L, -1))
+ hs_throw_error(L,
+ "expected key '%s' to be of type string, "\
+ "but got type %s instead",
+ p->key,
+ lua_typename(L, lua_type(L, -1)));
+ p->func.string(lua_tostring(L, -1), p->data);
+ break;
+
+ default:
+ // bad type value, throw error
+ hs_throw_error(L, "bad expected type '%d' for key '%s'",
+ p->type, p->key);
+ break;
+ }
+}
void hs_process_table_(lua_State *L,
int table_index,
int n_processors,
struct hs_table_processor *processors)
{
-
+ for (int i=0; i<n_processors; i++) {
+ lua_getfield(L, table_index, processors[i].key);
+ if (!lua_isnil(L, -1))
+ process_key(L, processors+i);
+ lua_pop(L, 1);
+ }
}
diff --git a/src/tests/hs_process_table_tests.c b/src/tests/hs_process_table_tests.c
index c336b96..e8e8c4b 100644
--- a/src/tests/hs_process_table_tests.c
+++ b/src/tests/hs_process_table_tests.c
@@ -2,8 +2,8 @@
#define process() \
bool boolean = false; \
- int integer = 0; \
- float number = 0; \
+ lua_Integer integer = 0; \
+ lua_Number number = 0; \
const char *string = ""; \
hs_process_table(L, -1, \
hs_process_bool("boolean", hs_pt_set_boolean, &boolean), \