diff --git a/crates/lune-std/src/globals/version.rs b/crates/lune-std/src/globals/version.rs index d1ff455..3eface4 100644 --- a/crates/lune-std/src/globals/version.rs +++ b/crates/lune-std/src/globals/version.rs @@ -2,7 +2,34 @@ use mlua::prelude::*; use lune_utils::get_version_string; +struct Version(String); + +impl LuaUserData for Version {} + pub fn create(lua: &Lua) -> LuaResult { - let s = get_version_string(env!("CARGO_PKG_VERSION")); + let v = match lua.app_data_ref::() { + Some(v) => v.0.to_string(), + None => env!("CARGO_PKG_VERSION").to_string(), + }; + let s = get_version_string(v); lua.create_string(s)?.into_lua(lua) } + +/** + Overrides the version string to be used by the `_VERSION` global. + + The global will be a string in the format `Lune x.y.z+luau`, + where `x.y.z` is the string passed to this function. + + The version string passed should be the version of the Lune runtime, + obtained from `env!("CARGO_PKG_VERSION")` or a similar mechanism. + + # Panics + + Panics if the version string is empty or contains invalid characters. +*/ +pub fn set_global_version(lua: &Lua, version: impl Into) { + let v = version.into(); + let _ = get_version_string(&v); // Validate version string + lua.set_app_data(Version(v)); +} diff --git a/crates/lune-std/src/lib.rs b/crates/lune-std/src/lib.rs index f5d5c81..4f64427 100644 --- a/crates/lune-std/src/lib.rs +++ b/crates/lune-std/src/lib.rs @@ -7,6 +7,7 @@ mod globals; mod library; pub use self::global::LuneStandardGlobal; +pub use self::globals::version::set_global_version; pub use self::library::LuneStandardLibrary; /**