luaur_vm/functions/
lua_l_optlstring.rs1use crate::functions::lua_l_checklstring::lua_l_checklstring;
2use crate::functions::lua_type::lua_type;
3use crate::type_aliases::lua_state::lua_State;
4use core::ffi::c_char;
5use core::ffi::c_int;
6
7#[no_mangle]
8pub unsafe fn lua_l_optlstring(
9 L: *mut lua_State,
10 narg: c_int,
11 def: *const c_char,
12 len: *mut usize,
13) -> *const c_char {
14 let is_none_or_nil = lua_type(L, narg) <= (crate::enums::lua_type::lua_Type::LUA_TNIL as c_int);
15
16 if is_none_or_nil {
17 if !len.is_null() {
18 if !def.is_null() {
19 let mut strlen: usize = 0;
20 let mut p = def;
21 while *p != 0 {
22 strlen += 1;
23 p = p.add(1);
24 }
25 *len = strlen;
26 } else {
27 *len = 0;
28 }
29 }
30 def
31 } else {
32 lua_l_checklstring(L, narg, len)
33 }
34}