Skip to content

Commit e9efb73

Browse files
committed
Update Luau FFI bindings (added some missing functions)
1 parent 386c6d8 commit e9efb73

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

mlua-sys/src/luau/lauxlib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,19 @@ pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *
122122
luaL_optlstring(L, n, d, ptr::null_mut())
123123
}
124124

125-
// TODO: luaL_opt
125+
#[inline(always)]
126+
pub unsafe fn luaL_opt<T>(
127+
L: *mut lua_State,
128+
f: unsafe extern "C-unwind" fn(*mut lua_State, c_int) -> T,
129+
n: c_int,
130+
d: T,
131+
) -> T {
132+
if lua::lua_isnoneornil(L, n) != 0 {
133+
d
134+
} else {
135+
f(L, n)
136+
}
137+
}
126138

127139
#[inline(always)]
128140
pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) -> c_int {

mlua-sys/src/luau/lua.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ pub const LUA_ERRRUN: c_int = 2;
3737
pub const LUA_ERRSYNTAX: c_int = 3;
3838
pub const LUA_ERRMEM: c_int = 4;
3939
pub const LUA_ERRERR: c_int = 5;
40+
pub const LUA_BREAK: c_int = 6; // yielded for a debug breakpoint
41+
42+
//
43+
// Coroutine status
44+
//
45+
pub const LUA_CORUN: c_int = 0; // running
46+
pub const LUA_COSUS: c_int = 1; // suspended
47+
pub const LUA_CONOR: c_int = 2; // 'normal' (it resumed another coroutine)
48+
pub const LUA_COFIN: c_int = 3; // finished
49+
pub const LUA_COERR: c_int = 4; // finished with error
4050

4151
/// A raw Lua state associated with a thread.
4252
#[repr(C)]
@@ -145,8 +155,10 @@ unsafe extern "C-unwind" {
145155
pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
146156
pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
147157
pub fn lua_tostringatom(L: *mut lua_State, idx: c_int, atom: *mut c_int) -> *const c_char;
158+
pub fn lua_tolstringatom(L: *mut lua_State, idx: c_int, len: *mut usize, atom: *mut c_int) -> *const c_char;
148159
pub fn lua_namecallatom(L: *mut lua_State, atom: *mut c_int) -> *const c_char;
149-
pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
160+
#[link_name = "lua_objlen"]
161+
pub fn lua_objlen_(L: *mut lua_State, idx: c_int) -> c_int;
150162
pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
151163
pub fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
152164
pub fn lua_tolightuserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
@@ -218,6 +230,7 @@ unsafe extern "C-unwind" {
218230
//
219231
pub fn lua_settable(L: *mut lua_State, idx: c_int);
220232
pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
233+
pub fn lua_rawsetfield(L: *mut lua_State, idx: c_int, k: *const c_char);
221234
pub fn lua_rawset(L: *mut lua_State, idx: c_int);
222235
#[link_name = "lua_rawseti"]
223236
pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
@@ -251,6 +264,12 @@ unsafe extern "C-unwind" {
251264
pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
252265
pub fn lua_getthreaddata(L: *mut lua_State) -> *mut c_void;
253266
pub fn lua_setthreaddata(L: *mut lua_State, data: *mut c_void);
267+
pub fn lua_costatus(L: *mut lua_State, co: *mut lua_State) -> c_int;
268+
}
269+
270+
#[inline(always)]
271+
pub unsafe fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize {
272+
lua_objlen_(L, idx) as usize
254273
}
255274

256275
//
@@ -287,7 +306,7 @@ unsafe extern "C-unwind" {
287306
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
288307
pub fn lua_rawiter(L: *mut lua_State, idx: c_int, iter: c_int) -> c_int;
289308
pub fn lua_concat(L: *mut lua_State, n: c_int);
290-
// TODO: lua_encodepointer
309+
pub fn lua_encodepointer(L: *mut lua_State, p: usize) -> usize;
291310
pub fn lua_clock() -> c_double;
292311
pub fn lua_setuserdatatag(L: *mut lua_State, idx: c_int, tag: c_int);
293312
pub fn lua_setuserdatadtor(L: *mut lua_State, tag: c_int, dtor: Option<lua_Destructor>);
@@ -298,6 +317,7 @@ unsafe extern "C-unwind" {
298317
pub fn lua_getlightuserdataname(L: *mut lua_State, tag: c_int) -> *const c_char;
299318
pub fn lua_clonefunction(L: *mut lua_State, idx: c_int);
300319
pub fn lua_cleartable(L: *mut lua_State, idx: c_int);
320+
pub fn lua_clonetable(L: *mut lua_State, idx: c_int);
301321
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
302322
}
303323

@@ -357,7 +377,10 @@ pub unsafe fn lua_newuserdata_t<T>(L: *mut lua_State, data: T) -> *mut T {
357377
ud_ptr
358378
}
359379

360-
// TODO: lua_strlen
380+
#[inline(always)]
381+
pub unsafe fn lua_strlen(L: *mut lua_State, i: c_int) -> usize {
382+
lua_objlen(L, i)
383+
}
361384

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

0 commit comments

Comments
 (0)