Merge pull request #171 from tpdickso/master

AnyLuaValue reads numeric strings as strings (fix #170)
This commit is contained in:
tomaka 2017-11-01 16:12:01 +01:00 committed by GitHub
commit b486640a51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

24
hlua/src/any.rs Normal file → Executable file
View File

@ -94,6 +94,27 @@ impl<'lua, L> LuaRead<L> for AnyLuaValue
{ {
#[inline] #[inline]
fn lua_read_at_position(lua: L, index: i32) -> Result<AnyLuaValue, L> { fn lua_read_at_position(lua: L, index: i32) -> Result<AnyLuaValue, L> {
// If we know that the value on the stack is a string, we should try
// to parse it as a string instead of a number or boolean, so that
// values such as '1.10' don't become `AnyLuaValue::LuaNumber(1.1)`.
let data_type = unsafe { ffi::lua_type(lua.as_lua().0, index) };
if data_type == ffi::LUA_TSTRING {
let lua = match LuaRead::lua_read_at_position(&lua, index) {
Ok(v) => return Ok(AnyLuaValue::LuaString(v)),
Err(lua) => lua,
};
let _lua = match LuaRead::lua_read_at_position(&lua, index) {
Ok(v) => return Ok(AnyLuaValue::LuaAnyString(v)),
Err(lua) => lua,
};
Ok(AnyLuaValue::LuaOther)
} else {
let lua = match LuaRead::lua_read_at_position(&lua, index) { let lua = match LuaRead::lua_read_at_position(&lua, index) {
Ok(v) => return Ok(AnyLuaValue::LuaNumber(v)), Ok(v) => return Ok(AnyLuaValue::LuaNumber(v)),
Err(lua) => lua, Err(lua) => lua,
@ -125,6 +146,7 @@ impl<'lua, L> LuaRead<L> for AnyLuaValue
Ok(AnyLuaValue::LuaOther) Ok(AnyLuaValue::LuaOther)
} }
}
} }
impl<'lua, L> Push<L> for AnyHashableLuaValue impl<'lua, L> Push<L> for AnyHashableLuaValue
@ -227,7 +249,7 @@ mod tests {
lua.set("b", 3.5f32); lua.set("b", 3.5f32);
let x: AnyLuaValue = lua.get("a").unwrap(); let x: AnyLuaValue = lua.get("a").unwrap();
assert_eq!(x, AnyLuaValue::LuaNumber(-2.0)); assert_eq!(x, AnyLuaValue::LuaString("-2".to_owned()));
let y: AnyLuaValue = lua.get("b").unwrap(); let y: AnyLuaValue = lua.get("b").unwrap();
assert_eq!(y, AnyLuaValue::LuaNumber(3.5)); assert_eq!(y, AnyLuaValue::LuaNumber(3.5));