summary refs log tree commit diff stats
path: root/src/mock
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-06-16 22:43:18 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-06-16 22:43:18 -0300
commit890d8bbef986d8b85d6eaaddd4c8895c5f61c09c (patch)
tree20bafad4faf0689db30c1f0c1382eb1824362b8f /src/mock
parentf9f0bfb13ea9e7f0b0d14887a69961657befaab2 (diff)
Migrate to edition 2021
Diffstat (limited to 'src/mock')
-rw-r--r--src/mock/mod.rs218
1 files changed, 122 insertions, 96 deletions
diff --git a/src/mock/mod.rs b/src/mock/mod.rs
index ed41780..92de66c 100644
--- a/src/mock/mod.rs
+++ b/src/mock/mod.rs
@@ -46,19 +46,25 @@ use std::marker::PhantomData;
 use std::marker::PhantomPinned;
 use std::pin::Pin;
 use std::ptr;
+use std::rc::Rc;
 
 use libc::c_char;
 
+use ::ltptr::*;
+
+use ::selfref::{Holder, Opaque, SelfRef, opaque};
+
+// MUST be repr(C)
 #[repr(C)]
-struct MockPlugin {
+struct MockPlugin<'dangling, 'env> {
     // NOTE: MUST be first thing in the struct.
     methods: crate::internals::Ph,
     filename: *const c_char,
-    plugin_name: *const c_char,
-    plugin_desc: *const c_char,
-    plugin_vers: *const c_char,
+    plugin_name: Cell<*const c_char>,
+    plugin_desc: Cell<*const c_char>,
+    plugin_vers: Cell<*const c_char>,
     free_strings: bool,
-    env: *const PluginEnvironment,
+    env: &'env PluginEnvironment<'dangling, 'env>,
     _pin: PhantomPinned,
 }
 
@@ -90,21 +96,40 @@ enum MockCallback {
     HookDeleted,
 }
 
-// This MockHook is deliberately incompatible with hexchat's own hook struct.
-struct MockHook<'hook, 'env> {
-    pl: &MockPlugin<'env>,
+struct MockHook<'dangling, 'env> {
+    //pl: Rc<Holder<'dangling, MockPluginK<'dangling, 'env>>>,
+    pl: Rc<MockPlugin<'dangling, 'env>>,
     cb: RefCell<MockCallback>,
     name: Option<Box<str>>,
     pri_or_fd: libc::c_int,
     userdata: *mut libc::c_void,
+    _pin: PhantomPinned,
 }
 
-pub struct PluginEnvironment<'env> {
-    plugins: Option<Vec<Pin<Box<MockPluginOpaque<'env>>>>>,
-    hooks: Option<Vec<Pin<Box<MockHookOpaque<'env>>>>>,
-    depth: usize,
+pub struct PluginEnvironment<'dangling, 'env> {
+    //plugins: RefCell<Vec<Pin<Rc<Holder<'dangling, MockPluginK<'dangling, 'env>>>>>>,
+    plugins: RefCell<Vec<Pin<Rc<MockPlugin<'dangling, 'env>>>>>,
+    // priority-based hooks
+    //hooks: RefCell<Vec<Pin<Rc<Holder<'dangling, MockHookK<'dangling, 'env>>>>>>,
+    hooks: RefCell<Vec<Pin<Rc<MockHook<'dangling, 'env>>>>>,
+    // fd/timer hooks
+    //extra_hooks: RefCell<Vec<Pin<Rc<Holder<'dangling, MockHookK<'dangling, 'env>>>>>>,
+    extra_hooks: RefCell<Vec<Pin<Rc<MockHook<'dangling, 'env>>>>>,
+    depth: Cell<usize>,
+}
+impl<'dangling, 'env> SelfRef<'env> for PluginEnvironment<'dangling, 'env> {}
+
+pub struct PluginEnvironmentK<'dangling> {
+    _p: PhantomData<&'dangling ()>,
+}
+opaque! {
+    impl['dangling] Opaque for PluginEnvironmentK<'dangling> {
+        type Kind<'env> = PluginEnvironment<'dangling, 'env>;
+    }
 }
 
+pub type GlobalPluginEnvironment<'dangling> = Holder<'dangling, PluginEnvironmentK<'dangling>>;
+
 unsafe extern "C" fn hexchat_hook_command(
     ph: *mut crate::internals::HexchatPlugin,
     name: *const libc::c_char,
@@ -325,93 +350,94 @@ unsafe extern "C" fn hexchat_event_attrs_free(ph: *mut crate::internals::Hexchat
     todo!();
 }
 
-impl PluginEnvironment {
-    pub fn new() -> Pin<Box<Self>> {
-        Box::pin(Self {
+impl<'dangling, 'env> PluginEnvironment<'dangling, 'env> {
+    pub fn new() -> Self {
+        Self {
             plugins: Default::default(),
             hooks: Default::default(),
+            extra_hooks: Default::default(),
             depth: Default::default(),
-            _pin: PhantomPinned,
-        })
-    }
-}
-
-impl MockPlugin {
-    pub fn new<T: for<'ph> crate::Plugin<'ph> + Default>(
-        env: Pin<&PluginEnvironment>,
-        filename: CString,
-        arg: Option<&CStr>,
-    ) -> Option<Pin<Box<MockPlugin>>> {
-        let filename = filename.into_raw();
-        let mut plug = Box::pin(MockPlugin {
-            methods: crate::internals::Ph {
-                hexchat_hook_command,
-                hexchat_hook_server,
-                hexchat_hook_print,
-                hexchat_hook_timer,
-                hexchat_hook_fd,
-                hexchat_unhook,
-                hexchat_print,
-                userdata: hexchat_printf as *mut libc::c_void,
-                hexchat_command,
-                hexchat_commandf_do_not_use: hexchat_commandf,
-                hexchat_nickcmp,
-                hexchat_set_context,
-                hexchat_find_context,
-                hexchat_get_context,
-                hexchat_get_info,
-                hexchat_get_prefs,
-                hexchat_list_get,
-                hexchat_list_free,
-                hexchat_list_fields,
-                hexchat_list_next,
-                hexchat_list_str,
-                hexchat_list_int,
-                hexchat_plugingui_add,
-                hexchat_plugingui_remove,
-                hexchat_emit_print,
-                hexchat_read_fd,
-                hexchat_list_time,
-                hexchat_gettext,
-                hexchat_send_modes,
-                hexchat_strip,
-                hexchat_free,
-                hexchat_pluginpref_set_str,
-                hexchat_pluginpref_get_str,
-                hexchat_pluginpref_set_int,
-                hexchat_pluginpref_get_int,
-                hexchat_pluginpref_delete,
-                hexchat_pluginpref_list,
-                hexchat_hook_server_attrs,
-                hexchat_hook_print_attrs,
-                hexchat_emit_print_attrs,
-                hexchat_event_attrs_create,
-                hexchat_event_attrs_free,
-            },
-            filename: filename,
-            plugin_name: filename,
-            plugin_desc: ptr::null(),
-            plugin_vers: ptr::null(),
-            free_strings: false,
-            env: &*env,
-            _pin: PhantomPinned,
-        });
-        if unsafe {
-            let ptr: *mut MockPlugin = &mut *plug;
-            crate::hexchat_plugin_init::<'_, T>(
-                crate::LtPhPtr {
-                    ph: ptr as *mut crate::internals::Ph,
-                    _lt: PhantomData,
-                },
-                ptr::addr_of_mut!((*ptr).plugin_name),
-                ptr::addr_of_mut!((*ptr).plugin_desc),
-                ptr::addr_of_mut!((*ptr).plugin_vers),
-                arg.map(|arg| arg.as_ptr()).unwrap_or(ptr::null()),
-            ) != 0
-        } {
-            Some(plug)
-        } else {
-            None
         }
     }
 }
+
+//
+//impl MockPlugin {
+//    pub fn new<T: for<'ph> crate::Plugin<'ph> + Default>(
+//        env: Pin<&PluginEnvironment>,
+//        filename: CString,
+//        arg: Option<&CStr>,
+//    ) -> Option<Pin<Box<MockPlugin>>> {
+//        let filename = filename.into_raw();
+//        let mut plug = Box::pin(MockPlugin {
+//            methods: crate::internals::Ph {
+//                hexchat_hook_command,
+//                hexchat_hook_server,
+//                hexchat_hook_print,
+//                hexchat_hook_timer,
+//                hexchat_hook_fd,
+//                hexchat_unhook,
+//                hexchat_print,
+//                userdata: hexchat_printf as *mut libc::c_void,
+//                hexchat_command,
+//                hexchat_commandf_do_not_use: hexchat_commandf,
+//                hexchat_nickcmp,
+//                hexchat_set_context,
+//                hexchat_find_context,
+//                hexchat_get_context,
+//                hexchat_get_info,
+//                hexchat_get_prefs,
+//                hexchat_list_get,
+//                hexchat_list_free,
+//                hexchat_list_fields,
+//                hexchat_list_next,
+//                hexchat_list_str,
+//                hexchat_list_int,
+//                hexchat_plugingui_add,
+//                hexchat_plugingui_remove,
+//                hexchat_emit_print,
+//                hexchat_read_fd,
+//                hexchat_list_time,
+//                hexchat_gettext,
+//                hexchat_send_modes,
+//                hexchat_strip,
+//                hexchat_free,
+//                hexchat_pluginpref_set_str,
+//                hexchat_pluginpref_get_str,
+//                hexchat_pluginpref_set_int,
+//                hexchat_pluginpref_get_int,
+//                hexchat_pluginpref_delete,
+//                hexchat_pluginpref_list,
+//                hexchat_hook_server_attrs,
+//                hexchat_hook_print_attrs,
+//                hexchat_emit_print_attrs,
+//                hexchat_event_attrs_create,
+//                hexchat_event_attrs_free,
+//            },
+//            filename: filename,
+//            plugin_name: filename,
+//            plugin_desc: ptr::null(),
+//            plugin_vers: ptr::null(),
+//            free_strings: false,
+//            env: &*env,
+//            _pin: PhantomPinned,
+//        });
+//        if unsafe {
+//            let ptr: *mut MockPlugin = &mut *plug;
+//            crate::hexchat_plugin_init::<'_, T>(
+//                crate::LtPhPtr {
+//                    ph: ptr as *mut crate::internals::Ph,
+//                    _lt: PhantomData,
+//                },
+//                ptr::addr_of_mut!((*ptr).plugin_name),
+//                ptr::addr_of_mut!((*ptr).plugin_desc),
+//                ptr::addr_of_mut!((*ptr).plugin_vers),
+//                arg.map(|arg| arg.as_ptr()).unwrap_or(ptr::null()),
+//            ) != 0
+//        } {
+//            Some(plug)
+//        } else {
+//            None
+//        }
+//    }
+//}