Skip to main content

simple/
simple.rs

1use low_luajit::{AmountReturned, LuaState, reg};
2
3extern "C-unwind" fn sum(lua: LuaState) -> AmountReturned {
4	let mut result = 0.0;
5
6	// Sum up all the arguments together, erroring if a non-number is passed.
7	for i in 1..=lua.get_top() {
8		result += lua.check_number(i);
9	}
10
11	lua.return_number(result)
12}
13
14#[allow(clippy::cast_possible_wrap)]
15extern "C-unwind" fn count_bits(mut lua: LuaState) -> AmountReturned {
16	let number = lua.check_integer(1);
17
18	// Push and return the amount of zeros and ones in the integer.
19	lua.push_integer(number.count_zeros() as isize);
20	lua.push_integer(number.count_ones() as isize);
21	lua.return_any(2)
22}
23
24extern "C-unwind" fn is_ascii(lua: LuaState) -> AmountReturned {
25	let string = lua.check_string(1);
26
27	let is_ascii = string.is_ascii();
28	lua.return_boolean(is_ascii)
29}
30
31#[unsafe(no_mangle)]
32extern "C-unwind" fn luaopen_simple(lua: LuaState) -> AmountReturned {
33	println!("Hello from Rust!");
34
35	lua.return_module(reg![sum, count_bits, is_ascii])
36}