From db81b925d776103326128bf629cbdda576a223e7 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 16 Apr 2022 11:55:09 -0500 Subject: move 3rd-party librarys into libs/ and add built-in honeysuckle --- libs/honeysuckle/.gitignore | 3 + libs/honeysuckle/CMakeLists.txt | 89 + libs/honeysuckle/LICENSE | 37 + libs/honeysuckle/README.md | 78 + .../examples/argument_parsing/argument_parsing.c | 23 + .../examples/table_creation/table_creation.c | 28 + .../examples/table_processing/table_processing.c | 60 + libs/honeysuckle/src/honeysuckle.h | 523 +++++ libs/honeysuckle/src/hs_create_table.c | 123 ++ libs/honeysuckle/src/hs_parse_args.c | 149 ++ libs/honeysuckle/src/hs_process_table.c | 86 + libs/honeysuckle/src/hs_pushstring.c | 31 + libs/honeysuckle/src/hs_throw_error.c | 12 + libs/honeysuckle/src/hs_traceback.c | 37 + libs/honeysuckle/src/hs_type_to_string.c | 31 + libs/honeysuckle/src/tests/colors.h | 22 + libs/honeysuckle/src/tests/hs_create_table_tests.c | 2239 ++++++++++++++++++++ libs/honeysuckle/src/tests/hs_parse_args_tests.c | 603 ++++++ .../src/tests/hs_parse_overloaded_tests.c | 188 ++ .../honeysuckle/src/tests/hs_process_table_tests.c | 511 +++++ libs/honeysuckle/src/tests/hs_pushstring_tests.c | 52 + libs/honeysuckle/src/tests/hs_tests.h | 63 + libs/honeysuckle/src/tests/hs_throw_error_tests.c | 57 + .../src/tests/hs_type_to_string_tests.c | 102 + libs/honeysuckle/src/tests/tests_main.c | 33 + 25 files changed, 5180 insertions(+) create mode 100644 libs/honeysuckle/.gitignore create mode 100644 libs/honeysuckle/CMakeLists.txt create mode 100644 libs/honeysuckle/LICENSE create mode 100644 libs/honeysuckle/README.md create mode 100644 libs/honeysuckle/examples/argument_parsing/argument_parsing.c create mode 100644 libs/honeysuckle/examples/table_creation/table_creation.c create mode 100644 libs/honeysuckle/examples/table_processing/table_processing.c create mode 100644 libs/honeysuckle/src/honeysuckle.h create mode 100644 libs/honeysuckle/src/hs_create_table.c create mode 100644 libs/honeysuckle/src/hs_parse_args.c create mode 100644 libs/honeysuckle/src/hs_process_table.c create mode 100644 libs/honeysuckle/src/hs_pushstring.c create mode 100644 libs/honeysuckle/src/hs_throw_error.c create mode 100644 libs/honeysuckle/src/hs_traceback.c create mode 100644 libs/honeysuckle/src/hs_type_to_string.c create mode 100644 libs/honeysuckle/src/tests/colors.h create mode 100644 libs/honeysuckle/src/tests/hs_create_table_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_parse_args_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_parse_overloaded_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_process_table_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_pushstring_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_tests.h create mode 100644 libs/honeysuckle/src/tests/hs_throw_error_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_type_to_string_tests.c create mode 100644 libs/honeysuckle/src/tests/tests_main.c (limited to 'libs/honeysuckle') diff --git a/libs/honeysuckle/.gitignore b/libs/honeysuckle/.gitignore new file mode 100644 index 0000000..588f95f --- /dev/null +++ b/libs/honeysuckle/.gitignore @@ -0,0 +1,3 @@ +*~ +build +src/va_nargs.c diff --git a/libs/honeysuckle/CMakeLists.txt b/libs/honeysuckle/CMakeLists.txt new file mode 100644 index 0000000..91222d2 --- /dev/null +++ b/libs/honeysuckle/CMakeLists.txt @@ -0,0 +1,89 @@ +cmake_minimum_required(VERSION 3.2) +project(honeysuckle + VERSION 0.1.0 + DESCRIPTION "A C library to make writing lua bindings simple") + +find_package(Lua51 REQUIRED) +Include_directories(${LUA_INCLUDE_DIR}) + +# build and link the library +set(HS_ROOT ${honeysuckle_SOURCE_DIR}/src) +set(HONEYSUCKLE_SOURCES + ${HS_ROOT}/hs_type_to_string.c + ${HS_ROOT}/hs_pushstring.c + ${HS_ROOT}/hs_throw_error.c + ${HS_ROOT}/hs_parse_args.c + ${HS_ROOT}/hs_create_table.c + ${HS_ROOT}/hs_process_table.c + ${HS_ROOT}/hs_traceback.c + ) +add_library(honeysuckle ${HONEYSUCKLE_SOURCES}) +set_target_properties(honeysuckle PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(honeysuckle ${LUA_LIBRARIES}) +set_target_properties(honeysuckle PROPERTIES + VERSION ${PROJECT_VERSION} + PUBLIC_HEADER src/honeysuckle.h) + + +# optionally build the tests +set(TEST_ROOT ${honeysuckle_SOURCE_DIR}/src/tests) +set(TEST_SOURCES + ${TEST_ROOT}/tests_main.c + ${TEST_ROOT}/hs_type_to_string_tests.c + ${TEST_ROOT}/hs_parse_args_tests.c + ${TEST_ROOT}/hs_parse_overloaded_tests.c + ${TEST_ROOT}/hs_create_table_tests.c + ${TEST_ROOT}/hs_process_table_tests.c + ${TEST_ROOT}/hs_throw_error_tests.c + # ${TEST_ROOT}/hs_traceback_tests.c + # ${TEST_ROOT}/hs_call_tests.c + # ${TEST_ROOT}/hs_call_args_tests.c + ${TEST_ROOT}/hs_pushstring_tests.c + # ${TEST_ROOT}/hs_rxx_tests.c + ) +add_executable(hs-test EXCLUDE_FROM_ALL ${TEST_SOURCES}) +set_target_properties(hs-test PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(hs-test ${LUA_LIBRARIES} honeysuckle) + + +# optionally build the examples +set(EX_ROOT ${honeysuckle_SOURCE_DIR}/examples) + +add_executable(example_table_creation EXCLUDE_FROM_ALL + ${EX_ROOT}/table_creation/table_creation.c) +set_target_properties(example_table_creation PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_table_creation ${LUA_LIBRARIES} honeysuckle) + +add_executable(example_argument_parsing EXCLUDE_FROM_ALL + ${EX_ROOT}/argument_parsing/argument_parsing.c) +set_target_properties(example_argument_parsing PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_argument_parsing ${LUA_LIBRARIES} honeysuckle) + +add_executable(example_table_processing EXCLUDE_FROM_ALL + ${EX_ROOT}/table_processing/table_processing.c) +set_target_properties(example_table_processing PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_table_processing ${LUA_LIBRARIES} honeysuckle) + + +add_custom_target(examples) +add_dependencies(examples + example_table_creation + example_argument_parsing + example_table_processing) + + +include(GNUInstallDirs) + +install(TARGETS honeysuckle + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/libs/honeysuckle/LICENSE b/libs/honeysuckle/LICENSE new file mode 100644 index 0000000..a3b67bc --- /dev/null +++ b/libs/honeysuckle/LICENSE @@ -0,0 +1,37 @@ +ANTI-CAPITALIST SOFTWARE LICENSE (v 1.4) + +Copyright © 2020-2021 Kate Swanson + +This is anti-capitalist software, released for free use by +individuals and organizations that do not operate by capitalist +principles. + +Permission is hereby granted, free of charge, to any person or +organization (the "User") obtaining a copy of this software and +associated documentation files (the "Software"), to use, copy, +modify, merge, distribute, and/or sell copies of the Software, +subject to the following conditions: + +1. The above copyright notice and this permission notice shall be +included in all copies or modified versions of the Software. + +2. The User is one of the following: +a. An individual person, laboring for themselves +b. A non-profit organization +c. An educational institution +d. An organization that seeks shared profit for all of its members, +and allows non-members to set the cost of their labor + +3. If the User is an organization with owners, then all owners are +workers and all workers are owners with equal equity and/or equal vote. + +4. If the User is an organization, then the User is not law +enforcement or military, or working for or under either. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT EXPRESS OR IMPLIED +WARRANTY OF ANY KIND, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/libs/honeysuckle/README.md b/libs/honeysuckle/README.md new file mode 100644 index 0000000..6919f58 --- /dev/null +++ b/libs/honeysuckle/README.md @@ -0,0 +1,78 @@ +--- +layout: base +title: honeysuckle README +--- + +honeysuckle +=========== + +A pure C library to make writing Lua bindings simple. honeysuckle provides +functions to make argument parsing, table operations, error creation and +handling, string wrangling, and registry operations easy and straightforward +from within your C code. + +Table of contents +----------------- + +- [Installation](#installation) +- [Documentation](#documentation) +- [Development](#development) +- [License](#license) +- [Footer](#footer) + +Installation +------------ + +[(Back to top)](#table-of-contents) + +honeysuckle requires CMake and the Lua 5.1 development files. + +First, [download a honeysuckle release](/projects/honeysuckle/releases), +and then do + +```bash +mkdir build +cd build +cmake .. +make +sudo make install +``` + +You should now be able to include `` into your C +files for use. + +To make the tests as well, do the above and then do `make test`. +To make the examples, do `make examples`. + + +Documentation +----- + +You can view the honeysuckle documentation [on the honey wiki](https://honey3d.org/wiki/doku.php?id=honeysuckle). + +Development +----------- +[(Back to top)](#table-of-contents) + +honeysuckle is still very early in development. As the specifics of +the build process and testing become clearer I'll update this +section. + +If you're interested in helping out, send me an email! The to-dos at +the time of writing are: + +* Write initial tests for errors, tracebacks, and function calling +* Implement everything +* Expand the test cases. + +License +------- + +[(Back to top)](#table-of-contents) + + +honeysuckle is licensed under the [Anti-Capitalist Software License]. +Basically: if you're a single person, a co-op, or a nonprofit, feel free to +use this. Otherwise, send me an email. c: + +[Anti-Capitalist Software License]: https://anticapitalist.software diff --git a/libs/honeysuckle/examples/argument_parsing/argument_parsing.c b/libs/honeysuckle/examples/argument_parsing/argument_parsing.c new file mode 100644 index 0000000..11a48f4 --- /dev/null +++ b/libs/honeysuckle/examples/argument_parsing/argument_parsing.c @@ -0,0 +1,23 @@ +#include + +int demo(lua_State *L) +{ + lua_Integer i; + lua_Number n; + char *string; + hs_parse_args(L, hs_int(i), hs_num(n), hs_str(string)); + printf("received %ld, %f, and %s\n", i, n, string); + return 0; +} + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, demo); + lua_setglobal(L, "demo"); + + luaL_dostring(L, "demo(12, 34.4, 'hi there!')"); + return 0; +} diff --git a/libs/honeysuckle/examples/table_creation/table_creation.c b/libs/honeysuckle/examples/table_creation/table_creation.c new file mode 100644 index 0000000..42088c6 --- /dev/null +++ b/libs/honeysuckle/examples/table_creation/table_creation.c @@ -0,0 +1,28 @@ +#include + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + /* create the table + * { "one"=1, "two"=2, "half"=0.5, "runme"=[some function] } + */ + luaL_dostring(L, + "return function(self) \n" \ + "print(self.one)\n" \ + "print(self.two)\n" \ + "print(self.half)\n" \ + "end"); + int func_index = lua_gettop(L); + + hs_create_table(L, + hs_str_int("one", 1), + hs_str_int("two", 2), + hs_str_num("half", 0.5f), + hs_str_func("runme", func_index)); + lua_setglobal(L, "runnable"); + + luaL_dostring(L, "runnable:runme()"); + return 0; +} diff --git a/libs/honeysuckle/examples/table_processing/table_processing.c b/libs/honeysuckle/examples/table_processing/table_processing.c new file mode 100644 index 0000000..64c1f01 --- /dev/null +++ b/libs/honeysuckle/examples/table_processing/table_processing.c @@ -0,0 +1,60 @@ +#include +#include +#include + +struct settings +{ + bool debug; + int debug_level; + char *logfile; +} global_settings; + +void set_logfile(const char* filename, void *s) +{ + struct settings *settings = (struct settings *) s; + settings->logfile = malloc(sizeof(char) * strlen(filename)); + strcpy(settings->logfile, filename); +} + +int process(lua_State *L) +{ + int table_index; + hs_parse_args(L, hs_tbl(table_index)); + hs_process_table(L, table_index, + hs_process_bool("debug", + hs_pt_set_boolean, + &(global_settings.debug)), + hs_process_int("level", + hs_pt_set_integer, + &(global_settings.debug_level)), + hs_process_str("logfile", + set_logfile, + &global_settings)); + return 0; +} + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, process); + lua_setglobal(L, "configure"); + + global_settings.debug = false; + global_settings.debug_level = 0; + global_settings.logfile = "nil"; + + printf("settings: [ %d, %d, '%s' ]\n", + global_settings.debug, + global_settings.debug_level, + global_settings.logfile); + + luaL_dostring(L, "configure{debug=true, level=6, logfile='output.log'}"); + + printf("settings: [ %d, %d, '%s' ]\n", + global_settings.debug, + global_settings.debug_level, + global_settings.logfile); + return 0; +} diff --git a/libs/honeysuckle/src/honeysuckle.h b/libs/honeysuckle/src/honeysuckle.h new file mode 100644 index 0000000..96243ba --- /dev/null +++ b/libs/honeysuckle/src/honeysuckle.h @@ -0,0 +1,523 @@ +#ifndef HONEYSUCKLE_H +#define HONEYSUCKLE_H + +#include + +#include +#include +#include + +/* variadic macro argument counting */ +#ifndef VA_NARGS + +#define VA_NARGS(...) VA_NARGS_SHIFT_COUNT(__VA_ARGS__, VA_NARGS_COUNTS) + +#define VA_NARGS_COUNTS \ + 255, 254, 253, 252, 251, 250, 249, 248, \ + 247, 246, 245, 244, 243, 242, 241, 240, \ + 239, 238, 237, 236, 235, 234, 233, 232, \ + 231, 230, 229, 228, 227, 226, 225, 224, \ + 223, 222, 221, 220, 219, 218, 217, 216, \ + 215, 214, 213, 212, 211, 210, 209, 208, \ + 207, 206, 205, 204, 203, 202, 201, 200, \ + 199, 198, 197, 196, 195, 194, 193, 192, \ + 191, 190, 189, 188, 187, 186, 185, 184, \ + 183, 182, 181, 180, 179, 178, 177, 176, \ + 175, 174, 173, 172, 171, 170, 169, 168, \ + 167, 166, 165, 164, 163, 162, 161, 160, \ + 159, 158, 157, 156, 155, 154, 153, 152, \ + 151, 150, 149, 148, 147, 146, 145, 144, \ + 143, 142, 141, 140, 139, 138, 137, 136, \ + 135, 134, 133, 132, 131, 130, 129, 128, \ + 127, 126, 125, 124, 123, 122, 121, 120, \ + 119, 118, 117, 116, 115, 114, 113, 112, \ + 111, 110, 109, 108, 107, 106, 105, 104, \ + 103, 102, 101, 100, 99, 98, 97, 96, \ + 95, 94, 93, 92, 91, 90, 89, 88, \ + 87, 86, 85, 84, 83, 82, 81, 80, \ + 79, 78, 77, 76, 75, 74, 73, 72, \ + 71, 70, 69, 68, 67, 66, 65, 64, \ + 63, 62, 61, 60, 59, 58, 57, 56, \ + 55, 54, 53, 52, 51, 50, 49, 48, \ + 47, 46, 45, 44, 43, 42, 41, 40, \ + 39, 38, 37, 36, 35, 34, 33, 32, \ + 31, 30, 29, 28, 27, 26, 25, 24, \ + 23, 22, 21, 20, 19, 18, 17, 16, \ + 15, 14, 13, 12, 11, 10, 9, 8, \ + 7, 6, 5, 4, 3, 2, 1, 0 + +#define VA_NARGS_GET_COUNT( \ + _255, _254, _253, _252, _251, _250, _249, _248, \ + _247, _246, _245, _244, _243, _242, _241, _240, \ + _239, _238, _237, _236, _235, _234, _233, _232, \ + _231, _230, _229, _228, _227, _226, _225, _224, \ + _223, _222, _221, _220, _219, _218, _217, _216, \ + _215, _214, _213, _212, _211, _210, _209, _208, \ + _207, _206, _205, _204, _203, _202, _201, _200, \ + _199, _198, _197, _196, _195, _194, _193, _192, \ + _191, _190, _189, _188, _187, _186, _185, _184, \ + _183, _182, _181, _180, _179, _178, _177, _176, \ + _175, _174, _173, _172, _171, _170, _169, _168, \ + _167, _166, _165, _164, _163, _162, _161, _160, \ + _159, _158, _157, _156, _155, _154, _153, _152, \ + _151, _150, _149, _148, _147, _146, _145, _144, \ + _143, _142, _141, _140, _139, _138, _137, _136, \ + _135, _134, _133, _132, _131, _130, _129, _128, \ + _127, _126, _125, _124, _123, _122, _121, _120, \ + _119, _118, _117, _116, _115, _114, _113, _112, \ + _111, _110, _109, _108, _107, _106, _105, _104, \ + _103, _102, _101, _100, _99, _98, _97, _96, \ + _95, _94, _93, _92, _91, _90, _89, _88, \ + _87, _86, _85, _84, _83, _82, _81, _80, \ + _79, _78, _77, _76, _75, _74, _73, _72, \ + _71, _70, _69, _68, _67, _66, _65, _64, \ + _63, _62, _61, _60, _59, _58, _57, _56, \ + _55, _54, _53, _52, _51, _50, _49, _48, \ + _47, _46, _45, _44, _43, _42, _41, _40, \ + _39, _38, _37, _36, _35, _34, _33, _32, \ + _31, _30, _29, _28, _27, _26, _25, _24, \ + _23, _22, _21, _20, _19, _18, _17, _16, \ + _15, _14, _13, _12, _11, _10, _9, _8, \ + _7, _6, _5, _4, _3, _2, _1, N, ...) N + +#define VA_NARGS_SHIFT_COUNT(...) VA_NARGS_GET_COUNT(__VA_ARGS__) + +#endif + + +/* type constants */ +typedef enum + { HS_BOOL, + HS_INT, + HS_NUM, + HS_STR, + HS_TBL, + HS_FUNC, + HS_CFUNC, + HS_USER, + HS_LIGHT, + HS_NIL, + HS_ANY, + } hs_type; + +const char* hs_type_to_string(hs_type type); + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * hs_parse_args and hs_parse_overloaded + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +struct hs_arg { + hs_type type; + union { + bool *boolean; + lua_Integer *integer; + lua_Number *number; + char **string; + int *stack_index; + lua_CFunction *function; + void **userdata; + } ptr; +}; + +#define hs_bool(x) { .type=HS_BOOL, .ptr.boolean = &(x) } +#define hs_int(x) { .type=HS_INT, .ptr.integer = &(x) } +#define hs_num(x) { .type=HS_NUM, .ptr.number = &(x) } +#define hs_str(x) { .type=HS_STR, .ptr.string = &(x) } +#define hs_tbl(x) { .type=HS_TBL, .ptr.stack_index = &(x) } +#define hs_func(x) { .type=HS_FUNC, .ptr.stack_index = &(x) } +#define hs_cfunc(x) { .type=HS_CFUNC, .ptr.function = &(x) } +#define hs_user(x) { .type=HS_USER, .ptr.userdata = &(x) } +#define hs_light(x) { .type=HS_LIGHT, .ptr.userdata = &(x) } +#define hs_nil(x) { .type=HS_NIL, .ptr.stack_index = &(x) } +#define hs_any(x) { .type=HS_ANY, .ptr.stack_index = &(x) } + +void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments); + +#define hs_parse_args(L, ...) \ + hs_parse_args_(L, \ + VA_NARGS(__VA_ARGS__)/2, \ + (struct hs_arg[]) { __VA_ARGS__ }) + + +#define hs_overload(...) VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ } + +int hs_parse_overloaded_(lua_State *L, ...); + +#define hs_parse_overloaded(L, ...) \ + hs_parse_overloaded_(L, __VA_ARGS__, -1) + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * hs_create_table + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +typedef union { + bool boolean; + lua_Integer integer; + lua_Number number; + char *string; + int stack_index; + lua_CFunction function; + void *userdata; +} hs_value; + +struct hs_tbl_entry { + hs_type key_type; + hs_value key; + hs_type value_type; + hs_value value; +}; + +#define hs_bool_bool(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_bool_int(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_bool_num(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_bool_str(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_bool_tbl(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_bool_func(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_bool_cfunc(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_bool_user(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_bool_light(k, v) \ + { .key_type=HS_BOOL, .key.boolean=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_int_bool(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_int_int(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_int_num(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_int_str(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_int_tbl(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_int_func(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_int_cfunc(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_int_user(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_int_light(k, v) \ + { .key_type=HS_INT, .key.integer=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_num_bool(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_num_int(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_num_num(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_num_str(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_num_tbl(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_num_func(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_num_cfunc(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_num_user(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_num_light(k, v) \ + { .key_type=HS_NUM, .key.number=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_str_bool(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_str_int(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_str_num(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_str_str(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_str_tbl(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_str_func(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_str_cfunc(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_str_user(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_str_light(k, v) \ + { .key_type=HS_STR, .key.string=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + + +#define hs_tbl_bool(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_tbl_int(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_tbl_num(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_tbl_str(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_tbl_tbl(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_tbl_func(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_tbl_cfunc(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_tbl_user(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_tbl_light(k, v) \ + { .key_type=HS_TBL, .key.stack_index=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_func_bool(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_func_int(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_func_num(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_func_str(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_func_tbl(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_func_func(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_func_cfunc(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_func_user(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_func_light(k, v) \ + { .key_type=HS_FUNC, .key.stack_index=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_cfunc_bool(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_cfunc_int(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_cfunc_num(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_cfunc_str(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_cfunc_tbl(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_cfunc_func(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_cfunc_cfunc(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_cfunc_user(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_cfunc_light(k, v) \ + { .key_type=HS_CFUNC, .key.function=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_user_bool(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_user_int(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_user_num(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_user_str(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_user_tbl(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_user_func(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_user_cfunc(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_user_user(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_user_light(k, v) \ + { .key_type=HS_USER, .key.stack_index=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +#define hs_light_bool(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_BOOL, .value.boolean=v } +#define hs_light_int(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_INT, .value.integer=v } +#define hs_light_num(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_NUM, .value.number=v } +#define hs_light_str(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_STR, .value.string=v } +#define hs_light_tbl(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_TBL, .value.stack_index=v } +#define hs_light_func(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_FUNC, .value.stack_index=v } +#define hs_light_cfunc(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_CFUNC, .value.function=v } +#define hs_light_user(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_USER, .value.stack_index=v } +#define hs_light_light(k, v) \ + { .key_type=HS_LIGHT, .key.userdata=k, \ + .value_type=HS_LIGHT, .value.userdata=v } + +int hs_create_table_(lua_State *L, int n_elements, struct hs_tbl_entry *elements); + +#define hs_create_table(L, ...) \ + hs_create_table_(L, \ + VA_NARGS(__VA_ARGS__)/4, \ + (struct hs_tbl_entry[]) { __VA_ARGS__ }) + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * hs_process_table + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +typedef void (*hs_pt_bool_callback)(bool, void *); +typedef void (*hs_pt_int_callback)(lua_Integer, void *); +typedef void (*hs_pt_num_callback)(lua_Number, void *); +typedef void (*hs_pt_str_callback)(const char *, void *); + + +// default processors +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); + + +struct hs_table_processor { + const char *key; + hs_type type; + union { + hs_pt_bool_callback boolean; + hs_pt_int_callback integer; + hs_pt_num_callback number; + hs_pt_str_callback string; + } func; + void *data; +}; + +#define hs_process_bool(str, f, d) \ + { .key=(str), .type=HS_BOOL, .func.boolean=(f), .data=(d) } +#define hs_process_int(str, f, d) \ + { .key=(str), .type=HS_INT, .func.integer=(f), .data=(d) } +#define hs_process_num(str, f, d) \ + { .key=(str), .type=HS_NUM, .func.number=(f), .data=(d) } +#define hs_process_str(str, f, d) \ + { .key=(str), .type=HS_STR, .func.string=(f), .data=(d) } + +void hs_process_table_(lua_State *L, + int table_index, + int n_processors, + struct hs_table_processor *processors); + +#define hs_process_table(L, table_index, ...) \ + hs_process_table_(L, table_index, \ + VA_NARGS(__VA_ARGS__)/4, \ + (struct hs_table_processor[]) {__VA_ARGS__}) + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * hs_pushstring + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +void hs_vpushstring(lua_State *L, const char *format_string, va_list args); +void hs_pushstring(lua_State *L, const char *format_string, ...); + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * error creation and handling + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +void hs_throw_error(lua_State *L, const char *format_string, ...); +int hs_traceback(lua_State *L); +int hs_call(lua_State *L, int nargs, int nret); + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * registry operations + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +#define hs_rstore(L) luaL_ref(L, LUA_REGISTRYINDEX); +#define hs_rload(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) +#define hs_rdel(L, ref) luaL_unref(L, LUA_REGISTRYINDEX, ref) + +#endif diff --git a/libs/honeysuckle/src/hs_create_table.c b/libs/honeysuckle/src/hs_create_table.c new file mode 100644 index 0000000..422a480 --- /dev/null +++ b/libs/honeysuckle/src/hs_create_table.c @@ -0,0 +1,123 @@ +#include + +#include "honeysuckle.h" + +static void push_value(lua_State *L, hs_type type, hs_value value) +{ + switch (type) { + case HS_BOOL: + lua_pushboolean(L, value.boolean); + break; + + case HS_INT: + lua_pushinteger(L, value.integer); + break; + + case HS_NUM: + lua_pushnumber(L, value.number); + break; + + case HS_STR: + lua_pushstring(L, value.string); + break; + + case HS_TBL: + lua_pushvalue(L, value.stack_index); + break; + + case HS_FUNC: + lua_pushvalue(L, value.stack_index); + break; + + case HS_CFUNC: + lua_pushcfunction(L, value.function); + break; + + case HS_USER: + lua_pushvalue(L, value.stack_index); + break; + + case HS_LIGHT: + lua_pushlightuserdata(L, value.userdata); + break; + + default: + hs_throw_error(L, "attempted to push data of invalid type %d", type); + break; + } +} + + +static inline bool poppable(hs_type type) +{ + if (type == HS_TBL || + type == HS_FUNC || + type == HS_USER) { + return true; + } + return false; +} + + +static void print_stack(lua_State *L) +{ + printf("stack: %d [", lua_gettop(L)); + for (int i=0; ikey_type, e->key); + // print_stack(L); + push_value(L, e->value_type, e->value); + // print_stack(L); + lua_rawset(L, index); + // print_stack(L); + } + + int n_poppable = 0; + int pop_indices[n_elements]; + + for (int i=0; ikey_type)) + pop_indices[n_poppable++] = e->key.stack_index; + if (poppable(e->value_type)) + pop_indices[n_poppable++] = e->value.stack_index; + } + + // printf("cleaning up\n"); + + qsort(pop_indices, n_poppable, sizeof(int), descending); + for (int i=0; i + +#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) || lua_isnumber(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; itype) { + 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 + +#include "honeysuckle.h" + +void hs_vpushstring(lua_State *L, const char *format_string, va_list args) +{ + va_list args_; + va_copy(args_, args); + + int string_size = vsnprintf(NULL, 0, format_string, args_); + va_end(args_); + + char *string = malloc((string_size+1) * sizeof(char)); + if (string == NULL) { + lua_pushstring(L, "there was an error allocating memory for a string"); + lua_error(L); + } + + vsnprintf(string, string_size+1, format_string, args); + lua_pushstring(L, string); + free(string); +} + + +void hs_pushstring(lua_State *L, const char *format_string, ...) +{ + va_list args; + va_start(args, format_string); + hs_vpushstring(L, format_string, args); + va_end(args); +} diff --git a/libs/honeysuckle/src/hs_throw_error.c b/libs/honeysuckle/src/hs_throw_error.c new file mode 100644 index 0000000..20a4c6d --- /dev/null +++ b/libs/honeysuckle/src/hs_throw_error.c @@ -0,0 +1,12 @@ +#include + +#include "honeysuckle.h" + +void hs_throw_error(lua_State *L, const char *format_string, ...) +{ + va_list args; + va_start(args, format_string); + hs_vpushstring(L, format_string, args); + va_end(args); + lua_error(L); +} diff --git a/libs/honeysuckle/src/hs_traceback.c b/libs/honeysuckle/src/hs_traceback.c new file mode 100644 index 0000000..1830721 --- /dev/null +++ b/libs/honeysuckle/src/hs_traceback.c @@ -0,0 +1,37 @@ +#include "honeysuckle.h" + +int hs_traceback(lua_State *L) +{ + if (!lua_isstring(L, 1)) + /* 'message' is not a string, keep intact */ + return 1; + + lua_getglobal(L, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + return 1; +} + + +int hs_call(lua_State *L, int nargs, int nret) +{ + int traceback_pos = lua_gettop(L) - nargs; + lua_pushcfunction(L, hs_traceback); + lua_insert(L, traceback_pos); + + int result = lua_pcall(L, nargs, nret, traceback_pos); + lua_remove(L, traceback_pos); + return result; +} diff --git a/libs/honeysuckle/src/hs_type_to_string.c b/libs/honeysuckle/src/hs_type_to_string.c new file mode 100644 index 0000000..d0a580c --- /dev/null +++ b/libs/honeysuckle/src/hs_type_to_string.c @@ -0,0 +1,31 @@ +#include "honeysuckle.h" + +const char * hs_type_to_string(hs_type type) +{ + switch(type) { + case HS_BOOL: + return "boolean"; + case HS_INT: + return "integer"; + case HS_NUM: + return "number"; + case HS_STR: + return "string"; + case HS_TBL: + return "table"; + case HS_FUNC: + return "function"; + case HS_CFUNC: + return "C function"; + case HS_USER: + return "userdata"; + case HS_LIGHT: + return "light userdata"; + case HS_NIL: + return "nil"; + case HS_ANY: + return "any"; + default: + return "(unknown)"; + } +} diff --git a/libs/honeysuckle/src/tests/colors.h b/libs/honeysuckle/src/tests/colors.h new file mode 100644 index 0000000..21451de --- /dev/null +++ b/libs/honeysuckle/src/tests/colors.h @@ -0,0 +1,22 @@ +#ifndef COLORS_H +#define COLORS_H + +#define RESET "\033[0m" +#define BLACK "\033[30m" /* Black */ +#define RED "\033[31m" /* Red */ +#define GREEN "\033[32m" /* Green */ +#define YELLOW "\033[33m" /* Yellow */ +#define BLUE "\033[34m" /* Blue */ +#define MAGENTA "\033[35m" /* Magenta */ +#define CYAN "\033[36m" /* Cyan */ +#define WHITE "\033[37m" /* White */ +#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ +#define BOLDRED "\033[1m\033[31m" /* Bold Red */ +#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ +#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ +#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ +#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ +#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ +#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ + +#endif diff --git a/libs/honeysuckle/src/tests/hs_create_table_tests.c b/libs/honeysuckle/src/tests/hs_create_table_tests.c new file mode 100644 index 0000000..1adc947 --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_create_table_tests.c @@ -0,0 +1,2239 @@ +#include "hs_tests.h" + +static int testfunc1(lua_State *L) { return 0; } +static int testfunc2(lua_State *L) { return 0; } + +static void print_stack(lua_State *L) +{ + printf("stack: %d [", lua_gettop(L)); + for (int i=0; i +#include +#include + +#include +#include +#include + +#include "../honeysuckle.h" +#include "colors.h" + +#define STR_IMPL(x) #x +#define STR(x) STR_IMPL(x) + +#define mu_indent " " + +/* minunit testing macros modified from those at + www.jera.com/techinfo/jtns/jtn002.html */ +#define mu_assert(message, test) do { \ + if (!(test)) return message \ + "\n" mu_indent MAGENTA " [" __FILE__ ":" STR(__LINE__) "]" RESET; \ + } while (0) +#define mu_assert_equal(a, b) mu_assert("'" #a "' is not equal to '" #b "'", (a) == (b)) +#define mu_assert_not_equal(a, b) mu_assert("'" #a "' is equal to '" #b "'", (a) != (b)) +#define mu_assert_str_equal(a, b) \ + mu_assert("'" #a "' is not equal to '" #b "'", strcmp((a), (b)) == 0) +#define mu_assert_str_not_equal(a, b) \ + mu_assert("'" #a "' is equal to '" #b "'", strcmp((a), (b)) != 0) + +#define mu_run_test(name, test) do { \ + lua_State *L = luaL_newstate(); \ + luaL_openlibs(L); \ + const char *message = test(L); \ + lua_close(L); \ + tests_run++; \ + if (message) { \ + printf(RED mu_indent "test '%s' failed:" RESET " %s\n", name, message); \ + tests_failed++; \ + } \ + } while (0) +#define TEST(name) static const char* name(lua_State *L) +#define mu_run_suite(suite) do { \ + tests_run_old = tests_run; \ + suite(); \ + printf(mu_indent "ran %d tests\n", tests_run - tests_run_old); \ + } while (0) + +extern int tests_run, tests_run_old, tests_failed; + +void hs_type_to_string_tests(); +void hs_parse_args_tests(); +void hs_parse_overloaded_tests(); +void hs_create_table_tests(); +void hs_create_enum_tests(); +void hs_process_table_tests(); +void hs_throw_error_tests(); +void hs_traceback_tests(); +void hs_call_tests(); +void hs_call_args_tests(); +void hs_pushstring_tests(); +void hs_rxx_tests(); diff --git a/libs/honeysuckle/src/tests/hs_throw_error_tests.c b/libs/honeysuckle/src/tests/hs_throw_error_tests.c new file mode 100644 index 0000000..7ae683c --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_throw_error_tests.c @@ -0,0 +1,57 @@ +#include "hs_tests.h" + +char err_string[32] = ""; + +int set_err_string(lua_State *L) +{ + if (lua_isstring(L, -1)) + strcpy(err_string, lua_tostring(L, -1)); + return 0; +} + + +#define HS_ERROR_TEST(name, error_body, expectation) \ + int name ## _errfunc(lua_State *L) error_body; \ + TEST(name) { \ + lua_pushcfunction(L, set_err_string); \ + int pos = lua_gettop(L); \ + lua_pushcfunction(L, name ## _errfunc); \ + int result = lua_pcall(L, 0, 0, pos); \ + mu_assert_equal(result, LUA_ERRRUN); \ + mu_assert_str_not_equal(err_string, ""); \ + mu_assert_str_equal(err_string, expectation); \ + return 0; \ + } + + +HS_ERROR_TEST(hs_throw_error_constant, { + hs_throw_error(L, "a constant error"); + return 0; + }, "a constant error"); + + +HS_ERROR_TEST(hs_throw_error_format, { + hs_throw_error(L, "%s number %d", "error", 10); + return 0; + }, "error number 10"); + + +HS_ERROR_TEST(hs_throw_error_long, { + hs_throw_error(L, "%s is a %s is a %s", + "a very, very, very long string", + "a very, very, very long string", + "a very, very, very long string"); + return 0; + }, "a very, very, very long string is a a very, very, very long string is a a very, very, very long string"); + + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void hs_throw_error_tests() +{ + printf("running hs_throw_error() tests...\n"); + + mu_run_test("throw constant error string", hs_throw_error_constant); + mu_run_test("throw error with format string", hs_throw_error_format); + mu_run_test("throw error with very long string", hs_throw_error_long); +} diff --git a/libs/honeysuckle/src/tests/hs_type_to_string_tests.c b/libs/honeysuckle/src/tests/hs_type_to_string_tests.c new file mode 100644 index 0000000..3b158ab --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_type_to_string_tests.c @@ -0,0 +1,102 @@ +#include "hs_tests.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_type_to_string + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +TEST(hs_bool_to_string) +{ + mu_assert("HS_BOOL does not result in 'boolean'!", + strcmp(hs_type_to_string(HS_BOOL), "boolean") == 0); + return 0; +} + +TEST(hs_int_to_string) +{ + mu_assert("HS_INT does not result in 'integer'!", + strcmp(hs_type_to_string(HS_INT), "integer") == 0); + return 0; +} + +TEST(hs_num_to_string) +{ + mu_assert("HS_NUM does not result in 'number'!", + strcmp(hs_type_to_string(HS_NUM), "number") == 0); + return 0; +} + +TEST(hs_str_to_string) +{ + mu_assert("HS_STR does not result in 'string'!", + strcmp(hs_type_to_string(HS_STR), "string") == 0); + return 0; +} + +TEST(hs_tbl_to_string) +{ + mu_assert("HS_TBL does not result in 'table'!", + strcmp(hs_type_to_string(HS_TBL), "table") == 0); + return 0; +} + +TEST(hs_func_to_string) +{ + mu_assert("HS_FUNC does not result in 'function'!", + strcmp(hs_type_to_string(HS_FUNC), "function") == 0); + return 0; +} + +TEST(hs_cfunc_to_string) +{ + mu_assert("HS_CFUNC does not result in 'C function'!", + strcmp(hs_type_to_string(HS_CFUNC), "C function") == 0); + return 0; +} + +TEST(hs_user_to_string) +{ + mu_assert("HS_USER does not result in 'userdata'!", + strcmp(hs_type_to_string(HS_USER), "userdata") == 0); + return 0; +} + +TEST(hs_light_to_string) +{ + mu_assert("HS_LIGHT does not result in 'light userdata'!", + strcmp(hs_type_to_string(HS_LIGHT), "light userdata") == 0); + return 0; +} + +TEST(hs_nil_to_string) +{ + mu_assert("HS_NIL does not result in 'nil'!", + strcmp(hs_type_to_string(HS_NIL), "nil") == 0); + return 0; +} + +TEST(hs_any_to_string) +{ + mu_assert("HS_ANY does not result in 'any'!", + strcmp(hs_type_to_string(HS_ANY), "any") == 0); + return 0; +} + + +void hs_type_to_string_tests() +{ + printf("running hs_type_to_string() tests...\n"); + mu_run_test("bool to string", hs_bool_to_string); + mu_run_test("int to string", hs_int_to_string); + mu_run_test("num to string", hs_num_to_string); + mu_run_test("str to string", hs_str_to_string); + mu_run_test("tbl to string", hs_tbl_to_string); + mu_run_test("func to string", hs_func_to_string); + mu_run_test("cfunc to string", hs_cfunc_to_string); + mu_run_test("user to string", hs_user_to_string); + mu_run_test("light to string", hs_light_to_string); + mu_run_test("nil to string", hs_nil_to_string); + mu_run_test("any to string", hs_any_to_string); +} diff --git a/libs/honeysuckle/src/tests/tests_main.c b/libs/honeysuckle/src/tests/tests_main.c new file mode 100644 index 0000000..26af7cc --- /dev/null +++ b/libs/honeysuckle/src/tests/tests_main.c @@ -0,0 +1,33 @@ +#include "hs_tests.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * RUN TESTS + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int tests_run = 0; +int tests_run_old = 0; +int tests_failed = 0; + +int main() +{ + printf("================ start tests ================\n\n"); + + mu_run_suite(hs_type_to_string_tests); + mu_run_suite(hs_parse_args_tests); + mu_run_suite(hs_parse_overloaded_tests); + mu_run_suite(hs_create_table_tests); + //mu_run_suite(hs_create_enum_tests); + mu_run_suite(hs_process_table_tests); + mu_run_suite(hs_throw_error_tests); + + mu_run_suite(hs_pushstring_tests); + + printf("\n=============== tests finished ===============\n\n"); + + const char *color = tests_failed == 0 ? GREEN : RED; + printf("%sran %d tests, %d failed\n" RESET, color, tests_run, tests_failed); + return tests_failed; +} -- cgit v1.2.1