From a51001a87587b2c90455dda631c541f98643312c Mon Sep 17 00:00:00 2001 From: unittensor Date: Sun, 21 Jan 2024 22:17:30 -0500 Subject: [PATCH] added properties for Vector2: Vector2.ZERO Vector2.ONE Vector2.NEG_ZERO Vector2.NEG_ONE Vector2.NEG_X Vector2.NEG_Y --- luau/tests/Vector.lua | 22 +++++++++++++--- luau/typemap.lua | 30 +++++++++++++++++----- src/luau.rs | 59 ++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 29 +++++++++++++++++++-- 4 files changed, 126 insertions(+), 14 deletions(-) diff --git a/luau/tests/Vector.lua b/luau/tests/Vector.lua index 1483300..ac99efa 100644 --- a/luau/tests/Vector.lua +++ b/luau/tests/Vector.lua @@ -2,8 +2,24 @@ local v2 = Vector2.new(1, 3) local v3 = Vector3.new(-10, 30, 50) local v4 = Vector4.new(50, 30, 10, -1000) +local function InspectVectorTable(Vector: {[string]: number}) + local aye: {string} = {"{"} + for k,v in Vector do + table.insert(aye, `{tostring(k)}={tostring(v)}`) + end + table.insert(aye, "}") + return table.concat(aye, " ") +end + 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(_VERSION) +print("Vector2=",InspectVectorTable(v2)) +print("Vector3=",InspectVectorTable(v3)) +print("Vector4=",InspectVectorTable(v4)) +print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO)) +print("Vector2.ONE=",InspectVectorTable(Vector2.ONE)) +print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO)) +print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE)) +print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X)) +print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y)) print("-----------------") \ No newline at end of file diff --git a/luau/typemap.lua b/luau/typemap.lua index bc8bf18..85a386c 100644 --- a/luau/typemap.lua +++ b/luau/typemap.lua @@ -5,14 +5,32 @@ type struct_Vector2 = {x: f64, y: f64} type struct_Vector3 = struct_Vector2 & {z: f64} type struct_Vector4 = struct_Vector3 & {w: f64} -type Vector2 = { - new: (x: f64, y: f64) -> struct_Vector2 +export type Vector2 = { + new: (x: f64, y: f64) -> struct_Vector2, + ONE: struct_Vector2, + ZERO: struct_Vector2, + NEG_ZERO: struct_Vector2, + NEG_ONE: struct_Vector2, + NEG_X: struct_Vector2, + NEG_Y: struct_Vector2, } -type Vector3 = { - new: (x: f64, y: f64, z: f64) -> struct_Vector3 +export type Vector3 = { + new: (x: f64, y: f64, z: f64) -> struct_Vector3, + ONE: struct_Vector3, + ZERO: struct_Vector3, + NEG_ZERO: struct_Vector3, + NEG_ONE: struct_Vector3, + NEG_X: struct_Vector3, + NEG_Y: struct_Vector3, } -type Vector4 = { - new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4 +export type Vector4 = { + new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4, + ONE: struct_Vector4, + ZERO: struct_Vector4, + NEG_ZERO: struct_Vector4, + NEG_ONE: struct_Vector4, + NEG_X: struct_Vector4, + NEG_Y: struct_Vector4, } local Vector2: Vector2 = Vector2 diff --git a/src/luau.rs b/src/luau.rs index e1d73c6..c5d2195 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -29,23 +29,64 @@ trait Luavm { impl Luavm for StrafeluaGlobals { fn vector2(&self) -> Table { + let zero = Vec2::ZERO; + let one = Vec2::ONE; + let neg_zero = Vec2::ZERO; + let neg_one = Vec2::NEG_ONE; + let neg_x = Vec2::NEG_X; + let neg_y = Vec2::NEG_Y; + let field_vector2 = self.vm.create_table().unwrap(); + let vector2_one = self.vm.create_table().unwrap(); + vector2_one.set("x", one.x).unwrap(); + vector2_one.set("y", one.y).unwrap(); + + let vector2_zero = self.vm.create_table().unwrap(); + vector2_zero.set("x", zero.x).unwrap(); + vector2_zero.set("y", zero.y).unwrap(); + + let vector2_neg_zero = self.vm.create_table().unwrap(); + vector2_neg_zero.set("x", neg_zero.x).unwrap(); + vector2_neg_zero.set("y", neg_zero.y).unwrap(); + + let vector2_neg_one = self.vm.create_table().unwrap(); + vector2_neg_one.set("x", neg_one.x).unwrap(); + vector2_neg_one.set("y", neg_one.y).unwrap(); + + let vector2_neg_x = self.vm.create_table().unwrap(); + vector2_neg_x.set("x", neg_x.x).unwrap(); + vector2_neg_x.set("y", neg_x.y).unwrap(); + + let vector2_neg_y = self.vm.create_table().unwrap(); + vector2_neg_y.set("x", neg_y.x).unwrap(); + vector2_neg_y.set("y", neg_y.y).unwrap(); + + field_vector2.set("ONE", vector2_one).unwrap(); + field_vector2.set("ZERO", vector2_zero).unwrap(); + field_vector2.set("NEG_ZERO", vector2_neg_zero).unwrap(); + field_vector2.set("NEG_ONE", vector2_neg_one).unwrap(); + field_vector2.set("NEG_X", vector2_neg_x).unwrap(); + field_vector2.set("NEG_Y", vector2_neg_y).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) -> Table { + let zero = Vec3::ZERO; + let one = Vec3::ONE; let field_vector3 = self.vm.create_table().unwrap(); + let vector3_one = 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); @@ -55,14 +96,21 @@ impl Luavm for StrafeluaGlobals { vec3.set("z", glam_vec.z).unwrap(); Ok(vec3) }).unwrap(); - field_vector3.set("new", new_vector3).unwrap(); + vector3_one.set("x", one.x).unwrap(); + vector3_one.set("y", one.y).unwrap(); + vector3_one.set("z", one.z).unwrap(); + field_vector3.set("one", vector3_one).unwrap(); + return field_vector3; } fn vector4(&self) -> Table { + let zero = Vec4::ZERO; + let one = Vec4::ONE; let field_vector4 = self.vm.create_table().unwrap(); + let vector4_one = 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); @@ -73,9 +121,14 @@ impl Luavm for StrafeluaGlobals { vec4.set("w", glam_vec.w).unwrap(); Ok(vec4) }).unwrap(); - field_vector4.set("new", new_vector4).unwrap(); + vector4_one.set("x", one.x).unwrap(); + vector4_one.set("y", one.y).unwrap(); + vector4_one.set("z", one.z).unwrap(); + vector4_one.set("w", one.w).unwrap(); + field_vector4.set("one", vector4_one).unwrap(); + return field_vector4; } diff --git a/src/main.rs b/src/main.rs index b3a523e..8b15cf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,11 +119,36 @@ pub fn default_models()->model::IndexedModelInstances{ fn main(){ let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua"); + // luau/tests/Vector.lua strafelua_vm.load(r#" - print(_VERSION) +local v2 = Vector2.new(1, 3) +local v3 = Vector3.new(-10, 30, 50) +local v4 = Vector4.new(50, 30, 10, -1000) + +local function InspectVectorTable(Vector: {[string]: number}) + local aye: {string} = {"{"} + for k,v in Vector do + table.insert(aye, `{tostring(k)}={tostring(v)}`) + end + table.insert(aye, "}") + return table.concat(aye, " ") +end + +print("----StrafeLua----") +print(_VERSION) +print("Vector2=",InspectVectorTable(v2)) +print("Vector3=",InspectVectorTable(v3)) +print("Vector4=",InspectVectorTable(v4)) +print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO)) +print("Vector2.ONE=",InspectVectorTable(Vector2.ONE)) +print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO)) +print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE)) +print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X)) +print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y)) +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 + //we got our first lua syntax error, todo: make an error handler in luau.rs setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION"))); }