From 23f36b2afb7bdc36a5638a568ba5507bc764eb67 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 25 Jul 2022 07:21:07 -0700 Subject: [PATCH] json: fix visited hash set We were skipping scalars/primitive values, which wasn't the intent. refs: https://github.com/wez/wezterm/issues/2302 --- lua-api-crates/json/src/lib.rs | 14 ++++++++------ luahelper/src/lib.rs | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lua-api-crates/json/src/lib.rs b/lua-api-crates/json/src/lib.rs index 5d4ddc4dd..595da467b 100644 --- a/lua-api-crates/json/src/lib.rs +++ b/lua-api-crates/json/src/lib.rs @@ -52,13 +52,15 @@ fn json_parse<'lua>(lua: &'lua Lua, text: String) -> mlua::Result { } fn lua_value_to_json_value(value: LuaValue, visited: &mut HashSet) -> mlua::Result { - let ptr = value.to_pointer() as usize; - if visited.contains(&ptr) { - // Skip this one, as we've seen it before. - // Treat it as a Null value. - return Ok(JValue::Null); + if let LuaValue::Table(_) = &value { + let ptr = value.to_pointer() as usize; + if visited.contains(&ptr) { + // Skip this one, as we've seen it before. + // Treat it as a Null value. + return Ok(JValue::Null); + } + visited.insert(ptr); } - visited.insert(ptr); Ok(match value { LuaValue::Nil => JValue::Null, LuaValue::String(s) => JValue::String(s.to_str()?.to_string()), diff --git a/luahelper/src/lib.rs b/luahelper/src/lib.rs index d0a637c01..4001dce9b 100644 --- a/luahelper/src/lib.rs +++ b/luahelper/src/lib.rs @@ -95,13 +95,15 @@ fn lua_value_to_dynamic_impl( value: LuaValue, visited: &mut HashSet, ) -> mlua::Result { - let ptr = value.to_pointer() as usize; - if visited.contains(&ptr) { - // Skip this one, as we've seen it before. - // Treat it as a Null value. - return Ok(DynValue::Null); + if let LuaValue::Table(_) = &value { + let ptr = value.to_pointer() as usize; + if visited.contains(&ptr) { + // Skip this one, as we've seen it before. + // Treat it as a Null value. + return Ok(DynValue::Null); + } + visited.insert(ptr); } - visited.insert(ptr); Ok(match value { LuaValue::Nil => DynValue::Null, LuaValue::String(s) => DynValue::String(s.to_str()?.to_string()),