add vector types 4,3,2,& 1; Lua error handling needed

This commit is contained in:
unittensor 2024-01-21 20:25:19 -05:00
parent 46a16d1169
commit e38080f980
4 changed files with 108 additions and 35 deletions

View File

@ -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))
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("-----------------")

View File

@ -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 {
Vector2 = Vector2,
Vector3 = Vector3,
Vector3add = Vector3add
Vector4 = Vector4,
}

View File

@ -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();
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();
Self {vm}
};
StrafeluaGlobals {vm}
}
fn strafe_player(self) -> Table {
self.vm.create_table().unwrap()
fn vector2(&self) -> Table;
fn vector3(&self) -> Table;
fn vector4(&self) -> Table;
}
fn vector3(&self) -> Function {
return self.vm.create_function(|vm, (x,y,z): (f32,f32,f32)| {
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) -> 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<()> {
pub fn new_state(isolated: bool) -> Result<Luau> {
let strafelua = StrafeluaGlobals::new_vm(isolated);
Ok(())
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)
}

View File

@ -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")));
}