From 8c2bbf82a4b63668fa5b8a11ed9f98eea79324fb Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sat, 30 Apr 2022 18:59:35 -0300 Subject: Add progress on mock It's broken. But we don't plan on finishing anytime soon. Just wanna publish the crate tbh. --- src/mock/mod.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/mock/mod.rs b/src/mock/mod.rs index f4193e0..ed41780 100644 --- a/src/mock/mod.rs +++ b/src/mock/mod.rs @@ -38,17 +38,19 @@ //! //! This API is not stable. Do not rely on this API to be stable. +use std::cell::Cell; use std::cell::RefCell; use std::ffi::CStr; use std::ffi::CString; use std::marker::PhantomData; +use std::marker::PhantomPinned; use std::pin::Pin; use std::ptr; use libc::c_char; #[repr(C)] -pub struct MockPlugin { +struct MockPlugin { // NOTE: MUST be first thing in the struct. methods: crate::internals::Ph, filename: *const c_char, @@ -56,6 +58,8 @@ pub struct MockPlugin { plugin_desc: *const c_char, plugin_vers: *const c_char, free_strings: bool, + env: *const PluginEnvironment, + _pin: PhantomPinned, } enum MockCallback { @@ -87,14 +91,18 @@ enum MockCallback { } // This MockHook is deliberately incompatible with hexchat's own hook struct. -struct MockHook { - pl: *mut MockPlugin, +struct MockHook<'hook, 'env> { + pl: &MockPlugin<'env>, cb: RefCell, name: Option>, pri_or_fd: libc::c_int, + userdata: *mut libc::c_void, } -struct PluginEnvironment { +pub struct PluginEnvironment<'env> { + plugins: Option>>>>, + hooks: Option>>>>, + depth: usize, } unsafe extern "C" fn hexchat_hook_command( @@ -317,8 +325,20 @@ unsafe extern "C" fn hexchat_event_attrs_free(ph: *mut crate::internals::Hexchat todo!(); } +impl PluginEnvironment { + pub fn new() -> Pin> { + Box::pin(Self { + plugins: Default::default(), + hooks: Default::default(), + depth: Default::default(), + _pin: PhantomPinned, + }) + } +} + impl MockPlugin { pub fn new crate::Plugin<'ph> + Default>( + env: Pin<&PluginEnvironment>, filename: CString, arg: Option<&CStr>, ) -> Option>> { @@ -373,6 +393,8 @@ impl MockPlugin { plugin_desc: ptr::null(), plugin_vers: ptr::null(), free_strings: false, + env: &*env, + _pin: PhantomPinned, }); if unsafe { let ptr: *mut MockPlugin = &mut *plug; -- cgit 1.4.1