diff --git a/packages/lib-roblox/src/datatypes/types/mod.rs b/packages/lib-roblox/src/datatypes/types/mod.rs index 6c6250d..394b210 100644 --- a/packages/lib-roblox/src/datatypes/types/mod.rs +++ b/packages/lib-roblox/src/datatypes/types/mod.rs @@ -49,71 +49,3 @@ pub use vector2::Vector2; pub use vector2int16::Vector2int16; pub use vector3::Vector3; pub use vector3int16::Vector3int16; - -#[cfg(test)] -mod tests { - use std::{env::set_current_dir, fs::read_to_string, path::PathBuf}; - - use anyhow::{Context, Result}; - use mlua::prelude::*; - - use crate::make_all_datatypes; - - macro_rules! create_tests { - ($($test_name:ident: $file_path:expr,)*) => { $( - #[test] - fn $test_name() -> Result<()> { - // NOTE: This path is relative to the lib - // package, not the cwd or workspace root, - // so we need to cd to the repo root first - let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let root_dir = crate_dir.join("../../").canonicalize()?; - set_current_dir(root_dir)?; - // Create all datatypes as globals - let lua = Lua::new(); - let env = lua.globals(); - for (name, tab) in make_all_datatypes(&lua)? { - env.set(name, tab)?; - } - // The rest of the test logic can continue as normal - let full_name = format!("tests/roblox/{}.luau", $file_path); - let script = read_to_string(full_name) - .with_context(|| format!( - "Failed to read test file '{}'", - $file_path - ))?; - lua.load(&script) - .set_name($file_path)? - .set_environment(env)? - .exec()?; - Ok(()) - } - )* } - } - - create_tests! { - axes: "datatypes/Axes", - brick_color: "datatypes/BrickColor", - cframe: "datatypes/CFrame", - color3: "datatypes/Color3", - color_sequence: "datatypes/ColorSequence", - color_sequence_keypoint: "datatypes/ColorSequenceKeypoint", - r#enum: "datatypes/Enum", - faces: "datatypes/Faces", - font: "datatypes/Font", - number_range: "datatypes/NumberRange", - number_sequence: "datatypes/NumberSequence", - number_sequence_keypoint: "datatypes/NumberSequenceKeypoint", - physical_properties: "datatypes/PhysicalProperties", - ray: "datatypes/Ray", - rect: "datatypes/Rect", - udim: "datatypes/UDim", - udim2: "datatypes/UDim2", - region3: "datatypes/Region3", - region3int16: "datatypes/Region3int16", - vector2: "datatypes/Vector2", - vector2int16: "datatypes/Vector2int16", - vector3: "datatypes/Vector3", - vector3int16: "datatypes/Vector3int16", - } -} diff --git a/packages/lib-roblox/src/lib.rs b/packages/lib-roblox/src/lib.rs index 5d8c3df..bb30f72 100644 --- a/packages/lib-roblox/src/lib.rs +++ b/packages/lib-roblox/src/lib.rs @@ -66,5 +66,6 @@ pub fn module(lua: &Lua) -> LuaResult { for (name, tab) in make_all_datatypes(lua)? { exports.set(name, tab)?; } + exports.set_readonly(true); Ok(exports) } diff --git a/packages/lib/Cargo.toml b/packages/lib/Cargo.toml index 652ab21..79f99d5 100644 --- a/packages/lib/Cargo.toml +++ b/packages/lib/Cargo.toml @@ -14,7 +14,7 @@ name = "lune" path = "src/lib.rs" [features] -default = [] +default = ["roblox"] roblox = ["dep:lune-roblox"] [dependencies] diff --git a/packages/lib/src/globals/mod.rs b/packages/lib/src/globals/mod.rs index ed2b79a..3172956 100644 --- a/packages/lib/src/globals/mod.rs +++ b/packages/lib/src/globals/mod.rs @@ -4,6 +4,8 @@ mod fs; mod net; mod process; mod require; +#[cfg(feature = "roblox")] +mod roblox; mod serde; mod stdio; mod task; @@ -17,6 +19,8 @@ pub fn create(lua: &'static Lua, args: Vec) -> LuaResult<()> { ("fs", fs::create(lua)?), ("net", net::create(lua)?), ("process", process::create(lua, args)?), + #[cfg(feature = "roblox")] + ("roblox", roblox::create(lua)?), ("serde", self::serde::create(lua)?), ("stdio", stdio::create(lua)?), ("task", task::create(lua)?), diff --git a/packages/lib/src/globals/roblox.rs b/packages/lib/src/globals/roblox.rs new file mode 100644 index 0000000..e99ee36 --- /dev/null +++ b/packages/lib/src/globals/roblox.rs @@ -0,0 +1,15 @@ +use mlua::prelude::*; + +use crate::lua::table::TableBuilder; + +pub fn create(lua: &'static Lua) -> LuaResult { + let mut roblox_constants = Vec::new(); + let roblox_module = lune_roblox::module(lua)?; + for pair in roblox_module.pairs::() { + roblox_constants.push(pair?); + } + // TODO: Add async functions for reading & writing documents, creating instances + TableBuilder::new(lua)? + .with_values(roblox_constants)? + .build_readonly() +} diff --git a/packages/lib/src/tests.rs b/packages/lib/src/tests.rs index 3c8bcda..c6f3c64 100644 --- a/packages/lib/src/tests.rs +++ b/packages/lib/src/tests.rs @@ -90,3 +90,30 @@ create_tests! { task_spawn: "task/spawn", task_wait: "task/wait", } + +#[cfg(feature = "roblox")] +create_tests! { + roblox_axes: "roblox/datatypes/Axes", + roblox_brick_color: "roblox/datatypes/BrickColor", + roblox_cframe: "roblox/datatypes/CFrame", + roblox_color3: "roblox/datatypes/Color3", + roblox_color_sequence: "roblox/datatypes/ColorSequence", + roblox_color_sequence_keypoint: "roblox/datatypes/ColorSequenceKeypoint", + roblox_enum: "roblox/datatypes/Enum", + roblox_faces: "roblox/datatypes/Faces", + roblox_font: "roblox/datatypes/Font", + roblox_number_range: "roblox/datatypes/NumberRange", + roblox_number_sequence: "roblox/datatypes/NumberSequence", + roblox_number_sequence_keypoint: "roblox/datatypes/NumberSequenceKeypoint", + roblox_physical_properties: "roblox/datatypes/PhysicalProperties", + roblox_ray: "roblox/datatypes/Ray", + roblox_rect: "roblox/datatypes/Rect", + roblox_udim: "roblox/datatypes/UDim", + roblox_udim2: "roblox/datatypes/UDim2", + roblox_region3: "roblox/datatypes/Region3", + roblox_region3int16: "roblox/datatypes/Region3int16", + roblox_vector2: "roblox/datatypes/Vector2", + roblox_vector2int16: "roblox/datatypes/Vector2int16", + roblox_vector3: "roblox/datatypes/Vector3", + roblox_vector3int16: "roblox/datatypes/Vector3int16", +} diff --git a/tests/roblox/datatypes/Axes.luau b/tests/roblox/datatypes/Axes.luau index 2694657..5ae0052 100644 --- a/tests/roblox/datatypes/Axes.luau +++ b/tests/roblox/datatypes/Axes.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Axes = _G.Axes -local Enum = _G.Enum +local roblox = require("@lune/roblox") :: any +local Axes = roblox.Axes +local Enum = roblox.Enum -- Constructors & properties diff --git a/tests/roblox/datatypes/BrickColor.luau b/tests/roblox/datatypes/BrickColor.luau index 917601e..3f2cb26 100644 --- a/tests/roblox/datatypes/BrickColor.luau +++ b/tests/roblox/datatypes/BrickColor.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local BrickColor = _G.BrickColor -local Color3 = _G.Color3 +local roblox = require("@lune/roblox") :: any +local BrickColor = roblox.BrickColor +local Color3 = roblox.Color3 -- Constructors & properties diff --git a/tests/roblox/datatypes/CFrame.luau b/tests/roblox/datatypes/CFrame.luau index 75232f7..8d5b094 100644 --- a/tests/roblox/datatypes/CFrame.luau +++ b/tests/roblox/datatypes/CFrame.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local CFrame = _G.CFrame -local Vector3 = _G.Vector3 +local roblox = require("@lune/roblox") :: any +local CFrame = roblox.CFrame +local Vector3 = roblox.Vector3 -- Constructors & properties diff --git a/tests/roblox/datatypes/Color3.luau b/tests/roblox/datatypes/Color3.luau index 7d4c13c..3e9f75a 100644 --- a/tests/roblox/datatypes/Color3.luau +++ b/tests/roblox/datatypes/Color3.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Color3 = _G.Color3 +local roblox = require("@lune/roblox") :: any +local Color3 = roblox.Color3 -- Constructors & properties diff --git a/tests/roblox/datatypes/ColorSequence.luau b/tests/roblox/datatypes/ColorSequence.luau index bae5cd7..4688ddc 100644 --- a/tests/roblox/datatypes/ColorSequence.luau +++ b/tests/roblox/datatypes/ColorSequence.luau @@ -1,8 +1,7 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Color3 = _G.Color3 -local ColorSequence = _G.ColorSequence -local ColorSequenceKeypoint = _G.ColorSequenceKeypoint +local roblox = require("@lune/roblox") :: any +local Color3 = roblox.Color3 +local ColorSequence = roblox.ColorSequence +local ColorSequenceKeypoint = roblox.ColorSequenceKeypoint -- Constructors & properties diff --git a/tests/roblox/datatypes/ColorSequenceKeypoint.luau b/tests/roblox/datatypes/ColorSequenceKeypoint.luau index d43560f..5b04ca1 100644 --- a/tests/roblox/datatypes/ColorSequenceKeypoint.luau +++ b/tests/roblox/datatypes/ColorSequenceKeypoint.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Color3 = _G.Color3 -local ColorSequenceKeypoint = _G.ColorSequenceKeypoint +local roblox = require("@lune/roblox") :: any +local Color3 = roblox.Color3 +local ColorSequenceKeypoint = roblox.ColorSequenceKeypoint -- Constructors & properties diff --git a/tests/roblox/datatypes/Enum.luau b/tests/roblox/datatypes/Enum.luau index 83c256a..0681f18 100644 --- a/tests/roblox/datatypes/Enum.luau +++ b/tests/roblox/datatypes/Enum.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Enum = _G.Enum +local roblox = require("@lune/roblox") :: any +local Enum = roblox.Enum -- Constructors & properties diff --git a/tests/roblox/datatypes/Faces.luau b/tests/roblox/datatypes/Faces.luau index b953f4b..fa37f51 100644 --- a/tests/roblox/datatypes/Faces.luau +++ b/tests/roblox/datatypes/Faces.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Faces = _G.Faces -local Enum = _G.Enum +local roblox = require("@lune/roblox") :: any +local Faces = roblox.Faces +local Enum = roblox.Enum -- Constructors & properties diff --git a/tests/roblox/datatypes/Font.luau b/tests/roblox/datatypes/Font.luau index b98d94c..0f824ea 100644 --- a/tests/roblox/datatypes/Font.luau +++ b/tests/roblox/datatypes/Font.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Enum = _G.Enum -local Font = _G.Font +local roblox = require("@lune/roblox") :: any +local Enum = roblox.Enum +local Font = roblox.Font -- Constructors diff --git a/tests/roblox/datatypes/NumberRange.luau b/tests/roblox/datatypes/NumberRange.luau index 455d627..fe90b25 100644 --- a/tests/roblox/datatypes/NumberRange.luau +++ b/tests/roblox/datatypes/NumberRange.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local NumberRange = _G.NumberRange +local roblox = require("@lune/roblox") :: any +local NumberRange = roblox.NumberRange -- Constructors & properties diff --git a/tests/roblox/datatypes/NumberSequence.luau b/tests/roblox/datatypes/NumberSequence.luau index d8ec067..ef577af 100644 --- a/tests/roblox/datatypes/NumberSequence.luau +++ b/tests/roblox/datatypes/NumberSequence.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local NumberSequence = _G.NumberSequence -local NumberSequenceKeypoint = _G.NumberSequenceKeypoint +local roblox = require("@lune/roblox") :: any +local NumberSequence = roblox.NumberSequence +local NumberSequenceKeypoint = roblox.NumberSequenceKeypoint -- Constructors & properties diff --git a/tests/roblox/datatypes/NumberSequenceKeypoint.luau b/tests/roblox/datatypes/NumberSequenceKeypoint.luau index ebbdf63..04b9330 100644 --- a/tests/roblox/datatypes/NumberSequenceKeypoint.luau +++ b/tests/roblox/datatypes/NumberSequenceKeypoint.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local NumberSequenceKeypoint = _G.NumberSequenceKeypoint +local roblox = require("@lune/roblox") :: any +local NumberSequenceKeypoint = roblox.NumberSequenceKeypoint -- Constructors & properties diff --git a/tests/roblox/datatypes/PhysicalProperties.luau b/tests/roblox/datatypes/PhysicalProperties.luau index edf9722..d87c3dd 100644 --- a/tests/roblox/datatypes/PhysicalProperties.luau +++ b/tests/roblox/datatypes/PhysicalProperties.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local PhysicalProperties = _G.PhysicalProperties -local Enum = _G.Enum +local roblox = require("@lune/roblox") :: any +local PhysicalProperties = roblox.PhysicalProperties +local Enum = roblox.Enum -- Constructors & properties diff --git a/tests/roblox/datatypes/Ray.luau b/tests/roblox/datatypes/Ray.luau index 67edae1..070eff0 100644 --- a/tests/roblox/datatypes/Ray.luau +++ b/tests/roblox/datatypes/Ray.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Ray = _G.Ray -local Vector3 = _G.Vector3 +local roblox = require("@lune/roblox") :: any +local Ray = roblox.Ray +local Vector3 = roblox.Vector3 local origin = Vector3.zero local direction = Vector3.zAxis * 10 diff --git a/tests/roblox/datatypes/Rect.luau b/tests/roblox/datatypes/Rect.luau index 9dd2b0a..1ce50c8 100644 --- a/tests/roblox/datatypes/Rect.luau +++ b/tests/roblox/datatypes/Rect.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Vector2 = _G.Vector2 -local Rect = _G.Rect +local roblox = require("@lune/roblox") :: any +local Vector2 = roblox.Vector2 +local Rect = roblox.Rect -- Constructors & properties diff --git a/tests/roblox/datatypes/Region3.luau b/tests/roblox/datatypes/Region3.luau index f1cc0f9..2cd8534 100644 --- a/tests/roblox/datatypes/Region3.luau +++ b/tests/roblox/datatypes/Region3.luau @@ -1,8 +1,7 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Region3 = _G.Region3 -local Vector3 = _G.Vector3 -local CFrame = _G.CFrame +local roblox = require("@lune/roblox") :: any +local Region3 = roblox.Region3 +local Vector3 = roblox.Vector3 +local CFrame = roblox.CFrame local min = Vector3.new(-2, -2, -2) local max = Vector3.new(2, 2, 2) diff --git a/tests/roblox/datatypes/Region3int16.luau b/tests/roblox/datatypes/Region3int16.luau index cfb4c48..042d2e3 100644 --- a/tests/roblox/datatypes/Region3int16.luau +++ b/tests/roblox/datatypes/Region3int16.luau @@ -1,8 +1,7 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Region3int16 = _G.Region3int16 -local Vector3int16 = _G.Vector3int16 -local Vector3 = _G.Vector3 +local roblox = require("@lune/roblox") :: any +local Region3int16 = roblox.Region3int16 +local Vector3int16 = roblox.Vector3int16 +local Vector3 = roblox.Vector3 local min = Vector3int16.new(0, 0, 0) local max = Vector3int16.new(2, 2, 2) diff --git a/tests/roblox/datatypes/UDim.luau b/tests/roblox/datatypes/UDim.luau index 0dec1d1..f9c8b34 100644 --- a/tests/roblox/datatypes/UDim.luau +++ b/tests/roblox/datatypes/UDim.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local UDim = _G.UDim +local roblox = require("@lune/roblox") :: any +local UDim = roblox.UDim -- Constructors & properties diff --git a/tests/roblox/datatypes/UDim2.luau b/tests/roblox/datatypes/UDim2.luau index eb69c90..50456d7 100644 --- a/tests/roblox/datatypes/UDim2.luau +++ b/tests/roblox/datatypes/UDim2.luau @@ -1,7 +1,6 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local UDim = _G.UDim -local UDim2 = _G.UDim2 +local roblox = require("@lune/roblox") :: any +local UDim = roblox.UDim +local UDim2 = roblox.UDim2 -- Constructors & properties diff --git a/tests/roblox/datatypes/Vector2.luau b/tests/roblox/datatypes/Vector2.luau index 0db21ef..cccf373 100644 --- a/tests/roblox/datatypes/Vector2.luau +++ b/tests/roblox/datatypes/Vector2.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Vector2 = _G.Vector2 +local roblox = require("@lune/roblox") :: any +local Vector2 = roblox.Vector2 -- Constructors & properties diff --git a/tests/roblox/datatypes/Vector2int16.luau b/tests/roblox/datatypes/Vector2int16.luau index ed4284d..48b91d1 100644 --- a/tests/roblox/datatypes/Vector2int16.luau +++ b/tests/roblox/datatypes/Vector2int16.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Vector2int16 = _G.Vector2int16 +local roblox = require("@lune/roblox") :: any +local Vector2int16 = roblox.Vector2int16 -- Constructors & properties diff --git a/tests/roblox/datatypes/Vector3.luau b/tests/roblox/datatypes/Vector3.luau index f3f8ed8..64570aa 100644 --- a/tests/roblox/datatypes/Vector3.luau +++ b/tests/roblox/datatypes/Vector3.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Vector3 = _G.Vector3 +local roblox = require("@lune/roblox") :: any +local Vector3 = roblox.Vector3 -- Constructors & properties diff --git a/tests/roblox/datatypes/Vector3int16.luau b/tests/roblox/datatypes/Vector3int16.luau index 3341ad6..3bbf500 100644 --- a/tests/roblox/datatypes/Vector3int16.luau +++ b/tests/roblox/datatypes/Vector3int16.luau @@ -1,6 +1,5 @@ --- HACK: Make luau happy, with the mlua rust --- crate all globals are also present in _G -local Vector3int16 = _G.Vector3int16 +local roblox = require("@lune/roblox") :: any +local Vector3int16 = roblox.Vector3int16 -- Constructors & properties