diff --git a/main.lua b/main.lua index b56179e..d59fa2d 100644 --- a/main.lua +++ b/main.lua @@ -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) diff --git a/src/scripting.rs b/src/scripting.rs index a1ef9f8..3310c0d 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -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(()) + } } -