Skip to content

Commit

Permalink
Integrate roblox lib into new require mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 21, 2023
1 parent 20cbf8a commit 4aa61c7
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 155 deletions.
68 changes: 0 additions & 68 deletions packages/lib-roblox/src/datatypes/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
1 change: 1 addition & 0 deletions packages/lib-roblox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
for (name, tab) in make_all_datatypes(lua)? {
exports.set(name, tab)?;
}
exports.set_readonly(true);
Ok(exports)
}
2 changes: 1 addition & 1 deletion packages/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ name = "lune"
path = "src/lib.rs"

[features]
default = []
default = ["roblox"]
roblox = ["dep:lune-roblox"]

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod fs;
mod net;
mod process;
mod require;
#[cfg(feature = "roblox")]
mod roblox;
mod serde;
mod stdio;
mod task;
Expand All @@ -17,6 +19,8 @@ pub fn create(lua: &'static Lua, args: Vec<String>) -> 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)?),
Expand Down
15 changes: 15 additions & 0 deletions packages/lib/src/globals/roblox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use mlua::prelude::*;

use crate::lua::table::TableBuilder;

pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
let mut roblox_constants = Vec::new();
let roblox_module = lune_roblox::module(lua)?;
for pair in roblox_module.pairs::<LuaValue, LuaValue>() {
roblox_constants.push(pair?);
}
// TODO: Add async functions for reading & writing documents, creating instances
TableBuilder::new(lua)?
.with_values(roblox_constants)?
.build_readonly()
}
27 changes: 27 additions & 0 deletions packages/lib/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/Axes.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/BrickColor.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/CFrame.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Color3.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 4 additions & 5 deletions tests/roblox/datatypes/ColorSequence.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/ColorSequenceKeypoint.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Enum.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/Faces.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/Font.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/NumberRange.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/NumberSequence.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/NumberSequenceKeypoint.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/PhysicalProperties.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/Ray.luau
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/Rect.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 4 additions & 5 deletions tests/roblox/datatypes/Region3.luau
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 4 additions & 5 deletions tests/roblox/datatypes/Region3int16.luau
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/UDim.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions tests/roblox/datatypes/UDim2.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Vector2.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Vector2int16.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Vector3.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/roblox/datatypes/Vector3int16.luau
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 4aa61c7

Please sign in to comment.