hello_lib/
hello_lib.rs

1use core::ffi::c_int;
2use lunka::prelude::*;
3
4unsafe extern "C-unwind" fn l_hello(l: *mut LuaState) -> c_int {
5	// SAFETY: Caller ensures `l` is valid.
6	let lua = unsafe { LuaThread::from_ptr(l) };
7
8	let n = lua.check_number(1);
9
10	// SAFETY: Ditto.
11	lua.push_string("Hello, world!");
12	lua.push_number(n * core::f64::consts::PI as LuaNumber);
13
14	2
15}
16
17const LIBRARY: LuaLibrary<1> = lua_library! {
18	hello: l_hello
19};
20
21#[unsafe(no_mangle)]
22unsafe extern "C-unwind" fn luaopen_hello(l: *mut LuaState) -> c_int {
23	// SAFETY: Caller ensures `l` is valid.
24	let lua = unsafe { LuaThread::from_ptr(l) };
25	lua.new_lib(&LIBRARY);
26	1
27}