From e40e960ef2cc0ca8f0dcb582467c1661f593677f Mon Sep 17 00:00:00 2001 From: unittensor Date: Mon, 22 Jan 2024 15:05:08 -0500 Subject: [PATCH] Provide optional values for Vector.new libraries `Vector4.new() -> 0,0,0,0` --- luau/tests/Vector.lua | 48 ++++++++++++++++++++------------------ luau/typemap.lua | 6 ++--- src/luau.rs | 54 +++++++++++++++++++++++++++++++++++-------- src/main.rs | 42 ++++++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 36 deletions(-) diff --git a/luau/tests/Vector.lua b/luau/tests/Vector.lua index 389a4ec..53b8878 100644 --- a/luau/tests/Vector.lua +++ b/luau/tests/Vector.lua @@ -12,26 +12,30 @@ local function InspectVectorTable(Vector: {[string]: number}) 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("Vector3.ZERO=",InspectVectorTable(Vector3.ZERO)) -print("Vector3.ONE=",InspectVectorTable(Vector3.ONE)) -print("Vector3.NEG_ZERO=",InspectVectorTable(Vector3.NEG_ZERO)) -print("Vector3.NEG_ONE=",InspectVectorTable(Vector3.NEG_ONE)) -print("Vector3.NEG_X=",InspectVectorTable(Vector3.NEG_X)) -print("Vector3.NEG_Y=",InspectVectorTable(Vector3.NEG_Y)) -print("Vector4.ZERO=",InspectVectorTable(Vector4.ZERO)) -print("Vector4.ONE=",InspectVectorTable(Vector4.ONE)) -print("Vector4.NEG_ZERO=",InspectVectorTable(Vector4.NEG_ZERO)) -print("Vector4.NEG_ONE=",InspectVectorTable(Vector4.NEG_ONE)) -print("Vector4.NEG_X=",InspectVectorTable(Vector4.NEG_X)) -print("Vector4.NEG_Y=",InspectVectorTable(Vector4.NEG_Y)) +warn(_VERSION) +print(`Vector2=\t{InspectVectorTable(v2)}`) +print(`Vector3=\t{InspectVectorTable(v3)}`) +print(`Vector4=\t{InspectVectorTable(v4)}`) +print(`Vector2.ZERO=\t{InspectVectorTable(Vector2.ZERO)}`) +print(`Vector2.ONE=\t{InspectVectorTable(Vector2.ONE)}`) +print(`Vector2.NEG_ZERO=\t{InspectVectorTable(Vector2.NEG_ZERO)}`) +print(`Vector2.NEG_ONE=\t{InspectVectorTable(Vector2.NEG_ONE)}`) +print(`Vector2.NEG_X=\t{InspectVectorTable(Vector2.NEG_X)}`) +print(`Vector2.NEG_Y=\t{InspectVectorTable(Vector2.NEG_Y)}`) +print(`Vector3.ZERO=\t{InspectVectorTable(Vector3.ZERO)}`) +print(`Vector3.ONE=\t{InspectVectorTable(Vector3.ONE)}`) +print(`Vector3.NEG_ZERO=\t{InspectVectorTable(Vector3.NEG_ZERO)}`) +print(`Vector3.NEG_ONE=\t{InspectVectorTable(Vector3.NEG_ONE)}`) +print(`Vector3.NEG_X=\t{InspectVectorTable(Vector3.NEG_X)}`) +print(`Vector3.NEG_Y=\t{InspectVectorTable(Vector3.NEG_Y)}`) +print(`Vector4.ZERO=\t{InspectVectorTable(Vector4.ZERO)}`) +print(`Vector4.ONE=\t{InspectVectorTable(Vector4.ONE)}`) +print(`Vector4.NEG_ZERO=\t{InspectVectorTable(Vector4.NEG_ZERO)}`) +print(`Vector4.NEG_ONE=\t{InspectVectorTable(Vector4.NEG_ONE)}`) +print(`Vector4.NEG_X=\t{InspectVectorTable(Vector4.NEG_X)}`) +print(`Vector4.NEG_Y=\t{InspectVectorTable(Vector4.NEG_Y)}`) +print("-----------------") +print(`No args Vector2.new=\t{InspectVectorTable(Vector2.new())}`) +print(`No args Vector3.new=\t{InspectVectorTable(Vector3.new())}`) +print(`No args Vector4.new=\t{InspectVectorTable(Vector4.new())}`) print("-----------------") \ No newline at end of file diff --git a/luau/typemap.lua b/luau/typemap.lua index ca09e23..c20ed68 100644 --- a/luau/typemap.lua +++ b/luau/typemap.lua @@ -8,7 +8,7 @@ type struct_Vector4 = struct_Vector3 & {w: f64} export type warn = (message: string) -> () export type Vector2 = { - new: (x: f64, y: f64) -> struct_Vector2, + new: (x: f64?, y: f64?) -> struct_Vector2, ONE: struct_Vector2, ZERO: struct_Vector2, NEG_ZERO: struct_Vector2, @@ -17,7 +17,7 @@ export type Vector2 = { NEG_Y: struct_Vector2, } export type Vector3 = { - new: (x: f64, y: f64, z: f64) -> struct_Vector3, + new: (x: f64?, y: f64?, z: f64?) -> struct_Vector3, ONE: struct_Vector3, ZERO: struct_Vector3, NEG_ZERO: struct_Vector3, @@ -26,7 +26,7 @@ export type Vector3 = { NEG_Y: struct_Vector3, } export type Vector4 = { - new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4, + new: (x: f64?, y: f64?, z: f64?, w: f64?) -> struct_Vector4, ONE: struct_Vector4, ZERO: struct_Vector4, NEG_ZERO: struct_Vector4, diff --git a/src/luau.rs b/src/luau.rs index 2cac27d..a941c2d 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -34,7 +34,7 @@ impl Luavm for StrafeluaGlobals { fn warn(&self) -> Function { return self.vm.create_function(|_, message: mlua::String| { match Some(message) { - Some(lua_string) => cprintln!("{}", lua_string.to_string_lossy()), + Some(lua_string) => cprintln!("{}", lua_string.to_str().unwrap()), None => println!("Nothing provided to warn"), }; Ok(()) @@ -42,7 +42,6 @@ impl Luavm for StrafeluaGlobals { } fn vector2(&self) -> Table { - //In a better world, this can all be a macro let zero = Vec2::ZERO; let one = Vec2::ONE; let neg_zero = Vec2::ZERO; @@ -83,8 +82,16 @@ impl Luavm for StrafeluaGlobals { field_vector2.set("NEG_X", vector2_neg_x).unwrap(); field_vector2.set("NEG_Y", vector2_neg_y).unwrap(); - field_vector2.set("new", self.vm.create_function(|this: &Luau, (x,y): (f32,f32)| { - let glam_vec = Vec2::new(x,y); + field_vector2.set("new", self.vm.create_function(|this: &Luau, (x,y): (Option, Option)| { + let vec2_x = match x { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec2_y = match y { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let glam_vec = Vec2::new(vec2_x,vec2_y); let vec2 = this.create_table().unwrap(); vec2.set("x", glam_vec.x).unwrap(); vec2.set("y", glam_vec.y).unwrap(); @@ -142,15 +149,26 @@ impl Luavm for StrafeluaGlobals { field_vector3.set("NEG_X", vector3_neg_x).unwrap(); field_vector3.set("NEG_Y", vector3_neg_y).unwrap(); - let new_vector3 = self.vm.create_function(|this: &Luau, (x,y,z): (f32,f32,f32)| { - let glam_vec = Vec3::new(x,y,z); + field_vector3.set("new", self.vm.create_function(|this: &Luau, (x,y,z): (Option, Option, Option)| { + let vec3_x = match x { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec3_y = match y { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec3_z = match z { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let glam_vec = Vec3::new(vec3_x,vec3_y,vec3_z); 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(); + }).unwrap()).unwrap(); return field_vector3; } @@ -208,8 +226,24 @@ impl Luavm for StrafeluaGlobals { field_vector4.set("NEG_X", vector4_neg_x).unwrap(); field_vector4.set("NEG_Y", vector4_neg_y).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 new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (Option, Option, Option, Option)| { + let vec4_x = match x { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec4_y = match y { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec4_z = match z { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let vec4_w = match w { + Some(n) => n as f32, + None => 0.0 as f32, + }; + let glam_vec = Vec4::new(vec4_x,vec4_y,vec4_z,vec4_w); let vec4 = this.create_table().unwrap(); vec4.set("x", glam_vec.x).unwrap(); vec4.set("y", glam_vec.y).unwrap(); diff --git a/src/main.rs b/src/main.rs index a6c4e17..0918c15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,47 @@ pub fn default_models()->model::IndexedModelInstances{ fn main(){ let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua"); luau::error_wrapper(strafelua_vm.load(r#" - warn("hi") +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----") +warn(_VERSION) +print(`Vector2=\t{InspectVectorTable(v2)}`) +print(`Vector3=\t{InspectVectorTable(v3)}`) +print(`Vector4=\t{InspectVectorTable(v4)}`) +print(`Vector2.ZERO=\t{InspectVectorTable(Vector2.ZERO)}`) +print(`Vector2.ONE=\t{InspectVectorTable(Vector2.ONE)}`) +print(`Vector2.NEG_ZERO=\t{InspectVectorTable(Vector2.NEG_ZERO)}`) +print(`Vector2.NEG_ONE=\t{InspectVectorTable(Vector2.NEG_ONE)}`) +print(`Vector2.NEG_X=\t{InspectVectorTable(Vector2.NEG_X)}`) +print(`Vector2.NEG_Y=\t{InspectVectorTable(Vector2.NEG_Y)}`) +print(`Vector3.ZERO=\t{InspectVectorTable(Vector3.ZERO)}`) +print(`Vector3.ONE=\t{InspectVectorTable(Vector3.ONE)}`) +print(`Vector3.NEG_ZERO=\t{InspectVectorTable(Vector3.NEG_ZERO)}`) +print(`Vector3.NEG_ONE=\t{InspectVectorTable(Vector3.NEG_ONE)}`) +print(`Vector3.NEG_X=\t{InspectVectorTable(Vector3.NEG_X)}`) +print(`Vector3.NEG_Y=\t{InspectVectorTable(Vector3.NEG_Y)}`) +print(`Vector4.ZERO=\t{InspectVectorTable(Vector4.ZERO)}`) +print(`Vector4.ONE=\t{InspectVectorTable(Vector4.ONE)}`) +print(`Vector4.NEG_ZERO=\t{InspectVectorTable(Vector4.NEG_ZERO)}`) +print(`Vector4.NEG_ONE=\t{InspectVectorTable(Vector4.NEG_ONE)}`) +print(`Vector4.NEG_X=\t{InspectVectorTable(Vector4.NEG_X)}`) +print(`Vector4.NEG_Y=\t{InspectVectorTable(Vector4.NEG_Y)}`) +print("-----------------") +print(`No args Vector2.new=\t{InspectVectorTable(Vector2.new())}`) +print(`No args Vector3.new=\t{InspectVectorTable(Vector3.new())}`) +print(`No args Vector4.new=\t{InspectVectorTable(Vector4.new())}`) +print("-----------------") "#).exec()); //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 an error handler in luau.rs