diff options
author | sanine <sanine.not@pm.me> | 2021-08-28 11:58:36 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-08-28 11:58:36 -0500 |
commit | 43a85192bbb76c6ed055d0328da9ce360a8d6dea (patch) | |
tree | 2aa9d613076244864521ad01bf8c33d427a9fe81 | |
parent | 80b31efd46f6e9fc1abd57494a5b43530c7fa180 (diff) |
fix some README example code and correct numerical types in tests
-rw-r--r-- | README.md | 29 | ||||
-rw-r--r-- | src/tests/hs_parse_args_tests.c | 4 | ||||
-rw-r--r-- | src/tests/hs_parse_overloaded_tests.c | 6 |
3 files changed, 23 insertions, 16 deletions
@@ -61,8 +61,8 @@ These constants are used when honeysuckle needs to know a type to expect. - `HS_BOOL` - booleans -- `HS_INT` - lua_Integer (aka long int) -- `HS_NUM` - lua_Number (aka double) +- `HS_INT` - lua_Integer (DO NOT use int, long int, or other types!) +- `HS_NUM` - lua_Number (DO NOT use float, double, or other types!) - `HS_STR` - strings - `HS_TBL` - tables - `HS_FUNC` - any function @@ -75,6 +75,9 @@ expect. `hs_type_to_string()` takes as argument a single type constant and returns a const string representing the type. +For the numeric types (`HS_INT` and `HS_NUM`, using variables other +than `lua_Integer` and `lua_Number` may result in undefined behavior. + ### Argument parsing [(Back to top)](#table-of-contents) @@ -109,7 +112,7 @@ int some_lua_binding(lua_State *L) void* user; hs_parse_args(L, // explicit initializer for struct - { HS_BOOL, ptr.bool = &b }, + { HS_BOOL, .ptr.bool = &b }, // macro initialization (recommended) hs_int(i), hs_tbl(tbl_index), @@ -131,6 +134,11 @@ in the macro `hs_overload()`. This function returns an integer containing the zero-indexed index of the overload option that was actually used. +Please note that because of Lua's type system, overloads that differ +only in an integer/number pair, function/c function pair, or +userdata/light userdata pair in the same position may not work as +expected. + If none of the argument lists match the provided arguments, this function throws an error. @@ -140,8 +148,8 @@ store arguments. ```c int overloaded_lua_binding(lua_State *L) { - long int i; - double n; + lua_Integer i; + lua_Number n; char *str; int choice = hs_parse_overloaded(L, hs_overload( hs_int(i) ), @@ -168,7 +176,7 @@ struct hs_tbl_entry`s. These can either be constructed by hand, or via function-like macros identified by lowercase versions of the typenames shown above (e.g. `hs_str_int` would create a string key with integer value). Most of the type constants expect their -associated type (e.g. `HS_INT` expects a `long int`, `HS_CFUNC` expects ` +associated type (e.g. `HS_INT` expects a `lua_Integer`, `HS_CFUNC` expects ` lua_CFunction`, etc.) but `HS_FUNC`, `HS_USER`, and `HS_TBL` expect an integer stack index. You cannot use `HS_NIL` or `HS_ANY` with this function. @@ -181,15 +189,14 @@ int *ptr; // some pointer hs_create_table(L, // manual struct initialization - { HS_STR, key.string="intVal", HS_INT, value.integer=4 }, + { HS_STR, .key.string="intVal", HS_INT, .value.integer=4 }, // macro initialization (recommended) hs_str_num("numVal", 6.0f), hs_int_tbl(12, hs_create_table(L, hs_str_cfunc("subTableCFunc", some_lua_binding), hs_int_bool(15, true), - ), - "lightUserdataVal", HS_LIGHT, (void *)ptr, - HS_END); + ) +); ``` ### Table processing @@ -226,7 +233,7 @@ void open_logfile(const char *logfile, void *settings) { struct settings { bool debug; - long int verbosity; + lua_Integer verbosity; float epsilon; }; diff --git a/src/tests/hs_parse_args_tests.c b/src/tests/hs_parse_args_tests.c index 300a519..83178f3 100644 --- a/src/tests/hs_parse_args_tests.c +++ b/src/tests/hs_parse_args_tests.c @@ -549,8 +549,8 @@ TEST(parse_readme_example) lua_pushstring(L, "c: c: c:"); void *userdata = lua_newuserdata(L, sizeof(char)); - bool b; long int i; int table_index; - double f; char *str; void *user; + bool b; lua_Integer i; int table_index; + lua_Number f; char *str; void *user; hs_parse_args (L, hs_bool(b), hs_int(i), hs_tbl(table_index), hs_num(f), hs_str(str), hs_user(user)); diff --git a/src/tests/hs_parse_overloaded_tests.c b/src/tests/hs_parse_overloaded_tests.c index 784807a..3c327cb 100644 --- a/src/tests/hs_parse_overloaded_tests.c +++ b/src/tests/hs_parse_overloaded_tests.c @@ -10,7 +10,7 @@ static int testfunc(lua_State *L) { return 0; } */ #define PARSE_OVERLOADED \ - bool b; long int i; int ti, fi, ni; double f; \ + bool b; lua_Integer i; int ti, fi, ni; lua_Number f; \ char *str; lua_CFunction fn; void *user, *light; \ int choice = hs_parse_overloaded \ (L, \ @@ -124,7 +124,7 @@ TEST(parse_2_3_overload_0) { lua_pushinteger(L, 69); lua_pushstring(L, "foo"); - long int i; char *str; void *data; + lua_Integer i; char *str; void *data; int choice = hs_parse_overloaded (L, hs_overload(hs_int(i), hs_str(str)), @@ -141,7 +141,7 @@ TEST(parse_2_3_overload_1) lua_pushinteger(L, 69); lua_pushstring(L, "foo"); void *userdata = lua_newuserdata(L, sizeof(char)); - long int i; char *str; void *data; + lua_Integer i; char *str; void *data; int choice = hs_parse_overloaded (L, hs_overload(hs_int(i), hs_str(str)), |