diff --git a/luau/tests/Vector.lua b/luau/tests/Vector.lua index 80581cd..f715568 100644 --- a/luau/tests/Vector.lua +++ b/luau/tests/Vector.lua @@ -1,8 +1,6 @@ ---!strict - -local v2 = Vector2.new(1, 3) -local v3 = Vector3.new(-10, 30, 50) -local v4 = Vector4.new(50, 30, 10, -1000) +local vec2 = Vector.new(1,2) +local vec3 = Vector.new(1,2,3) +local vec4 = Vector.new(1,2,3,4) local function InspectVectorTable(Vector: {[string]: number}) local aye: {string} = {"{"} @@ -15,32 +13,8 @@ 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(`Vector3.NEG_Z = \t{InspectVectorTable(Vector3.NEG_Z)}`) -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(`Vector4.NEG_Z = \t{InspectVectorTable(Vector4.NEG_Z)}`) -print(`Vector4.NEG_W = \t{InspectVectorTable(Vector4.NEG_W)}`) -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(`Vector.new = {InspectVectorTable(Vector.new())}`) +print(`vec2 = {InspectVectorTable(vec2)}`) +print(`vec3 = {InspectVectorTable(vec3)}`) +print(`vec4 = {InspectVectorTable(vec4)}`) print("-----------------") \ No newline at end of file diff --git a/src/luau.rs b/src/luau.rs index e7b8ca3..8269c5b 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -8,28 +8,6 @@ struct StrafeluaGlobals { vm: Luau, } trait Luavm { - const VEC2_ZERO: Vec2 = Vec2::ZERO; - const VEC2_ONE: Vec2 = Vec2::ONE; - const VEC2_NEG_ZERO: Vec2 = Vec2::ZERO; - const VEC2_NEG_ONE: Vec2 = Vec2::NEG_ONE; - const VEC2_NEG_X: Vec2 = Vec2::NEG_X; - const VEC2_NEG_Y: Vec2 = Vec2::NEG_Y; - const VEC3_ZERO: Vec3 = Vec3::ZERO; - const VEC3_ONE: Vec3 = Vec3::ONE; - const VEC3_NEG_ZERO: Vec3 = Vec3::ZERO; - const VEC3_NEG_ONE: Vec3 = Vec3::NEG_ONE; - const VEC3_NEG_X: Vec3 = Vec3::NEG_X; - const VEC3_NEG_Y: Vec3 = Vec3::NEG_Y; - const VEC3_NEG_Z: Vec3 = Vec3::NEG_Z; - const VEC4_ZERO: Vec4 = Vec4::ZERO; - const VEC4_ONE: Vec4 = Vec4::ONE; - const VEC4_NEG_ZERO: Vec4 = Vec4::ZERO; - const VEC4_NEG_ONE: Vec4 = Vec4::NEG_ONE; - const VEC4_NEG_X: Vec4 = Vec4::NEG_X; - const VEC4_NEG_Y: Vec4 = Vec4::NEG_Y; - const VEC4_NEG_Z: Vec4 = Vec4::NEG_Z; - const VEC4_NEG_W: Vec4 = Vec4::NEG_W; - fn new_vm(isolated: bool) -> StrafeluaGlobals { let vm = Luau::new(); vm.sandbox(isolated).unwrap(); @@ -45,22 +23,9 @@ trait Luavm { StrafeluaGlobals {vm} } - fn warn(&self) -> mlua::Function; - fn vector2(&self) -> mlua::Table; - fn vector3(&self) -> mlua::Table; - fn vector4(&self) -> mlua::Table; -} -trait Vectors { - fn arg(result: Option) -> f32; -} -impl Vectors for StrafeluaGlobals { - fn arg(result: Option) -> f32 { - match result { - Some(n) => n as f32, - None => 0.0 as f32, - } - } + fn warn(&self) -> mlua::Function; + fn vector(&self) -> mlua::Table; } impl Luavm for StrafeluaGlobals { @@ -75,191 +40,40 @@ impl Luavm for StrafeluaGlobals { }).unwrap(); } - fn vector2(&self) -> mlua::Table { - let field_vector2 = self.vm.create_table().unwrap(); + fn vector(&self) -> mlua::Table { + let field_vector = self.vm.create_table().unwrap(); - let vector2_one = self.vm.create_table().unwrap(); - vector2_one.set("x", Self::VEC2_ONE.x).unwrap(); - vector2_one.set("y", Self::VEC2_ONE.y).unwrap(); - - let vector2_zero = self.vm.create_table().unwrap(); - vector2_zero.set("x", Self::VEC2_ZERO.x).unwrap(); - vector2_zero.set("y", Self::VEC2_ZERO.y).unwrap(); - - let vector2_neg_zero = self.vm.create_table().unwrap(); - vector2_neg_zero.set("x", Self::VEC2_NEG_ZERO.x).unwrap(); - vector2_neg_zero.set("y", Self::VEC2_NEG_ZERO.y).unwrap(); - - let vector2_neg_one = self.vm.create_table().unwrap(); - vector2_neg_one.set("x", Self::VEC2_NEG_ONE.x).unwrap(); - vector2_neg_one.set("y", Self::VEC2_NEG_ONE.y).unwrap(); - - let vector2_neg_x = self.vm.create_table().unwrap(); - vector2_neg_x.set("x", Self::VEC2_NEG_X.x).unwrap(); - vector2_neg_x.set("y", Self::VEC2_NEG_X.y).unwrap(); - - let vector2_neg_y = self.vm.create_table().unwrap(); - vector2_neg_y.set("x", Self::VEC2_NEG_Y.x).unwrap(); - vector2_neg_y.set("y", Self::VEC2_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(); - - field_vector2.set("new", self.vm.create_function(|this: &Luau, (x,y): (Option, Option)| { - let vec2_x = ::arg(x); - let vec2_y = Vectors::arg(y); - 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(); - - Ok(vec2) + field_vector.set("new", self.vm.create_function(|this: &Luau, (x,y,z,w): (Option, Option, Option, Option)| { + let vector = this.create_table().unwrap(); + vector.set("x", x.unwrap_or(0.0)).unwrap(); + vector.set("y", y.unwrap_or(0.0)).unwrap(); + vector.set("z", z.unwrap_or(0.0)).unwrap(); + vector.set("w", w.unwrap_or(0.0)).unwrap(); + Ok(vector) }).unwrap()).unwrap(); - return field_vector2 - } + let vector_one2 = self.vm.create_table().unwrap(); + vector_one2.set("x", 1.0).unwrap(); + vector_one2.set("y", 1.0).unwrap(); + vector_one2.set("z", 0.0).unwrap(); + vector_one2.set("w", 0.0).unwrap(); + field_vector.set("one2", vector_one2).unwrap(); - fn vector3(&self) -> mlua::Table { - let field_vector3 = self.vm.create_table().unwrap(); + let vector_one3 = self.vm.create_table().unwrap(); + vector_one3.set("x", 1.0).unwrap(); + vector_one3.set("y", 1.0).unwrap(); + vector_one3.set("z", 1.0).unwrap(); + vector_one3.set("w", 0.0).unwrap(); + field_vector.set("one3", vector_one3).unwrap(); - let vector3_one = self.vm.create_table().unwrap(); - vector3_one.set("x", Self::VEC3_ONE.x).unwrap(); - vector3_one.set("y", Self::VEC3_ONE.y).unwrap(); - vector3_one.set("z", Self::VEC3_ONE.z).unwrap(); + let vector_one4 = self.vm.create_table().unwrap(); + vector_one4.set("x", 1.0).unwrap(); + vector_one4.set("y", 1.0).unwrap(); + vector_one4.set("z", 1.0).unwrap(); + vector_one4.set("w", 1.0).unwrap(); + field_vector.set("one4", vector_one4).unwrap(); - let vector3_zero = self.vm.create_table().unwrap(); - vector3_zero.set("x", Self::VEC3_ZERO.x).unwrap(); - vector3_zero.set("y", Self::VEC3_ZERO.y).unwrap(); - vector3_zero.set("z", Self::VEC3_ZERO.z).unwrap(); - - let vector3_neg_zero = self.vm.create_table().unwrap(); - vector3_neg_zero.set("x", Self::VEC3_NEG_ZERO.x).unwrap(); - vector3_neg_zero.set("y", Self::VEC3_NEG_ZERO.y).unwrap(); - vector3_neg_zero.set("z", Self::VEC3_NEG_ZERO.z).unwrap(); - - let vector3_neg_one = self.vm.create_table().unwrap(); - vector3_neg_one.set("x", Self::VEC3_NEG_ONE.x).unwrap(); - vector3_neg_one.set("y", Self::VEC3_NEG_ONE.y).unwrap(); - vector3_neg_one.set("z", Self::VEC3_NEG_ONE.z).unwrap(); - - let vector3_neg_x = self.vm.create_table().unwrap(); - vector3_neg_x.set("x", Self::VEC3_NEG_X.x).unwrap(); - vector3_neg_x.set("y", Self::VEC3_NEG_X.y).unwrap(); - vector3_neg_x.set("z", Self::VEC3_NEG_X.z).unwrap(); - - let vector3_neg_y = self.vm.create_table().unwrap(); - vector3_neg_y.set("x", Self::VEC3_NEG_Y.x).unwrap(); - vector3_neg_y.set("y", Self::VEC3_NEG_Y.y).unwrap(); - vector3_neg_x.set("z", Self::VEC3_NEG_Y.z).unwrap(); - - let vector3_neg_z = self.vm.create_table().unwrap(); - vector3_neg_z.set("x", Self::VEC3_NEG_Z.x).unwrap(); - vector3_neg_z.set("y", Self::VEC3_NEG_Z.y).unwrap(); - vector3_neg_z.set("z", Self::VEC3_NEG_Z.z).unwrap(); - - field_vector3.set("ONE", vector3_one).unwrap(); - field_vector3.set("ZERO", vector3_zero).unwrap(); - field_vector3.set("NEG_ZERO", vector3_neg_zero).unwrap(); - field_vector3.set("NEG_ONE", vector3_neg_one).unwrap(); - field_vector3.set("NEG_X", vector3_neg_x).unwrap(); - field_vector3.set("NEG_Y", vector3_neg_y).unwrap(); - field_vector3.set("NEG_Z", vector3_neg_z).unwrap(); - - field_vector3.set("new", self.vm.create_function(|this: &Luau, (x,y,z): (Option, Option, Option)| { - let vec3_x = vector_arg(x); - let vec3_y = vector_arg(y); - let vec3_z = vector_arg(z); - 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()).unwrap(); - - return field_vector3; - } - - fn vector4(&self) -> mlua::Table { - let field_vector4 = self.vm.create_table().unwrap(); - - let vector4_one = self.vm.create_table().unwrap(); - vector4_one.set("x", Self::VEC4_ONE.x).unwrap(); - vector4_one.set("y", Self::VEC4_ONE.y).unwrap(); - vector4_one.set("z", Self::VEC4_ONE.z).unwrap(); - vector4_one.set("w", Self::VEC4_ONE.w).unwrap(); - - let vector4_zero = self.vm.create_table().unwrap(); - vector4_zero.set("x", Self::VEC4_ZERO.x).unwrap(); - vector4_zero.set("y", Self::VEC4_ZERO.y).unwrap(); - vector4_zero.set("z", Self::VEC4_ZERO.z).unwrap(); - vector4_zero.set("w", Self::VEC4_ZERO.w).unwrap(); - - let vector4_neg_zero = self.vm.create_table().unwrap(); - vector4_neg_zero.set("x", Self::VEC4_NEG_ZERO.x).unwrap(); - vector4_neg_zero.set("y", Self::VEC4_NEG_ZERO.y).unwrap(); - vector4_neg_zero.set("z", Self::VEC4_NEG_ZERO.z).unwrap(); - vector4_neg_zero.set("w", Self::VEC4_NEG_ZERO.w).unwrap(); - - let vector4_neg_one = self.vm.create_table().unwrap(); - vector4_neg_one.set("x", Self::VEC4_NEG_ONE.x).unwrap(); - vector4_neg_one.set("y", Self::VEC4_NEG_ONE.y).unwrap(); - vector4_neg_one.set("z", Self::VEC4_NEG_ONE.z).unwrap(); - vector4_neg_one.set("w", Self::VEC4_NEG_ONE.w).unwrap(); - - let vector4_neg_x = self.vm.create_table().unwrap(); - vector4_neg_x.set("x", Self::VEC4_NEG_X.x).unwrap(); - vector4_neg_x.set("y", Self::VEC4_NEG_X.y).unwrap(); - vector4_neg_x.set("z", Self::VEC4_NEG_X.z).unwrap(); - vector4_neg_x.set("w", Self::VEC4_NEG_X.w).unwrap(); - - let vector4_neg_y = self.vm.create_table().unwrap(); - vector4_neg_y.set("x", Self::VEC4_NEG_Y.x).unwrap(); - vector4_neg_y.set("y", Self::VEC4_NEG_Y.y).unwrap(); - vector4_neg_x.set("z", Self::VEC4_NEG_Y.z).unwrap(); - vector4_neg_x.set("w", Self::VEC4_NEG_Y.w).unwrap(); - - let vector4_neg_z = self.vm.create_table().unwrap(); - vector4_neg_z.set("x", Self::VEC4_NEG_Z.x).unwrap(); - vector4_neg_z.set("y", Self::VEC4_NEG_Z.y).unwrap(); - vector4_neg_z.set("z", Self::VEC4_NEG_Z.z).unwrap(); - vector4_neg_z.set("w", Self::VEC4_NEG_Z.w).unwrap(); - - let vector4_neg_w = self.vm.create_table().unwrap(); - vector4_neg_w.set("x", Self::VEC4_NEG_W.x).unwrap(); - vector4_neg_w.set("y", Self::VEC4_NEG_W.y).unwrap(); - vector4_neg_w.set("z", Self::VEC4_NEG_W.z).unwrap(); - vector4_neg_w.set("w", Self::VEC4_NEG_W.w).unwrap(); - - field_vector4.set("ONE", vector4_one).unwrap(); - field_vector4.set("ZERO", vector4_zero).unwrap(); - field_vector4.set("NEG_ZERO", vector4_neg_zero).unwrap(); - field_vector4.set("NEG_ONE", vector4_neg_one).unwrap(); - field_vector4.set("NEG_X", vector4_neg_x).unwrap(); - field_vector4.set("NEG_Y", vector4_neg_y).unwrap(); - field_vector4.set("NEG_Z", vector4_neg_z).unwrap(); - field_vector4.set("NEG_W", vector4_neg_w).unwrap(); - - let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (Option, Option, Option, Option)| { - let vec4_x = vector_arg(x); - let vec4_y = vector_arg(y); - let vec4_z = vector_arg(z); - let vec4_w = vector_arg(w); - 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(); - 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; + return field_vector } } @@ -275,9 +89,7 @@ pub fn new_state(isolated: bool) -> Result { let strafelua = StrafeluaGlobals::new_vm(isolated); strafelua.vm.globals().set("warn", strafelua.warn()).unwrap(); - strafelua.vm.globals().set("Vector2", strafelua.vector2()).unwrap(); - strafelua.vm.globals().set("Vector3", strafelua.vector3()).unwrap(); - strafelua.vm.globals().set("Vector4", strafelua.vector4()).unwrap(); + strafelua.vm.globals().set("Vector", strafelua.vector()).unwrap(); Ok(strafelua.vm) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0918c15..eec57f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,9 +120,9 @@ 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#" -local v2 = Vector2.new(1, 3) -local v3 = Vector3.new(-10, 30, 50) -local v4 = Vector4.new(50, 30, 10, -1000) +local vec2 = Vector.new(1,2) +local vec3 = Vector.new(1,2,3) +local vec4 = Vector.new(1,2,3,4) local function InspectVectorTable(Vector: {[string]: number}) local aye: {string} = {"{"} @@ -135,31 +135,10 @@ 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(`Vector.new = {InspectVectorTable(Vector.new())}`) +print(`vec2 = {InspectVectorTable(vec2)}`) +print(`vec3 = {InspectVectorTable(vec3)}`) +print(`vec4 = {InspectVectorTable(vec4)}`) print("-----------------") "#).exec()); //Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false }