luajit_bindings/
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    // https://www.lua.org/manual/5.1/manual.html#lua_createtable
59    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
60
61    // https://www.lua.org/manual/5.1/manual.html#lua_error
62    pub fn lua_error(L: *mut lua_State) -> !;
63
64    // https://www.lua.org/manual/5.1/manual.html#lua_call
65    pub fn lua_getfield(L: *mut lua_State, index: c_int, k: *const c_char);
66
67    // https://www.lua.org/manual/5.1/manual.html#lua_getmetatable
68    pub fn lua_getmetatable(L: *mut lua_State, index: c_int) -> c_int;
69
70    // https://www.lua.org/manual/5.1/manual.html#lua_gettop
71    pub fn lua_gettop(L: *mut lua_State) -> c_int;
72
73    // https://www.lua.org/manual/5.1/manual.html#lua_newuserdata
74    pub fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void;
75
76    // https://www.lua.org/manual/5.1/manual.html#lua_next
77    pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int;
78
79    // https://www.lua.org/manual/5.1/manual.html#lua_objlen
80    pub fn lua_objlen(L: *mut lua_State, index: c_int) -> usize;
81
82    // https://www.lua.org/manual/5.1/manual.html#lua_pcall
83    pub fn lua_pcall(
84        L: *mut lua_State,
85        nargs: c_int,
86        nresults: c_int,
87        errorfunc: c_int,
88    ) -> c_int;
89
90    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
91    pub fn lua_pushboolean(L: *mut lua_State, n: lua_Integer);
92
93    // https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure
94    pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int);
95
96    // https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
97    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
98
99    // https://www.lua.org/manual/5.1/manual.html#lua_pushlightuserdata
100    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
101
102    // https://www.lua.org/manual/5.1/manual.html#lua_pushlstring
103    pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, len: usize);
104
105    // https://www.lua.org/manual/5.1/manual.html#lua_pushnil
106    pub fn lua_pushnil(L: *mut lua_State);
107
108    // https://www.lua.org/manual/5.1/manual.html#lua_pushnumber
109    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
110
111    // https://www.lua.org/manual/5.1/manual.html#lua_pushstring
112    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
113
114    // https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
115    pub fn lua_pushvalue(L: *mut lua_State, index: c_int);
116
117    // https://www.lua.org/manual/5.1/manual.html#lua_rawgeti
118    pub fn lua_rawgeti(L: *mut lua_State, index: c_int, n: c_int);
119
120    // https://www.lua.org/manual/5.1/manual.html#lua_rawset
121    pub fn lua_rawset(L: *mut lua_State, index: c_int);
122
123    // https://www.lua.org/manual/5.1/manual.html#lua_rawseti
124    pub fn lua_rawseti(L: *mut lua_State, index: c_int, n: c_int);
125
126    // https://www.lua.org/manual/5.1/manual.html#lua_settop
127    pub fn lua_settop(L: *mut lua_State, index: c_int);
128
129    // https://www.lua.org/manual/5.1/manual.html#lua_toboolean
130    pub fn lua_toboolean(L: *mut lua_State, index: c_int) -> c_int;
131
132    // https://www.lua.org/manual/5.1/manual.html#lua_tointeger
133    pub fn lua_tointeger(L: *mut lua_State, index: c_int) -> lua_Integer;
134
135    // https://www.lua.org/manual/5.1/manual.html#lua_tolstring
136    pub fn lua_tolstring(
137        L: *mut lua_State,
138        index: c_int,
139        len: *mut usize,
140    ) -> *const c_char;
141
142    // https://www.lua.org/manual/5.1/manual.html#lua_tonumber
143    pub fn lua_tonumber(L: *mut lua_State, index: c_int) -> lua_Number;
144
145    // https://www.lua.org/manual/5.1/manual.html#lua_touserdata
146    pub fn lua_touserdata(L: *mut lua_State, index: c_int) -> *mut c_void;
147
148    // https://www.lua.org/manual/5.1/manual.html#lua_type
149    pub fn lua_type(L: *mut lua_State, index: c_int) -> c_int;
150
151    // https://www.lua.org/manual/5.1/manual.html#lua_typename
152    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
153
154    // Lua auxiliary library.
155
156    // https://www.lua.org/manual/5.1/manual.html#luaL_error
157    pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
158
159    // https://www.lua.org/manual/5.1/manual.html#luaL_ref
160    pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int;
161
162    // https://www.lua.org/manual/5.1/manual.html#luaL_unref
163    pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int);
164}
165
166// https://www.lua.org/manual/5.1/manual.html#lua_getglobal
167pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) {
168    lua_getfield(L, LUA_GLOBALSINDEX, name)
169}
170
171// https://www.lua.org/manual/5.1/manual.html#lua_pop
172pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
173    lua_settop(L, -n - 1)
174}
175
176// https://www.lua.org/manual/5.1/manual.html#lua_pushcfunction
177pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction) {
178    lua_pushcclosure(L, r#fn, 0)
179}
180
181// https://www.lua.org/manual/5.1/manual.html#lua_tostring
182pub unsafe fn lua_tostring(L: *mut lua_State, index: c_int) -> *const c_char {
183    lua_tolstring(L, index, std::ptr::null_mut())
184}
185
186// https://www.lua.org/manual/5.1/manual.html#luaL_typename
187pub unsafe fn luaL_typename(L: *mut lua_State, index: c_int) -> *const c_char {
188    lua_typename(L, lua_type(L, index))
189}