mond/ffi/
lauxlib.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 `lauxlib.h`.
24
25use libc::{c_char, c_int, c_long, c_void, size_t};
26use ffi::lua;
27use ffi::lua::{lua_CFunction, lua_Integer, lua_Number, lua_State};
28use ffi::luaconf::LUAL_BUFFERSIZE;
29use std::ptr;
30
31pub use super::glue::LUAL_NUMSIZES;
32pub use super::glue::LUA_FILEHANDLE;
33
34// extra error code for 'luaL_load'
35pub const LUA_ERRFILE: c_int = lua::LUA_ERRERR + 1;
36
37#[repr(C)]
38pub struct luaL_Reg {
39    pub name: *const c_char,
40    pub func: lua_CFunction,
41}
42
43pub unsafe fn luaL_checkversion(L: *mut lua_State) {
44    luaL_checkversion_(L, f64::from(lua::LUA_VERSION_NUM), LUAL_NUMSIZES as size_t)
45}
46
47extern "C" {
48    pub fn luaL_checkversion_(L: *mut lua_State, ver: lua_Number, sz: size_t);
49
50    pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
51    pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
52    pub fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut size_t) -> *const c_char;
53    pub fn luaL_argerror(L: *mut lua_State, arg: c_int, l: *const c_char) -> c_int;
54    pub fn luaL_checklstring(L: *mut lua_State, arg: c_int, l: *mut size_t) -> *const c_char;
55    pub fn luaL_optlstring(
56        L: *mut lua_State,
57        arg: c_int,
58        def: *const c_char,
59        l: *mut size_t,
60    ) -> *const c_char;
61    pub fn luaL_checknumber(L: *mut lua_State, arg: c_int) -> lua_Number;
62    pub fn luaL_optnumber(L: *mut lua_State, arg: c_int, def: lua_Number) -> lua_Number;
63    pub fn luaL_checkinteger(L: *mut lua_State, arg: c_int) -> lua_Integer;
64    pub fn luaL_optinteger(L: *mut lua_State, arg: c_int, def: lua_Integer) -> lua_Integer;
65
66    pub fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char);
67    pub fn luaL_checktype(L: *mut lua_State, arg: c_int, t: c_int);
68    pub fn luaL_checkany(L: *mut lua_State, arg: c_int);
69
70    pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
71    pub fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char);
72    pub fn luaL_testudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
73    pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
74
75    pub fn luaL_where(L: *mut lua_State, lvl: c_int);
76    pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> c_int;
77
78    // TODO: test this
79    pub fn luaL_checkoption(
80        L: *mut lua_State,
81        arg: c_int,
82        def: *const c_char,
83        lst: *const *const c_char,
84    ) -> c_int;
85
86    pub fn luaL_fileresult(L: *mut lua_State, stat: c_int, fname: *const c_char) -> c_int;
87    pub fn luaL_execresult(L: *mut lua_State, stat: c_int) -> c_int;
88}
89
90// pre-defined references
91pub const LUA_NOREF: c_int = -2;
92pub const LUA_REFNIL: c_int = -1;
93
94extern "C" {
95    pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int;
96    pub fn luaL_unref(L: *mut lua_State, t: c_int, r: c_int);
97
98    pub fn luaL_loadfilex(L: *mut lua_State, filename: *const c_char, mode: *const c_char)
99        -> c_int;
100}
101
102pub unsafe fn luaL_loadfile(L: *mut lua_State, f: *const c_char) -> c_int {
103    luaL_loadfilex(L, f, ptr::null())
104}
105
106extern "C" {
107    pub fn luaL_loadbufferx(
108        L: *mut lua_State,
109        buff: *const c_char,
110        sz: size_t,
111        name: *const c_char,
112        mode: *const c_char,
113    ) -> c_int;
114    pub fn luaL_loadstring(L: *mut lua_State, s: *const c_char) -> c_int;
115
116    pub fn luaL_newstate() -> *mut lua_State;
117
118    pub fn luaL_len(L: *mut lua_State, idx: c_int) -> lua_Integer;
119
120    pub fn luaL_gsub(
121        L: *mut lua_State,
122        s: *const c_char,
123        p: *const c_char,
124        r: *const c_char,
125    ) -> *const c_char;
126
127    pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: c_int);
128
129    pub fn luaL_getsubtable(L: *mut lua_State, idx: c_int, fname: *const c_char) -> c_int;
130
131    pub fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const c_char, level: c_int);
132
133    pub fn luaL_requiref(
134        L: *mut lua_State,
135        modname: *const c_char,
136        openf: lua_CFunction,
137        glb: c_int,
138    );
139}
140
141#[allow(unused_variables)]
142pub unsafe fn luaL_newlibtable(L: *mut lua_State, l: *const luaL_Reg) {
143    // TODO: figure out how to pass an appropriate hint for the second param
144    // this involves correcting the second parameter's type; in C this is
145    // sizeof(l)/sizeof(l[0])
146    lua::lua_createtable(L, 0, 0)
147}
148
149pub unsafe fn luaL_newlib(L: *mut lua_State, l: *const luaL_Reg) {
150    luaL_checkversion(L);
151    luaL_newlibtable(L, l);
152    luaL_setfuncs(L, l, 0)
153}
154
155pub unsafe fn luaL_argcheck(L: *mut lua_State, cond: c_int, arg: c_int, extramsg: *const c_char) {
156    if cond == 0 {
157        luaL_argerror(L, arg, extramsg);
158    }
159}
160
161pub unsafe fn luaL_checkstring(L: *mut lua_State, n: c_int) -> *const c_char {
162    luaL_checklstring(L, n, ptr::null_mut())
163}
164
165pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *const c_char {
166    luaL_optlstring(L, n, d, ptr::null_mut())
167}
168
169// From 5.3 user manual:
170// Macros to project non-default integer types (luaL_checkint, luaL_optint,
171// luaL_checklong, luaL_optlong) were deprecated. Use their equivalent over
172// lua_Integer with a type cast (or, when possible, use lua_Integer in your
173// code).
174
175//#[deprecated]
176pub unsafe fn luaL_checkint(L: *mut lua_State, n: c_int) -> c_int {
177    luaL_checkinteger(L, n) as c_int
178}
179
180//#[deprecated]
181pub unsafe fn luaL_optint(L: *mut lua_State, n: c_int, d: c_int) -> c_int {
182    luaL_optinteger(L, n, i64::from(d)) as c_int
183}
184
185//#[deprecated]
186pub unsafe fn luaL_checklong(L: *mut lua_State, n: c_int) -> c_long {
187    luaL_checkinteger(L, n) as c_long
188}
189
190//#[deprecated]
191pub unsafe fn luaL_optlong(L: *mut lua_State, n: c_int, d: c_long) -> c_long {
192    luaL_optinteger(L, n, d as lua_Integer) as c_long
193}
194
195pub unsafe fn luaL_typename(L: *mut lua_State, i: c_int) -> *const c_char {
196    lua::lua_typename(L, lua::lua_type(L, i))
197}
198
199pub unsafe fn luaL_dofile(L: *mut lua_State, filename: *const c_char) -> c_int {
200    let status = luaL_loadfile(L, filename);
201    if status == 0 {
202        lua::lua_pcall(L, 0, lua::LUA_MULTRET, 0)
203    } else {
204        status
205    }
206}
207
208pub unsafe fn luaL_dostring(L: *mut lua_State, s: *const c_char) -> c_int {
209    let status = luaL_loadstring(L, s);
210    if status == 0 {
211        lua::lua_pcall(L, 0, lua::LUA_MULTRET, 0)
212    } else {
213        status
214    }
215}
216
217pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) {
218    lua::lua_getfield(L, lua::LUA_REGISTRYINDEX, n);
219}
220
221// luaL_opt would be implemented here but it is undocumented, so it's omitted
222
223pub unsafe fn luaL_loadbuffer(
224    L: *mut lua_State,
225    s: *const c_char,
226    sz: size_t,
227    n: *const c_char,
228) -> c_int {
229    luaL_loadbufferx(L, s, sz, n, ptr::null())
230}
231
232#[repr(C)]
233pub struct luaL_Buffer {
234    pub b: *mut c_char,
235    pub size: size_t,
236    pub n: size_t,
237    pub L: *mut lua_State,
238    pub initb: [c_char; LUAL_BUFFERSIZE as usize],
239}
240
241// TODO: Test this thoroughly
242
243pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: c_char) {
244    // (B)->n < (B) -> size || luaL_prepbuffsize((B), 1)
245    if (*B).n < (*B).size {
246        luaL_prepbuffsize(B, 1);
247    }
248    // (B)->b[(B)->n++] = (c)
249    let offset = (*B).b.offset((*B).n as isize);
250    ptr::write(offset, c);
251    (*B).n += 1;
252}
253
254pub unsafe fn luaL_addsize(B: *mut luaL_Buffer, s: size_t) {
255    (*B).n += s;
256}
257
258extern "C" {
259    pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
260    pub fn luaL_prepbuffsize(B: *mut luaL_Buffer, sz: size_t) -> *mut c_char;
261    pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const c_char, l: size_t);
262    pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const c_char);
263    pub fn luaL_addvalue(B: *mut luaL_Buffer);
264    pub fn luaL_pushresult(B: *mut luaL_Buffer);
265    pub fn luaL_pushresultsize(B: *mut luaL_Buffer, sz: size_t);
266    pub fn luaL_buffinitsize(L: *mut lua_State, B: *mut luaL_Buffer, sz: size_t) -> *mut c_char;
267}
268
269pub unsafe fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut c_char {
270    luaL_prepbuffsize(B, LUAL_BUFFERSIZE as size_t)
271}
272
273#[repr(C)]
274pub struct luaL_Stream {
275    pub f: *mut ::libc::FILE,
276    pub closef: lua_CFunction,
277}
278
279// omitted: old module system compatibility