diff --git a/yazi-fm/src/app/commands/accept_payload.rs b/yazi-fm/src/app/commands/accept_payload.rs index 5f15e0fd..af460b04 100644 --- a/yazi-fm/src/app/commands/accept_payload.rs +++ b/yazi-fm/src/app/commands/accept_payload.rs @@ -1,7 +1,7 @@ use mlua::IntoLua; use tracing::error; use yazi_dds::{LOCAL, Payload, REMOTE}; -use yazi_plugin::{LUA, RtRefMut}; +use yazi_plugin::{LUA, runtime_mut}; use yazi_shared::event::CmdCow; use crate::{app::App, lives::Lives}; @@ -27,11 +27,11 @@ impl App { _ = Lives::scope(&self.cx, || { let body = payload.body.into_lua(&LUA)?; for (id, cb) in handlers { - LUA.named_registry_value::("ir")?.push(&id); + runtime_mut!(LUA)?.push(&id); if let Err(e) = cb.call::<()>(body.clone()) { error!("Failed to run `{kind}` event handler in your `{id}` plugin: {e}"); } - LUA.named_registry_value::("ir")?.pop(); + runtime_mut!(LUA)?.pop(); } Ok(()) }); diff --git a/yazi-fm/src/app/commands/plugin.rs b/yazi-fm/src/app/commands/plugin.rs index cd5aaef2..ba997c69 100644 --- a/yazi-fm/src/app/commands/plugin.rs +++ b/yazi-fm/src/app/commands/plugin.rs @@ -4,7 +4,7 @@ use mlua::ObjectLike; use scopeguard::defer; use tracing::{error, warn}; use yazi_dds::Sendable; -use yazi_plugin::{LUA, RtRefMut, loader::{LOADER, Loader}}; +use yazi_plugin::{LUA, loader::{LOADER, Loader}, runtime_mut}; use yazi_proxy::{AppProxy, options::{PluginMode, PluginOpt}}; use crate::{app::App, lives::Lives}; @@ -55,11 +55,11 @@ impl App { return self.cx.tasks.plugin_micro(opt); } - match LUA.named_registry_value::("ir") { + match runtime_mut!(LUA) { Ok(mut r) => r.push(&opt.id), Err(e) => return warn!("{e}"), } - defer! { _ = LUA.named_registry_value::("ir").map(|mut r| r.pop()) } + defer! { _ = runtime_mut!(LUA).map(|mut r| r.pop()) } let plugin = match LOADER.load_with(&LUA, &opt.id, chunk) { Ok(t) => t, diff --git a/yazi-plugin/src/isolate/isolate.rs b/yazi-plugin/src/isolate/isolate.rs index d3fae54c..40afddc5 100644 --- a/yazi-plugin/src/isolate/isolate.rs +++ b/yazi-plugin/src/isolate/isolate.rs @@ -5,7 +5,7 @@ use crate::runtime::Runtime; pub fn slim_lua(name: &str) -> mlua::Result { let lua = Lua::new(); - lua.set_named_registry_value("ir", Runtime::new(name))?; + lua.set_app_data(Runtime::new(name)); // Base let globals = lua.globals(); diff --git a/yazi-plugin/src/loader/require.rs b/yazi-plugin/src/loader/require.rs index fb497fbf..0351953e 100644 --- a/yazi-plugin/src/loader/require.rs +++ b/yazi-plugin/src/loader/require.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use mlua::{ExternalResult, Function, IntoLua, Lua, MetaMethod, MultiValue, ObjectLike, Table, Value}; use super::LOADER; -use crate::RtRefMut; +use crate::runtime_mut; pub(super) struct Require; @@ -15,9 +15,9 @@ impl Require { let s = &id.to_str()?; futures::executor::block_on(LOADER.ensure(s, |_| ())).into_lua_err()?; - lua.named_registry_value::("ir")?.push(s); + runtime_mut!(lua)?.push(s); let mod_ = LOADER.load(lua, s); - lua.named_registry_value::("ir")?.pop(); + runtime_mut!(lua)?.pop(); Self::create_mt(lua, s, mod_?, true) })?, @@ -31,9 +31,9 @@ impl Require { let s = &id.to_str()?; LOADER.ensure(s, |_| ()).await.into_lua_err()?; - lua.named_registry_value::("ir")?.push(s); + runtime_mut!(lua)?.push(s); let mod_ = LOADER.load(&lua, s); - lua.named_registry_value::("ir")?.pop(); + runtime_mut!(lua)?.pop(); Self::create_mt(&lua, s, mod_?, false) })?, @@ -73,9 +73,9 @@ impl Require { if sync { lua.create_function(move |lua, args: MultiValue| { let (r#mod, args) = Self::split_mod_and_args(lua, &id, args)?; - lua.named_registry_value::("ir")?.push(&id); + runtime_mut!(lua)?.push(&id); let result = r#mod.call_function::(&f, args); - lua.named_registry_value::("ir")?.pop(); + runtime_mut!(lua)?.pop(); result }) } else { @@ -83,9 +83,9 @@ impl Require { let (id, f) = (id.clone(), f.clone()); async move { let (r#mod, args) = Self::split_mod_and_args(&lua, &id, args)?; - lua.named_registry_value::("ir")?.push(&id); + runtime_mut!(lua)?.push(&id); let result = r#mod.call_async_function::(&f, args).await; - lua.named_registry_value::("ir")?.pop(); + runtime_mut!(lua)?.pop(); result } }) diff --git a/yazi-plugin/src/lua.rs b/yazi-plugin/src/lua.rs index 22c78263..3fea3abb 100644 --- a/yazi-plugin/src/lua.rs +++ b/yazi-plugin/src/lua.rs @@ -17,7 +17,7 @@ pub(super) fn init_lua() -> Result<()> { } fn stage_1(lua: &'static Lua) -> Result<()> { - lua.set_named_registry_value("ir", Runtime::default())?; + lua.set_app_data(Runtime::default()); // Base let globals = lua.globals(); diff --git a/yazi-plugin/src/macros.rs b/yazi-plugin/src/macros.rs index 3834d44d..7fd425b8 100644 --- a/yazi-plugin/src/macros.rs +++ b/yazi-plugin/src/macros.rs @@ -120,6 +120,22 @@ macro_rules! impl_file_methods { }; } +#[macro_export] +macro_rules! runtime { + ($lua:ident) => {{ + use mlua::ExternalError; + $lua.app_data_ref::<$crate::Runtime>().ok_or_else(|| "Runtime not found".into_lua_err()) + }}; +} + +#[macro_export] +macro_rules! runtime_mut { + ($lua:ident) => {{ + use mlua::ExternalError; + $lua.app_data_mut::<$crate::Runtime>().ok_or_else(|| "Runtime not found".into_lua_err()) + }}; +} + #[macro_export] macro_rules! deprecate { ($lua:ident, $tt:tt) => {{ diff --git a/yazi-plugin/src/pubsub/pubsub.rs b/yazi-plugin/src/pubsub/pubsub.rs index 565f6b18..5f44b344 100644 --- a/yazi-plugin/src/pubsub/pubsub.rs +++ b/yazi-plugin/src/pubsub/pubsub.rs @@ -2,7 +2,7 @@ use mlua::{ExternalResult, Function, Lua, Value}; use yazi_binding::Id; use yazi_dds::body::Body; -use crate::runtime::RtRef; +use crate::runtime; pub struct Pubsub; @@ -23,7 +23,7 @@ impl Pubsub { pub(super) fn sub(lua: &Lua) -> mlua::Result { lua.create_function(|lua, (kind, f): (mlua::String, Function)| { - let rt = lua.named_registry_value::("ir")?; + let rt = runtime!(lua)?; let Some(cur) = rt.current() else { return Err("`sub()` must be called in a sync plugin").into_lua_err(); }; @@ -36,7 +36,7 @@ impl Pubsub { pub(super) fn sub_remote(lua: &Lua) -> mlua::Result { lua.create_function(|lua, (kind, f): (mlua::String, Function)| { - let rt = lua.named_registry_value::("ir")?; + let rt = runtime!(lua)?; let Some(cur) = rt.current() else { return Err("`sub_remote()` must be called in a sync plugin").into_lua_err(); }; @@ -49,7 +49,7 @@ impl Pubsub { pub(super) fn unsub(lua: &Lua) -> mlua::Result { lua.create_function(|lua, kind: mlua::String| { - if let Some(cur) = lua.named_registry_value::("ir")?.current() { + if let Some(cur) = runtime!(lua)?.current() { Ok(yazi_dds::Pubsub::unsub(cur, &kind.to_str()?)) } else { Err("`unsub()` must be called in a sync plugin").into_lua_err() @@ -59,7 +59,7 @@ impl Pubsub { pub(super) fn unsub_remote(lua: &Lua) -> mlua::Result { lua.create_function(|lua, kind: mlua::String| { - if let Some(cur) = lua.named_registry_value::("ir")?.current() { + if let Some(cur) = runtime!(lua)?.current() { Ok(yazi_dds::Pubsub::unsub_remote(cur, &kind.to_str()?)) } else { Err("`unsub_remote()` must be called in a sync plugin").into_lua_err() diff --git a/yazi-plugin/src/runtime.rs b/yazi-plugin/src/runtime.rs index 2d028040..17da1ad2 100644 --- a/yazi-plugin/src/runtime.rs +++ b/yazi-plugin/src/runtime.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, VecDeque}; -use mlua::{Function, UserData}; +use mlua::Function; #[derive(Debug, Default)] pub struct Runtime { @@ -14,9 +14,6 @@ struct RuntimeFrame { calls: usize, } -pub type RtRef = mlua::UserDataRef; -pub type RtRefMut = mlua::UserDataRefMut; - impl Runtime { pub fn new(id: &str) -> Self { Self { @@ -56,5 +53,3 @@ impl Runtime { true } } - -impl UserData for Runtime {} diff --git a/yazi-plugin/src/utils/sync.rs b/yazi-plugin/src/utils/sync.rs index e9fd6b8e..dededa66 100644 --- a/yazi-plugin/src/utils/sync.rs +++ b/yazi-plugin/src/utils/sync.rs @@ -6,18 +6,18 @@ use yazi_proxy::{AppProxy, options::{PluginCallback, PluginOpt}}; use yazi_shared::event::Data; use super::Utils; -use crate::{RtRefMut, bindings::{MpscRx, MpscTx, MpscUnboundedRx, MpscUnboundedTx, OneshotRx, OneshotTx}, loader::LOADER, runtime::RtRef}; +use crate::{bindings::{MpscRx, MpscTx, MpscUnboundedRx, MpscUnboundedTx, OneshotRx, OneshotTx}, loader::LOADER, runtime, runtime_mut}; impl Utils { pub(super) fn sync(lua: &Lua, isolate: bool) -> mlua::Result { if isolate { lua.create_function(|lua, ()| { - let Some(block) = lua.named_registry_value::("ir")?.next_block() else { + let Some(block) = runtime_mut!(lua)?.next_block() else { return Err("`ya.sync()` must be called in a plugin").into_lua_err(); }; lua.create_async_function(move |lua, args: MultiValue| async move { - let Some(cur) = lua.named_registry_value::("ir")?.current_owned() else { + let Some(cur) = runtime!(lua)?.current_owned() else { return Err("block spawned by `ya.sync()` must be called in a plugin").into_lua_err(); }; Sendable::list_to_values(&lua, Self::retrieve(cur, block, args).await?) @@ -25,7 +25,7 @@ impl Utils { }) } else { lua.create_function(|lua, f: Function| { - let mut rt = lua.named_registry_value::("ir")?; + let mut rt = runtime_mut!(lua)?; if !rt.put_block(f.clone()) { return Err("`ya.sync()` must be called in a plugin").into_lua_err(); } @@ -84,7 +84,7 @@ impl Utils { let callback: PluginCallback = { let id = id.clone(); Box::new(move |lua, plugin| { - let Some(block) = lua.named_registry_value::("ir")?.get_block(&id, calls) else { + let Some(block) = runtime!(lua)?.get_block(&id, calls) else { return Err("sync block not found".into_lua_err()); };