rtk_lua/
lib.rs

1//! This crate implements the Lua scripting engine for RTK that defines how users can implement RTK
2//! systems for their own languages.
3
4mod api;
5mod ext;
6mod macros;
7mod versioning;
8
9use anyhow::Context;
10pub use api::{
11    Attribute, ClosureTypeValue, EnumTypeValue, EnumTypeValueVariant, FunctionCall,
12    FunctionTypeValue, Location, MethodCall, MethodCallQuery, RtkLuaScriptExecutor,
13    StructTypeValue, StructTypeValueField, TraitImpl, TypeValue, Value,
14};
15pub use mlua::Either;
16use mlua::{LuaOptions, StdLib};
17pub use versioning::RtkRustcDriverVersion;
18
19pub struct RtkLua {
20    lua: mlua::Lua,
21}
22
23impl RtkLua {
24    pub fn new(exec: impl RtkLuaScriptExecutor) -> anyhow::Result<Self> {
25        let lua = unsafe { mlua::Lua::unsafe_new_with(StdLib::ALL, LuaOptions::new()) };
26
27        let api = lua.create_table().context("failed to create api table")?;
28        api::inject(&lua, &api, exec).context("failed to inject api into table")?;
29
30        lua.globals()
31            .set("rtk", api)
32            .context("failed to set rtk api in preload")?;
33
34        Ok(RtkLua { lua })
35    }
36
37    pub fn execute(&self, script: &str) -> anyhow::Result<()> {
38        self.lua.load(script).exec()?;
39
40        Ok(())
41    }
42}