1#![allow(non_snake_case)]
2
3use core::ffi::{c_char, c_double, c_int, c_void};
4use core::marker::{PhantomData, PhantomPinned};
5
6#[repr(C)]
7pub struct State {
8 _data: [u8; 0],
9
10 _marker: PhantomData<(*mut u8, PhantomPinned)>,
14}
15
16pub const LUA_REGISTRYINDEX: c_int = -10000;
18pub const LUA_ENVIRONINDEX: c_int = -10001;
19pub const LUA_GLOBALSINDEX: c_int = -10002;
20
21pub const fn lua_upvalueindex(i: c_int) -> c_int {
22 LUA_GLOBALSINDEX - i
23}
24
25pub const LUA_OK: c_int = 0;
27pub const LUA_ERRRUN: c_int = 2;
28pub const LUA_ERRMEM: c_int = 4;
29pub const LUA_ERRERR: c_int = 5;
30
31pub const LUA_TNONE: c_int = -1;
33pub const LUA_TNIL: c_int = 0;
34pub const LUA_TBOOLEAN: c_int = 1;
35pub const LUA_TLIGHTUSERDATA: c_int = 2;
36pub const LUA_TNUMBER: c_int = 3;
37pub const LUA_TSTRING: c_int = 4;
38pub const LUA_TTABLE: c_int = 5;
39pub const LUA_TFUNCTION: c_int = 6;
40pub const LUA_TUSERDATA: c_int = 7;
41pub const LUA_TTHREAD: c_int = 8;
42
43pub type CFunction = unsafe extern "C" fn(L: *mut State) -> c_int;
45
46pub type Integer = isize;
48
49pub type Number = c_double;
51
52#[cfg_attr(
53 all(target_os = "windows", target_env = "msvc"),
54 link(name = "lua51", kind = "raw-dylib")
55)]
56unsafe extern "C" {
57 pub fn lua_call(L: *mut State, nargs: c_int, nresults: c_int);
59
60 pub fn lua_createtable(L: *mut State, narr: c_int, nrec: c_int);
72
73 pub fn lua_error(L: *mut State) -> !;
75
76 pub fn lua_getfield(L: *mut State, index: c_int, k: *const c_char);
78
79 pub fn lua_getmetatable(L: *mut State, index: c_int) -> c_int;
81
82 pub fn lua_gettop(L: *mut State) -> c_int;
84
85 pub fn lua_newuserdata(L: *mut State, size: usize) -> *mut c_void;
87
88 pub fn lua_next(L: *mut State, index: c_int) -> c_int;
90
91 pub fn lua_objlen(L: *mut State, index: c_int) -> usize;
93
94 pub fn lua_pcall(
96 L: *mut State,
97 nargs: c_int,
98 nresults: c_int,
99 errorfunc: c_int,
100 ) -> c_int;
101
102 pub fn lua_pushboolean(L: *mut State, n: Integer);
104
105 pub fn lua_pushcclosure(L: *mut State, r#fn: CFunction, n: c_int);
107
108 pub fn lua_pushinteger(L: *mut State, n: Integer);
110
111 pub fn lua_pushlightuserdata(L: *mut State, p: *mut c_void);
113
114 pub fn lua_pushlstring(L: *mut State, s: *const c_char, len: usize);
116
117 pub fn lua_pushnil(L: *mut State);
119
120 pub fn lua_pushnumber(L: *mut State, n: Number);
122
123 pub fn lua_pushstring(L: *mut State, s: *const c_char);
125
126 pub fn lua_pushvalue(L: *mut State, index: c_int);
128
129 pub fn lua_rawgeti(L: *mut State, index: c_int, n: c_int);
131
132 pub fn lua_rawset(L: *mut State, index: c_int);
134
135 pub fn lua_rawseti(L: *mut State, index: c_int, n: c_int);
137
138 pub fn lua_settop(L: *mut State, index: c_int);
140
141 pub fn lua_toboolean(L: *mut State, index: c_int) -> c_int;
143
144 pub fn lua_tointeger(L: *mut State, index: c_int) -> Integer;
146
147 pub fn lua_tolstring(
149 L: *mut State,
150 index: c_int,
151 len: *mut usize,
152 ) -> *const c_char;
153
154 pub fn lua_tonumber(L: *mut State, index: c_int) -> Number;
156
157 pub fn lua_touserdata(L: *mut State, index: c_int) -> *mut c_void;
159
160 pub fn lua_type(L: *mut State, index: c_int) -> c_int;
162
163 pub fn lua_typename(L: *mut State, tp: c_int) -> *const c_char;
165
166 pub fn luaL_error(L: *mut State, fmt: *const c_char, ...) -> !;
170
171 pub fn luaL_ref(L: *mut State, t: c_int) -> c_int;
173
174 pub fn luaL_unref(L: *mut State, t: c_int, r#ref: c_int);
176}
177
178pub unsafe fn lua_getglobal(L: *mut State, name: *const c_char) {
180 lua_getfield(L, LUA_GLOBALSINDEX, name)
181}
182
183pub unsafe fn lua_pop(L: *mut State, n: c_int) {
185 lua_settop(L, -n - 1)
186}
187
188pub unsafe fn lua_pushcfunction(L: *mut State, r#fn: CFunction) {
190 lua_pushcclosure(L, r#fn, 0)
191}
192
193pub unsafe fn lua_tostring(L: *mut State, index: c_int) -> *const c_char {
195 lua_tolstring(L, index, std::ptr::null_mut())
196}
197
198pub unsafe fn luaL_typename(L: *mut State, index: c_int) -> *const c_char {
200 lua_typename(L, lua_type(L, index))
201}