Add `pcall_ignore`

This commit is contained in:
William Venner 2021-11-02 15:13:08 +00:00
parent 2b3de9234c
commit 3ad2750c09
2 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "gmod" name = "gmod"
version = "9.0.0" version = "9.1.0"
authors = ["William Venner <william@venner.io>"] authors = ["William Venner <william@venner.io>"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"

View File

@ -155,6 +155,25 @@ impl LuaState {
(LUA_SHARED.lua_pcall)(*self, nargs, nresults, errfunc) (LUA_SHARED.lua_pcall)(*self, nargs, nresults, errfunc)
} }
/// Same as pcall, but ignores any runtime error and calls `ErrorNoHaltWithStack` instead with the error message.
///
/// Returns whether the execution was successful.
pub unsafe fn pcall_ignore(&self, nargs: i32, nresults: i32) -> bool {
let res = self.pcall(nargs, nresults, 0);
if res == LUA_ERRRUN {
self.get_global(crate::lua_string!("ErrorNoHaltWithStack"));
if self.is_nil(-1) {
eprintln!("[ERROR] {:?}", self.get_string(-2));
return false;
}
self.push_value(-2);
self.call(1, 0);
self.pop();
return false;
}
true
}
pub unsafe fn load_string(&self, src: LuaString) -> Result<(), LuaError> { pub unsafe fn load_string(&self, src: LuaString) -> Result<(), LuaError> {
let lua_error_code = (LUA_SHARED.lual_loadstring)(*self, src); let lua_error_code = (LUA_SHARED.lual_loadstring)(*self, src);
if lua_error_code == 0 { if lua_error_code == 0 {
@ -259,6 +278,9 @@ impl LuaState {
} }
#[inline(always)] #[inline(always)]
/// WARNING: Any Lua errors caused by calling the function will longjmp and prevent any further execution of your code.
///
/// To workaround this, use `pcall_ignore`, which will call `ErrorNoHaltWithStack` instead and allow your code to continue executing.
pub unsafe fn call(&self, nargs: i32, nresults: i32) { pub unsafe fn call(&self, nargs: i32, nresults: i32) {
(LUA_SHARED.lua_call)(*self, nargs, nresults) (LUA_SHARED.lua_call)(*self, nargs, nresults)
} }