Skip to content

Instantly share code, notes, and snippets.

@ssghost
Last active December 17, 2024 06:09
Show Gist options
  • Save ssghost/ae75ad25dfe98f4c86b94a69babf6764 to your computer and use it in GitHub Desktop.
Save ssghost/ae75ad25dfe98f4c86b94a69babf6764 to your computer and use it in GitHub Desktop.
use mlua::{IntoLua, Lua, Value};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let lua = Lua::new();
let utils = lua.create_table()?;
let double_fn = lua.create_function(|lua: &Lua, arg_0: i64| {
let res = arg_0 * 2;
res.into_lua(lua)
})?;
fn add_fn(lua: &Lua, (arg_0, arg_1): (i64, i64)) -> mlua::Result<Value> {
let res = arg_0 + arg_1;
res.into_lua(lua)
}
utils.set("double_stuff", double_fn)?;
utils.set("add_stuff", lua.create_function(add_fn)?)?;
lua.globals().set("utils", utils)?;
let chunk = lua.load(
r#"
local num = 123
local stuff = utils.double_stuff(1000)
local sum = utils.add_stuff(10000, 30000)
return "" .. num .. " " .. stuff .. " " .. sum
"#,
);
let res = chunk.eval::<Value>()?;
println!("->> {res:?}");
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment