Compare commits

..

2 Commits

Author SHA1 Message Date
e38080f980 add vector types 4,3,2,& 1; Lua error handling needed 2024-01-21 20:25:19 -05:00
46a16d1169 cargo update 2024-01-21 20:24:36 -05:00
5 changed files with 118 additions and 45 deletions

20
Cargo.lock generated
View File

@ -1314,9 +1314,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.76" version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -1497,9 +1497,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.10.2" version = "1.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -1509,9 +1509,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.4.3" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" checksum = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -2160,9 +2160,9 @@ dependencies = [
[[package]] [[package]]
name = "wgpu" name = "wgpu"
version = "0.19.0" version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0b71d2ded29e2161db50ab731d6cb42c037bd7ab94864a98fa66ff36b4721a8" checksum = "0bfe9a310dcf2e6b85f00c46059aaeaf4184caa8e29a1ecd4b7a704c3482332d"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"cfg-if", "cfg-if",
@ -2211,9 +2211,9 @@ dependencies = [
[[package]] [[package]]
name = "wgpu-hal" name = "wgpu-hal"
version = "0.19.0" version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11f259ceb56727fb097da108d92f8a5cbdb5b74a77f9e396bd43626f67299d61" checksum = "e3bb47856236bfafc0bc591a925eb036ac19cd987624a447ff353e7a7e7e6f72"
dependencies = [ dependencies = [
"android_system_properties", "android_system_properties",
"arrayvec", "arrayvec",

View File

@ -1,6 +1,9 @@
type f64 = number local v2 = Vector2.new(1, 3)
type vec3 = (x: f64, y: f64, z: f64) -> {x: f64, y: f64, z: f64} local v3 = Vector3.new(-10, 30, 50)
local Vector3: vec3 = Vector3 local v4 = Vector4.new(50, 30, 10, -1000)
local v3 = Vector3(-10, 30, 50) print("----StrafeLua----")
print((`{("\n"):rep(3)} x=%d,y=%d,z=%d {("\n"):rep(3)}`):format(v3.x, v3.y, v3.z)) 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 --A type map for the luau analyzer
type f64 = number type f64 = number
type Vector2 = {x: f64, y: f64} type struct_Vector2 = {x: f64, y: f64}
type Vector3 = Vector2 & {z: f64} type struct_Vector3 = struct_Vector2 & {z: f64}
type Vector4 = Vector3 & {w: f64} type struct_Vector4 = struct_Vector3 & {w: f64}
type fn_Vector3 = (x: f64, y: f64, z: f64) -> Vector3 type Vector2 = {
local Vector3: fn_Vector3 = Vector3 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 Vector2: Vector2 = Vector2
local Vector3add: fn_Vector3add = Vector3add local Vector3: Vector3 = Vector3
local Vector4: Vector4 = Vector4
return { return {
Vector3 = Vector3, Vector2 = Vector2,
Vector3add = Vector3add Vector3 = Vector3,
Vector4 = Vector4,
} }

View File

@ -1,35 +1,83 @@
use mlua::{Lua as Luau, Result, Table, Function}; use mlua::{Lua as Luau, Result, Table};
use glam::Vec3; use glam::{Vec2, Vec3, Vec4};
struct StrafeluaGlobals { struct StrafeluaGlobals {
vm: Luau vm: Luau,
} }
impl StrafeluaGlobals { trait Luavm {
fn new_vm() -> Self { fn new_vm(isolated: bool) -> StrafeluaGlobals {
let vm = Luau::new(); let vm = Luau::new();
vm.sandbox(true).unwrap(); vm.sandbox(isolated).unwrap();
//Prevent bad actors if isolated {
vm.globals().set("getfenv", mlua::Nil).unwrap(); //Depercated in Luau but not removed *yet* //Prevent bad actors
vm.globals().set("setfenv", mlua::Nil).unwrap(); //same with this vm.globals().set("getfenv", mlua::Nil).unwrap(); //Depercated in Luau but not removed *yet*
vm.globals().set("loadstring", mlua::Nil).unwrap(); vm.globals().set("setfenv", mlua::Nil).unwrap(); //same with this
Self {vm} vm.globals().set("loadstring", mlua::Nil).unwrap();
};
StrafeluaGlobals {vm}
} }
fn strafe_player(self) -> Table { fn vector2(&self) -> Table;
self.vm.create_table().unwrap() 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 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("x", glam_vec.x).unwrap();
vec3.set("y", glam_vec.y).unwrap(); vec3.set("y", glam_vec.y).unwrap();
vec3.set("z", glam_vec.z).unwrap(); vec3.set("z", glam_vec.z).unwrap();
Ok(vec3) Ok(vec3)
}).unwrap(); }).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(){ 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"))); setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")));
} }