From cd4a04ee73e50056f3d04e9f13b490dbf5ed800a Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 21 Aug 2021 23:06:27 -0500 Subject: begin refactoring tests to reflect struct-based API and implement hs_pushstring() --- src/hs_parse_args.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/hs_parse_args.c (limited to 'src/hs_parse_args.c') diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c new file mode 100644 index 0000000..6fb8f31 --- /dev/null +++ b/src/hs_parse_args.c @@ -0,0 +1,111 @@ +#include "honeysuckle.h" + +static bool check_parse(lua_State *L, int index, struct hs_arg *expected) +{ + switch(expected->type) { + case HS_BOOL: + if (!lua_isboolean(L, index)) + return false; + *(expected->ptr.boolean) = lua_toboolean(L, index); + return true; + + case HS_INT: + if (!lua_isnumber(L, index)) + return false; + *(expected->ptr.integer) = lua_tointeger(L, index); + return true; + + case HS_NUM: + if (!lua_isnumber(L, index)) + return false; + *(expected->ptr.number) = lua_tonumber(L, index); + return true; + + case HS_STR: + if (!lua_isstring(L, index)) + return false; + *(expected->ptr.string) = (char *) lua_tostring(L, index); + return true; + + case HS_TBL: + if (!lua_istable(L, index)) + return false; + *(expected->ptr.stack_index) = index; + return true; + + case HS_FUNC: + if (!lua_isfunction(L, index)) + return false; + *(expected->ptr.stack_index) = index; + return true; + + case HS_CFUNC: + if (!lua_iscfunction(L, index)) + return false; + *(expected->ptr.function) = lua_tocfunction(L, index); + return true; + + case HS_USER: + if (!lua_isuserdata(L, index)) + return false; + *(expected->ptr.userdata) = lua_touserdata(L, index); + return true; + + case HS_LIGHT: + if (!lua_islightuserdata(L, index)) + return false; + *(expected->ptr.userdata) = lua_touserdata(L, index); + return true; + + case HS_NIL: + if (!lua_isnil(L, index)) + return false; + *(expected->ptr.stack_index) = index; + + case HS_ANY: + *(expected->ptr.stack_index) = index; + return true; + + default: + return false; + } +} + + +static bool try_parse_args(lua_State *L, int n_args, struct hs_arg *arguments) +{ + int args_provided = lua_gettop(L); + if (args_provided != n_args) + return false; + + for (int i=0; i