nvim_oxi_luajit/
ffi.rs

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    /// This marker ensures the struct is not `Send`, `Sync` and `Unpin` (the
11    /// raw pointer is neither `Send` nor `Sync`, `PhantomPinned` is not
12    /// `Unpin`).
13    _marker: PhantomData<(*mut u8, PhantomPinned)>,
14}
15
16// Pseudo-indices.
17pub 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
25// Thread status.
26pub 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
31// Type codes.
32pub 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
43// https://www.lua.org/manual/5.1/manual.html#lua_CFunction
44pub type CFunction = unsafe extern "C" fn(L: *mut State) -> c_int;
45
46// https://www.lua.org/manual/5.1/manual.html#lua_Integer
47pub type Integer = isize;
48
49// https://www.lua.org/manual/5.1/manual.html#lua_Number
50pub 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    // https://www.lua.org/manual/5.1/manual.html#lua_call
58    pub fn lua_call(L: *mut State, nargs: c_int, nresults: c_int);
59
60    /// Binding to [`lua_createtable()`] (-0, +1).
61    ///
62    /// Creates a new empty table and pushes it onto the stack. The new table
63    /// has space pre-allocated for `narr` array elements and `nrec` non-array
64    /// elements.
65    ///
66    /// This pre-allocation is useful when you know exactly how many
67    /// elements the table will have. Otherwise you can use the function
68    /// `lua_newtable`.
69    ///
70    /// [`lua_createtable`]: https://www.lua.org/manual/5.1/manual.html#lua_createtable
71    pub fn lua_createtable(L: *mut State, narr: c_int, nrec: c_int);
72
73    // https://www.lua.org/manual/5.1/manual.html#lua_error
74    pub fn lua_error(L: *mut State) -> !;
75
76    // https://www.lua.org/manual/5.1/manual.html#lua_call
77    pub fn lua_getfield(L: *mut State, index: c_int, k: *const c_char);
78
79    // https://www.lua.org/manual/5.1/manual.html#lua_getmetatable
80    pub fn lua_getmetatable(L: *mut State, index: c_int) -> c_int;
81
82    // https://www.lua.org/manual/5.1/manual.html#lua_gettop
83    pub fn lua_gettop(L: *mut State) -> c_int;
84
85    // https://www.lua.org/manual/5.1/manual.html#lua_newuserdata
86    pub fn lua_newuserdata(L: *mut State, size: usize) -> *mut c_void;
87
88    // https://www.lua.org/manual/5.1/manual.html#lua_next
89    pub fn lua_next(L: *mut State, index: c_int) -> c_int;
90
91    // https://www.lua.org/manual/5.1/manual.html#lua_objlen
92    pub fn lua_objlen(L: *mut State, index: c_int) -> usize;
93
94    // https://www.lua.org/manual/5.1/manual.html#lua_pcall
95    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    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
103    pub fn lua_pushboolean(L: *mut State, n: Integer);
104
105    // https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure
106    pub fn lua_pushcclosure(L: *mut State, r#fn: CFunction, n: c_int);
107
108    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
109    pub fn lua_pushinteger(L: *mut State, n: Integer);
110
111    // https://www.lua.org/manual/5.1/manual.html#lua_pushlightuserdata
112    pub fn lua_pushlightuserdata(L: *mut State, p: *mut c_void);
113
114    // https://www.lua.org/manual/5.1/manual.html#lua_pushlstring
115    pub fn lua_pushlstring(L: *mut State, s: *const c_char, len: usize);
116
117    // https://www.lua.org/manual/5.1/manual.html#lua_pushnil
118    pub fn lua_pushnil(L: *mut State);
119
120    // https://www.lua.org/manual/5.1/manual.html#lua_pushnumber
121    pub fn lua_pushnumber(L: *mut State, n: Number);
122
123    // https://www.lua.org/manual/5.1/manual.html#lua_pushstring
124    pub fn lua_pushstring(L: *mut State, s: *const c_char);
125
126    // https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
127    pub fn lua_pushvalue(L: *mut State, index: c_int);
128
129    // https://www.lua.org/manual/5.1/manual.html#lua_rawgeti
130    pub fn lua_rawgeti(L: *mut State, index: c_int, n: c_int);
131
132    // https://www.lua.org/manual/5.1/manual.html#lua_rawset
133    pub fn lua_rawset(L: *mut State, index: c_int);
134
135    // https://www.lua.org/manual/5.1/manual.html#lua_rawseti
136    pub fn lua_rawseti(L: *mut State, index: c_int, n: c_int);
137
138    // https://www.lua.org/manual/5.1/manual.html#lua_settop
139    pub fn lua_settop(L: *mut State, index: c_int);
140
141    // https://www.lua.org/manual/5.1/manual.html#lua_toboolean
142    pub fn lua_toboolean(L: *mut State, index: c_int) -> c_int;
143
144    // https://www.lua.org/manual/5.1/manual.html#lua_tointeger
145    pub fn lua_tointeger(L: *mut State, index: c_int) -> Integer;
146
147    // https://www.lua.org/manual/5.1/manual.html#lua_tolstring
148    pub fn lua_tolstring(
149        L: *mut State,
150        index: c_int,
151        len: *mut usize,
152    ) -> *const c_char;
153
154    // https://www.lua.org/manual/5.1/manual.html#lua_tonumber
155    pub fn lua_tonumber(L: *mut State, index: c_int) -> Number;
156
157    // https://www.lua.org/manual/5.1/manual.html#lua_touserdata
158    pub fn lua_touserdata(L: *mut State, index: c_int) -> *mut c_void;
159
160    // https://www.lua.org/manual/5.1/manual.html#lua_type
161    pub fn lua_type(L: *mut State, index: c_int) -> c_int;
162
163    // https://www.lua.org/manual/5.1/manual.html#lua_typename
164    pub fn lua_typename(L: *mut State, tp: c_int) -> *const c_char;
165
166    // Lua auxiliary library.
167
168    // https://www.lua.org/manual/5.1/manual.html#luaL_error
169    pub fn luaL_error(L: *mut State, fmt: *const c_char, ...) -> !;
170
171    // https://www.lua.org/manual/5.1/manual.html#luaL_ref
172    pub fn luaL_ref(L: *mut State, t: c_int) -> c_int;
173
174    // https://www.lua.org/manual/5.1/manual.html#luaL_unref
175    pub fn luaL_unref(L: *mut State, t: c_int, r#ref: c_int);
176}
177
178// https://www.lua.org/manual/5.1/manual.html#lua_getglobal
179pub unsafe fn lua_getglobal(L: *mut State, name: *const c_char) {
180    lua_getfield(L, LUA_GLOBALSINDEX, name)
181}
182
183// https://www.lua.org/manual/5.1/manual.html#lua_pop
184pub unsafe fn lua_pop(L: *mut State, n: c_int) {
185    lua_settop(L, -n - 1)
186}
187
188// https://www.lua.org/manual/5.1/manual.html#lua_pushcfunction
189pub unsafe fn lua_pushcfunction(L: *mut State, r#fn: CFunction) {
190    lua_pushcclosure(L, r#fn, 0)
191}
192
193// https://www.lua.org/manual/5.1/manual.html#lua_tostring
194pub unsafe fn lua_tostring(L: *mut State, index: c_int) -> *const c_char {
195    lua_tolstring(L, index, std::ptr::null_mut())
196}
197
198// https://www.lua.org/manual/5.1/manual.html#luaL_typename
199pub unsafe fn luaL_typename(L: *mut State, index: c_int) -> *const c_char {
200    lua_typename(L, lua_type(L, index))
201}