diff --git a/crates/lune-std/src/globals/version.rs b/crates/lune-std/src/globals/version.rs index 29b4359..d1ff455 100644 --- a/crates/lune-std/src/globals/version.rs +++ b/crates/lune-std/src/globals/version.rs @@ -3,6 +3,6 @@ use mlua::prelude::*; use lune_utils::get_version_string; pub fn create(lua: &Lua) -> LuaResult { - let s = get_version_string().to_string(); + let s = get_version_string(env!("CARGO_PKG_VERSION")); lua.create_string(s)?.into_lua(lua) } diff --git a/crates/lune-std/src/lib.rs b/crates/lune-std/src/lib.rs index be7ff9b..f5d5c81 100644 --- a/crates/lune-std/src/lib.rs +++ b/crates/lune-std/src/lib.rs @@ -1,8 +1,27 @@ #![allow(clippy::cargo_common_metadata)] +use mlua::prelude::*; + mod global; mod globals; mod library; pub use self::global::LuneStandardGlobal; pub use self::library::LuneStandardLibrary; + +/** + Injects all standard globals into the given Lua state / VM. + + This includes all enabled standard libraries, which can + be used from Lua with `require("@lune/library-name")`. + + # Errors + + Errors when out of memory, or if *default* Lua globals are missing. +*/ +pub fn inject_globals(lua: &Lua) -> LuaResult<()> { + for global in LuneStandardGlobal::ALL { + lua.globals().set(global.name(), global.create(lua)?)?; + } + Ok(()) +} diff --git a/crates/lune-utils/src/version_string.rs b/crates/lune-utils/src/version_string.rs index 73ba3a0..6c4bbcf 100644 --- a/crates/lune-utils/src/version_string.rs +++ b/crates/lune-utils/src/version_string.rs @@ -3,20 +3,32 @@ use std::sync::Arc; use mlua::prelude::*; use once_cell::sync::Lazy; -static VERSION_STRING: Lazy> = Lazy::new(create_version_string); +static LUAU_VERSION: Lazy> = Lazy::new(create_luau_version_string); /** - Returns the current Lune version string, in the format `Lune x.y.z+luau`. + Returns a Lune version string, in the format `Lune x.y.z+luau`. - This version string is strongly guaranteed to follow the above - format and may safely be used for parsing & version comparisons. + 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. */ #[must_use] -pub fn get_version_string() -> Arc { - Arc::clone(&VERSION_STRING) +pub fn get_version_string(lune_version: impl AsRef) -> String { + let lune_version = lune_version.as_ref(); + + assert!(!lune_version.is_empty(), "Lune version string is empty"); + assert!( + lune_version.chars().all(is_valid_version_char), + "Lune version string contains invalid characters" + ); + + format!("Lune {lune_version}+{}", *LUAU_VERSION) } -fn create_version_string() -> Arc { +fn create_luau_version_string() -> Arc { // Extract the current Luau version from a fresh Lua state / VM that can't be accessed externally. let luau_version_full = { let temp_lua = Lua::new(); @@ -55,10 +67,7 @@ fn create_version_string() -> Arc { ) } - Arc::new(format!( - "Lune {}+{luau_version_noprefix}", - env!("CARGO_PKG_VERSION") - )) + luau_version_noprefix.to_string().into() } fn is_valid_version_char(c: char) -> bool {