Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit defb50d

Browse files
committed
Make result errors throw lua error rather than panic and crash
Fixes #10
1 parent 0571dbb commit defb50d

File tree

7 files changed

+77
-4
lines changed

7 files changed

+77
-4
lines changed

examples/engine/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn get_iface() -> Option<&'static EngineClient> {
99
#[lua_function]
1010
fn concmd(l: LuaState) -> i32 {
1111
if let Some(iface) = get_iface() {
12-
iface.ExecuteClientCmd( luaL_checklstring(l, 1, 0) );
12+
iface.ExecuteClientCmd( luaL_checkstring(l, 1) );
1313
}
1414
0
1515
}

examples/exception/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "gmod_exception"
3+
description = "Binary module to display exception handling with rglua"
4+
version = "0.1.0"
5+
edition = "2021"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
rglua = { path = "../../rglua" }
13+
thiserror = "1.0.30"

examples/exception/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use rglua::prelude::*;
2+
3+
#[derive(Debug, thiserror::Error)]
4+
enum ResultError {
5+
#[error("Number was negative!")]
6+
Negative,
7+
#[error("Number greater than 5!")]
8+
Catastrophic
9+
}
10+
11+
#[lua_function]
12+
fn result(l: LuaState) -> Result<i32, ResultError> {
13+
let num = luaL_checkinteger(l, 1);
14+
15+
if num > 5 {
16+
return Err( ResultError::Catastrophic )
17+
} else if num < 0 {
18+
return Err( ResultError::Negative )
19+
} else {
20+
lua_pushinteger(l, num);
21+
return Ok(1)
22+
}
23+
}
24+
25+
#[gmod_open]
26+
fn open(l: LuaState) -> i32 {
27+
printgm!(l, "Loaded exception module!");
28+
29+
let lib = reg! [
30+
// "panic" => panic, (Soon™️)
31+
"result" => result
32+
];
33+
34+
luaL_register(l, cstr!("except"), lib.as_ptr());
35+
0
36+
}
37+
38+
#[gmod_close]
39+
fn close(_l: LuaState) -> i32 {
40+
0
41+
}

rglua-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rglua-macros"
33
description = "Procedural macros to be used with rglua"
4-
version = "0.1.0"
4+
version = "0.2.0"
55
authors = ["Vurv <[email protected]>"]
66
keywords = ["glua", "garrysmod", "lua", "gmod"]
77
categories = ["api-bindings", "external-ffi-bindings", "development-tools::ffi", "game-development", "accessibility"]

rglua-macros/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@ fn handle_gmod(item: TokenStream, export: Option<&str>) -> TokenStream {
7979
#(#inner_stmts)*
8080
}
8181
};
82+
8283
let resultant = quote! {
83-
return #inner_fn(#lua_shared_param).unwrap();
84+
match #inner_fn(#lua_shared_param) {
85+
Err(why) => {
86+
// Your error must implement display / .to_string().
87+
// I'd recommend ``thiserror``.
88+
let err = why.to_string();
89+
let err = cstr!(err);
90+
rglua::lua::luaL_error(#lua_shared_param, cstr!("%s"), err.as_ptr());
91+
},
92+
Ok(n) => { return n }
93+
}
8494
};
8595

8696
ast.block

rglua-macros/tests/base.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ use rglua_macros::{gmod_close, gmod_open, lua_function};
44
#[derive(Debug)]
55
enum LuaError {}
66

7+
8+
// The errors you return must implement display, to be relayed to gmod.
9+
use std::fmt::{Display, Formatter};
10+
impl Display for LuaError {
11+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12+
write!(f, "LuaError")
13+
}
14+
}
15+
716
#[lua_function]
817
fn add(_state: LuaState) -> Result<i32, LuaError> {
918
Ok(0)

rglua/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rglua"
33
description = "Toolkit for garrysmod development with the source sdk and luajit api"
4-
version = "2.0.1"
4+
version = "2.1.0"
55
authors = ["Vurv <[email protected]>"]
66
keywords = ["glua", "garrysmod", "lua", "gmod"]
77
categories = ["api-bindings", "external-ffi-bindings", "development-tools::ffi", "game-development", "accessibility"]

0 commit comments

Comments
 (0)