first luau.rs commit, this one doesn't work

This commit is contained in:
unittensor 2024-01-20 20:19:42 -05:00
parent 99b9c527b4
commit 5327b201c7

58
src/luau.rs Normal file
View File

@ -0,0 +1,58 @@
use mlua::{Lua as Luau, Result, Table};
use glam::Vec3;
struct StrafeluaGlobals<'a> {
current_vm: &'a Luau
}
impl<'a> StrafeluaGlobals<'a> {
fn new_vm(current_vm: &'a Luau) -> Self {
Self {current_vm}
}
fn strafe_player(&self) -> Table {
let main_table = &self.current_vm.create_table().unwrap();
main_table.clone()
}
fn vector3(&self) -> Table<'a> {
let vec3 = &self.current_vm.create_table().unwrap();
// self.current_vm.create_function(|vm, (x,y,z): (f32,f32,f32)| {
// let vec = Vec3::new(x,y,z);
// vec3.set("x", vec.x).unwrap();
// vec3.set("y", vec.y).unwrap();
// vec3.set("z", vec.z).unwrap();
// Ok(vec3)
// }).unwrap();
return vec3.clone()
}
}
pub fn new_state() -> Result<Luau> {
let vm = Luau::new();
vm.sandbox(true).unwrap();
//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();
let strafe_globals = StrafeluaGlobals::new_vm(&vm);
let strafe_player = strafe_globals.strafe_player();
let strafe_vector3 = strafe_globals.vector3();
vm.globals().set("Player", strafe_player).unwrap();
vm.globals().set("Vector3", strafe_vector3).unwrap();
let gib = *strafe_globals.current_vm;
let _ = vm.load(r#"
type f64 = number
type vec3 = (x: f64, y: f64, z: f64) -> {x: f64, y: f64, z: f64}
local Vector3: vec3 = Vector3
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))
"#).exec();
Ok(gib)
}