From cc568ee1fe7d03a676370c92a8f1e1fdf70d37d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Fri, 24 Jan 2025 23:19:29 +0800 Subject: [PATCH] fix: JSON value `null` should be deserialized as Lua `nil`, not lightweight userdata `null` (#2242) --- yazi-plugin/src/config/config.rs | 10 +++++----- yazi-plugin/src/lib.rs | 6 ++---- yazi-plugin/src/lua.rs | 2 +- yazi-plugin/src/utils/json.rs | 4 ++-- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/yazi-plugin/src/config/config.rs b/yazi-plugin/src/config/config.rs index 0e1f853d..ba217e06 100644 --- a/yazi-plugin/src/config/config.rs +++ b/yazi-plugin/src/config/config.rs @@ -4,7 +4,7 @@ use yazi_config::{MANAGER, PREVIEW, THEME}; use super::Plugin; -const OPTIONS: SerializeOptions = +pub const SER_OPTS: SerializeOptions = SerializeOptions::new().serialize_none_to_null(false).serialize_unit_to_null(false); pub struct Config<'a> { @@ -15,22 +15,22 @@ impl<'a> Config<'a> { pub fn new(lua: &'a Lua) -> Self { Self { lua } } pub fn install_boot(self) -> mlua::Result { - self.lua.globals().raw_set("BOOT", self.lua.to_value_with(&*BOOT, OPTIONS)?)?; + self.lua.globals().raw_set("BOOT", self.lua.to_value_with(&*BOOT, SER_OPTS)?)?; Ok(self) } pub fn install_manager(self) -> mlua::Result { - self.lua.globals().raw_set("MANAGER", self.lua.to_value_with(&*MANAGER, OPTIONS)?)?; + self.lua.globals().raw_set("MANAGER", self.lua.to_value_with(&*MANAGER, SER_OPTS)?)?; Ok(self) } pub fn install_theme(self) -> mlua::Result { - self.lua.globals().raw_set("THEME", self.lua.to_value_with(&*THEME, OPTIONS)?)?; + self.lua.globals().raw_set("THEME", self.lua.to_value_with(&*THEME, SER_OPTS)?)?; Ok(self) } pub fn install_preview(self) -> mlua::Result { - self.lua.globals().raw_set("PREVIEW", self.lua.to_value_with(&*PREVIEW, OPTIONS)?)?; + self.lua.globals().raw_set("PREVIEW", self.lua.to_value_with(&*PREVIEW, SER_OPTS)?)?; Ok(self) } diff --git a/yazi-plugin/src/lib.rs b/yazi-plugin/src/lib.rs index 354e0c30..81e3d743 100644 --- a/yazi-plugin/src/lib.rs +++ b/yazi-plugin/src/lib.rs @@ -2,11 +2,9 @@ mod macros; -yazi_macro::mod_pub!( - bindings, elements, external, file, fs, isolate, loader, process, pubsub, url, utils -); +yazi_macro::mod_pub!(bindings config elements external file fs isolate loader process pubsub url utils); -yazi_macro::mod_flat!(clipboard config error lua runtime); +yazi_macro::mod_flat!(clipboard error lua runtime); pub fn init() -> anyhow::Result<()> { CLIPBOARD.with(<_>::default); diff --git a/yazi-plugin/src/lua.rs b/yazi-plugin/src/lua.rs index f0cf7f15..26109f7c 100644 --- a/yazi-plugin/src/lua.rs +++ b/yazi-plugin/src/lua.rs @@ -18,7 +18,7 @@ pub(super) fn init_lua() -> Result<()> { fn stage_1(lua: &'static Lua) -> Result<()> { lua.set_named_registry_value("rt", Runtime::default())?; - crate::Config::new(lua).install_boot()?.install_manager()?.install_theme()?; + crate::config::Config::new(lua).install_boot()?.install_manager()?.install_theme()?; // Base let globals = lua.globals(); diff --git a/yazi-plugin/src/utils/json.rs b/yazi-plugin/src/utils/json.rs index 5913f992..97227cea 100644 --- a/yazi-plugin/src/utils/json.rs +++ b/yazi-plugin/src/utils/json.rs @@ -1,7 +1,7 @@ use mlua::{Function, IntoLuaMulti, Lua, LuaSerdeExt, Value}; use super::Utils; -use crate::Error; +use crate::{Error, config::SER_OPTS}; impl Utils { pub(super) fn json_encode(lua: &Lua) -> mlua::Result { @@ -16,7 +16,7 @@ impl Utils { pub(super) fn json_decode(lua: &Lua) -> mlua::Result { lua.create_async_function(|lua, s: mlua::String| async move { match serde_json::from_slice::(&s.as_bytes()) { - Ok(v) => (lua.to_value(&v)?, Value::Nil).into_lua_multi(&lua), + Ok(v) => (lua.to_value_with(&v, SER_OPTS)?, Value::Nil).into_lua_multi(&lua), Err(e) => (Value::Nil, Error::Serde(e)).into_lua_multi(&lua), } })