Skip to content

Instantly share code, notes, and snippets.

@Ameliorate
Created March 30, 2016 22:22
Show Gist options
  • Select an option

  • Save Ameliorate/4a652272fbae858959e4fa5252853417 to your computer and use it in GitHub Desktop.

Select an option

Save Ameliorate/4a652272fbae858959e4fa5252853417 to your computer and use it in GitHub Desktop.
Hlua no functions with arguments workaround
/// Call the given lua function in the prelude table with the given arguments.
pub fn call_fn(interpreter: &mut Lua,
fn_to_call: &str,
args: Vec<AnyLuaValue>)
-> Result<Option<AnyLuaValue>, LuaError> {
let mut prelude_table: LuaTable<_> = interpreter
.get("prelude")
.expect("The prelude_table wasn't found. Was \
the prelude properly loaded?");
prelude_table.set("fn_to_call", fn_to_call);
prelude_table.set("args", args);
{
let mut call_fn_lua: LuaFunction<_> = prelude_table.get("call_fn")
.expect("prelude.call_prelude_fn not \
found. Was the prelude \
properly loaded?");
try!(call_fn_lua.call::<()>());
}
let ret: Option<AnyLuaValue> = prelude_table.get("ret");
Ok(ret)
}
prelude = {}
function prelude.call_fn ()
-- Assumes that prelude.fn_to_call is a string pointing to the function to call, and
-- prelude.args is the arguments to that function. The return value of the function is then placed in
-- prelude.ret.
-- This function is used to call functions with arguments in rust,
-- since it isn't exposed in hlua.
if next(prelude.args) ~= nil then
local ret = _G[prelude.fn_to_call](unpack(prelude.args))
else
local ret = _G[prelude.fn_to_call]()
end
prelude.ret = ret
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment