Skip to content

Commit d35f506

Browse files
committed
Improve function call signature and impl
1 parent d219c19 commit d35f506

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/glua.gleam

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,30 @@ pub fn failure(error: e) -> Action(a, e) {
447447
Error(CustomError(error))
448448
}
449449

450+
/// Returns the `error` function.
451+
@external(erlang, "glua_stdlib_ffi", "error")
452+
fn error_func() -> Value
453+
454+
const non_error = "The error function returned a non error which should never happen! Please report this issue to the git repository."
455+
450456
/// Invokes the Lua `error` function with the provided message.
451-
pub fn error(message: String) -> Action(List(Value), e) {
452-
call_function_by_name(["error"], [string(message)])
457+
pub fn error(message: String) -> Action(a, e) {
458+
use state <- Action
459+
case call_function(error_func(), [string(message)]).function(state) {
460+
Ok(_) -> panic as non_error
461+
Error(e) -> Error(e)
462+
}
453463
}
454464

455465
/// Invokes the Lua `error` function with the provided message and level.
456-
pub fn error_with_level(message: String, level: Int) -> Action(List(Value), e) {
457-
call_function_by_name(["error"], [string(message), int(level)])
466+
pub fn error_with_level(message: String, level: Int) -> Action(a, e) {
467+
use state <- Action
468+
case
469+
call_function(error_func(), [string(message), int(level)]).function(state)
470+
{
471+
Ok(_) -> panic as non_error
472+
Error(e) -> Error(e)
473+
}
458474
}
459475

460476
/// Transforms the return value of an `Action` with the provided function.

0 commit comments

Comments
 (0)