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