oxi_luajit/
ffi.rs

1#![allow(non_snake_case)]
2#![allow(non_camel_case_types)]
3
4use std::ffi::{c_char, c_double, c_int, c_void};
5use std::marker::{PhantomData, PhantomPinned};
6
7#[repr(C)]
8#[doc(hidden)]
9pub struct lua_State {
10    _data: [u8; 0],
11
12    /// This marker ensures the struct is not `Send`, `Sync` and `Unpin` (the
13    /// raw pointer is neither `Send` nor `Sync`, `PhantomPinned` is not
14    /// `Unpin`).
15    _marker: PhantomData<(*mut u8, PhantomPinned)>,
16}
17
18// Pseudo-indices.
19pub const LUA_REGISTRYINDEX: c_int = -10000;
20pub const LUA_ENVIRONINDEX: c_int = -10001;
21pub const LUA_GLOBALSINDEX: c_int = -10002;
22
23pub const fn lua_upvalueindex(i: c_int) -> c_int {
24    LUA_GLOBALSINDEX - i
25}
26
27// Thread status.
28pub const LUA_OK: c_int = 0;
29pub const LUA_ERRRUN: c_int = 2;
30pub const LUA_ERRMEM: c_int = 4;
31pub const LUA_ERRERR: c_int = 5;
32
33// Type codes.
34pub const LUA_TNONE: c_int = -1;
35pub const LUA_TNIL: c_int = 0;
36pub const LUA_TBOOLEAN: c_int = 1;
37pub const LUA_TLIGHTUSERDATA: c_int = 2;
38pub const LUA_TNUMBER: c_int = 3;
39pub const LUA_TSTRING: c_int = 4;
40pub const LUA_TTABLE: c_int = 5;
41pub const LUA_TFUNCTION: c_int = 6;
42pub const LUA_TUSERDATA: c_int = 7;
43pub const LUA_TTHREAD: c_int = 8;
44
45// https://www.lua.org/manual/5.1/manual.html#lua_CFunction
46pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
47
48// https://www.lua.org/manual/5.1/manual.html#lua_Integer
49pub type lua_Integer = isize;
50
51// https://www.lua.org/manual/5.1/manual.html#lua_Number
52pub type lua_Number = c_double;
53
54extern "C" {
55    // https://www.lua.org/manual/5.1/manual.html#lua_call
56    pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
57
58    /// Binding to [`lua_createtable()`] (-0, +1).
59    ///
60    /// Creates a new empty table and pushes it onto the stack. The new table
61    /// has space pre-allocated for `narr` array elements and `nrec` non-array
62    /// elements.
63    ///
64    /// This pre-allocation is useful when you know exactly how many
65    /// elements the table will have. Otherwise you can use the function
66    /// `lua_newtable`.
67    ///
68    /// [`lua_createtable`]: https://www.lua.org/manual/5.1/manual.html#lua_createtable
69    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
70
71    // https://www.lua.org/manual/5.1/manual.html#lua_error
72    pub fn lua_error(L: *mut lua_State) -> !;
73
74    // https://www.lua.org/manual/5.1/manual.html#lua_call
75    pub fn lua_getfield(L: *mut lua_State, index: c_int, k: *const c_char);
76
77    // https://www.lua.org/manual/5.1/manual.html#lua_getmetatable
78    pub fn lua_getmetatable(L: *mut lua_State, index: c_int) -> c_int;
79
80    // https://www.lua.org/manual/5.1/manual.html#lua_gettop
81    pub fn lua_gettop(L: *mut lua_State) -> c_int;
82
83    // https://www.lua.org/manual/5.1/manual.html#lua_newuserdata
84    pub fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void;
85
86    // https://www.lua.org/manual/5.1/manual.html#lua_next
87    pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int;
88
89    // https://www.lua.org/manual/5.1/manual.html#lua_objlen
90    pub fn lua_objlen(L: *mut lua_State, index: c_int) -> usize;
91
92    // https://www.lua.org/manual/5.1/manual.html#lua_pcall
93    pub fn lua_pcall(
94        L: *mut lua_State,
95        nargs: c_int,
96        nresults: c_int,
97        errorfunc: c_int,
98    ) -> c_int;
99
100    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
101    pub fn lua_pushboolean(L: *mut lua_State, n: lua_Integer);
102
103    // https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure
104    pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int);
105
106    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
107    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
108
109    // https://www.lua.org/manual/5.1/manual.html#lua_pushlightuserdata
110    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
111
112    // https://www.lua.org/manual/5.1/manual.html#lua_pushlstring
113    pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, len: usize);
114
115    // https://www.lua.org/manual/5.1/manual.html#lua_pushnil
116    pub fn lua_pushnil(L: *mut lua_State);
117
118    // https://www.lua.org/manual/5.1/manual.html#lua_pushnumber
119    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
120
121    // https://www.lua.org/manual/5.1/manual.html#lua_pushstring
122    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
123
124    // https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
125    pub fn lua_pushvalue(L: *mut lua_State, index: c_int);
126
127    // https://www.lua.org/manual/5.1/manual.html#lua_rawgeti
128    pub fn lua_rawgeti(L: *mut lua_State, index: c_int, n: c_int);
129
130    // https://www.lua.org/manual/5.1/manual.html#lua_rawset
131    pub fn lua_rawset(L: *mut lua_State, index: c_int);
132
133    // https://www.lua.org/manual/5.1/manual.html#lua_rawseti
134    pub fn lua_rawseti(L: *mut lua_State, index: c_int, n: c_int);
135
136    // https://www.lua.org/manual/5.1/manual.html#lua_settop
137    pub fn lua_settop(L: *mut lua_State, index: c_int);
138
139    // https://www.lua.org/manual/5.1/manual.html#lua_toboolean
140    pub fn lua_toboolean(L: *mut lua_State, index: c_int) -> c_int;
141
142    // https://www.lua.org/manual/5.1/manual.html#lua_tointeger
143    pub fn lua_tointeger(L: *mut lua_State, index: c_int) -> lua_Integer;
144
145    // https://www.lua.org/manual/5.1/manual.html#lua_tolstring
146    pub fn lua_tolstring(
147        L: *mut lua_State,
148        index: c_int,
149        len: *mut usize,
150    ) -> *const c_char;
151
152    // https://www.lua.org/manual/5.1/manual.html#lua_tonumber
153    pub fn lua_tonumber(L: *mut lua_State, index: c_int) -> lua_Number;
154
155    // https://www.lua.org/manual/5.1/manual.html#lua_touserdata
156    pub fn lua_touserdata(L: *mut lua_State, index: c_int) -> *mut c_void;
157
158    // https://www.lua.org/manual/5.1/manual.html#lua_type
159    pub fn lua_type(L: *mut lua_State, index: c_int) -> c_int;
160
161    // https://www.lua.org/manual/5.1/manual.html#lua_typename
162    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
163
164    // Lua auxiliary library.
165
166    // https://www.lua.org/manual/5.1/manual.html#luaL_error
167    pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
168
169    // https://www.lua.org/manual/5.1/manual.html#luaL_ref
170    pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int;
171
172    // https://www.lua.org/manual/5.1/manual.html#luaL_unref
173    pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int);
174}
175
176// https://www.lua.org/manual/5.1/manual.html#lua_getglobal
177pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) {
178    lua_getfield(L, LUA_GLOBALSINDEX, name)
179}
180
181// https://www.lua.org/manual/5.1/manual.html#lua_pop
182pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
183    lua_settop(L, -n - 1)
184}
185
186// https://www.lua.org/manual/5.1/manual.html#lua_pushcfunction
187pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction) {
188    lua_pushcclosure(L, r#fn, 0)
189}
190
191// https://www.lua.org/manual/5.1/manual.html#lua_tostring
192pub unsafe fn lua_tostring(L: *mut lua_State, index: c_int) -> *const c_char {
193    lua_tolstring(L, index, std::ptr::null_mut())
194}
195
196// https://www.lua.org/manual/5.1/manual.html#luaL_typename
197pub unsafe fn luaL_typename(L: *mut lua_State, index: c_int) -> *const c_char {
198    lua_typename(L, lua_type(L, index))
199}