diff --git a/ckiwi/ckiwi.cpp b/ckiwi/ckiwi.cpp index b0247c9..476066c 100644 --- a/ckiwi/ckiwi.cpp +++ b/ckiwi/ckiwi.cpp @@ -55,44 +55,26 @@ const KiwiErr* wrap_err(F&& f) { try { f(); } catch (const UnsatisfiableConstraint& ex) { - static const constexpr KiwiErr err { - KiwiErrUnsatisfiableConstraint, - "The constraint cannot be satisfied." - }; + static const constexpr KiwiErr err {KiwiErrUnsatisfiableConstraint}; return &err; } catch (const UnknownConstraint& ex) { - static const constexpr KiwiErr err { - KiwiErrUnknownConstraint, - "The constraint has not been added to the solver." - }; + static const constexpr KiwiErr err {KiwiErrUnknownConstraint}; return &err; } catch (const DuplicateConstraint& ex) { - static const constexpr KiwiErr err { - KiwiErrDuplicateConstraint, - "The constraint has already been added to the solver." - }; + static const constexpr KiwiErr err {KiwiErrDuplicateConstraint}; return &err; } catch (const UnknownEditVariable& ex) { - static const constexpr KiwiErr err { - KiwiErrUnknownEditVar, - "The edit variable has not been added to the solver." - }; + static const constexpr KiwiErr err {KiwiErrUnknownEditVar}; return &err; } catch (const DuplicateEditVariable& ex) { - static const constexpr KiwiErr err { - KiwiErrDuplicateEditVar, - "The edit variable has already been added to the solver." - }; + static const constexpr KiwiErr err {KiwiErrDuplicateEditVar}; return &err; } catch (const BadRequiredStrength& ex) { - static const constexpr KiwiErr err { - KiwiErrBadRequiredStrength, - "A required strength cannot be used in this context." - }; + static const constexpr KiwiErr err {KiwiErrBadRequiredStrength}; return &err; } catch (const InternalSolverError& ex) { @@ -161,9 +143,9 @@ void kiwi_str_release(char* str) { std::free(str); } -void kiwi_err_release(KiwiErr* err) { +void kiwi_err_release(const KiwiErr* err) { if (lk_likely(err && err->must_release)) - std::free(err); + std::free(const_cast(err)); } KiwiVar* kiwi_var_new(const char* name) { diff --git a/ckiwi/ckiwi.h b/ckiwi/ckiwi.h index 88f2d98..39477a8 100644 --- a/ckiwi/ckiwi.h +++ b/ckiwi/ckiwi.h @@ -78,7 +78,7 @@ struct KiwiSolver; LJKIWI_EXP void kiwi_solver_type_info(unsigned sz_align[2]); LJKIWI_EXP void kiwi_str_release(char* str); -LJKIWI_EXP void kiwi_err_release(KiwiErr* err); +LJKIWI_EXP void kiwi_err_release(const KiwiErr* err); LJKIWI_EXP KiwiVar* kiwi_var_new(const char* name); LJKIWI_EXP void kiwi_var_release(KiwiVar* var); diff --git a/kiwi.lua b/kiwi.lua index 4673de9..e7dc66f 100644 --- a/kiwi.lua +++ b/kiwi.lua @@ -110,7 +110,7 @@ typedef struct KiwiErr { struct KiwiSolver; void kiwi_str_release(char *); -void kiwi_err_release(KiwiErr *); +void kiwi_err_release(const KiwiErr *); KiwiVar* kiwi_var_new(const char* name); void kiwi_var_release(KiwiVar* var); @@ -438,8 +438,7 @@ do if RUST then function Var_mt:__new(name) - --return ffi_gc(ljkiwi.kiwi_var_new(name)[0], ljkiwi.kiwi_var_release) - return ljkiwi.kiwi_var_new(name)[0] + return ffi_gc(ljkiwi.kiwi_var_new(name)[0], ljkiwi.kiwi_var_release) end --- Get the name of the variable. @@ -1020,6 +1019,18 @@ do }, Error_mt) end + local ERR_MESSAGES = { + "The constraint cannot be satisfied.", + "The constraint has not been added to the solver.", + "The constraint has already been added to the solver.", + "The edit variable has not been added to the solver.", + "The edit variable has already been added to the solver.", + "A required strength cannot be used in this context.", + "An internal solver error occurred.", + "A memory allocation failed.", + "null object passed as argument.", + "An unknown error occurred.", + } ---@generic T ---@param f fun(solver: kiwi.Solver, item: T, ...): kiwi.KiwiErr? ---@param solver kiwi.Solver @@ -1029,10 +1040,12 @@ do local err = f(solver, item, ...) if err ~= nil then if err.must_release then - ffi_gc(err, ljkiwi.kiwi_error_release) + ffi_gc(err, ljkiwi.kiwi_err_release) end local kind = err.kind - local message = err.message ~= nil and ffi_string(err.message) or "" + local message = err.message ~= nil and ffi_string(err.message) + or ERR_MESSAGES[tonumber(err.kind)] + or "" local errdata = new_error(kind, message, solver, item) local error_mask = ljkiwi.kiwi_solver_get_error_mask(solver) return item,