From e38080f9804d6f09011253f88243cb98ed254e9f Mon Sep 17 00:00:00 2001 From: unittensor Date: Sun, 21 Jan 2024 20:25:19 -0500 Subject: [PATCH] add vector types 4,3,2,& 1; Lua error handling needed --- luau/tests/Vector.lua | 13 ++++--- luau/typemap.lua | 27 +++++++++----- src/luau.rs | 86 +++++++++++++++++++++++++++++++++---------- src/main.rs | 17 ++++++++- 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/luau/tests/Vector.lua b/luau/tests/Vector.lua index 26a2cc8..1483300 100644 --- a/luau/tests/Vector.lua +++ b/luau/tests/Vector.lua @@ -1,6 +1,9 @@ -type f64 = number -type vec3 = (x: f64, y: f64, z: f64) -> {x: f64, y: f64, z: f64} -local Vector3: vec3 = Vector3 +local v2 = Vector2.new(1, 3) +local v3 = Vector3.new(-10, 30, 50) +local v4 = Vector4.new(50, 30, 10, -1000) -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)) \ No newline at end of file +print("----StrafeLua----") +print(("\n Vector2= x=%d,y=%d \n"):format(v2.x, v2.y)) +print(("\n Vector3= x=%d,y=%d,z=%d \n"):format(v3.x, v3.y, v3.z)) +print(("\n Vector4= x=%d,y=%d,z=%d,w=%d \n"):format(v4.x, v4.y, v4.z, v4.w)) +print("-----------------") \ No newline at end of file diff --git a/luau/typemap.lua b/luau/typemap.lua index 22c83f2..bc8bf18 100644 --- a/luau/typemap.lua +++ b/luau/typemap.lua @@ -1,17 +1,26 @@ --A type map for the luau analyzer type f64 = number -type Vector2 = {x: f64, y: f64} -type Vector3 = Vector2 & {z: f64} -type Vector4 = Vector3 & {w: f64} +type struct_Vector2 = {x: f64, y: f64} +type struct_Vector3 = struct_Vector2 & {z: f64} +type struct_Vector4 = struct_Vector3 & {w: f64} -type fn_Vector3 = (x: f64, y: f64, z: f64) -> Vector3 -local Vector3: fn_Vector3 = Vector3 +type Vector2 = { + new: (x: f64, y: f64) -> struct_Vector2 +} +type Vector3 = { + new: (x: f64, y: f64, z: f64) -> struct_Vector3 +} +type Vector4 = { + new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4 +} -type fn_Vector3add = (v1: Vector3, v2: Vector3) -> Vector3 -local Vector3add: fn_Vector3add = Vector3add +local Vector2: Vector2 = Vector2 +local Vector3: Vector3 = Vector3 +local Vector4: Vector4 = Vector4 return { - Vector3 = Vector3, - Vector3add = Vector3add + Vector2 = Vector2, + Vector3 = Vector3, + Vector4 = Vector4, } \ No newline at end of file diff --git a/src/luau.rs b/src/luau.rs index 2b8a80a..7eb7c30 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -1,35 +1,83 @@ -use mlua::{Lua as Luau, Result, Table, Function}; -use glam::Vec3; +use mlua::{Lua as Luau, Result, Table}; +use glam::{Vec2, Vec3, Vec4}; struct StrafeluaGlobals { - vm: Luau + vm: Luau, } -impl StrafeluaGlobals { - fn new_vm() -> Self { +trait Luavm { + fn new_vm(isolated: bool) -> StrafeluaGlobals { 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} + vm.sandbox(isolated).unwrap(); + if isolated { + //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(); + }; + + StrafeluaGlobals {vm} } - fn strafe_player(self) -> Table { - self.vm.create_table().unwrap() + fn vector2(&self) -> Table; + fn vector3(&self) -> Table; + fn vector4(&self) -> Table; +} + +impl Luavm for StrafeluaGlobals { + fn vector2(&self) -> Table { + let field_vector2 = self.vm.create_table().unwrap(); + + let new_vector2 = self.vm.create_function(|this: &Luau, (x,y): (f32,f32)| { + let glam_vec = Vec2::new(x,y); + let vec2 = this.create_table().unwrap(); + vec2.set("x", glam_vec.x).unwrap(); + vec2.set("y", glam_vec.y).unwrap(); + Ok(vec2) + }).unwrap(); + field_vector2.set("new", new_vector2).unwrap(); + + return field_vector2 } - fn vector3(&self) -> Function { - return self.vm.create_function(|vm, (x,y,z): (f32,f32,f32)| { + + fn vector3(&self) -> Table { + let field_vector3 = self.vm.create_table().unwrap(); + + let new_vector3 = self.vm.create_function(|this: &Luau, (x,y,z): (f32,f32,f32)| { let glam_vec = Vec3::new(x,y,z); - let vec3 = vm.create_table().unwrap(); + let vec3 = this.create_table().unwrap(); vec3.set("x", glam_vec.x).unwrap(); vec3.set("y", glam_vec.y).unwrap(); vec3.set("z", glam_vec.z).unwrap(); Ok(vec3) }).unwrap(); + field_vector3.set("new", new_vector3).unwrap(); + + return field_vector3; + } + + fn vector4(&self) -> Table { + let field_vector4 = self.vm.create_table().unwrap(); + + let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (f32,f32,f32,f32)| { + let glam_vec = Vec4::new(x,y,z,w); + let vec4 = this.create_table().unwrap(); + vec4.set("x", glam_vec.x).unwrap(); + vec4.set("y", glam_vec.y).unwrap(); + vec4.set("z", glam_vec.z).unwrap(); + vec4.set("w", glam_vec.w).unwrap(); + Ok(vec4) + }).unwrap(); + field_vector4.set("new", new_vector4).unwrap(); + + return field_vector4; } } -pub fn new_state() -> Result<()> { - - Ok(()) +pub fn new_state(isolated: bool) -> Result { + let strafelua = StrafeluaGlobals::new_vm(isolated); + + strafelua.vm.globals().set("Vector2", strafelua.vector2()).unwrap(); + strafelua.vm.globals().set("Vector3", strafelua.vector3()).unwrap(); + strafelua.vm.globals().set("Vector4", strafelua.vector4()).unwrap(); + + Ok(strafelua.vm) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2951cd2..5430112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,20 @@ pub fn default_models()->model::IndexedModelInstances{ } fn main(){ - luau::new_state().expect("Failed to load strafe lua"); - + let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua"); + strafelua_vm.load(r#" +local v2 = Vector2.new(1, 3) +local v3 = Vector3.new(-10, 30, 50) +local v4 = Vector4.new(50, 30, 10, -1000) + +print("----StrafeLua----") +print(("\n Vector2= x=%d,y=%d \n"):format(v2.x, v2.y)) +print(("\n Vector3= x=%d,y=%d,z=%d \n"):format(v3.x, v3.y, v3.z)) +print(("\n Vector4= x=%d,y=%d,z=%d,w=%d \n"):format(v4.x, v4.y, v4.z, v4.w)) +print("-----------------") + "#).exec().expect("Lua syntax error!"); + //Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false } + //we got our first lua syntax error, todo: make error an error handler in luau.rs + setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION"))); }