Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 0571dbb

Browse files
committed
Remove Option type from all FFI functions
Thought it would be safe for ffi, but it isn't in all cases.
1 parent 57e340f commit 0571dbb

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

rglua/src/lua/shared.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ dyn_symbols! {
117117

118118
/// Loads a file as a Lua chunk.
119119
/// This function uses [lua_load] to load the chunk in the file named ``filename``.
120-
/// If filename is None, then it loads from the standard input.
120+
/// If filename is [std::ptr::null_mut()], then it loads from the standard input.
121121
/// The first line in the file is ignored if it starts with a # (shebang)
122-
pub extern "C" fn luaL_loadfile(l: LuaState, filename: Option<LuaString>) -> c_int;
122+
pub extern "C" fn luaL_loadfile(l: LuaState, filename: LuaString) -> c_int;
123123

124124
/// Same as how [lua_loadx] is to [lua_load].
125125
/// You should probably use [luaL_loadfile] instead.
@@ -249,10 +249,10 @@ dyn_symbols! {
249249

250250
/// Converts the Lua value at the given index to a C string.
251251
/// If len is not None, it also sets *len with the string length.
252-
/// The Lua value must be a string or a number; otherwise, the function returns a [None].
252+
/// The Lua value must be a string or a number; otherwise, the function returns a [std::ptr::null()].
253253
/// If the value is a number, then lua_tolstring also changes the actual value in the stack to a string.
254254
/// (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.)
255-
pub extern "C" fn lua_tolstring(l: LuaState, ind: c_int, len: Option<*mut SizeT>) -> Option<LuaString>;
255+
pub extern "C" fn lua_tolstring(l: LuaState, ind: c_int, len: *mut SizeT) -> LuaString;
256256

257257
/// Converts the Lua value at the given acceptable index to a C boolean value (0 or 1).
258258
/// Like all tests in Lua, lua_toboolean returns 1 for any Lua value different from false and nil; otherwise returning 0.
@@ -267,11 +267,11 @@ dyn_symbols! {
267267
/// #[gmod_open]
268268
/// fn entry(l: LuaState) -> i32 {
269269
/// lua_getglobal(l, cstr!("CurTime"));
270-
/// let curtime = lua_tocfunction(l, -1).unwrap();
270+
/// let curtime = lua_tocfunction(l, -1);
271271
/// 0
272272
/// }
273273
/// ```
274-
pub extern "C" fn lua_tocfunction(l: LuaState, idx: c_int) -> Option<LuaCFunction>;
274+
pub extern "C" fn lua_tocfunction(l: LuaState, idx: c_int) -> LuaCFunction;
275275

276276
/// Converts the Lua value at the given acceptable index to the signed integral type [LuaInteger].
277277
/// The Lua value must be a number or a string convertible to a number; otherwise, this returns 0.
@@ -286,18 +286,18 @@ dyn_symbols! {
286286
/// The value can be a userdata, a table, a thread, or a function; otherwise this returns None.
287287
/// Different objects will give different pointers.
288288
/// There is no way to convert the pointer back to its original value.
289-
pub extern "C" fn lua_topointer(l: LuaState, idx: c_int) -> Option<*mut c_void>;
289+
pub extern "C" fn lua_topointer(l: LuaState, idx: c_int) -> *mut c_void;
290290

291291
/// Converts the value at the given acceptable index to a Lua thread (represented as [LuaState]).
292292
/// This value must be a thread; otherwise, the function returns None.
293-
pub extern "C" fn lua_tothread(l: LuaState, idx: c_int) -> Option<LuaState>;
293+
pub extern "C" fn lua_tothread(l: LuaState, idx: c_int) -> LuaState;
294294

295295
/// Returns the value at the given index assuming it is a userdata.
296296
/// # Returns
297297
/// If the value at the given acceptable index is a full userdata, returns its block address.
298298
/// If the value is a light userdata, returns its pointer.
299-
/// Otherwise, returns None.
300-
pub extern "C" fn lua_touserdata(l: LuaState, idx: c_int) -> Option<*mut c_void>;
299+
/// Otherwise, returns [std::ptr::null_mut()].
300+
pub extern "C" fn lua_touserdata(l: LuaState, idx: c_int) -> *mut c_void;
301301
}
302302

303303
dyn_symbols! {
@@ -357,8 +357,8 @@ dyn_symbols! {
357357
pub extern "C" fn luaL_checknumber(l: LuaState, narg: c_int) -> LuaNumber;
358358

359359
/// Checks whether the function argument ``narg`` is a string and returns this string.
360-
/// If len is not None fills *len with the string's length.
361-
pub extern "C" fn luaL_checklstring(l: LuaState, narg: c_int, len: Option<*mut SizeT>) -> LuaString;
360+
/// If len is not [std::ptr::null_mut()] fills *len with the string's length.
361+
pub extern "C" fn luaL_checklstring(l: LuaState, narg: c_int, len: *mut SizeT) -> LuaString;
362362

363363
/// Checks whether the function has an argument of any type (including nil) at position narg.
364364
pub extern "C" fn luaL_checkany(l: LuaState, narg: c_int) -> ();
@@ -376,16 +376,16 @@ dyn_symbols! {
376376
/// Creates a new Lua state.
377377
/// This calls [lua_newstate] with an allocator based on the standard C realloc function and then sets a panic function (see lua_atpanic) that prints an error message to the standard error output in case of fatal errors.
378378
/// # Returns
379-
/// The newly created [LuaState], or None if the allocation failed (due to memory).
380-
pub extern "C" fn luaL_newstate() -> Option<LuaState>;
379+
/// The newly created [LuaState], or [std::ptr::null_mut()] if the allocation failed (due to memory).
380+
pub extern "C" fn luaL_newstate() -> LuaState;
381381

382382
/// Creates a new, independent state.
383383
/// Note you might be looking for [luaL_newstate], which has no parameters
384384
/// Returns None if cannot create the state (due to lack of memory).
385385
/// The argument f is the allocator function;
386386
/// Lua does all memory allocation for this state through this function.
387387
/// The second argument, ud, is an opaque pointer that Lua simply passes to the allocator in every call.
388-
pub extern "C" fn lua_newstate(f: LuaAlloc, ud: *mut c_void) -> Option<LuaState>;
388+
pub extern "C" fn lua_newstate(f: LuaAlloc, ud: *mut c_void) -> LuaState;
389389

390390
/// Creates a new empty table and pushes it onto the stack.
391391
/// The new table has space pre-allocated for ``narr`` array elements and ``nrec`` non-array elements.
@@ -406,11 +406,11 @@ dyn_symbols! {
406406
/// This is a C API extension to allow control of the VM from "C"
407407
/// # Parameters
408408
/// * `l` - Lua state
409-
/// * `idx` - Stack index of the function to set the mode of. None to set the mode of the entirety of luajit.
409+
/// * `idx` - Stack index of the function to set the mode of. `0` to set the mode of the entirety of luajit.
410410
/// * `mode` - The mode to set, 'or'ed with a flag from [lua::jit]
411411
/// # Returns
412412
/// 1 for success, 0 for failure.
413-
pub extern "C" fn luaJIT_setmode(l: LuaState, idx: Option<c_int>, jit_mode: c_int) -> c_int;
413+
pub extern "C" fn luaJIT_setmode(l: LuaState, idx: c_int, jit_mode: c_int) -> c_int;
414414
}
415415

416416
// Coroutines
@@ -569,8 +569,8 @@ dyn_symbols! {
569569
/// If the function argument narg is a string, returns this string.
570570
/// If this argument is absent or is nil, returns ``default``. Otherwise, raises an error.
571571
///
572-
/// If ``len`` is not None, fills the position *``len`` with the results's length.
573-
pub extern "C" fn luaL_optlstring(l: LuaState, arg: c_int, default: LuaString, len: Option<*mut SizeT>)
572+
/// If ``len`` is not nullptr, fills the position *``len`` with the results's length.
573+
pub extern "C" fn luaL_optlstring(l: LuaState, arg: c_int, default: LuaString, len: *mut SizeT)
574574
-> LuaString;
575575

576576
/// If the function argument ``arg`` is a number, returns this number.
@@ -582,24 +582,24 @@ dyn_symbols! {
582582
// x / ref functions
583583
/// Converts the Lua value at the given index to the signed integral type [LuaInteger].
584584
/// The Lua value must be an integer, or a number or string convertible to an integer; otherwise, this returns 0.
585-
/// If ``isnum`` is not [None], its referent is assigned a boolean value that indicates whether the operation succeeded.
586-
pub extern "C" fn lua_tointegerx(l: LuaState, index: c_int, isnum: Option<*mut c_int>) -> LuaInteger;
585+
/// If ``isnum`` is not [std::ptr::null_mut()], its referent is assigned a boolean value that indicates whether the operation succeeded.
586+
pub extern "C" fn lua_tointegerx(l: LuaState, index: c_int, isnum: *mut c_int) -> LuaInteger;
587587

588588

589589
/// Converts the Lua value at the given index to a [LuaNumber].
590590
/// The Lua value must be a number or a string convertible to a number; otherwise, this returns 0.
591-
/// If ``isnum`` is not None, its referent is assigned a boolean value that indicates whether the operation succeeded.
592-
pub extern "C" fn lua_tonumberx(l: LuaState, index: c_int, isnum: Option<*mut c_int>) -> LuaNumber;
591+
/// If ``isnum`` is not [std::ptr::null_mut()], its referent is assigned a boolean value that indicates whether the operation succeeded.
592+
pub extern "C" fn lua_tonumberx(l: LuaState, index: c_int, isnum: *mut c_int) -> LuaNumber;
593593
}
594594

595595
dyn_symbols! {
596596
/// Creates and pushes a traceback of the stack L1.
597-
/// If msg is not None it is appended at the beginning of the traceback.
597+
/// If msg is not [std::ptr::null_mut()] it is appended at the beginning of the traceback.
598598
/// The level parameter tells at which level to start the traceback.
599599
pub extern "C" fn luaL_traceback(
600600
l: LuaState,
601601
state1: LuaState,
602-
msg: Option<LuaString>,
602+
msg: LuaString,
603603
level: c_int,
604604
) -> ();
605605

@@ -919,10 +919,11 @@ lua_macros! {
919919
lua_pushcclosure(l, fnc, 0);
920920
};
921921

922-
/// Equivalent to ``lua_tolstring(l, idx, None)``
923-
/// This may return None if the value at ``idx`` is not a string or a number, use [luaL_optstring] instead if you do not desire an Option<>, or lua_checkstring for arguments.
924-
pub fn lua_tostring(l: LuaState, idx: c_int) -> Option<LuaString> {
925-
lua_tolstring(l, idx, None)
922+
/// Equivalent to ``lua_tolstring(l, idx, [std::ptr::null_mut()])``
923+
/// This may return None if the value at ``idx`` is not a string or a number.
924+
/// You should use [luaL_optstring] instead if you are unsure of the value, or [luaL_checkstring] for function arguments.
925+
pub fn lua_tostring(l: LuaState, idx: c_int) -> LuaString {
926+
lua_tolstring(l, idx, std::ptr::null_mut())
926927
};
927928

928929
/// Starts and resumes a coroutine in a given thread
@@ -981,7 +982,7 @@ lua_macros! {
981982
/// Returns if the code was successfully executed
982983
/// Error will be left on the stack if the code failed to execute
983984
pub fn luaL_dofile(l: LuaState, filename: LuaString) -> bool {
984-
luaL_loadfile(l, Some(filename)) == 0 || lua_pcall(l, 0, lua::MULTRET, 0) == 0
985+
luaL_loadfile(l, filename) == 0 || lua_pcall(l, 0, lua::MULTRET, 0) == 0
985986
};
986987

987988
/// Returns value at [crate::lua::REGISTRYINDEX] with name 'name'
@@ -1003,13 +1004,13 @@ lua_macros! {
10031004

10041005
/// Asserts that a string argument exists at index 'i'
10051006
pub fn luaL_checkstring(l: LuaState, i: c_int) -> LuaString {
1006-
luaL_checklstring(l, i, None)
1007+
luaL_checklstring(l, i, std::ptr::null_mut())
10071008
};
10081009

10091010
/// Like lua_tostring or luaL_checkstring, but instead of returning an invalid string / erroring,
10101011
/// It returns the given `default` string.
10111012
pub fn luaL_optstring(l: LuaState, i: c_int, default: LuaString) -> LuaString {
1012-
luaL_optlstring(l, i, default, None)
1013+
luaL_optlstring(l, i, default, std::ptr::null_mut())
10131014
};
10141015

10151016
/// Sets the C function ``f`` as the value of global name ``name``.
@@ -1113,7 +1114,7 @@ pub fn luaL_testudata(l: LuaState, arg: c_int, tname: LuaString) -> Option<*mut
11131114
lua_getmetatable(l, arg); // Object metatable
11141115
luaL_getmetatable(l, tname); // Desired global metatable
11151116
if lua_rawequal(l, -1, -2) == 1 {
1116-
return lua_touserdata(l, arg).map(|ud| ud as *mut super::Userdata);
1117+
return Some(lua_touserdata(l, arg) as *mut super::Userdata);
11171118
}
11181119
}
11191120
None

rglua/src/util/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn dump_stack(l: LuaState) -> Result<String, std::fmt::Error> {
206206
write!(&mut buf, "[{}] '{}' = ", i, rstr!(luaL_typename(l, i)));
207207
match lua_type(l, i) {
208208
TNUMBER => write!(&mut buf, "{}", lua_tonumber(l, i)),
209-
TSTRING => write!(&mut buf, "{}", rstr!(lua_tostring(l, i).unwrap())),
209+
TSTRING => write!(&mut buf, "{}", rstr!(lua_tostring(l, i))),
210210
TBOOLEAN => write!(
211211
&mut buf,
212212
"{}",
@@ -218,8 +218,8 @@ pub fn dump_stack(l: LuaState) -> Result<String, std::fmt::Error> {
218218
),
219219
TNIL => write!(&mut buf, "nil"),
220220
TNONE => write!(&mut buf, "none"),
221-
TUSERDATA | TLIGHTUSERDATA => write!(&mut buf, "{:p}", lua_touserdata(l, i).unwrap()),
222-
TTHREAD => write!(&mut buf, "{:p}", lua_tothread(l, i).unwrap()),
221+
TUSERDATA | TLIGHTUSERDATA => write!(&mut buf, "{:p}", lua_touserdata(l, i)),
222+
TTHREAD => write!(&mut buf, "{:p}", lua_tothread(l, i)),
223223
_ => write!(&mut buf, "Unknown type"),
224224
}?
225225
}

0 commit comments

Comments
 (0)