pub struct LuaFunction { /* private fields */ }
Expand description
Reperesents a callable function on the Lua stack
Functions that can be called may be defined in Lua, Rust, C, or any other language with Lua bindings.
§Examples
context.do_string("function double(x) return x * 2 end").unwrap();
let lua_double: LuaFunction = context.push_global("double")
.get_value(&mut context).unwrap();
let result = lua_double.call_singleret(&mut context, &[&4])
.and_then(|v| v.get_value(&mut context));
assert_eq!(result, Some(8));
Implementations§
Source§impl LuaFunction
impl LuaFunction
Sourcepub fn new(i: Index) -> LuaFunction
pub fn new(i: Index) -> LuaFunction
Create a new LuaFunction given an index
Sourcepub fn pcall(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
errfunc: Option<&LuaFunction>,
nresults: i32,
) -> Result<Vec<LuaGeneric>>
pub fn pcall( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, nresults: i32, ) -> Result<Vec<LuaGeneric>>
Protected function call, errors return an Err If the function call is successful, returns a list of all return values (with a maximum size nresults)
§Errors
Returns an Error if Lua encounters a runtime error while running this function. If an errfunc is given, then the given function will be called with the error message as an argument, and the function should return a new error message. errfunc will not be called if no error was encountered, the error that occured was an out of memeory error, or if another error occurred while running errfunc.
Sourcepub fn pcall_multiret(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
errfunc: Option<&LuaFunction>,
) -> Result<Vec<LuaGeneric>>
pub fn pcall_multiret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<Vec<LuaGeneric>>
Same as pcall, but returns all return values
Sourcepub fn pcall_singleret(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
errfunc: Option<&LuaFunction>,
) -> Result<Option<LuaGeneric>>
pub fn pcall_singleret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<Option<LuaGeneric>>
Same as pcall, but only returns at most one return value.
Sourcepub fn pcall_noret(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
errfunc: Option<&LuaFunction>,
) -> Result<()>
pub fn pcall_noret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], errfunc: Option<&LuaFunction>, ) -> Result<()>
Same as pcall, but returns nothing.
Sourcepub fn call(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
nresults: i32,
) -> Vec<LuaGeneric>
pub fn call( &self, context: &mut Context<'_>, args: &[&dyn ToLua], nresults: i32, ) -> Vec<LuaGeneric>
Call this function, returns a list of return values (with a maximum size nresults).
§Panics
This function will panic if Lua encounters a runtime error. It’s not really a panic, it’s actually a longjmp, but it might as well be a panic.
Sourcepub fn call_singleret(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
) -> Option<LuaGeneric>
pub fn call_singleret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], ) -> Option<LuaGeneric>
Same as call, but returns at most one return value.
Sourcepub fn call_multiret(
&self,
context: &mut Context<'_>,
args: &[&dyn ToLua],
) -> Vec<LuaGeneric>
pub fn call_multiret( &self, context: &mut Context<'_>, args: &[&dyn ToLua], ) -> Vec<LuaGeneric>
Same as call, but returns all return values.
Sourcepub fn call_noret(&self, context: &mut Context<'_>, args: &[&dyn ToLua])
pub fn call_noret(&self, context: &mut Context<'_>, args: &[&dyn ToLua])
Same as call, but does not return any return values.