lua52_sys/
lib.rs

1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(dead_code)]
4#![allow(improper_ctypes)]
5
6extern crate libc;
7
8use libc::c_int;
9use std::{default, ptr};
10
11pub const MULTRET: c_int = -1;
12
13pub const LUAI_MAXSTACK: c_int = 1000000; // or 15000 with 32b        // TODO:
14pub const LUAI_FIRSTPSEUDOIDX: c_int = (-LUAI_MAXSTACK - 1000);
15pub const LUA_REGISTRYINDEX: c_int = LUAI_FIRSTPSEUDOIDX;
16
17pub const LUA_OK: c_int = 0;
18pub const LUA_YIELD: c_int = 1;
19pub const LUA_ERRRUN: c_int = 2;
20pub const LUA_ERRSYNTAX: c_int = 3;
21pub const LUA_ERRMEM: c_int = 4;
22pub const LUA_ERRGCMM: c_int = 5;
23pub const LUA_ERRERR: c_int = 6;
24
25#[repr(C)]
26#[allow(missing_copy_implementations)]
27pub struct lua_State;
28
29pub type lua_CFunction = extern "C" fn(L: *mut lua_State) -> c_int;
30
31pub type lua_Reader = extern "C" fn(
32    L: *mut lua_State,
33    ud: *mut libc::c_void,
34    sz: *mut libc::size_t,
35) -> *const libc::c_char;
36pub type lua_Writer = extern "C" fn(
37    L: *mut lua_State,
38    p: *const libc::c_void,
39    sz: libc::size_t,
40    ud: *mut libc::c_void,
41) -> libc::c_int;
42
43pub type lua_Alloc = extern "C" fn(
44    ud: *mut libc::c_void,
45    ptr: *mut libc::c_void,
46    osize: libc::size_t,
47    nsize: libc::size_t,
48) -> *mut libc::c_void;
49
50pub type lua_Hook = extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
51
52pub const LUA_TNONE: c_int = -1;
53
54pub const LUA_TNIL: c_int = 0;
55pub const LUA_TBOOLEAN: c_int = 1;
56pub const LUA_TLIGHTUSERDATA: c_int = 2;
57pub const LUA_TNUMBER: c_int = 3;
58pub const LUA_TSTRING: c_int = 4;
59pub const LUA_TTABLE: c_int = 5;
60pub const LUA_TFUNCTION: c_int = 6;
61pub const LUA_TUSERDATA: c_int = 7;
62pub const LUA_TTHREAD: c_int = 8;
63
64pub const LUA_MINSTACK: c_int = 20;
65
66pub const LUA_RIDX_MAINTHREAD: c_int = 1;
67pub const LUA_RIDX_GLOBALS: c_int = 2;
68
69pub const LUA_REFNIL: c_int = -1;
70pub const LUA_NOREF: c_int = -2;
71
72pub type lua_Number = libc::c_double;
73pub type lua_Integer = libc::ptrdiff_t;
74pub type lua_Unsigned = libc::c_ulong;
75
76pub const LUA_OPADD: c_int = 0;
77pub const LUA_OPSUB: c_int = 1;
78pub const LUA_OPMUL: c_int = 2;
79pub const LUA_OPDIV: c_int = 3;
80pub const LUA_OPMOD: c_int = 4;
81pub const LUA_OPPOW: c_int = 5;
82pub const LUA_OPUNM: c_int = 6;
83
84pub const LUA_OPEQ: c_int = 0;
85pub const LUA_OPLT: c_int = 1;
86pub const LUA_OPLE: c_int = 2;
87
88pub const LUA_GCSTOP: c_int = 0;
89pub const LUA_GCRESTART: c_int = 1;
90pub const LUA_GCCOLLECT: c_int = 2;
91pub const LUA_GCCOUNT: c_int = 3;
92pub const LUA_GCCOUNTB: c_int = 4;
93pub const LUA_GCSTEP: c_int = 5;
94pub const LUA_GCSETPAUSE: c_int = 6;
95pub const LUA_GCSETSTEPMUL: c_int = 7;
96pub const LUA_GCSETMAJORINC: c_int = 8;
97pub const LUA_GCISRUNNING: c_int = 9;
98pub const LUA_GCGEN: c_int = 10;
99pub const LUA_GCINC: c_int = 11;
100
101pub const LUA_HOOKCALL: c_int = 0;
102pub const LUA_HOOKRET: c_int = 1;
103pub const LUA_HOOKLINE: c_int = 2;
104pub const LUA_HOOKCOUNT: c_int = 3;
105pub const LUA_HOOKTAILRET: c_int = 4;
106
107pub const LUA_MASKCALL: c_int = 1 << LUA_HOOKCALL as usize;
108pub const LUA_MASKRET: c_int = 1 << LUA_HOOKRET as usize;
109pub const LUA_MASKLINE: c_int = 1 << LUA_HOOKLINE as usize;
110pub const LUA_MASKCOUNT: c_int = 1 << LUA_HOOKCOUNT as usize;
111
112#[repr(C)]
113#[allow(missing_copy_implementations)]
114pub struct lua_Debug {
115    pub event: c_int,
116    pub name: *const libc::c_char,
117    pub namewhat: *const libc::c_char,
118    pub what: *const libc::c_char,
119    pub source: *const libc::c_char,
120    pub currentline: c_int,
121    pub linedefined: c_int,
122    pub lastlinedefined: c_int,
123    pub nups: libc::c_uchar,
124    pub nparams: libc::c_uchar,
125    pub isvararg: libc::c_char,
126    pub istailcall: libc::c_char,
127    pub short_src: [libc::c_char; 60],
128    //i_ci: *CallInfo
129}
130
131extern "C" {
132    pub fn lua_newstate(f: lua_Alloc, ud: *mut libc::c_void) -> *mut lua_State;
133    pub fn lua_close(L: *mut lua_State);
134    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
135
136    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
137
138    pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
139
140    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
141    pub fn lua_gettop(L: *mut lua_State) -> c_int;
142    pub fn lua_settop(L: *mut lua_State, idx: c_int);
143    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
144    pub fn lua_remove(L: *mut lua_State, idx: c_int);
145    pub fn lua_insert(L: *mut lua_State, idx: c_int);
146    pub fn lua_replace(L: *mut lua_State, idx: c_int);
147    pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
148    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
149
150    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
151
152    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
153    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
154    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
155    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
156    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
157    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const libc::c_char;
158
159    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
160    pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
161    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
162    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
163    pub fn lua_tolstring(
164        L: *mut lua_State,
165        idx: c_int,
166        len: *mut libc::size_t,
167    ) -> *const libc::c_char;
168    pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> libc::size_t;
169    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
170    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut libc::c_void;
171    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
172    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const libc::c_void;
173
174    pub fn lua_arith(L: *mut lua_State, op: c_int);
175    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
176    pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
177
178    pub fn lua_pushnil(L: *mut lua_State);
179    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
180    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
181    pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
182    pub fn lua_pushlstring(L: *mut lua_State, s: *const libc::c_char, l: libc::size_t);
183    pub fn lua_pushstring(L: *mut lua_State, s: *const libc::c_char);
184    // TODO: lua_pushvfstring()
185    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const libc::c_char, ...)
186        -> *const libc::c_char;
187    pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
188    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
189    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut libc::c_void);
190    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
191
192    pub fn lua_getglobal(L: *mut lua_State, var: *const libc::c_char);
193    pub fn lua_gettable(L: *mut lua_State, idx: c_int);
194    pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const libc::c_char);
195    pub fn lua_rawget(L: *mut lua_State, idx: c_int);
196    pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: c_int);
197    pub fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const libc::c_char);
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: libc::size_t) -> *mut libc::c_void;
200    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
201    pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
202
203    pub fn lua_setglobal(L: *mut lua_State, var: *const libc::c_char);
204    pub fn lua_settable(L: *mut lua_State, idx: c_int);
205    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const libc::c_char);
206    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
207    pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: c_int);
208    pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const libc::c_char);
209    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
210    pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
211
212    pub fn lua_callk(
213        L: *mut lua_State,
214        nargs: c_int,
215        nresults: c_int,
216        ctx: c_int,
217        k: Option<lua_CFunction>,
218    );
219    pub fn lua_getctx(L: *mut lua_State, ctx: c_int) -> c_int;
220    pub fn lua_pcallk(
221        L: *mut lua_State,
222        nargs: c_int,
223        nresults: c_int,
224        errfunc: c_int,
225        ctx: c_int,
226        k: Option<lua_CFunction>,
227    ) -> c_int;
228    pub fn lua_load(
229        L: *mut lua_State,
230        reader: lua_Reader,
231        dt: *mut libc::c_void,
232        chunkname: *const libc::c_char,
233        mode: *const libc::c_char,
234    ) -> c_int;
235    pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut libc::c_void) -> c_int;
236
237    pub fn lua_yieldk(
238        L: *mut lua_State,
239        nresults: c_int,
240        ctx: c_int,
241        k: Option<lua_CFunction>,
242    ) -> c_int;
243    pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
244    pub fn lua_status(L: *mut lua_State) -> c_int;
245
246    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
247
248    pub fn lua_error(L: *mut lua_State) -> c_int;
249    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
250    pub fn lua_concat(L: *mut lua_State, n: c_int);
251    pub fn lua_len(L: *mut lua_State, idx: c_int);
252
253    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut libc::c_void) -> lua_Alloc;
254    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut libc::c_void);
255
256    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
257    pub fn lua_getinfo(L: *mut lua_State, what: *const libc::c_char, ar: *mut lua_Debug) -> c_int;
258    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const libc::c_char;
259    pub fn lua_setlocal(L: *mut lua_State, ar: *mut lua_Debug, n: c_int) -> *const libc::c_char;
260    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const libc::c_char;
261    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const libc::c_char;
262
263    pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *const libc::c_void;
264    pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
265
266    pub fn lua_sethook(L: *mut lua_State, func: lua_Hook, mask: c_int, count: c_int) -> c_int;
267    pub fn lua_gethook(L: *mut lua_State) -> lua_Hook;
268    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
269    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
270
271    pub fn luaL_openlibs(L: *mut lua_State);
272    pub fn luaL_ref(L: *mut lua_State, idx: c_int) -> c_int;
273    pub fn luaL_unref(L: *mut lua_State, idx: c_int, ref_id: c_int);
274
275    pub fn luaopen_base(L: *mut lua_State) -> c_int;
276    pub fn luaopen_bit32(L: *mut lua_State) -> c_int;
277    pub fn luaopen_coroutine(L: *mut lua_State) -> c_int;
278    pub fn luaopen_debug(L: *mut lua_State) -> c_int;
279    pub fn luaopen_io(L: *mut lua_State) -> c_int;
280    pub fn luaopen_math(L: *mut lua_State) -> c_int;
281    pub fn luaopen_os(L: *mut lua_State) -> c_int;
282    pub fn luaopen_package(L: *mut lua_State) -> c_int;
283    pub fn luaopen_string(L: *mut lua_State) -> c_int;
284    pub fn luaopen_table(L: *mut lua_State) -> c_int;
285}
286
287#[inline(always)]
288pub fn lua_upvalueindex(i: c_int) -> c_int {
289    LUA_REGISTRYINDEX - i
290}
291
292#[inline(always)]
293pub unsafe fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int) {
294    lua_callk(L, nargs, nresults, 0, None)
295}
296
297#[inline(always)]
298pub unsafe fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int {
299    lua_pcallk(L, nargs, nresults, errfunc, 0, None)
300}
301
302#[inline(always)]
303pub unsafe fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int {
304    lua_yieldk(L, nresults, 0, None)
305}
306
307#[inline(always)]
308pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
309    lua_settop(L, -n - 1)
310}
311
312#[inline(always)]
313pub unsafe fn lua_newtable(L: *mut lua_State) {
314    lua_createtable(L, 0, 0)
315}
316
317#[inline(always)]
318pub unsafe fn lua_register(L: *mut lua_State, name: *const libc::c_char, f: lua_CFunction) {
319    lua_pushcfunction(L, f);
320    lua_setglobal(L, name)
321}
322
323#[inline(always)]
324pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
325    lua_pushcclosure(L, f, 0)
326}
327
328#[inline(always)]
329pub unsafe fn lua_isfunction(L: *mut lua_State, idx: c_int) -> bool {
330    lua_type(L, idx) == LUA_TFUNCTION
331}
332
333#[inline(always)]
334pub unsafe fn lua_istable(L: *mut lua_State, idx: c_int) -> bool {
335    lua_type(L, idx) == LUA_TTABLE
336}
337
338#[inline(always)]
339pub unsafe fn lua_islightuserdata(L: *mut lua_State, idx: c_int) -> bool {
340    lua_type(L, idx) == LUA_TLIGHTUSERDATA
341}
342
343#[inline(always)]
344pub unsafe fn lua_isnil(L: *mut lua_State, idx: c_int) -> bool {
345    lua_type(L, idx) == LUA_TNIL
346}
347
348#[inline(always)]
349pub unsafe fn lua_isboolean(L: *mut lua_State, idx: c_int) -> bool {
350    lua_type(L, idx) == LUA_TBOOLEAN
351}
352
353#[inline(always)]
354pub unsafe fn lua_isthread(L: *mut lua_State, idx: c_int) -> bool {
355    lua_type(L, idx) == LUA_TTHREAD
356}
357
358#[inline(always)]
359pub unsafe fn lua_isnone(L: *mut lua_State, idx: c_int) -> bool {
360    lua_type(L, idx) == LUA_TNONE
361}
362
363#[inline(always)]
364pub unsafe fn lua_isnoneornil(L: *mut lua_State, idx: c_int) -> bool {
365    lua_type(L, idx) <= 0
366}
367
368// TODO: lua_pushliteral
369
370#[inline(always)]
371pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
372    lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
373}
374
375#[inline(always)]
376pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const libc::c_char {
377    lua_tolstring(L, i, ptr::null_mut())
378}
379
380impl default::Default for lua_Debug {
381    fn default() -> lua_Debug {
382        lua_Debug {
383            event: 0,
384            name: ptr::null(),
385            namewhat: ptr::null(),
386            what: ptr::null(),
387            source: ptr::null(),
388            currentline: 0,
389            linedefined: 0,
390            lastlinedefined: 0,
391            nups: 0,
392            nparams: 0,
393            isvararg: 0,
394            istailcall: 0,
395            short_src: [0; 60],
396        }
397    }
398}