summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2021-08-08 16:15:01 -0500
committersanine-a <sanine.not@pm.me>2021-08-08 16:15:01 -0500
commit1459422a3dc158fa4c7222f7780d134261458b7c (patch)
tree5a86808284ce948d65fafc1bd4fcd4cfdc97202e
parente97f0a3b4e27a7c9539c0423e1363ba6f3faf0c1 (diff)
add hs_throw_error() implementation and fix bug in tests
-rw-r--r--src/hs_throw_error.c23
-rw-r--r--src/tests/hs_throw_error_tests.c10
2 files changed, 29 insertions, 4 deletions
diff --git a/src/hs_throw_error.c b/src/hs_throw_error.c
new file mode 100644
index 0000000..b2377dd
--- /dev/null
+++ b/src/hs_throw_error.c
@@ -0,0 +1,23 @@
+#include <stdlib.h>
+
+#include "honeysuckle.h"
+
+void hs_throw_error(lua_State *L, const char *format_string, ...)
+{
+ va_list args, args_;
+ va_start(args, format_string);
+ 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 an error message");
+ else {
+ vsnprintf(string, string_size+1, format_string, args);
+ lua_pushstring(L, string);
+ free(string);
+ }
+ lua_error(L);
+}
diff --git a/src/tests/hs_throw_error_tests.c b/src/tests/hs_throw_error_tests.c
index 1c5af6a..ebb05b4 100644
--- a/src/tests/hs_throw_error_tests.c
+++ b/src/tests/hs_throw_error_tests.c
@@ -1,11 +1,11 @@
#include "hs_tests.h"
-const char *err_string = "";
+char err_string[32] = "";
int set_err_string(lua_State *L)
{
if (lua_isstring(L, -1))
- err_string = lua_tostring(L, -1);
+ strcpy(err_string, lua_tostring(L, -1));
return 0;
}
@@ -18,8 +18,9 @@ int throw_const_error(lua_State *L)
TEST(hs_throw_error_constant)
{
- lua_pushcfunction(L, throw_const_error);
+ lua_pushcfunction(L, set_err_string);
int pos = lua_gettop(L);
+ lua_pushcfunction(L, throw_const_error);
int result = lua_pcall(L, 0, 0, pos);
mu_assert_equal(result, LUA_ERRRUN);
mu_assert_str_not_equal(err_string, "");
@@ -36,8 +37,9 @@ int throw_number_error(lua_State *L)
TEST(hs_throw_error_format)
{
- lua_pushcfunction(L, throw_number_error);
+ lua_pushcfunction(L, set_err_string);
int pos = lua_gettop(L);
+ lua_pushcfunction(L, throw_number_error);
int result = lua_pcall(L, 0, 0, pos);
mu_assert_equal(result, LUA_ERRRUN);
mu_assert_str_not_equal(err_string, "");