debug "warn" global for StrafeLua

This commit is contained in:
unittensor 2024-01-22 14:19:52 -05:00
parent b5f7a8cc6f
commit 76ca43a645
3 changed files with 19 additions and 5 deletions

View File

@ -5,6 +5,8 @@ type struct_Vector2 = {x: f64, y: f64}
type struct_Vector3 = struct_Vector2 & {z: f64}
type struct_Vector4 = struct_Vector3 & {w: f64}
export type warn = (message: string) -> ()
export type Vector2 = {
new: (x: f64, y: f64) -> struct_Vector2,
ONE: struct_Vector2,
@ -36,9 +38,11 @@ export type Vector4 = {
local Vector2: Vector2 = Vector2
local Vector3: Vector3 = Vector3
local Vector4: Vector4 = Vector4
local warn: warn = warn
return {
Vector2 = Vector2,
Vector3 = Vector3,
Vector4 = Vector4,
warn = warn
}

View File

@ -1,4 +1,4 @@
use mlua::{Lua as Luau, Result, Table};
use mlua::{Lua as Luau, Result, Table, Function};
use glam::{Vec2, Vec3, Vec4, Quat};
use color_print::cprintln;
@ -23,12 +23,24 @@ trait Luavm {
StrafeluaGlobals {vm}
}
fn warn(&self) -> Function;
fn vector2(&self) -> Table;
fn vector3(&self) -> Table;
fn vector4(&self) -> Table;
}
impl Luavm for StrafeluaGlobals {
//Debug stuff
fn warn(&self) -> Function {
return self.vm.create_function(|_, message: mlua::String| {
match Some(message) {
Some(lua_string) => cprintln!("<yellow>{}</yellow>", lua_string.to_string_lossy()),
None => println!("Nothing provided to warn"),
};
Ok(())
}).unwrap();
}
fn vector2(&self) -> Table {
//In a better world, this can all be a macro
let zero = Vec2::ZERO;
@ -209,8 +221,6 @@ impl Luavm for StrafeluaGlobals {
return field_vector4;
}
}
/// Prevent strafe client from panicking when there is a Lua error related to syntax or anything else Lua related
@ -224,6 +234,7 @@ pub fn error_wrapper(execute_result: Result<()>) {
pub fn new_state(isolated: bool) -> Result<Luau> {
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();

View File

@ -120,8 +120,7 @@ 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#"
--Purposely throw an error
Squidward()
warn("hi")
"#).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