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 self.assert_string_index(index);
36
37 // SAFETY: self.0 is always valid.
38 unsafe { ffi::lua_objlen(self.0, index) }
39 }
40
41 /// Returns `true` if the two values at acceptable indices `index1` and `index2` are equal. Returns `false` if they're not equal or invalid.
42 ///
43 /// # Lua Errors
44 ///
45 /// Comparing may trigger the `__eq` metamethod which could error.
46 ///
47 /// To avoid triggering any metamethod, see [`LuaState::raw_equal`].
48 #[inline]
49 #[must_use]
50 pub fn equal(&self, index1: i32, index2: i32) -> bool {
51 // SAFETY: self.0 is always valid.
52 unsafe { ffi::lua_equal(self.0, index1, index2) != 0 }
53 }
54
55 /// Returns `true` if the two values at acceptable indices `index1` and `index2` are equal. Returns `false` if they're not equal or invalid.
56 ///
57 /// Unlike [`LuaState::equal`], this function will not trigger the `__eq` metamethod.
58 #[inline]
59 #[must_use]
60 pub fn raw_equal(&self, index1: i32, index2: i32) -> bool {
61 // SAFETY: self.0 is always valid.
62 unsafe { ffi::lua_rawequal(self.0, index1, index2) != 0 }
63 }
64
65 /// Returns `true` if the value at acceptable index `index1` is less than the value at acceptable index `index2`.
66 /// Returns `false` if they're not equal or invalid.
67 ///
68 /// # Lua Errors
69 ///
70 /// Some types may be incompatible for this comparison (like comparing a `boolean` with a `string`, or `nil` with...anything).
71 ///
72 /// Comparing may trigger the `__lt` metamethod which could error.
73 #[inline]
74 #[must_use]
75 pub fn less_than(&self, index1: i32, index2: i32) -> bool {
76 // SAFETY: self.0 is always valid.
77 unsafe { ffi::lua_lessthan(self.0, index1, index2) != 0 }
78 }
79}