low_luajit/lua/math.rs
1use crate::{LuaState, ffi};
2
3/// Math functions.
4impl LuaState {
5 /// Concatenates the first `n` elements on top of the stack, popping them in the process, and pushes the result at the top.
6 ///
7 /// If `n` is `1` or negative, nothing happens. If `n` is `0`, the result is an empty string.
8 ///
9 /// # Panics
10 ///
11 /// Panics if the stack contains less than `n` values currently.
12 ///
13 /// # Lua Errors
14 ///
15 /// Some types (like `nil` or `boolean`) may be unable to be concatenated.
16 ///
17 /// Concatenating may also trigger the `__concat` metamethod which could error.
18 #[inline]
19 pub fn concat(&mut self, n: i32) {
20 assert!(n <= self.get_top(), "tried to concatenate {n} values but the stack currently holds {} values", self.get_top());
21
22 // SAFETY: self.0 is always valid and the stack contains enough values to pop.
23 unsafe { ffi::lua_concat(self.0, n); }
24 }
25
26 /// Returns the "length" of the value at the given acceptable `index`:
27 /// - Strings return the string's length.
28 /// - Numbers are converted (permanently!) to a string and return the string's length.
29 /// - Tables return the result of the length operator (`#`), bypassing the `__len` metamethod.
30 /// - Userdata returns the size of the allocated block.
31 /// - Anything else will always return 0.
32 #[inline]
33 #[must_use]
34 pub fn object_length(&self, index: i32) -> usize {
35 // SAFETY: self.0 is always valid.
36 unsafe { ffi::lua_objlen(self.0, index) }
37 }
38
39 /// Returns `true` if the two values at acceptable indices `index1` and `index2` are equal. Returns `false` if they're not equal or invalid.
40 ///
41 /// # Lua Errors
42 ///
43 /// Comparing may trigger the `__eq` metamethod which could error.
44 ///
45 /// To avoid triggering any metamethod, see [`LuaState::raw_equal`].
46 #[inline]
47 #[must_use]
48 pub fn equal(&self, index1: i32, index2: i32) -> bool {
49 // SAFETY: self.0 is always valid.
50 unsafe { ffi::lua_equal(self.0, index1, index2) != 0 }
51 }
52
53 /// Returns `true` if the two values at acceptable indices `index1` and `index2` are equal. Returns `false` if they're not equal or invalid.
54 ///
55 /// Unlike [`LuaState::equal`], this function will not trigger the `__eq` metamethod.
56 #[inline]
57 #[must_use]
58 pub fn raw_equal(&self, index1: i32, index2: i32) -> bool {
59 // SAFETY: self.0 is always valid.
60 unsafe { ffi::lua_rawequal(self.0, index1, index2) != 0 }
61 }
62
63 /// Returns `true` if the value at acceptable index `index1` is less than the value at acceptable index `index2`.
64 /// Returns `false` if they're not equal or invalid.
65 ///
66 /// # Lua Errors
67 ///
68 /// Some types may be incompatible for this comparison (like comparing a `boolean` with a `string`, or `nil` with...anything).
69 ///
70 /// Comparing may trigger the `__lt` metamethod which could error.
71 #[inline]
72 #[must_use]
73 pub fn less_than(&self, index1: i32, index2: i32) -> bool {
74 // SAFETY: self.0 is always valid.
75 unsafe { ffi::lua_lessthan(self.0, index1, index2) != 0 }
76 }
77}