diff --git a/yazi-fm/src/app/commands/plugin.rs b/yazi-fm/src/app/commands/plugin.rs index 872e6c13..34fdddf0 100644 --- a/yazi-fm/src/app/commands/plugin.rs +++ b/yazi-fm/src/app/commands/plugin.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic}; +use mlua::{ExternalError, ExternalResult, Table, TableExt}; use tracing::warn; use yazi_plugin::{LOADED, LUA}; use yazi_shared::{emit, event::Cmd, Layer}; @@ -35,20 +35,23 @@ impl App { Err(e) => return warn!("{e}"), }; - let args = Variadic::from_iter(opt.data.args.into_iter().filter_map(|v| v.into_lua(&LUA).ok())); let result = Lives::scope(&self.cx, |_| { LUA.globals().set("YAZI_PLUGIN_NAME", LUA.create_string(&opt.name)?)?; let mut plugin: Option = None; if let Some(b) = LOADED.read().get(&opt.name) { - plugin = LUA.load(b).call(args)?; + plugin = LUA.load(b).call(())?; } let Some(plugin) = plugin else { return Err("plugin not found".into_lua_err()); }; - if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", ()) } + if let Some(cb) = opt.data.cb { + cb(plugin) + } else { + plugin.call_method("entry", opt.data.args) + } }); let Some(tx) = opt.data.tx else { diff --git a/yazi-plugin/preset/state.lua b/yazi-plugin/preset/state.lua index 82eef48f..2c7ee591 100644 --- a/yazi-plugin/preset/state.lua +++ b/yazi-plugin/preset/state.lua @@ -1,16 +1,25 @@ local cache = {} +local sub_mt = { + __index = function(target, k) + local bucket = rawget(target, "__yazi_bucket") + return cache[bucket] and cache[bucket][k] + end, + __newindex = function(target, k, v) + local bucket = rawget(target, "__yazi_bucket") + cache[bucket] = cache[bucket] or {} + cache[bucket][k] = v + end, +} -state = setmetatable({ - clear = function() cache[YAZI_PLUGIN_NAME] = nil end, -}, { +state = setmetatable({}, { __index = function(_, k) local bucket = YAZI_PLUGIN_NAME return cache[bucket] and cache[bucket][k] end, - __newindex = function(_, k, v) local bucket = YAZI_PLUGIN_NAME cache[bucket] = cache[bucket] or {} cache[bucket][k] = v end, + __call = function() return setmetatable({ __yazi_bucket = YAZI_PLUGIN_NAME }, sub_mt) end, }) diff --git a/yazi-plugin/src/isolate/entry.rs b/yazi-plugin/src/isolate/entry.rs index 142c0ed5..73ddd006 100644 --- a/yazi-plugin/src/isolate/entry.rs +++ b/yazi-plugin/src/isolate/entry.rs @@ -1,4 +1,4 @@ -use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic}; +use mlua::{ExternalError, ExternalResult, Table, TableExt}; use tokio::runtime::Handle; use super::slim_lua; @@ -11,14 +11,13 @@ pub async fn entry(name: String, args: Vec) -> mlua::Result<()> { let lua = slim_lua()?; lua.globals().set("YAZI_PLUGIN_NAME", lua.create_string(&name)?)?; - let args = Variadic::from_iter(args.into_iter().filter_map(|v| v.into_lua(&lua).ok())); let plugin: Table = if let Some(b) = LOADED.read().get(&name) { - lua.load(b).call(args)? + lua.load(b).call(())? } else { return Err("unloaded plugin".into_lua_err()); }; - Handle::current().block_on(plugin.call_async_method("entry", ())) + Handle::current().block_on(plugin.call_async_method("entry", args)) }) .await .into_lua_err()?