px8_plugin_lua/ffi/
lua.rs

1// The MIT License (MIT)
2//
3// Copyright (c) 2014 J.C. Moyer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23//! Contains definitions from `lua.h`.
24
25use libc::{c_void, c_int, c_char, c_uchar, size_t};
26use ffi::luaconf;
27use std::ptr;
28
29pub use super::glue::{LUA_VERSION_MAJOR, LUA_VERSION_MINOR, LUA_VERSION_NUM, LUA_VERSION_RELEASE};
30pub use super::glue::{LUA_VERSION, LUA_RELEASE, LUA_COPYRIGHT, LUA_AUTHORS};
31pub use super::glue::{LUA_REGISTRYINDEX};
32
33pub const LUA_SIGNATURE: &'static [u8] = b"\x1bLua";
34
35// option for multiple returns in 'lua_pcall' and 'lua_call'
36pub const LUA_MULTRET: c_int = -1;
37
38#[inline(always)]
39pub fn lua_upvalueindex(i: c_int) -> c_int {
40  LUA_REGISTRYINDEX - i
41}
42
43// thread status
44pub const LUA_OK: c_int = 0;
45pub const LUA_YIELD: c_int = 1;
46pub const LUA_ERRRUN: c_int = 2;
47pub const LUA_ERRSYNTAX: c_int = 3;
48pub const LUA_ERRMEM: c_int = 4;
49pub const LUA_ERRGCMM: c_int = 5;
50pub const LUA_ERRERR: c_int = 6;
51
52pub type lua_State = c_void;
53
54// basic types
55pub const LUA_TNONE: c_int = -1;
56
57pub const LUA_TNIL: c_int = 0;
58pub const LUA_TBOOLEAN: c_int = 1;
59pub const LUA_TLIGHTUSERDATA: c_int = 2;
60pub const LUA_TNUMBER: c_int = 3;
61pub const LUA_TSTRING: c_int = 4;
62pub const LUA_TTABLE: c_int = 5;
63pub const LUA_TFUNCTION: c_int = 6;
64pub const LUA_TUSERDATA: c_int = 7;
65pub const LUA_TTHREAD: c_int = 8;
66
67pub const LUA_NUMTAGS: c_int = 9;
68
69// minimum stack available to a C function
70pub const LUA_MINSTACK: c_int = 20;
71
72// predefined values in the registry
73pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1;
74pub const LUA_RIDX_GLOBALS: lua_Integer = 2;
75pub const LUA_RIDX_LAST: lua_Integer = LUA_RIDX_GLOBALS;
76
77/// A Lua number, usually equivalent to `f64`.
78pub type lua_Number = luaconf::LUA_NUMBER;
79
80/// A Lua integer, usually equivalent to `i64`.
81pub type lua_Integer = luaconf::LUA_INTEGER;
82
83// unsigned integer type
84pub type lua_Unsigned = luaconf::LUA_UNSIGNED;
85
86// type for continuation-function contexts
87pub type lua_KContext = luaconf::LUA_KCONTEXT;
88
89/// Type for native functions that can be passed to Lua.
90pub type lua_CFunction = Option<unsafe extern "C" fn(L: *mut lua_State) -> c_int>;
91
92// Type for continuation functions
93pub type lua_KFunction = Option<unsafe extern "C" fn(L: *mut lua_State, status: c_int, ctx: lua_KContext) -> c_int>;
94
95// Type for functions that read/write blocks when loading/dumping Lua chunks
96pub type lua_Reader = Option<unsafe extern "C" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut size_t) -> *const c_char>;
97pub type lua_Writer = Option<unsafe extern "C" fn(L: *mut lua_State, p: *const c_void, sz: size_t, ud: *mut c_void) -> c_int>;
98
99/// Type for memory-allocation functions.
100pub type lua_Alloc = Option<unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: size_t, nsize: size_t) -> *mut c_void>;
101
102extern {
103  // state manipulation
104  pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
105  pub fn lua_close(L: *mut lua_State);
106  pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
107
108  pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
109
110  pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
111
112  // basic stack manipulation
113  pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
114  pub fn lua_gettop(L: *mut lua_State) -> c_int;
115  pub fn lua_settop(L: *mut lua_State, idx: c_int);
116  pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
117  pub fn lua_rotate(L: *mut lua_State, idx: c_int, n: c_int);
118  pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
119  pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
120
121  pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
122
123  // access functions (stack -> C)
124  pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
125  pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
126  pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
127  pub fn lua_isinteger(L: *mut lua_State, idx: c_int) -> c_int;
128  pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
129  pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
130  pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
131
132  pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
133  pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
134  pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
135  pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut size_t) -> *const c_char;
136  pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> size_t;
137  pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> lua_CFunction;
138  pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
139  pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
140  pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
141}
142
143// Comparison and arithmetic functions
144pub const LUA_OPADD: c_int = 0;
145pub const LUA_OPSUB: c_int = 1;
146pub const LUA_OPMUL: c_int = 2;
147pub const LUA_OPMOD: c_int = 3;
148pub const LUA_OPPOW: c_int = 4;
149pub const LUA_OPDIV: c_int = 5;
150pub const LUA_OPIDIV: c_int = 6;
151pub const LUA_OPBAND: c_int = 7;
152pub const LUA_OPBOR: c_int = 8;
153pub const LUA_OPBXOR: c_int = 9;
154pub const LUA_OPSHL: c_int = 10;
155pub const LUA_OPSHR: c_int = 11;
156pub const LUA_OPUNM: c_int = 12;
157pub const LUA_OPBNOT: c_int = 13;
158
159extern {
160  pub fn lua_arith(L: *mut lua_State, op: c_int);
161}
162
163pub const LUA_OPEQ: c_int = 0;
164pub const LUA_OPLT: c_int = 1;
165pub const LUA_OPLE: c_int = 2;
166
167extern {
168  pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
169  pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
170}
171
172// push functions (C -> stack)
173extern {
174  pub fn lua_pushnil(L: *mut lua_State);
175  pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
176  pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
177  pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: size_t) -> *const c_char;
178  pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
179  // TODO: omitted:
180  // lua_pushvfstring
181  pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
182  pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
183  pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
184  pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
185  pub fn lua_pushthread(L: *mut lua_State) -> c_int;
186}
187
188// get functions (Lua -> stack)
189extern {
190  pub fn lua_getglobal(L: *mut lua_State, var: *const c_char) -> c_int;
191  pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
192  pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
193  pub fn lua_geti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
194  pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
195  pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
196  pub fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_int;
197
198  pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
199  pub fn lua_newuserdata(L: *mut lua_State, sz: size_t) -> *mut c_void;
200  pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
201  pub fn lua_getuservalue(L: *mut lua_State, idx: c_int) -> c_int;
202}
203
204// set functions (stack -> Lua)
205extern {
206  pub fn lua_setglobal(L: *mut lua_State, var: *const c_char);
207  pub fn lua_settable(L: *mut lua_State, idx: c_int);
208  pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
209  pub fn lua_seti(L: *mut lua_State, idx: c_int, n: lua_Integer);
210  pub fn lua_rawset(L: *mut lua_State, idx: c_int);
211  pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer);
212  pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
213  pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
214  pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
215}
216
217// 'load' and 'call' functions (load and run Lua code)
218extern {
219  pub fn lua_callk(L: *mut lua_State, nargs: c_int, nresults: c_int, ctx: lua_KContext, k: lua_KFunction);
220  pub fn lua_pcallk(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int, ctx: lua_KContext, k: lua_KFunction) -> c_int;
221  pub fn lua_load(L: *mut lua_State, reader: lua_Reader, dt: *mut c_void, chunkname: *const c_char, mode: *const c_char) -> c_int;
222  pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut c_void, strip: c_int) -> c_int;
223}
224
225#[inline(always)]
226pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
227  lua_callk(L, n, r, 0, None)
228}
229
230#[inline(always)]
231pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
232  lua_pcallk(L, n, r, f, 0, None)
233}
234
235// coroutine functions
236extern {
237  pub fn lua_yieldk(L: *mut lua_State, nresults: c_int, ctx: lua_KContext, k: lua_KFunction) -> c_int;
238  pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
239  pub fn lua_status(L: *mut lua_State) -> c_int;
240  pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
241}
242
243#[inline(always)]
244pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
245  lua_yieldk(L, n, 0, None)
246}
247
248// garbage-collection function and options
249pub const LUA_GCSTOP: c_int = 0;
250pub const LUA_GCRESTART: c_int = 1;
251pub const LUA_GCCOLLECT: c_int = 2;
252pub const LUA_GCCOUNT: c_int = 3;
253pub const LUA_GCCOUNTB: c_int = 4;
254pub const LUA_GCSTEP: c_int = 5;
255pub const LUA_GCSETPAUSE: c_int = 6;
256pub const LUA_GCSETSTEPMUL: c_int = 7;
257pub const LUA_GCISRUNNING: c_int = 9;
258
259extern {
260  pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
261}
262
263// miscellaneous functions
264extern {
265  pub fn lua_error(L: *mut lua_State) -> c_int;
266  pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
267  pub fn lua_concat(L: *mut lua_State, n: c_int);
268  pub fn lua_len(L: *mut lua_State, idx: c_int);
269  pub fn lua_stringtonumber(L: *mut lua_State, s: *const c_char) -> size_t;
270  pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
271  pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
272}
273
274// some useful macros
275// here, implemented as Rust functions
276#[inline(always)]
277pub unsafe fn lua_getextraspace(L: *mut lua_State) -> *mut c_void {
278  L.offset(-super::glue::LUA_EXTRASPACE as isize) as *mut c_void
279}
280
281#[inline(always)]
282pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
283  lua_tonumberx(L, i, ptr::null_mut())
284}
285
286#[inline(always)]
287pub unsafe fn lua_tointeger(L: *mut lua_State, i: c_int) -> lua_Integer {
288  lua_tointegerx(L, i, ptr::null_mut())
289}
290
291#[inline(always)]
292pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
293  lua_settop(L, -n - 1)
294}
295
296#[inline(always)]
297pub unsafe fn lua_newtable(L: *mut lua_State) {
298  lua_createtable(L, 0, 0)
299}
300
301#[inline(always)]
302pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
303  lua_pushcfunction(L, f);
304  lua_setglobal(L, n)
305}
306
307#[inline(always)]
308pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
309  lua_pushcclosure(L, f, 0)
310}
311
312#[inline(always)]
313pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
314  (lua_type(L, n) == LUA_TFUNCTION) as c_int
315}
316
317#[inline(always)]
318pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
319  (lua_type(L, n) == LUA_TTABLE) as c_int
320}
321
322#[inline(always)]
323pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
324  (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
325}
326
327#[inline(always)]
328pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
329  (lua_type(L, n) == LUA_TNIL) as c_int
330}
331
332#[inline(always)]
333pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
334  (lua_type(L, n) == LUA_TBOOLEAN) as c_int
335}
336
337#[inline(always)]
338pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
339  (lua_type(L, n) == LUA_TTHREAD) as c_int
340}
341
342#[inline(always)]
343pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
344  (lua_type(L, n) == LUA_TNONE) as c_int
345}
346
347#[inline(always)]
348pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
349  (lua_type(L, n) <= 0) as c_int
350}
351
352// TODO: Test
353#[inline(always)]
354pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
355  use std::ffi::CString;
356  let c_str = CString::new(s).unwrap();
357  lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len() as size_t)
358}
359
360#[inline(always)]
361pub unsafe fn lua_pushglobaltable(L: *mut lua_State) -> c_int {
362  lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
363}
364
365#[inline(always)]
366pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
367  lua_tolstring(L, i, ptr::null_mut())
368}
369
370#[inline(always)]
371pub unsafe fn lua_insert(L: *mut lua_State, idx: c_int) {
372  lua_rotate(L, idx, 1)
373}
374
375#[inline(always)]
376pub unsafe fn lua_remove(L: *mut lua_State, idx: c_int) {
377  lua_rotate(L, idx, -1);
378  lua_pop(L, 1)
379}
380
381#[inline(always)]
382pub unsafe fn lua_replace(L: *mut lua_State, idx: c_int) {
383  lua_copy(L, -1, idx);
384  lua_pop(L, 1)
385}
386
387// Debug API
388// Event codes
389pub const LUA_HOOKCALL: c_int = 0;
390pub const LUA_HOOKRET: c_int = 1;
391pub const LUA_HOOKLINE: c_int = 2;
392pub const LUA_HOOKCOUNT: c_int = 3;
393pub const LUA_HOOKTAILCALL: c_int = 4;
394
395// Event masks
396pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
397pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
398pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
399pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
400
401/// Type for functions to be called on debug events.
402pub type lua_Hook = Option<extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug)>;
403
404extern {
405  pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
406  pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
407  pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
408  pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
409  pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
410  pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
411
412  pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
413  pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
414
415  pub fn lua_sethook(L: *mut lua_State, func: lua_Hook, mask: c_int, count: c_int);
416  pub fn lua_gethook(L: *mut lua_State) -> lua_Hook;
417  pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
418  pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
419}
420
421#[repr(C)]
422pub struct lua_Debug {
423  pub event: c_int,
424  pub name: *const c_char,
425  pub namewhat: *const c_char,
426  pub what: *const c_char,
427  pub source: *const c_char,
428  pub currentline: c_int,
429  pub linedefined: c_int,
430  pub lastlinedefined: c_int,
431  pub nups: c_uchar,
432  pub nparams: c_uchar,
433  pub isvararg: c_char,
434  pub istailcall: c_char,
435  pub short_src: [c_char; luaconf::LUA_IDSIZE as usize],
436  // lua.h mentions this is for private use
437  i_ci: *mut c_void,
438}
439