Error handler so strafe client wont panic on a Lua error

This commit is contained in:
unittensor 2024-01-22 13:42:21 -05:00
parent 66eea1f106
commit 88ad707f39
2 changed files with 15 additions and 42 deletions

View File

@ -12,13 +12,13 @@ trait Luavm {
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("getfenv", mlua::Nil).unwrap(); //Deprecated in Luau but not removed *yet*
vm.globals().set("setfenv", mlua::Nil).unwrap(); //same with this
vm.globals().set("loadstring", mlua::Nil).unwrap();
};
let version_luau: String = vm.globals().get("_VERSION").unwrap();
vm.globals().set("_VERSION", format!("StrafeLuau {}, {}", STRAFE_VERSION, version_luau)).unwrap();
let luau_version: String = vm.globals().get("_VERSION").unwrap();
vm.globals().set("_VERSION", format!("StrafeLuau {}, {}", STRAFE_VERSION, luau_version)).unwrap();
StrafeluaGlobals {vm}
}
@ -212,6 +212,14 @@ impl Luavm for StrafeluaGlobals {
}
/// Prevent strafe client from panicking when there is a Lua error related to syntax or anything else Lua related
pub fn error_wrapper(execute_result: Result<()>) {
match execute_result {
Ok(t) => t,
Err(e) => println!("[StrafeLua ERROR]: {}", e),
}
}
pub fn new_state(isolated: bool) -> Result<Luau> {
let strafelua = StrafeluaGlobals::new_vm(isolated);

View File

@ -120,45 +120,10 @@ pub fn default_models()->model::IndexedModelInstances{
fn main(){
let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua");
// luau/tests/Vector.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)
local function InspectVectorTable(Vector: {[string]: number})
local aye: {string} = {"{"}
for k,v in Vector do
table.insert(aye, `{tostring(k)}={tostring(v)}`)
end
table.insert(aye, "}")
return table.concat(aye, " ")
end
print("----StrafeLua----")
print(_VERSION)
print("Vector2=",InspectVectorTable(v2))
print("Vector3=",InspectVectorTable(v3))
print("Vector4=",InspectVectorTable(v4))
print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO))
print("Vector2.ONE=",InspectVectorTable(Vector2.ONE))
print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO))
print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE))
print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X))
print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y))
print("Vector3.ZERO=",InspectVectorTable(Vector3.ZERO))
print("Vector3.ONE=",InspectVectorTable(Vector3.ONE))
print("Vector3.NEG_ZERO=",InspectVectorTable(Vector3.NEG_ZERO))
print("Vector3.NEG_ONE=",InspectVectorTable(Vector3.NEG_ONE))
print("Vector3.NEG_X=",InspectVectorTable(Vector3.NEG_X))
print("Vector3.NEG_Y=",InspectVectorTable(Vector3.NEG_Y))
print("Vector4.ZERO=",InspectVectorTable(Vector4.ZERO))
print("Vector4.ONE=",InspectVectorTable(Vector4.ONE))
print("Vector4.NEG_ZERO=",InspectVectorTable(Vector4.NEG_ZERO))
print("Vector4.NEG_ONE=",InspectVectorTable(Vector4.NEG_ONE))
print("Vector4.NEG_X=",InspectVectorTable(Vector4.NEG_X))
print("Vector4.NEG_Y=",InspectVectorTable(Vector4.NEG_Y))
print("-----------------")
"#).exec().expect("Lua syntax error!");
luau::error_wrapper(strafelua_vm.load(r#"
--Purposely throw an error
Squidward()
"#).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