luafuncs macro

This commit is contained in:
Dane Johnson 2022-04-11 17:41:18 -05:00
parent 860654d4ba
commit 99f100c812
2 changed files with 20 additions and 4 deletions

View File

@ -3,6 +3,7 @@ print("Hello from a lua script!")
function init()
print("Hello from \"init\"")
couch.debug("init")
couch.say_hello("dane", "Hi!")
end
function update(delta)

View File

@ -19,6 +19,7 @@ impl Lua {
// Create the ``couch'' api
let couch = ctx.create_table().unwrap();
couch.set("debug", ctx.create_function(debug).unwrap()).unwrap();
couch.set("say_hello", ctx.create_function(say_hello).unwrap()).unwrap();
// Hook in to globals
ctx.globals().set("couch", couch).unwrap();
let path = std::path::Path::new("main.lua");
@ -43,8 +44,22 @@ impl ScriptLang for Lua {
}
}
fn debug(_: &LuaContext, msg: String) -> Result<(), mlua::Error> {
println!("Couch Debug Message: {}", msg);
Ok(())
macro_rules! luafuncs {
($($name:ident($($arg:ident: $type: ty),*) $body:block),*) => {
$(
fn $name(_: &LuaContext, ($($arg),*,): ($($type),*,)) -> Result<(), mlua::Error> $body
)*
};
}
luafuncs!{
debug(msg: String) {
println!("Couch Debug Message: {}", msg);
Ok(())
},
say_hello(name: String, msg: String) {
println!("Couch says {} to {}", msg, name);
Ok(())
}
}