Skip to content

Commit fd245da

Browse files
authored
Expose RawLua via Lua::exec_raw_lua (#670)
1 parent 7fb7e86 commit fd245da

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/state.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,38 @@ impl Lua {
337337
R::from_stack_multi(nresults, &lua)
338338
}
339339

340+
/// Runs callback with the inner RawLua value. It can be used to manually push and get values on the stack.
341+
///
342+
/// This function is safe because all unsafe actions with RawLua can only be done with unsafe
343+
///
344+
/// # Example
345+
/// ```
346+
/// # use mlua::{Lua, Result, FromLua, IntoLua};
347+
/// # fn main() -> Result<()> {
348+
/// let lua = Lua::new();
349+
/// let n: i32 = {
350+
/// let num = 11i32;
351+
/// lua.exec_raw_lua(|lua| {
352+
/// unsafe {
353+
/// <i32 as IntoLua>::push_into_stack(num, lua)?;
354+
/// }
355+
///
356+
/// let n = unsafe {
357+
/// <i32 as FromLua>::from_stack(-1, lua)?
358+
/// };
359+
/// Result::Ok(n)
360+
/// })
361+
/// }?;
362+
/// assert_eq!(n, 11);
363+
/// # Ok(())
364+
/// # }
365+
/// ```
366+
#[doc(hidden)]
367+
pub fn exec_raw_lua<R>(&self, f: impl FnOnce(&RawLua) -> R) -> R {
368+
let lua = self.lock();
369+
f(&lua)
370+
}
371+
340372
/// Loads the specified subset of the standard libraries into an existing Lua state.
341373
///
342374
/// Use the [`StdLib`] flags to specify the libraries you want to load.

0 commit comments

Comments
 (0)