low_luajit/lua/error.rs
1use core::{ffi::CStr, hint::unreachable_unchecked};
2use crate::{LuaState, ffi};
3
4/// Error handling functions.
5impl LuaState {
6 /* TODO: Currently this function effectively does nothing for some reason.
7 /// Sets a new panic function, returning the previous one.
8 ///
9 /// `panic_f` will be called when an error happens outside of a protected environment (see [`LuaState::pcall`] and [`LuaState::cpcall`]) before exiting.
10 /// You can avoid exiting by having `panic_f` not return.
11 ///
12 /// When `panic_f` is called, the error message will be at the top of the stack.
13 ///
14 /// The default panic function simply prints the error message.
15 ///
16 /// Note: when running Lua within Rust, errors (treated as panics) might cause an early abort, skipping `panic_f`
17 #[inline]
18 pub fn at_panic(&self, panic_f: CFunction) -> CFunction {
19 unsafe { mem::transmute::<lua_CFunction, CFunction>(ffi::lua_atpanic(self.0, mem::transmute::<CFunction, lua_CFunction>(panic_f)))}
20 }*/
21
22 /// Triggers a Lua error using `msg` as the error message (alongside file and line information).
23 ///
24 /// # Lua Errors
25 ///
26 /// This function always errors intentionally.
27 #[inline]
28 pub fn error(&self, msg: &CStr) -> ! {
29 // SAFETY: self.0 and the two CStrs are always valid and luaL_error never returns.
30 unsafe {
31 ffi::luaL_error(self.0, c"%s".as_ptr(), msg.as_ptr());
32 unreachable_unchecked();
33 }
34 }
35
36 /// Triggers a Lua error with the following message (alongside file and line information):
37 ///
38 /// ```text
39 /// bad argument #{arg_n} to '{func}' ({type_name} expected, got {arg_type})
40 /// ```
41 ///
42 /// (`func` is retrieved from the call stack and `arg_type` is the type of the element at index `arg_n`.)
43 ///
44 /// # Lua Errors
45 ///
46 /// This function always errors intentionally.
47 pub fn type_error(&self, arg_n: i32, type_name: &CStr) -> ! {
48 // SAFETY: self.0 and type_name are always valid and luaL_typeerror never returns.
49 unsafe {
50 ffi::luaL_typerror(self.0, arg_n, type_name.as_ptr());
51 unreachable_unchecked();
52 }
53 }
54
55 /// Triggers a Lua error with the following message (alongside file and line information):
56 ///
57 /// ```text
58 /// bad argument #{arg_n} to '{func}' ({extra_msg})
59 /// ```
60 ///
61 /// (`func` is retrieved from the call stack.)
62 ///
63 /// # Lua Errors
64 ///
65 /// This function always errors intentionally.
66 #[inline]
67 pub fn arg_error(&self, arg_n: i32, extra_msg: &CStr) -> ! {
68 // SAFETY: self.0 and extra_msg are always valid and luaL_argerror never returns.
69 unsafe {
70 ffi::luaL_argerror(self.0, arg_n, extra_msg.as_ptr());
71 unreachable_unchecked();
72 }
73 }
74
75 /// Pushes onto the stack a string specifying the position of the function at the given `level`:
76 /// level `0` is the current running function, level `1` is the function that called the current running function, and so on.
77 ///
78 /// The pushed string has the following format:
79 ///
80 /// ```text
81 /// chunk_name:current_line:
82 /// ```
83 ///
84 /// *(A space is appended at the end.)*
85 ///
86 /// If the location of the function at the given `level` is unknown, or `level` is invalid, an empty string is pushed instead.
87 #[inline]
88 pub fn location(&mut self, level: i32) {
89 // SAFETY: self.0 is always valid.
90 unsafe { ffi::luaL_where(self.0, level); }
91 }
92
93 /// Triggers a Lua error, popping the top element and using it as the full error message (no file or line information is included).
94 ///
95 /// If the stack is empty, a generic error message is pushed beforehand.
96 ///
97 /// You probably want to use [`LuaState::error`] instead.
98 ///
99 /// # Lua Errors
100 ///
101 /// This function always errors intentionally.
102 #[inline]
103 pub fn raw_error(&mut self) -> ! {
104 if self.get_top() < 1 {
105 self.push_byte_string(b"(no error message specified)");
106 }
107
108 // SAFETY: self.0 is always valid, lua_error never returns, and the stack is not empty.
109 unsafe {
110 ffi::lua_error(self.0);
111 unreachable_unchecked();
112 }
113 }
114}