1pub use bindings::*;
2use std::{
3 mem,
4 os::raw::{c_char, c_int, c_void},
5 ptr,
6};
7
8#[allow(non_upper_case_globals)]
9#[allow(non_camel_case_types)]
10#[allow(non_snake_case)]
11pub mod bindings;
12
13pub unsafe fn lua_getextraspace(state: *mut lua_State) -> *mut c_void {
15 (state as *mut c_void).offset(-(mem::size_of::<*mut c_void>() as isize))
16}
17
18pub unsafe fn lua_pcall(
19 state: *mut lua_State,
20 nargs: c_int,
21 nresults: c_int,
22 msgh: c_int,
23) -> c_int {
24 lua_pcallk(state, nargs, nresults, msgh, 0, None)
25}
26
27pub unsafe fn lua_newuserdata(state: *mut lua_State, s: usize) -> *mut ::std::os::raw::c_void {
28 lua_newuserdatauv(state, s, 1)
29}
30
31pub unsafe fn lua_getuservalue(state: *mut lua_State, idx: c_int) -> c_int {
32 lua_getiuservalue(state, idx, 1)
33}
34
35pub unsafe fn lua_setuservalue(state: *mut lua_State, idx: c_int) -> c_int {
36 lua_setiuservalue(state, idx, 1)
37}
38
39pub unsafe fn lua_tonumber(state: *mut lua_State, idx: c_int) -> lua_Number {
40 return lua_tonumberx(state, idx, ptr::null_mut());
41}
42pub unsafe fn lua_tointeger(state: *mut lua_State, idx: c_int) -> lua_Number {
43 return lua_tonumberx(state, idx, ptr::null_mut());
44}
45pub unsafe fn lua_pop(state: *mut lua_State, n: c_int) {
46 lua_settop(state, -(n) - 1);
47}
48pub unsafe fn lua_isfunction(state: *mut lua_State, n: c_int) -> bool {
49 return lua_type(state, n) == LUA_TFUNCTION as i32;
50}
51pub unsafe fn lua_istable(state: *mut lua_State, n: c_int) -> bool {
52 return lua_type(state, n) == LUA_TTABLE as i32;
53}
54pub unsafe fn lua_islightuserdata(state: *mut lua_State, n: c_int) -> bool {
55 return lua_type(state, n) == LUA_TLIGHTUSERDATA as i32;
56}
57pub unsafe fn lua_isnil(state: *mut lua_State, n: c_int) -> bool {
58 return lua_type(state, n) == LUA_TNIL as i32;
59}
60pub unsafe fn lua_isnumber(state: *mut lua_State, n: c_int) -> bool {
61 return lua_type(state, n) == LUA_TNUMBER as i32;
62}
63pub unsafe fn lua_isinteger(state: *mut lua_State, n: c_int) -> bool {
64 return lua_type(state, n) == LUA_TNUMBER as i32;
65}
66pub unsafe fn lua_isboolean(state: *mut lua_State, n: c_int) -> bool {
67 return lua_type(state, n) == LUA_TBOOLEAN as i32;
68}
69pub unsafe fn lua_isthread(state: *mut lua_State, n: c_int) -> bool {
70 return lua_type(state, n) == LUA_TTHREAD as i32;
71}
72pub unsafe fn lua_isnone(state: *mut lua_State, n: c_int) -> bool {
73 return lua_type(state, n) == LUA_TNONE as i32;
74}
75pub unsafe fn lua_isnoneornil(state: *mut lua_State, n: c_int) -> bool {
76 return lua_type(state, n) == LUA_TNONE as i32;
77}
78
79pub unsafe fn lua_pushliteral(state: *mut lua_State, str: *const c_char) -> *const c_char {
80 return lua_pushstring(state, str);
81}
82
83pub unsafe fn lua_pushglobaltable(state: *mut lua_State) {
84 lua_rawgeti(state, LUA_REGISTRYINDEX as i32, LUA_RIDX_GLOBALS as i64);
85}
86
87pub unsafe fn lua_newtable(state: *mut lua_State) {
88 lua_createtable(state, 0, 0);
89}
90
91pub unsafe fn lua_register(state: *mut lua_State, n: *const c_char, f: lua_CFunction) {
92 lua_pushcfunction(state, f);
93 lua_setglobal(state, n);
94}
95
96pub unsafe fn lua_pushcfunction(state: *mut lua_State, f: lua_CFunction) {
97 lua_pushcclosure(state, f, 0);
98}
99
100pub unsafe fn lua_tostring(state: *mut lua_State, i: c_int) -> *const c_char {
101 return lua_tolstring(state, i, ptr::null_mut());
102}
103
104pub unsafe fn lua_insert(state: *mut lua_State, idx: c_int) {
105 lua_rotate(state, idx, 1);
106}
107
108pub unsafe fn lua_remove(state: *mut lua_State, idx: c_int) {
109 lua_rotate(state, idx, -1);
110 lua_pop(state, 1);
111}
112
113pub unsafe fn lua_replace(state: *mut lua_State, idx: c_int) {
114 lua_copy(state, -1, idx);
115 lua_pop(state, 1);
116}
117
118pub unsafe fn lua_upvalueindex(index: c_int) -> i32 {
119 return LUA_REGISTRYINDEX - index;
120}
121
122pub unsafe fn lua_call(state: *mut lua_State, nargs: c_int, nresults: c_int) {
123 lua_callk(state, nargs, nresults, 0, None)
124}
125
126pub unsafe fn lua_yield(state: *mut lua_State, n: c_int) -> c_int {
127 lua_yieldk(state, n, 0, None)
128}
129
130#[allow(non_snake_case)]
131pub unsafe fn luaL_getmetatable(state: *mut lua_State, n: *const i8) -> c_int {
132 lua_getfield(state, LUA_REGISTRYINDEX, n)
133}
134
135#[allow(non_snake_case)]
136pub unsafe fn luaL_loadfile(state: *mut lua_State, f: *const c_char) -> c_int {
137 luaL_loadfilex(state, f, std::ptr::null())
138}
139
140#[allow(non_snake_case)]
141pub unsafe fn luaL_checkstring(state: *mut lua_State, n: c_int) -> *const c_char {
142 luaL_checklstring(state, n, std::ptr::null_mut())
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148 use std::ffi::{CStr, CString};
149
150 #[test]
151 fn version_test() {
152 let l = unsafe { luaL_newstate() };
154 unsafe { luaL_openlibs(l) };
155
156 let version = CString::new("_VERSION").expect("version");
158 unsafe { lua_getglobal(l, version.as_ptr()) };
159
160 let version_string_ptr = unsafe { lua_tostring(l, -1) };
162 let version_string = unsafe { CStr::from_ptr(version_string_ptr) }
163 .to_str()
164 .unwrap();
165
166 assert_eq!(version_string, "Lua 5.4")
167 }
168}