Skip to content

Commit a162b0c

Browse files
committed
Update Lua 5.1 FFI bindings (add buffer manipulation, etc)
1 parent 8e6d652 commit a162b0c

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

mlua-sys/src/lua51/lauxlib.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *
107107
luaL_optlstring(L, n, d, ptr::null_mut())
108108
}
109109

110-
// Deprecated from 5.3: luaL_checkint, luaL_optint, luaL_checklong, luaL_optlong
111-
112110
#[inline(always)]
113111
pub unsafe fn luaL_typename(L: *mut lua_State, i: c_int) -> *const c_char {
114112
lua::lua_typename(L, lua::lua_type(L, i))
@@ -138,8 +136,58 @@ pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) {
138136
lua::lua_getfield_(L, lua::LUA_REGISTRYINDEX, n);
139137
}
140138

141-
// TODO: luaL_opt
139+
#[inline(always)]
140+
pub unsafe fn luaL_opt<T>(
141+
L: *mut lua_State,
142+
f: unsafe extern "C-unwind" fn(*mut lua_State, c_int) -> T,
143+
n: c_int,
144+
d: T,
145+
) -> T {
146+
if lua::lua_isnoneornil(L, n) != 0 {
147+
d
148+
} else {
149+
f(L, n)
150+
}
151+
}
142152

143153
//
144-
// TODO: Generic Buffer Manipulation
154+
// Generic Buffer Manipulation
145155
//
156+
157+
// The buffer size used by the lauxlib buffer system.
158+
// The "16384" workaround is taken from the LuaJIT source code.
159+
#[rustfmt::skip]
160+
pub const LUAL_BUFFERSIZE: usize = if libc::BUFSIZ > 16384 { 8192 } else { libc::BUFSIZ as usize };
161+
162+
#[repr(C)]
163+
pub struct luaL_Buffer {
164+
pub p: *mut c_char, // current position in buffer
165+
pub lvl: c_int, // number of strings in the stack
166+
pub L: *mut lua_State,
167+
pub buffer: [c_char; LUAL_BUFFERSIZE],
168+
}
169+
170+
#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
171+
unsafe extern "C-unwind" {
172+
pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
173+
pub fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut c_char;
174+
pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const c_char, l: usize);
175+
pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const c_char);
176+
pub fn luaL_addvalue(B: *mut luaL_Buffer);
177+
pub fn luaL_pushresult(B: *mut luaL_Buffer);
178+
}
179+
180+
#[inline(always)]
181+
pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: c_char) {
182+
let buffer_end = (*B).buffer.as_mut_ptr().add(LUAL_BUFFERSIZE);
183+
if (*B).p >= buffer_end {
184+
luaL_prepbuffer(B);
185+
}
186+
*(*B).p = c;
187+
(*B).p = (*B).p.add(1);
188+
}
189+
190+
#[inline(always)]
191+
pub unsafe fn luaL_addsize(B: *mut luaL_Buffer, n: usize) {
192+
(*B).p = (*B).p.add(n);
193+
}

mlua-sys/src/lua51/lua.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
270270
lua_pushcclosure(L, f, 0)
271271
}
272272

273-
// TODO: lua_strlen
273+
#[inline(always)]
274+
pub unsafe fn lua_strlen(L: *mut lua_State, i: c_int) -> usize {
275+
lua_objlen(L, i)
276+
}
274277

275278
#[inline(always)]
276279
pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {

0 commit comments

Comments
 (0)