low-luajit 0.4.0

Low level but safe bindings to the LuaJIT C API with no dependencies and no-std support to create Lua modules using Rust.
Documentation
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 }
	}
}