diff --git a/src/luau.rs b/src/luau.rs index f357ae3..2b8a80a 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -1,20 +1,24 @@ use mlua::{Lua as Luau, Result, Table, Function}; use glam::Vec3; -struct StrafeluaGlobals<'a> { - current_vm: &'a Luau +struct StrafeluaGlobals { + vm: Luau } - -impl<'a> StrafeluaGlobals<'a> { - fn new_vm(current_vm: &'a Luau) -> Self { - Self {current_vm} +impl StrafeluaGlobals { + fn new_vm() -> Self { + let vm = Luau::new(); + vm.sandbox(true).unwrap(); + //Prevent bad actors + vm.globals().set("getfenv", mlua::Nil).unwrap(); //Depercated in Luau but not removed *yet* + vm.globals().set("setfenv", mlua::Nil).unwrap(); //same with this + vm.globals().set("loadstring", mlua::Nil).unwrap(); + Self {vm} } - fn strafe_player(&self) -> Table { - let main_table = &self.current_vm.create_table().unwrap(); - main_table.clone() + fn strafe_player(self) -> Table { + self.vm.create_table().unwrap() } fn vector3(&self) -> Function { - return self.current_vm.create_function(|vm, (x,y,z): (f32,f32,f32)| { + return self.vm.create_function(|vm, (x,y,z): (f32,f32,f32)| { let glam_vec = Vec3::new(x,y,z); let vec3 = vm.create_table().unwrap(); vec3.set("x", glam_vec.x).unwrap(); @@ -26,28 +30,6 @@ impl<'a> StrafeluaGlobals<'a> { } pub fn new_state() -> Result<()> { - let vm = Luau::new(); - vm.sandbox(true).unwrap(); - //Prevent bad actors - vm.globals().set("getfenv", mlua::Nil).unwrap(); //Depercated in Luau but not removed *yet* - vm.globals().set("setfenv", mlua::Nil).unwrap(); //same with this - vm.globals().set("loadstring", mlua::Nil).unwrap(); - - let strafe_globals = StrafeluaGlobals::new_vm(&vm); - - let strafe_player = strafe_globals.strafe_player(); - let strafe_vector3 = strafe_globals.vector3(); - vm.globals().set("Player", strafe_player).unwrap(); - vm.globals().set("Vector3", strafe_vector3).unwrap(); - let _ = vm.load(r#" -type f64 = number -type vec3 = (x: f64, y: f64, z: f64) -> {x: f64, y: f64, z: f64} -local Vector3: vec3 = Vector3 - -local v3 = Vector3(-10, 30, 50) -print((`{("\n"):rep(3)} x=%d,y=%d,z=%d {("\n"):rep(3)}`):format(v3.x, v3.y, v3.z)) - "#).exec(); - Ok(()) } \ No newline at end of file