Skip to content

Commit

Permalink
add luau_try macro
Browse files Browse the repository at this point in the history
  • Loading branch information
bjcscat committed Dec 16, 2024
1 parent 7f2cff1 commit e69f30f
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ impl Luau {
unsafe { lua_break(self.state) }
}

/// Produces an error with the value on the top of the stack
pub fn error(&self) -> c_int {
luau_stack_precondition!(self.check_index(-1));

// SAFETY: a value on the top of the stack exists as verified by the precondition
unsafe {lua_error(self.state)}
}

/// Returns the type of a luau value at `idx`
pub fn type_of(&self, idx: c_int) -> LuauType {
luau_stack_precondition!(self.check_index(idx));
Expand Down Expand Up @@ -1256,6 +1264,16 @@ impl Drop for Luau {
}
}

#[macro_export]
macro_rules! try_luau {
($state:ident, $block:block) => {
{
$state.push_function(|$state| $block, Some("_try_lua"), 0);
$state.call(0, 0)
}
};
}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
Expand All @@ -1273,6 +1291,19 @@ mod tests {
LuauLibs, LuauStatus, LuauType,
};

#[test]
fn try_test() {
let luau = Luau::default();

let status = try_luau!(luau, {
luau.push_boolean(true);
luau.error()
});

assert!(matches!(status, LuauStatus::LUA_ERRRUN), "Expected a runtime error");
assert!(luau.to_boolean(-1), "Expected the boolean to be true");
}

#[test]
#[should_panic]
fn stack_checking_no_value() {
Expand Down Expand Up @@ -1349,6 +1380,7 @@ mod tests {
assert_eq!(luau.type_of(-1), LuauType::LUA_TFUNCTION);
}


#[test]
fn tables() {
let luau = Luau::default();
Expand Down Expand Up @@ -1559,25 +1591,6 @@ mod tests {
.is_err());
}

// #[test]
// fn try_safety() {
// let luau = Luau::default();

// let mut did_error = false;

// luau.lua_try_catch(
// |state, _| state.error(LuauError::RuntimeError("error!")),
// |_, did_error| {
// *did_error = true;
// },
// &mut did_error,
// );

// assert!(did_error, "Expected error callback to be invoked.")

// todo!();
// }

#[test]
fn function_check() {
let luau = Luau::default();
Expand Down

0 comments on commit e69f30f

Please sign in to comment.