diff --git a/luau/typemap.lua b/luau/typemap.lua index 85a386c..ca09e23 100644 --- a/luau/typemap.lua +++ b/luau/typemap.lua @@ -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 } \ No newline at end of file diff --git a/src/luau.rs b/src/luau.rs index b208667..2cac27d 100644 --- a/src/luau.rs +++ b/src/luau.rs @@ -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!("{}", 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 { 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(); diff --git a/src/main.rs b/src/main.rs index 7dfae3f..a6c4e17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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