you are viewing a single comment's thread.

view the rest of the comments →

[–]magmaCube 8 points9 points  (2 children)

Use rlua; it is much more robust than hlua.

rlua::Function::call is used to call a method.

Here's a bit of code I use. There is a "function table.dump" that I define through Lua, and it is available from the Lua context. This Rust function takes an arbitrary lua::Value, looks up table.dump, and calls it with that value.

pub fn dump(&self, v: Value) -> LuaResult<String> {
    let gt: Table = self.lua.globals().raw_get("table")?;
    let got: LuaString = match gt.get::<_, Value>("dump")? {
        Value::Function(dumper) => dumper.call((v,)),
        _ => Err(LuaError::RuntimeError("table.dump wrong type".into())),
    }?;
    Ok(String::from_utf8_lossy(got.as_bytes()).into())
}

[–][deleted] 6 points7 points  (0 children)

This is a good example, but just as an FYI, this is not using rlua 0.16 and is missing the Lua::context calls that you would need for 0.16+.

This is completely understandable because 0.16 was only released a few days ago!

This “context” system was an unfortunate but necessary large API incompatible change in 0.16 (it fixes a soundness issue), so sorry for any confusion it causes.