diff options
Diffstat (limited to 'src/common.h')
-rw-r--r-- | src/common.h | 81 |
1 files changed, 34 insertions, 47 deletions
diff --git a/src/common.h b/src/common.h index 556304f..6ae1c40 100644 --- a/src/common.h +++ b/src/common.h @@ -72,40 +72,25 @@ typedef enum { HONEY_N_ERRORS } honey_result; -#define HONEY_ERROR_DATA_STRING_LENGTH 4096 - -static struct { - char string1[HONEY_ERROR_DATA_STRING_LENGTH]; - char string2[HONEY_ERROR_DATA_STRING_LENGTH]; -} honey_error_data; - -void honey_error_clear_strings(); -void honey_error_set_string1(char* string); -void honey_error_set_string2(char* string); - -/** @brief Generate a human-readable error message. - * - * @param[out] error_string A string with at least 3*HONEY_ERROR_DATA_STRING_LENGTH characters to store the result - * @param[in] error The error to generate a message for - */ -void honey_human_readable_error(char* error_string, honey_result error); +honey_result honey_format_string(char** string, + char* format_string, + ...); -/** @brief Generate a string from a format string. +/** @brief Throw an error with a string generated by a printf format string. * - * This function allocates memory for the destination; the user is - * responsible for deallocating it. As a side effect of this, the destination - * pointer cannot overlap with any of the varargs. + * This function attempts to create a string from the given format string; + * if an error should occur, it will instead throw a lua error with the appropriate + * message. * - * @param[out] string Pointer to the destination string. - * @param[in] format_string The format string used to generate the result. + * @param[in] L The Lua state to throw an error from. + * @param[in] format_string The format string used to generate the error message. * @param[in] ... The arguments for the format string. * - * @returns HONEY_OK on success; HONEY_MEMORY_ALLOCATION_ERROR on a - * memory allocation error. + * @returns Nothing. */ -honey_result honey_format_string(char** string, - char* format_string, - ...); +void honey_lua_throw_error(lua_State* L, + char* format_string, + ...); /* lua binding functions */ @@ -122,35 +107,37 @@ typedef enum { HONEY_ANY } honey_lua_type; +typedef struct { + honey_lua_type type; + void* ptr; +} honey_lua_argument_pair; + /** @brief Get arguments from a function, checking to ensure the types match. * - * If a function should accept a variable list of arguments, but you still wish to ensure - * correct types, use honey_lua_validate_types() and throw an error only if all of your - * possiblities fail to match. + * Each argument type should be specified as [# of args], type, ptr, type, ptr..., + * e.g. + * ``` + * honey_lua_parse_arguments(L, 3, + * // option 0 + * 2, HONEY_INTEGER, &a, HONEY_INTEGER, &b, + * // option 1 + * 1, HONEY_INTEGER, &a, + * // option 2 + * 0); + * ``` * * Note that this function will check for correct types of HONEY_TABLE, HONEY_NIL, and * HONEY_FUNCTION, but does not expect a pointer to them. It performs no check for * HONEY_ANY, and also does not expect a pointer. * * @param[in] L The lua state to parse arguments from. - * @param[in] n The number of arguments to parse. - * @param[in] ... Variadic list of alternating types and pointers to store the type. - * - * @returns Nothing, but throws a lua_error if a type mismatch is detected. - */ -void honey_lua_parse_arguments(lua_State* L, int n, ...); - - -/** @brief Check that a functions' arguments are of the correct type. - * - * @param[in] L The lua state to validate. - * @param[in] n_types The number of types to validate. - * @param[in] ... Variadic list of honey_lua_types to validate against the stack. + * @param[in] n The number of argument options to parse. + * @param[in] ... Variadic list of argument options. * - * @returns true if the validation was successful; false otherwise, pushing an error message - * to the lua stack. + * @returns The zero-indexed index of the actual argument option used. Throws an + * error if no options matched the provided arguments. */ -bool honey_lua_validate_types(lua_State* L, unsigned int n_types, ...); +int honey_lua_parse_arguments(lua_State* L, int n, ...); /** @brief Wrap C objects for lua. */ typedef struct honey_lua_element { |