Require lua script from rust #525
-
I want to know how to require("aluascript") inside rust. From the readme, it seems like the reverse* is possible. from lua calling rust code as a module. The reason I want to allow custom scripting as per execution by user. Any help is very useful. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Given an MLua instance let spec: LuaString = lua.create_string("module.spec")?;
let require: LuaFunction = lua.globals().get("require")?;
let my_module: LuaTable = require.call::<LuaTable>(spec)?; Obviously this presumes the Lua module you are loading returns a table. You may need to adjust that if it returns a function or other value type. |
Beta Was this translation helpful? Give feedback.
-
I see.so then I can do something like a original lua code
to
let me try it, |
Beta Was this translation helpful? Give feedback.
Not quite. You have to keep going in Rust syntax not Lua. At the end of that sequence
my_module
will be of typeLuaTable
on the Rust side. You can call functions in that table, but you're going to need to first.get()
them (just like I did to get therequire()
function out of Lua and bring it over to a Rust handle I can.call()
. That or you need to use the method call functions on theLuaTable
type.