Skip to content

Commit 3851530

Browse files
committed
Merge branch 'wrong-type-verbosity' into 'master'
feat(lua_ffi): make WrongType error more verbose See merge request picodata/brod/tarantool-module!49
2 parents 71c6536 + 16503fd commit 3851530

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

hlua/ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ extern "C" {
112112
pub fn lua_gettable(l: *mut lua_State, index: c_int);
113113
pub fn lua_settable(l: *mut lua_State, idx: c_int);
114114
pub fn lua_type(state: *mut lua_State, index: c_int) -> c_int;
115+
pub fn lua_typename(state: *mut lua_State, tp: c_int) -> *mut c_schar;
115116
pub fn lua_setmetatable(l: *mut lua_State, objindex: c_int) -> c_int;
116117
pub fn lua_getmetatable(l: *mut lua_State, objindex: c_int) -> c_int;
117118

hlua/hlua/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ pub enum LuaError {
408408
ReadError(IoError),
409409

410410
/// The call to `execute` has requested the wrong type of data.
411-
WrongType,
411+
WrongType{
412+
rust_expected: String,
413+
lua_actual: String,
414+
},
412415
}
413416

414417
impl fmt::Display for LuaError {
@@ -419,7 +422,10 @@ impl fmt::Display for LuaError {
419422
SyntaxError(ref s) => write!(f, "Syntax error: {}", s),
420423
ExecutionError(ref s) => write!(f, "Execution error: {}", s),
421424
ReadError(ref e) => write!(f, "Read error: {}", e),
422-
WrongType => write!(f, "Wrong type returned by Lua"),
425+
WrongType{
426+
rust_expected: ref e1,
427+
lua_actual: ref e2
428+
} => write!(f, "Wrong type returned by Lua: {} expected, got {}", e1, e2),
423429
}
424430
}
425431
}
@@ -432,7 +438,7 @@ impl Error for LuaError {
432438
SyntaxError(ref s) => &s,
433439
ExecutionError(ref s) => &s,
434440
ReadError(_) => "read error",
435-
WrongType => "wrong type returned by Lua",
441+
WrongType{rust_expected: _, lua_actual: _} => "wrong type returned by Lua",
436442
}
437443
}
438444

@@ -443,7 +449,7 @@ impl Error for LuaError {
443449
SyntaxError(_) => None,
444450
ExecutionError(_) => None,
445451
ReadError(ref e) => Some(e),
446-
WrongType => None,
452+
WrongType{rust_expected: _, lua_actual: _} => None,
447453
}
448454
}
449455
}

hlua/hlua/src/lua_functions.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,14 @@ impl<'lua, L> LuaFunction<L>
284284

285285
match pcall_return_value {
286286
0 => match LuaRead::lua_read(pushed_value) {
287-
Err(_) => Err(LuaFunctionCallError::LuaError(LuaError::WrongType)),
287+
Err(lua) => Err(LuaFunctionCallError::LuaError(LuaError::WrongType{
288+
rust_expected: std::any::type_name::<V>().to_string(),
289+
lua_actual: unsafe {
290+
let lua_type = ffi::lua_type(lua.raw_lua.state_ptr(), -1);
291+
let typename = ffi::lua_typename(lua.raw_lua.state_ptr(), lua_type);
292+
std::ffi::CStr::from_ptr(typename).to_string_lossy().into_owned()
293+
}
294+
})),
288295
Ok(x) => Ok(x),
289296
},
290297
ffi::LUA_ERRMEM => panic!("lua_pcall returned LUA_ERRMEM"),

tests/src/hlua/lua_functions.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,26 @@ pub fn execution_error() {
4444
};
4545
}
4646

47-
pub fn wrong_type() {
47+
pub fn check_types() {
4848
let mut lua = crate::hlua::global();
4949
let mut f = LuaFunction::load(&mut lua, "return 12").unwrap();
50-
match f.call::<LuaFunction<_>>() {
51-
Err(LuaError::WrongType) => (),
52-
_ => panic!(),
50+
let err = f.call::<bool>().unwrap_err();
51+
match err {
52+
LuaError::WrongType{ref rust_expected, ref lua_actual} => {
53+
assert_eq!(rust_expected, "bool");
54+
assert_eq!(lua_actual, "number");
55+
},
56+
v => panic!("{}", v),
5357
};
58+
assert_eq!(
59+
err.to_string(),
60+
"Wrong type returned by Lua: bool expected, got number"
61+
);
62+
63+
assert_eq!(f.call::<i32>().unwrap(), 12i32);
64+
assert_eq!(f.call::<f32>().unwrap(), 12f32);
65+
assert_eq!(f.call::<f64>().unwrap(), 12f64);
66+
assert_eq!(f.call::<String>().unwrap(), "12".to_string());
5467
}
5568

5669
pub fn call_and_read_table() {

tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn run_tests(cfg: TestConfig) -> Result<bool, io::Error> {
183183
hlua::lua_functions::args_in_order,
184184
hlua::lua_functions::syntax_error,
185185
hlua::lua_functions::execution_error,
186-
hlua::lua_functions::wrong_type,
186+
hlua::lua_functions::check_types,
187187
hlua::lua_functions::call_and_read_table,
188188
hlua::lua_functions::lua_function_returns_function,
189189
hlua::lua_functions::execute_from_reader_errors_if_cant_read,

0 commit comments

Comments
 (0)