From 0f80d29da5889051c652f93822ee629730ea7cf2 Mon Sep 17 00:00:00 2001 From: Y0SH1M4S73R Date: Fri, 2 Sep 2022 21:52:15 -0400 Subject: [PATCH] 1.3.0 [release] --- CHANGELOG.md | 6 ++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 294288e..1c286c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [1.3.0] + +### Added + +- Adds the `loadstring` function. It compiles a string into lua code - if the code compiles, `loadstring` will return a function that runs that code; otherwise, it returns `nil` and the error that prevented the code from compiling. + ## [1.2.1] ### Fixed diff --git a/Cargo.lock b/Cargo.lock index e716271..e2bfbe9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "auxlua" -version = "1.2.1" +version = "1.3.0" dependencies = [ "auxtools", "mlua", diff --git a/Cargo.toml b/Cargo.toml index b2214ea..b34ef0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "auxlua" -version = "1.2.1" +version = "1.3.0" authors = ["Y0SH1M4S73R "] edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index 2770339..3deb2a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,6 +147,16 @@ fn print(lua: &Lua, args: MultiValue) -> mlua::Result<()> { }) } +fn loadstring(state: &Lua, mut code: String) -> mlua::Result<(mlua::Value, mlua::Value)> { + if code.is_empty() { + code = String::from(" "); //mlua bug workaround + } + match state.load(&code).into_function() { + Ok(f) => Ok((mlua::Value::Function(f), mlua::Nil)), + Err(e) => Ok((mlua::Nil, mlua::Value::Error(e))), + } +} + /// Applies auxlua-specific data structures to the lua state fn apply_state_vars(state: &Lua, id: String) -> DMResult<()> { let globals = state.globals(); @@ -273,6 +283,14 @@ fn apply_state_vars(state: &Lua, id: String) -> DMResult<()> { .raw_set("print", print) .map_err(|e| specific_runtime!("{}", e))?; + // Set loadstring + let loadstring = state + .create_function(loadstring) + .map_err(|e| specific_runtime!("{}", e))?; + globals + .raw_set("loadstring", loadstring) + .map_err(|e| specific_runtime!("{}", e))?; + let global_metatable = state .create_table() .map_err(|e| specific_runtime!("{}", e))?;