lua_sys/
laux.rs

1// Lua Auxiliary Library
2
3use crate::*;
4use core::fmt;
5use core::mem;
6use core::ptr;
7
8// //////////////////////////////////////////// //
9// Constants                                    //
10// //////////////////////////////////////////// //
11
12pub const LUA_ERRFILE: libc::c_int = LUA_ERRERR + 1;
13
14cfg_if::cfg_if! {
15    if #[cfg(LUA_VERSION = "5.3")] {
16        pub const LUA_LOADED_TABLE: &str = "_LOADED";
17
18        pub const LUA_PRELOAD_TABLE: &str = "_PRELOAD";
19    }
20}
21
22pub const LUA_NOREF: libc::c_int = -2;
23pub const LUA_REFNIL: libc::c_int = -1;
24
25#[cfg(LUA_VERSION = "5.1")]
26pub const LUA_FILEHANDLE: &str = "FILE*";
27
28// //////////////////////////////////////////// //
29// Types                                        //
30// //////////////////////////////////////////// //
31
32#[cfg_attr(LUA_VERSION = "5.2", repr(C), derive(Clone))]
33#[cfg(LUA_VERSION = "5.2")]
34pub struct luaL_Buffer {
35    /// Buffer address
36    pub b: *mut libc::c_char,
37    /// Buffer size
38    pub size: usize,
39    /// number of characters in buffer
40    pub n: usize,
41    pub L: *mut lua_State,
42    /// initial buffer
43    pub initb: [libc::c_char; LUAL_BUFFERSIZE],
44}
45
46#[cfg_attr(not(LUA_VERSION = "5.2"), repr(C), derive(Clone))]
47#[cfg(not(LUA_VERSION = "5.2"))]
48pub struct luaL_Buffer {
49    /// Current position in buffer
50    pub p: *mut libc::c_char,
51    /// Number of strings in the stack (level)
52    pub lvl: libc::c_int,
53    pub L: *mut lua_State,
54    pub buffer: [libc::c_char; LUAL_BUFFERSIZE],
55}
56
57impl fmt::Debug for luaL_Buffer {
58    #[cfg(LUA_VERSION = "5.2")]
59    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
60        let initb = &self.initb as *const _;
61        fmt.debug_struct("luaL_Buffer")
62            .field("b", &self.b)
63            .field("size", &self.size)
64            .field("n", &self.n)
65            .field("L", &self.L)
66            .field("initb", &initb)
67            .finish()
68    }
69
70    #[cfg(not(LUA_VERSION = "5.2"))]
71    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
72        let buffer = &self.buffer as *const _;
73        fmt.debug_struct("luaL_Buffer")
74            .field("p", &self.p)
75            .field("lvl", &self.lvl)
76            .field("L", &self.L)
77            .field("buffer", &buffer)
78            .finish()
79    }
80}
81
82#[repr(C)]
83#[derive(Debug, Clone)]
84pub struct luaL_Reg {
85    pub name: *const libc::c_char,
86    pub func: lua_CFunction,
87}
88
89#[repr(C)]
90#[derive(Debug, Clone)]
91struct luaL_Stream {
92    /// Stream (NULL for incompletely created streams)
93    f: *mut libc::FILE,
94    /// To close stream (NULL for closed streams)
95    closef: lua_CFunction,
96}
97
98// //////////////////////////////////////////// //
99// Functions                                    //
100// //////////////////////////////////////////// //
101
102extern "C" {
103    pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const libc::c_char, l: usize);
104    pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const libc::c_char);
105    pub fn luaL_addvalue(B: *mut luaL_Buffer);
106    pub fn luaL_argerror(
107        L: *mut lua_State,
108        arg: libc::c_int,
109        extramsg: *const libc::c_char,
110    ) -> libc::c_int;
111    pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
112
113    // Introduced in Lua 5.2
114    #[cfg(LUA_VERSION = "5.2")]
115    pub fn luaL_buffinitsize(
116        L: *mut lua_State,
117        B: *mut luaL_Buffer,
118        sz: usize,
119    ) -> *mut libc::c_char;
120
121    pub fn luaL_callmeta(
122        L: *mut lua_State,
123        obj: libc::c_int,
124        e: *const libc::c_char,
125    ) -> libc::c_int;
126    pub fn luaL_checkany(L: *mut lua_State, arg: libc::c_int);
127    pub fn luaL_checkinteger(L: *mut lua_State, arg: libc::c_int) -> lua_Integer;
128    pub fn luaL_checklstring(
129        L: *mut lua_State,
130        arg: libc::c_int,
131        l: *mut usize,
132    ) -> *const libc::c_char;
133    pub fn luaL_checknumber(L: *mut lua_State, arg: libc::c_int) -> lua_Number;
134    pub fn luaL_checkoption(
135        L: *mut lua_State,
136        arg: libc::c_int,
137        def: *const libc::c_char,
138        lst: *const *const libc::c_char,
139    ) -> libc::c_int;
140    pub fn luaL_checkstack(L: *mut lua_State, sz: libc::c_int, msg: *const libc::c_char);
141    pub fn luaL_checktype(L: *mut lua_State, arg: libc::c_int, t: libc::c_int);
142    pub fn luaL_checkudata(
143        L: *mut lua_State,
144        ud: libc::c_int,
145        tname: *const libc::c_char,
146    ) -> *mut libc::c_void;
147
148    // Deprecated in Lua 5.3
149    #[cfg(any(
150        all(LUA_VERSION = "5.2", not(LUA_VERSION = "5.3")),
151        all(LUA_VERSION = "5.3", feature = "lua-compat")
152    ))]
153    pub fn luaL_checkunsigned(L: *mut lua_State, arg: libc::c_int) -> lua_Unsigned;
154
155    fn luaL_checkversion_(L: *mut lua_State, ver: lua_Number, sz: usize);
156    pub fn luaL_error(L: *mut lua_State, fmt: *const libc::c_char, ...) -> libc::c_int;
157
158    // Introduced in Lua 5.2
159    #[cfg(LUA_VERSION = "5.2")]
160    pub fn luaL_execresult(L: *mut lua_State, stat: libc::c_int) -> libc::c_int;
161
162    // Introduced in Lua 5.2
163    #[cfg(LUA_VERSION = "5.2")]
164    pub fn luaL_fileresult(
165        L: *mut lua_State,
166        stat: libc::c_int,
167        fname: *const libc::c_char,
168    ) -> libc::c_int;
169
170    pub fn luaL_getmetafield(
171        L: *mut lua_State,
172        obj: libc::c_int,
173        e: *const libc::c_char,
174    ) -> libc::c_int;
175
176    // Deprecated in Lua 5.1
177    #[cfg(any(
178        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.1")),
179        all(LUA_VERSION = "5.1", feature = "lua-compat")
180    ))]
181    pub fn luaL_getn(L: *mut lua_State, t: libc::c_int) -> libc::c_int;
182
183    // Introduced in Lua 5.2
184    #[cfg(LUA_VERSION = "5.2")]
185    pub fn luaL_getsubtable(
186        L: *mut lua_State,
187        idx: libc::c_int,
188        fname: *const libc::c_char,
189    ) -> libc::c_int;
190
191    pub fn luaL_gsub(
192        L: *mut lua_State,
193        s: *const libc::c_char,
194        p: *const libc::c_char,
195        r: *const libc::c_char,
196    ) -> *const libc::c_char;
197
198    // Introduced in Lua 5.2
199    #[cfg(LUA_VERSION = "5.2")]
200    pub fn luaL_len(L: *mut lua_State, idx: libc::c_int) -> lua_Integer;
201
202    // Defined as a macro as of Lua 5.2
203    #[cfg(not(LUA_VERSION = "5.2"))]
204    pub fn luaL_loadbuffer(
205        L: *mut lua_State,
206        buff: *const libc::c_char,
207        sz: usize,
208        name: *const libc::c_char,
209    ) -> libc::c_int;
210
211    // Introduced in Lua 5.2
212    #[cfg(LUA_VERSION = "5.2")]
213    pub fn luaL_loadbufferx(
214        L: *mut lua_State,
215        buff: *const libc::c_char,
216        sz: usize,
217        name: *const libc::c_char,
218        mode: *const libc::c_char,
219    ) -> libc::c_int;
220
221    // Defined as a macro as of Lua 5.2
222    #[cfg(not(LUA_VERSION = "5.2"))]
223    pub fn luaL_loadfile(L: *mut lua_State, filename: *const libc::c_char) -> libc::c_int;
224
225    // Introduced in Lua 5.2
226    #[cfg(LUA_VERSION = "5.2")]
227    pub fn luaL_loadfilex(
228        L: *mut lua_State,
229        filename: *const libc::c_char,
230        mode: *const libc::c_char,
231    ) -> libc::c_int;
232
233    pub fn luaL_loadstring(L: *mut lua_State, s: *const libc::c_char) -> libc::c_int;
234    pub fn luaL_newmetatable(L: *mut lua_State, tname: *const libc::c_char) -> libc::c_int;
235
236    // Introduced in Lua 5.1
237    #[cfg(LUA_VERSION = "5.1")]
238    pub fn luaL_newstate() -> *mut lua_State;
239
240    pub fn luaL_optinteger(L: *mut lua_State, arg: libc::c_int, def: lua_Integer) -> lua_Integer;
241    pub fn luaL_optlstring(
242        L: *mut lua_State,
243        arg: libc::c_int,
244        def: *const libc::c_char,
245        l: *mut usize,
246    ) -> *const libc::c_char;
247    pub fn luaL_optnumber(L: *mut lua_State, arg: libc::c_int, def: lua_Number) -> lua_Number;
248
249    // Deprecated in Lua 5.3
250    #[cfg_attr(
251        any(
252            all(LUA_VERSION = "5.2", not(LUA_VERSION = "5.3")),
253            all(LUA_VERSION = "5.3", feature = "lua-compat")
254        ),
255        inline
256    )]
257    #[cfg(any(
258        all(LUA_VERSION = "5.2", not(LUA_VERSION = "5.3")),
259        all(LUA_VERSION = "5.3", feature = "lua-compat")
260    ))]
261    pub fn luaL_optunsigned(L: *mut lua_State, arg: libc::c_int, def: lua_Unsigned)
262        -> lua_Unsigned;
263
264    // Defined as a macro in Lua 5.2+
265    #[cfg(not(LUA_VERSION = "5.2"))]
266    pub fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut libc::c_char;
267
268    // Introduced in Lua 5.2
269    #[cfg(LUA_VERSION = "5.2")]
270    pub fn luaL_prepbuffsize(B: *mut luaL_Buffer, sz: usize) -> *mut libc::c_char;
271
272    pub fn luaL_pushresult(B: *mut luaL_Buffer);
273
274    // Introduced in Lua 5.2
275    #[cfg(LUA_VERSION = "5.2")]
276    pub fn luaL_pushresultsize(B: *mut luaL_Buffer, sz: usize);
277
278    pub fn luaL_ref(L: *mut lua_State, t: libc::c_int) -> libc::c_int;
279
280    #[cfg(any(
281        all(LUA_VERSION = "5.1", not(LUA_VERSION = "5.2")),
282        all(LUA_VERSION = "5.2", feature = "lua-compat")
283    ))]
284    pub fn luaL_register(
285        L: *mut lua_State,
286        libname: *const libc::c_char,
287        l: *const luaL_Reg,
288    ) -> libc::c_int;
289
290    pub fn luaL_requiref(
291        L: *mut lua_State,
292        modname: *const libc::c_char,
293        openf: lua_CFunction,
294        glb: libc::c_int,
295    );
296    pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: libc::c_int);
297    pub fn luaL_setmetatable(L: *mut lua_State, tname: *const libc::c_char);
298
299    // Deprecated in Lua 5.1
300    #[cfg(any(
301        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.1")),
302        all(LUA_VERSION = "5.1", feature = "lua-compat")
303    ))]
304    pub fn luaL_setn(L: *mut lua_State, t: libc::c_int, n: libc::c_int);
305
306    pub fn luaL_testudata(
307        L: *mut lua_State,
308        ud: libc::c_int,
309        tname: *const libc::c_char,
310    ) -> *mut libc::c_void;
311
312    #[cfg(LUA_VERSION = "5.2")]
313    pub fn luaL_tolstring(
314        L: *mut lua_State,
315        idx: libc::c_int,
316        len: *mut usize,
317    ) -> *const libc::c_char;
318
319    pub fn luaL_traceback(
320        L: *mut lua_State,
321        L1: *mut lua_State,
322        msg: *const libc::c_char,
323        level: libc::c_int,
324    );
325
326    // Removed in Lua 5.2
327    #[cfg(all(LUA_VERSION = "5.1", not(LUA_VERSION = "5.2")))]
328    pub fn luaL_typerror(
329        L: *mut lua_State,
330        narg: libc::c_int,
331        tname: *const libc::c_char,
332    ) -> libc::c_int;
333
334    pub fn luaL_unref(L: *mut lua_State, t: libc::c_int, ref_: libc::c_int);
335    pub fn luaL_where(L: *mut lua_State, lvl: libc::c_int);
336}
337
338// //////////////////////////////////////////// //
339// Macros (represented as inline functions)     //
340// //////////////////////////////////////////// //
341
342#[cfg_attr(LUA_VERSION = "5.2", inline)]
343#[cfg(LUA_VERSION = "5.2")]
344pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: libc::c_char) {
345    if (*B).n >= (*B).size {
346        luaL_prepbuffsize(B, 1);
347    }
348    (*B).b.add((*B).n).write(c);
349    (*B).n += 1;
350}
351
352#[cfg_attr(not(LUA_VERSION = "5.2"), inline)]
353#[cfg(not(LUA_VERSION = "5.2"))]
354pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: libc::c_char) {
355    if (*B).p as usize >= (&(*B).buffer as *const _ as usize) + LUAL_BUFFERSIZE as usize {
356        luaL_prepbuffer(B);
357    }
358    (*B).p.write(c);
359    (*B).p = (*B).p.add(1);
360}
361
362#[cfg_attr(LUA_VERSION = "5.2", inline)]
363#[cfg(LUA_VERSION = "5.2")]
364pub unsafe fn luaL_addsize(B: *mut luaL_Buffer, n: usize) {
365    (*B).n += n;
366}
367
368#[cfg_attr(not(LUA_VERSION = "5.2"), inline)]
369#[cfg(not(LUA_VERSION = "5.2"))]
370pub unsafe fn luaL_addsize(B: *mut luaL_Buffer, n: usize) {
371    (*B).p = (*B).p.add(n);
372}
373
374#[inline]
375pub unsafe fn luaL_argcheck(
376    L: *mut lua_State,
377    cond: libc::c_int,
378    arg: libc::c_int,
379    extramsg: *const libc::c_char,
380) {
381    if cond == 0 {
382        luaL_argerror(L, arg, extramsg);
383    }
384}
385
386// Deprecated in Lua 5.3
387#[cfg_attr(
388    any(
389        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
390        all(LUA_VERSION = "5.3", feature = "lua-compat")
391    ),
392    inline
393)]
394#[cfg(any(
395    all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
396    all(LUA_VERSION = "5.3", feature = "lua-compat")
397))]
398pub unsafe fn luaL_checkint(L: *mut lua_State, arg: libc::c_int) -> libc::c_int {
399    luaL_checkinteger(L, arg) as libc::c_int
400}
401
402// Deprecated in Lua 5.3
403#[cfg_attr(
404    any(
405        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
406        all(LUA_VERSION = "5.3", feature = "lua-compat")
407    ),
408    inline
409)]
410#[cfg(any(
411    all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
412    all(LUA_VERSION = "5.3", feature = "lua-compat")
413))]
414pub unsafe fn luaL_checklong(L: *mut lua_State, arg: libc::c_int) -> libc::c_long {
415    luaL_checkinteger(L, arg) as libc::c_long
416}
417
418#[inline]
419pub unsafe fn luaL_checkstring(L: *mut lua_State, arg: libc::c_int) {
420    luaL_checklstring(L, arg, ptr::null_mut());
421}
422
423#[inline]
424pub unsafe fn luaL_checkversion(L: *mut lua_State) {
425    luaL_checkversion_(
426        L,
427        LUA_VERSION_NUM,
428        mem::size_of::<lua_Integer>() * 16 + mem::size_of::<lua_Number>(),
429    );
430}
431
432#[inline]
433pub unsafe fn luaL_dofile(L: *mut lua_State, filename: *const libc::c_char) -> libc::c_int {
434    (luaL_loadfile(L, filename) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0) as libc::c_int
435}
436
437#[inline]
438pub unsafe fn luaL_dostring(L: *mut lua_State, string: *const libc::c_char) -> libc::c_int {
439    (luaL_loadstring(L, string) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) != 0) as libc::c_int
440}
441
442#[inline]
443pub unsafe fn luaL_getmetatable(L: *mut lua_State, tname: *const libc::c_char) -> libc::c_int {
444    lua_getfield(L, LUA_REGISTRYINDEX, tname)
445}
446
447// Deprecated in Lua 5.1
448#[cfg(all(
449    LUA_VERSION = "5.1",
450    not(LUA_VERSION = "5.2"),
451    not(feature = "lua-compat")
452))]
453pub unsafe fn luaL_getn(L: *mut lua_State, t: libc::c_int) -> libc::c_int {
454    lua_objlen(L, t) as libc::c_int
455}
456
457// Defined as a macro in Lua 5.2+
458#[cfg_attr(LUA_VERSION = "5.2", inline)]
459#[cfg(LUA_VERSION = "5.2")]
460pub unsafe fn luaL_loadbuffer(
461    L: *mut lua_State,
462    buff: *const libc::c_char,
463    sz: usize,
464    name: *const libc::c_char,
465) -> libc::c_int {
466    luaL_loadbufferx(L, buff, sz, name, ptr::null())
467}
468
469// Defined as a macro in Lua 5.2+
470#[cfg_attr(LUA_VERSION = "5.2", inline)]
471#[cfg(LUA_VERSION = "5.2")]
472pub unsafe fn luaL_loadfile(L: *mut lua_State, filename: *const libc::c_char) -> libc::c_int {
473    luaL_loadfilex(L, filename, ptr::null())
474}
475
476// Introduced in Lua 5.2
477#[cfg_attr(LUA_VERSION = "5.2", inline)]
478#[cfg(LUA_VERSION = "5.2")]
479pub unsafe fn luaL_newlib(L: *mut lua_State, l: *const luaL_Reg) {
480    luaL_checkversion(L);
481    luaL_newlibtable(L, l);
482    luaL_setfuncs(L, l, 0)
483}
484
485// Introduced in Lua 5.2
486#[cfg_attr(LUA_VERSION = "5.2", inline)]
487#[cfg(LUA_VERSION = "5.2")]
488pub unsafe fn luaL_newlibtable(L: *mut lua_State, _l: *const luaL_Reg) {
489    lua_createtable(
490        L,
491        0,
492        (mem::size_of::<*const luaL_Reg>() / mem::size_of::<luaL_Reg>() - 1) as libc::c_int,
493    );
494}
495
496// Introduced in Lua 5.3
497#[cfg_attr(LUA_VERSION = "5.3", inline)]
498#[cfg(LUA_VERSION = "5.3")]
499pub unsafe fn luaL_opt<T, D: Into<T>>(
500    L: *mut lua_State,
501    func: unsafe extern "C" fn(*mut lua_State, libc::c_int) -> T,
502    arg: libc::c_int,
503    dflt: D,
504) -> T {
505    if lua_isnoneornil(L, arg) != 0 {
506        dflt.into()
507    } else {
508        func(L, arg)
509    }
510}
511
512#[inline]
513pub unsafe fn luaL_optstring(
514    L: *mut lua_State,
515    arg: libc::c_int,
516    d: *const libc::c_char,
517) -> *const libc::c_char {
518    luaL_optlstring(L, arg, d, ptr::null_mut())
519}
520
521// Deprecated in Lua 5.3
522#[cfg_attr(
523    any(
524        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
525        all(LUA_VERSION = "5.3", feature = "lua-compat")
526    ),
527    inline
528)]
529#[cfg(any(
530    all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
531    all(LUA_VERSION = "5.3", feature = "lua-compat")
532))]
533pub unsafe fn luaL_optint(L: *mut lua_State, arg: libc::c_int, d: libc::c_int) -> libc::c_int {
534    luaL_optinteger(L, arg, d as lua_Integer) as libc::c_int
535}
536
537// Deprecated in Lua 5.3
538#[cfg_attr(
539    any(
540        all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
541        all(LUA_VERSION = "5.3", feature = "lua-compat")
542    ),
543    inline
544)]
545#[cfg(any(
546    all(LUA_VERSION = "5.0", not(LUA_VERSION = "5.3")),
547    all(LUA_VERSION = "5.3", feature = "lua-compat")
548))]
549pub unsafe fn luaL_optlong(L: *mut lua_State, arg: libc::c_int, d: libc::c_long) -> libc::c_long {
550    luaL_optinteger(L, arg, d as lua_Integer) as libc::c_long
551}
552
553// Defined as a macro in Lua 5.2+
554#[cfg_attr(LUA_VERSION = "5.2", inline)]
555#[cfg(LUA_VERSION = "5.2")]
556pub unsafe fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut libc::c_char {
557    luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
558}
559
560// Deprecated in Lua 5.1
561#[cfg(all(
562    LUA_VERSION = "5.1",
563    not(LUA_VERSION = "5.2"),
564    not(feature = "lua-compat")
565))]
566pub unsafe fn luaL_setn(_L: *mut lua_State, _t: libc::c_int, _n: libc::c_int) {
567    // no op
568}
569
570#[inline]
571pub unsafe fn luaL_typename(L: *mut lua_State, index: libc::c_int) -> *const libc::c_char {
572    lua_typename(L, lua_type(L, index))
573}