diff options
author | sanine <sanine.not@pm.me> | 2021-08-26 11:30:22 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-08-26 11:30:22 -0500 |
commit | 2526f9e419923fdd871c7065523dcd2cf80cec72 (patch) | |
tree | 481c88a934838bf47f50e5fa5941c3a36b9bcd47 | |
parent | 9d4fa04c41ce44ad8bfaa9cce65e2b26d8b642d1 (diff) |
add first-pass implementation of hs_parse_overloaded
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/hs_parse_args.c | 25 |
2 files changed, 31 insertions, 1 deletions
@@ -94,7 +94,9 @@ with helpful error messages. integer that will be filled with the stack index of the argument. Please note that, due to technical limitations, this function can -only parse up to 255 arguments. +only parse up to 255 arguments. This function may mutate any +preexisting contents of the variables passed to contain the +arguments. ```c int some_lua_binding(lua_State *L) @@ -132,6 +134,9 @@ actually used. If none of the argument lists match the provided arguments, this function throws an error. +This function may mutate preexisting data in the variables passed to +store arguments. + ```c int overloaded_lua_binding(lua_State *L) { diff --git a/src/hs_parse_args.c b/src/hs_parse_args.c index 685561e..d6d3b3c 100644 --- a/src/hs_parse_args.c +++ b/src/hs_parse_args.c @@ -112,6 +112,16 @@ void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments) } } + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +static void hs_overloaded_error(lua_State *L, va_list args) +{ + lua_pushstring(L, "hs_overloaded failed"); + lua_error(L); +} + + int hs_parse_overloaded_(lua_State *L, ...) { va_list args, args_error; @@ -119,6 +129,21 @@ int hs_parse_overloaded_(lua_State *L, ...) va_copy(args_error, args); int choice = 0; + + while(true) { + int n_args = va_arg(args, int); + if (n_args == -1) + break; + else { + struct hs_arg *arguments = va_arg(args, struct hs_arg *); + if (try_parse_args(L, n_args, arguments)) + return choice; + } + choice++; + } + + hs_overloaded_error(L, args_error); + va_end(args); va_end(args_error); } |