Skip to content

Commit 8f086bf

Browse files
committed
Update Lua 5.4 FFI bindings (add buffer manipulation, etc)
1 parent ad16761 commit 8f086bf

1 file changed

Lines changed: 94 additions & 6 deletions

File tree

mlua-sys/src/lua54/lauxlib.rs

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains definitions from `lauxlib.h`.
22
3-
use std::os::raw::{c_char, c_int, c_void};
4-
use std::ptr;
3+
use std::os::raw::{c_char, c_double, c_int, c_long, c_void};
4+
use std::{mem, ptr};
55

66
use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State};
77

@@ -91,7 +91,7 @@ unsafe extern "C-unwind" {
9191

9292
pub fn luaL_len(L: *mut lua_State, idx: c_int) -> lua_Integer;
9393

94-
// TODO: luaL_addgsub
94+
pub fn luaL_addgsub(B: *mut luaL_Buffer, s: *const c_char, p: *const c_char, r: *const c_char);
9595

9696
pub fn luaL_gsub(
9797
L: *mut lua_State,
@@ -162,8 +162,6 @@ pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) {
162162
lua::lua_getfield(L, lua::LUA_REGISTRYINDEX, n);
163163
}
164164

165-
// luaL_opt would be implemented here but it is undocumented, so it's omitted
166-
167165
#[inline(always)]
168166
pub unsafe fn luaL_loadbuffer(L: *mut lua_State, s: *const c_char, sz: usize, n: *const c_char) -> c_int {
169167
luaL_loadbufferx(L, s, sz, n, ptr::null())
@@ -188,6 +186,96 @@ pub unsafe fn luaL_loadbufferenv(
188186
status
189187
}
190188

189+
#[inline(always)]
190+
pub unsafe fn luaL_opt<T>(
191+
L: *mut lua_State,
192+
f: unsafe extern "C-unwind" fn(*mut lua_State, c_int) -> T,
193+
n: c_int,
194+
d: T,
195+
) -> T {
196+
if lua::lua_isnoneornil(L, n) != 0 {
197+
d
198+
} else {
199+
f(L, n)
200+
}
201+
}
202+
191203
//
192-
// TODO: Generic Buffer Manipulation
204+
// Generic Buffer Manipulation
193205
//
206+
207+
// The buffer size used by the lauxlib buffer system.
208+
// LUAL_BUFFERSIZE = (int)(16 * sizeof(void*) * sizeof(lua_Number))
209+
#[rustfmt::skip]
210+
pub const LUAL_BUFFERSIZE: usize = 16 * mem::size_of::<*const ()>() * mem::size_of::<lua_Number>();
211+
212+
// Union used for the initial buffer with maximum alignment.
213+
// This ensures proper alignment for the buffer data.
214+
#[repr(C)]
215+
pub union luaL_BufferInit {
216+
// Alignment matches LUAI_MAXALIGN
217+
pub _align_n: lua_Number,
218+
pub _align_u: c_double,
219+
pub _align_s: *mut c_void,
220+
pub _align_i: lua_Integer,
221+
pub _align_l: c_long,
222+
// Initial buffer space
223+
pub b: [c_char; LUAL_BUFFERSIZE],
224+
}
225+
226+
#[repr(C)]
227+
pub struct luaL_Buffer {
228+
pub b: *mut c_char, // buffer address
229+
pub size: usize, // buffer size
230+
pub n: usize, // number of characters in buffer
231+
pub L: *mut lua_State,
232+
pub init: luaL_BufferInit, // initial buffer (union with alignment)
233+
}
234+
235+
#[cfg_attr(all(windows, raw_dylib), link(name = "lua54", kind = "raw-dylib"))]
236+
unsafe extern "C-unwind" {
237+
pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
238+
pub fn luaL_prepbuffsize(B: *mut luaL_Buffer, sz: usize) -> *mut c_char;
239+
pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const c_char, l: usize);
240+
pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const c_char);
241+
pub fn luaL_addvalue(B: *mut luaL_Buffer);
242+
pub fn luaL_pushresult(B: *mut luaL_Buffer);
243+
pub fn luaL_pushresultsize(B: *mut luaL_Buffer, sz: usize);
244+
pub fn luaL_buffinitsize(L: *mut lua_State, B: *mut luaL_Buffer, sz: usize) -> *mut c_char;
245+
}
246+
247+
// Macro implementations as inline functions
248+
249+
#[inline(always)]
250+
pub unsafe fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut c_char {
251+
luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
252+
}
253+
254+
#[inline(always)]
255+
pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: c_char) {
256+
if (*B).n >= (*B).size {
257+
luaL_prepbuffsize(B, 1);
258+
}
259+
*(*B).b.add((*B).n) = c;
260+
(*B).n += 1;
261+
}
262+
263+
#[inline(always)]
264+
pub unsafe fn luaL_addsize(B: *mut luaL_Buffer, n: usize) {
265+
(*B).n += n;
266+
}
267+
268+
#[inline(always)]
269+
pub unsafe fn luaL_buffsub(B: *mut luaL_Buffer, n: usize) {
270+
(*B).n -= n;
271+
}
272+
273+
#[inline(always)]
274+
pub unsafe fn luaL_bufflen(B: *mut luaL_Buffer) -> usize {
275+
(*B).n
276+
}
277+
278+
#[inline(always)]
279+
pub unsafe fn luaL_buffaddr(B: *mut luaL_Buffer) -> *mut c_char {
280+
(*B).b
281+
}

0 commit comments

Comments
 (0)