diff --git a/Cargo.lock b/Cargo.lock index 351366c..5f43a06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -688,6 +688,7 @@ dependencies = [ name = "giterated-abi" version = "0.1.0" dependencies = [ + "anyhow", "giterated-models", ] diff --git a/giterated-abi/Cargo.toml b/giterated-abi/Cargo.toml index 5f1ec2d..8bbc031 100644 --- a/giterated-abi/Cargo.toml +++ b/giterated-abi/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -giterated-models = { path = "../giterated-models"} \ No newline at end of file +giterated-models = { path = "../giterated-models"} +anyhow = "1" diff --git a/giterated-abi/src/callback.rs b/giterated-abi/src/callback.rs deleted file mode 100644 index fac3780..0000000 --- a/giterated-abi/src/callback.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::marker::PhantomData; - -pub trait Callback { - type CallbackFunc; -} - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct CallbackPtr { - callback_ptr: *const (), - func: T::CallbackFunc, - _marker: PhantomData, -} - -impl CallbackPtr { - pub unsafe fn from_raw(data: T, func: T::CallbackFunc) -> Self { - todo!() - } - - pub fn func(&self) -> &T::CallbackFunc { - &self.func - } -} diff --git a/giterated-abi/src/callback/mod.rs b/giterated-abi/src/callback/mod.rs new file mode 100644 index 0000000..b835922 --- /dev/null +++ b/giterated-abi/src/callback/mod.rs @@ -0,0 +1,27 @@ +pub mod operation; +pub mod setting; +pub mod value; + +use std::marker::PhantomData; + +pub trait Callback { + type CallbackFunc; +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct CallbackPtr { + callback_ptr: *const (), + func: T::CallbackFunc, + _marker: PhantomData, +} + +impl CallbackPtr { + pub unsafe fn from_raw(data: T, func: T::CallbackFunc) -> Self { + todo!() + } + + pub fn func(&self) -> &T::CallbackFunc { + &self.func + } +} diff --git a/giterated-abi/src/callback/operation.rs b/giterated-abi/src/callback/operation.rs new file mode 100644 index 0000000..3df4557 --- /dev/null +++ b/giterated-abi/src/callback/operation.rs @@ -0,0 +1,140 @@ +use std::future::Future; + +use giterated_models::{ + error::OperationError, object::GiteratedObject, operation::GiteratedOperation, +}; + +use crate::{ + result::FfiResult, + state::State, + value_ex::FfiValueUntyped, + vtable::{operation::Operation, Object}, + FfiFuture, FfiValueMut, FfiValueRef, +}; + +use super::{Callback, CallbackPtr}; + +use std::fmt::Debug; + +pub struct OperationHandlerCallback(FfiValueUntyped); + +impl Callback for OperationHandlerCallback { + type CallbackFunc = unsafe extern "C" fn( + CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + operation: FfiValueRef, + ) -> FfiFuture< + FfiResult>, + >; +} + +pub trait IntoPluginOperationHandler, A> { + unsafe extern "C" fn handle( + callback_ptr: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + operation: FfiValueRef, + ) -> FfiFuture>>; + fn callback_ptr(&self) -> CallbackPtr; +} + +impl IntoPluginOperationHandler for F +where + Fut: Future>> + Send + Sync, + F: Fn(O, D) -> Fut + Send + Sync + 'static, + O: Debug + GiteratedObject + 'static, + D: Debug + GiteratedOperation + 'static, +{ + unsafe extern "C" fn handle( + callback: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + operation: FfiValueRef, + ) -> FfiFuture>> { + todo!() + // let _guard = trace_span!( + // "operation handler", + // object = type_name::(), + // operation = type_name::() + // ) + // .entered(); + // let state = unsafe { state.transmute_ref::() }; + + // // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes + // let object = unsafe { object.transmute_owned::() }; + // let operation = unsafe { operation.transmute_owned::() }; + + // // Cast the callback ptr to ourselves + // let callback: *const F = std::mem::transmute(callback.0); + // let callback = callback.as_ref().unwrap(); + + // let state = state.clone(); + // runtime_state.spawn_future(async move { + // let result = callback(state, *object, *operation).await; + + // match result { + // Ok(success) => unsafe { + // todo!() + // // Ok(AnySuccess::from_raw( + // // FFIBox::from_box(Box::new(success)).untyped(), + // // OperationVTable::new::(), + // // )) + // }, + // Err(err) => match err { + // OperationError::Operation(_) => todo!(), + // OperationError::Internal(_) => todo!(), + // OperationError::Unhandled => todo!(), + // }, + // } + // }) + } + + fn callback_ptr(&self) -> CallbackPtr { + // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } + + todo!() + } +} + +impl IntoPluginOperationHandler for F +where + Fut: Future>>, + F: Fn(O, D, A1) -> Fut, + O: Debug + GiteratedObject, + D: Debug + GiteratedOperation, +{ + unsafe extern "C" fn handle( + _callback_ptr: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + operation: FfiValueRef, + ) -> FfiFuture>> { + todo!() + } + + fn callback_ptr(&self) -> CallbackPtr { + todo!() + } +} + +impl IntoPluginOperationHandler for F +where + Fut: Future>>, + F: Fn(O, D, A1, A2) -> Fut, + O: Debug + GiteratedObject, + D: Debug + GiteratedOperation, +{ + unsafe extern "C" fn handle( + _callback_ptr: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + operation: FfiValueRef, + ) -> FfiFuture>> { + todo!() + } + + fn callback_ptr(&self) -> CallbackPtr { + todo!() + } +} diff --git a/giterated-abi/src/callback/setting.rs b/giterated-abi/src/callback/setting.rs new file mode 100644 index 0000000..5cdfeab --- /dev/null +++ b/giterated-abi/src/callback/setting.rs @@ -0,0 +1,161 @@ +use std::future::Future; + +use giterated_models::{error::OperationError, object::GiteratedObject}; + +use crate::{ + state::State, + value_ex::FfiValueUntyped, + vtable::{Object, Setting}, + FfiFuture, FfiValueMut, FfiValueRef, +}; + +use super::{Callback, CallbackPtr}; + +pub struct SettingGetterCallback(FfiValueUntyped); + +impl Callback for SettingGetterCallback { + type CallbackFunc = unsafe extern "C" fn( + CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + ) -> FfiFuture>; +} + +pub trait IntoPluginSettingGetter { + unsafe extern "C" fn get_setting( + callback_ptr: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + ) -> FfiFuture>; + + fn callback_ptr(&self) -> CallbackPtr { + // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } + todo!() + } +} + +impl IntoPluginSettingGetter for F +where + Fut: Future>> + Send + Sync + 'static, + O: GiteratedObject + Send + Sync + 'static, + OS: giterated_models::settings::Setting + Send + Sync + 'static, + F: Fn(O) -> Fut + Send + Sync + 'static, +{ + unsafe extern "C" fn get_setting( + callback: CallbackPtr, + state: FfiValueMut, + mut object: FfiValueRef, + ) -> FfiFuture> { + // let _guard = trace_span!( + // "get_setting handler", + // object = O::object_name(), + // setting = OS::name() + // ) + // .entered(); + // let state = unsafe { state.transmute_ref::() }; + + // let object = unsafe { object.transmute_owned::() }; + + // // Cast the callback ptr to ourselves + // let callback: *const F = std::mem::transmute(callback.0); + // let callback = callback.as_ref().unwrap(); + + // let state = state.clone(); + // runtime_state.spawn_future(async move { + // let result = callback(state, *object).await; + + // match result { + // Ok(success) => unsafe { Ok(NewAnySetting::new(success)) }, + // Err(err) => match err { + // OperationError::Operation(_) => todo!(), + // OperationError::Internal(_) => todo!(), + // OperationError::Unhandled => todo!(), + // }, + // } + + todo!() + // }) + } +} + +pub trait IntoPluginSettingSetter { + unsafe extern "C" fn set_setting( + callback_ptr: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + setting: Setting, + ) -> FfiFuture>; + + fn callback_ptr(&self) -> CallbackPtr { + // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } + todo!() + } +} + +impl IntoPluginSettingSetter for F +where + Fut: Future>>, + O: GiteratedObject, + OS: giterated_models::settings::Setting, + F: Fn(O, OS) -> Fut, +{ + unsafe extern "C" fn set_setting( + callback: CallbackPtr, + state: FfiValueMut, + mut object: FfiValueRef, + _setting: Setting, + ) -> FfiFuture> { + // let _guard = trace_span!( + // "get_setting handler", + // object = O::object_name(), + // setting = OS::name() + // ) + // .entered(); + // let _state = unsafe { state.transmute_ref::() }; + + // let _object = unsafe { object.transmute_owned::() }; + + // // Cast the callback ptr to ourselves + // let callback: *const F = std::mem::transmute(callback.0); + // let _callback = callback.as_ref().unwrap(); + + // let result = callback(state.clone(), *object); + + // match result { + // Ok(setting) => Ok(NewAnySetting::new(setting)), + // Err(_) => todo!(), + // } + todo!() + } +} + +pub struct SettingChangeCallback(FfiValueUntyped); + +impl Callback for SettingChangeCallback { + type CallbackFunc = unsafe extern "C" fn( + state: FfiValueMut, + object: FfiValueRef, + setting_name: &str, + new_setting: Setting, + ); +} + +pub trait IntoSettingChangeCallback { + unsafe extern "C" fn setting_changed( + state: FfiValueMut, + object: FfiValueRef, + setting_name: &str, + new_setting: Setting, + ); +} + +impl IntoSettingChangeCallback for F { + unsafe extern "C" fn setting_changed( + state: FfiValueMut, + _object: FfiValueRef, + _setting_name: &str, + _new_setting: Setting, + ) { + todo!() + } +} diff --git a/giterated-abi/src/callback/value.rs b/giterated-abi/src/callback/value.rs new file mode 100644 index 0000000..ce40f53 --- /dev/null +++ b/giterated-abi/src/callback/value.rs @@ -0,0 +1,117 @@ +use std::future::Future; + +use giterated_models::{ + error::OperationError, object::GiteratedObject, value::GiteratedObjectValue, +}; + +use crate::{ + result::{FfiError, FfiResult}, + state::State, + vtable::{Object, Value}, + FfiFuture, FfiSliceRef, FfiValueMut, FfiValueRef, +}; + +use super::{setting::SettingGetterCallback, Callback, CallbackPtr}; + +#[derive(Copy, Clone)] +pub struct ValueGetterCallback(CallbackPtr); + +impl Callback for ValueGetterCallback { + type CallbackFunc = unsafe extern "C" fn( + CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + ) -> FfiFuture>; +} + +pub trait IntoPluginValueGetter { + unsafe extern "C" fn get_value( + callback: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + ) -> FfiFuture>; + + fn callback_ptr(&self) -> CallbackPtr; +} + +impl IntoPluginValueGetter for F +where + Fut: Future>> + Send + Sync, + O: GiteratedObject + 'static, + V: GiteratedObjectValue + Send + Sync + 'static, + F: Fn(O) -> Fut + Send + Sync + 'static, +{ + unsafe extern "C" fn get_value( + callback: CallbackPtr, + state: FfiValueMut, + mut object: FfiValueRef, + ) -> FfiFuture> { + // let _guard = trace_span!( + // "get_value handler", + // object = O::object_name(), + // value = V::value_name() + // ) + // .entered(); + // let state = unsafe { state.transmute_ref::() }; + + // let object = unsafe { object.transmute_owned::() }; + + // // Cast the callback ptr to ourselves + // let callback: *const F = std::mem::transmute(callback.0); + // let callback = callback.as_ref().unwrap(); + + // let state = state.clone(); + // runtime_state.spawn_future(async move { + // let result = callback(state, *object).await; + + // match result { + // Ok(success) => unsafe { Ok(NewAnyValue::new(success)) }, + // Err(err) => match err { + // OperationError::Operation(_) => todo!(), + // OperationError::Internal(_) => todo!(), + // OperationError::Unhandled => todo!(), + // }, + // } + // }) + + todo!() + } + + fn callback_ptr(&self) -> CallbackPtr { + todo!() + // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } + } +} + +pub struct ValueChangeCallback(CallbackPtr); + +impl Callback for ValueChangeCallback { + type CallbackFunc = unsafe extern "C" fn( + state: FfiValueMut, + object: FfiValueRef, + value_name: FfiSliceRef, + new_value: Value, + ) -> FfiFuture<()>; +} + +pub trait IntoValueChangeCallback { + unsafe extern "C" fn value_changed( + callback: CallbackPtr, + state: FfiValueMut, + object: FfiValueRef, + value_name: FfiSliceRef, + new_value: Value, + ) -> FfiFuture<()>; +} + +impl IntoValueChangeCallback for F { + unsafe extern "C" fn value_changed( + callback: CallbackPtr, + state: FfiValueMut, + _object: FfiValueRef, + _value_name: FfiSliceRef, + _new_value: Value, + ) -> FfiFuture<()> { + todo!() + } +} diff --git a/giterated-abi/src/future.rs b/giterated-abi/src/future.rs index e399187..f1fb043 100644 --- a/giterated-abi/src/future.rs +++ b/giterated-abi/src/future.rs @@ -8,7 +8,7 @@ pub struct FfiFuture { wake_fn: unsafe extern "C" fn(FfiValueMut>, FfiValueMut<()>), poll_state: FfiValue<()>, - waker_state: Option>, + pub waker_state: Option>, _output_marker: PhantomData, } @@ -42,7 +42,7 @@ impl FfiFuture { /// Very not in progress text :) pub unsafe fn write_waker( &mut self, - _wake_fn: unsafe extern "C" fn(FfiValueMut>, FfiValueMut<()>), + _wake_fn: unsafe extern "C" fn(FfiValueMut>), _waker_state: WS, ) { todo!() diff --git a/giterated-abi/src/heap.rs b/giterated-abi/src/heap.rs index 0bab735..1b4e10e 100644 --- a/giterated-abi/src/heap.rs +++ b/giterated-abi/src/heap.rs @@ -1,9 +1,26 @@ -use crate::FfiValue; +use std::{mem::MaybeUninit, ptr::drop_in_place}; + +use crate::{abi_backing::HeapValueBacking, FfiValue}; pub trait HeapPlacable { unsafe extern "C" fn free(value: FfiValue, taken: bool); } impl HeapPlacable for T { - unsafe extern "C" fn free(_value: FfiValue, _taken: bool) {} + unsafe extern "C" fn free(value: FfiValue, taken: bool) { + if !taken { + drop(Box::from_raw(value.inner as *mut HeapValueBacking)) + } else { + let allocation = Box::from_raw(value.inner as *mut T); + + // Since we "took" the value, kindly inform the compiler that it can't + // treat the value like it exists + let allocation_uninit: Box>> = + unsafe { core::mem::transmute(allocation) }; + + // Since the compiler has no idea whether the value exists or not, it won't try and + // drop it. Success! + drop(allocation_uninit); + } + } } diff --git a/giterated-abi/src/lib.rs b/giterated-abi/src/lib.rs index 1452da9..29c9ba1 100644 --- a/giterated-abi/src/lib.rs +++ b/giterated-abi/src/lib.rs @@ -86,10 +86,12 @@ mod future; pub mod heap; pub mod model_impl; pub mod result; +pub mod state; pub mod vtable; use abi_backing::{HeapValueBacking, SliceBacking}; pub use future::{FfiFuture, RuntimeFuturePoll}; use heap::HeapPlacable; +use prelude::value_ex::FfiValueUntyped; use std::{ marker::PhantomData, @@ -269,6 +271,10 @@ impl FfiValue { } } + pub fn erase_type(self) -> FfiValueUntyped { + unsafe { transmute(self) } + } + pub fn pin(&self) -> HeapPinnedValue<'_, T> { unsafe { HeapPinnedValue::from_raw(self) } } diff --git a/giterated-abi/src/model_impl/mod.rs b/giterated-abi/src/model_impl/mod.rs index b53b42b..dff24e8 100644 --- a/giterated-abi/src/model_impl/mod.rs +++ b/giterated-abi/src/model_impl/mod.rs @@ -93,12 +93,12 @@ impl IntoValueVTable for T { } } -// impl IntoSettingVTable for T { -// unsafe extern "C" fn serialize(this: Setting) -> FfiResult, FfiError> { -// todo!() -// } - -// unsafe extern "C" fn deserialize(buffer: FfiSliceRef<[u8]>) -> FfiResult { -// todo!() -// } -// } +impl IntoSettingVTable for T { + unsafe extern "C" fn serialize(this: Setting) -> FfiResult, FfiError> { + todo!() + } + + unsafe extern "C" fn deserialize(buffer: FfiSliceRef<[u8]>) -> FfiResult { + todo!() + } +} diff --git a/giterated-abi/src/state.rs b/giterated-abi/src/state.rs new file mode 100644 index 0000000..38a2cb7 --- /dev/null +++ b/giterated-abi/src/state.rs @@ -0,0 +1,136 @@ +use anyhow::Error; + +pub trait FromOperationState: Sized { + fn from_operation_state( + state: &mut State, + object: &O, + operation: &D, + ) -> Result>; +} + +pub struct StateExtractor(T); + +impl FromState for StateExtractor { + fn from_state(state: &mut State) -> Result { + todo!() + } +} + +use giterated_models::error::OperationError; + +use crate::{ + value_ex::FfiValueUntyped, + vtable::{ObjectABI, VTable}, +}; + +#[repr(transparent)] +pub struct State { + inner: StateHandle, +} + +#[repr(transparent)] +struct StateHandle { + state: FfiValueUntyped, +} + +#[repr(C)] +struct StateItem { + /// The pointer to the next item. + /// + /// `next_item` is most likely always an `FfiValue>` and that's how we free them. + next_item: *const StateItem<()>, + pub state_uuid: u128, + pub state: T, +} + +impl Drop for State { + fn drop(&mut self) { + let state_manager = unsafe { StateManager::new(self) }; + + for state in state_manager {} + } +} + +struct StateManager<'s> { + state: &'s mut State, + last: Option, +} + +impl<'s> StateManager<'s> { + pub unsafe fn new(handle: &'s mut State) -> Self { + todo!() + } + + pub unsafe fn write_state(&mut self, state: S) -> Self { + todo!() + } + + pub unsafe fn get_state(&mut self) -> Option<&S> { + todo!() + } +} + +impl<'s> Iterator for StateManager<'s> { + type Item = StateItem<()>; + + fn next(&mut self) -> Option> { + todo!() + } +} + +pub trait StateUUID { + fn uuid() -> u128; + + fn unsafe_hint_copy() -> Option { + None + } +} + +/// State values for the current execution domain. 99.99% of the time this means "plugin-specific" +/// +/// The remainder 0.01% of the time it refers to the daemon's runtime domain. +pub struct DomainState(StateItem<()>); + +impl StateUUID for DomainState { + fn uuid() -> u128 { + todo!() + } +} + +pub struct RuntimeState(StateItem<&'static VTable>); + +impl StateUUID for RuntimeState { + fn uuid() -> u128 { + todo!() + } +} + +impl RuntimeState { + pub fn queue_insert_state(&mut self, state: S) { + todo!() + } +} + +pub struct Runtime { + pub queue_insert_state: unsafe extern "C" fn(state_uuid: u128, state: FfiValueUntyped), +} + +impl ObjectABI for Runtime { + type VTable = Runtime; +} + +pub trait FromState: Sized { + fn from_state(state: &mut State) -> Result; +} + +impl FromState for T { + fn from_state(state: &mut State) -> Result { + todo!() + } +} + +impl FromState for Option { + fn from_state(state: &mut State) -> Result { + todo!() + } +} diff --git a/giterated-abi/src/vtable/mod.rs b/giterated-abi/src/vtable/mod.rs index 88000c5..d0a21d3 100644 --- a/giterated-abi/src/vtable/mod.rs +++ b/giterated-abi/src/vtable/mod.rs @@ -1,11 +1,12 @@ use std::{marker::PhantomData, mem::transmute, ops::Deref}; -mod object; +pub mod object; pub mod operation; +pub mod plugin; +pub mod plugin_initialization; +pub mod runtime; mod setting; mod value; -mod runtime; -mod plugin_initialization; pub use object::*; pub use setting::*; diff --git a/giterated-abi/src/vtable/plugin.rs b/giterated-abi/src/vtable/plugin.rs new file mode 100644 index 0000000..4ebb631 --- /dev/null +++ b/giterated-abi/src/vtable/plugin.rs @@ -0,0 +1,24 @@ +use crate::value_ex::FfiValueRefUntyped; + +use super::ObjectABI; + +pub struct Plugin {} + +impl ObjectABI for Plugin { + type VTable = PluginVTable; +} + +pub struct PluginVTable { + pub plugin_name: unsafe extern "C" fn() -> &'static str, + pub type_metadata: unsafe extern "C" fn() -> FfiValueRefUntyped, +} + +impl PluginVTable { + pub fn plugin_name(&self) -> &'static str { + todo!() + } + + pub fn type_metadata(&self) -> FfiValueRefUntyped { + todo!() + } +} diff --git a/giterated-abi/src/vtable/plugin_initialization.rs b/giterated-abi/src/vtable/plugin_initialization.rs index 5f3702a..4501471 100644 --- a/giterated-abi/src/vtable/plugin_initialization.rs +++ b/giterated-abi/src/vtable/plugin_initialization.rs @@ -1,61 +1,65 @@ +use crate::{ + callback::{ + operation::OperationHandlerCallback, setting::SettingGetterCallback, + value::ValueGetterCallback, CallbackPtr, + }, + FfiValueMut, +}; + +use super::{operation::Operation, Object, ObjectABI, Setting, VTable, Value}; + #[repr(C)] pub struct HostVTable {} #[repr(C)] #[derive(Clone, Copy)] -pub struct Initialization { +pub struct InitializationVTable { pub register_object: - unsafe extern "C" fn(*mut PluginInitializationState, &'static str, &'static VTable), + unsafe extern "C" fn(state: FfiValueMut<()>, &'static str, &'static VTable), pub register_operation: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, &'static VTable, ), pub register_setting: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, &'static VTable, ), pub register_value: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, &'static VTable, ), pub operation_handler: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, CallbackPtr, ), pub value_getter: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, CallbackPtr, ), pub setting_getter: unsafe extern "C" fn( - *mut PluginInitializationState, + state: FfiValueMut<()>, &'static str, &'static str, CallbackPtr, ), } -impl ObjectABI for Initialization { - type VTable = Initialization; -} - -impl Debug for Initialization { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("InitializationVTable").finish() - } +impl ObjectABI for InitializationVTable { + type VTable = InitializationVTable; } -unsafe impl Sync for Initialization {} -unsafe impl Send for Initialization {} +unsafe impl Sync for InitializationVTable {} +unsafe impl Send for InitializationVTable {} diff --git a/giterated-abi/src/vtable/runtime.rs b/giterated-abi/src/vtable/runtime.rs index 1592e03..4e68401 100644 --- a/giterated-abi/src/vtable/runtime.rs +++ b/giterated-abi/src/vtable/runtime.rs @@ -1,37 +1,35 @@ -use giterated_abi::{ +use giterated_models::{error::OperationError, object::ObjectRequestError}; + +use crate::{ result::{FfiError, FfiResult}, + state::State, value_ex::FfiValueUntyped, - vtable::Object, - FfiSliceRef, -}; -use giterated_models::{ - error::OperationError, object::ObjectRequestError, operation::OperationState, + FfiFuture, FfiSliceRef, FfiValueMut, }; -use crate::{ - future::RuntimeFuture, - new_stack::{PluginState, TypeMetadata}, -}; +use super::{Object, ObjectABI}; + +pub struct Runtime {} + +impl ObjectABI for Runtime { + type VTable = RuntimeVTable; +} #[derive(Clone, Copy)] pub struct RuntimeVTable { - pub(crate) runtime: PluginState, - pub(crate) type_metadata: *const TypeMetadata, pub(crate) handle_fn: unsafe extern "C" fn( - PluginState, FfiSliceRef, FfiSliceRef, FfiSliceRef, FfiSliceRef<[u8]>, FfiSliceRef<[u8]>, - ) -> RuntimeFuture< + ) -> FfiFuture< FfiResult>, >, pub(crate) get_object: unsafe extern "C" fn( - PluginState, &str, - *mut OperationState, + state: FfiValueMut, ) -> FfiResult>, } @@ -40,17 +38,15 @@ unsafe impl Sync for RuntimeVTable {} pub trait IntoRuntimeVtable { unsafe extern "C" fn handle( - this: PluginState, object_kind: FfiSliceRef, operation_name: FfiSliceRef, object: FfiSliceRef, operation_payload: FfiSliceRef<[u8]>, operation_state: FfiSliceRef<[u8]>, - ) -> RuntimeFuture>>; + ) -> FfiFuture>>; unsafe extern "C" fn get_object( - this: PluginState, object_str: &str, - operation_state: *mut OperationState, + operation_state: FfiValueMut, ) -> FfiResult>; } diff --git a/giterated-abi/src/vtable/setting.rs b/giterated-abi/src/vtable/setting.rs index 4daa590..2c70c0d 100644 --- a/giterated-abi/src/vtable/setting.rs +++ b/giterated-abi/src/vtable/setting.rs @@ -12,11 +12,11 @@ pub struct Setting { vtable: &'static VTable, } -impl From for Setting { - fn from(value: T) -> Self { - todo!() - } -} +// impl From for Setting { +// fn from(value: T) -> Self { +// todo!() +// } +// } impl ObjectABI for Setting { type VTable = SettingVTable; diff --git a/giterated-core/src/callback/mod.rs b/giterated-core/src/callback/mod.rs deleted file mode 100644 index 078eb9d..0000000 --- a/giterated-core/src/callback/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod operation; -mod setting; -mod value; diff --git a/giterated-core/src/callback/operation.rs b/giterated-core/src/callback/operation.rs deleted file mode 100644 index 741efd2..0000000 --- a/giterated-core/src/callback/operation.rs +++ /dev/null @@ -1,125 +0,0 @@ -pub struct OperationHandlerCallback(FfiValueUntyped); - -impl Callback for OperationHandlerCallback { - type CallbackFunc = unsafe extern "C" fn( - CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - operation: FfiValueRef, - ) -> RuntimeFuture< - FfiResult>, - >; -} - -pub trait IntoPluginOperationHandler, A> { - unsafe extern "C" fn handle( - callback_ptr: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - operation: FfiValueRef, - ) -> RuntimeFuture>>; - fn callback_ptr(&self) -> CallbackPtr; -} - -impl IntoPluginOperationHandler for F -where - Fut: Future>> + Send + Sync, - F: Fn(S, O, D) -> Fut + Send + Sync + 'static, - S: Clone + Debug + Send + Sync + 'static, - O: Debug + GiteratedObject + 'static, - D: Debug + GiteratedOperation + 'static, -{ - unsafe extern "C" fn handle( - callback: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - operation: FfiValueRef, - ) -> RuntimeFuture>> { - todo!() - // let _guard = trace_span!( - // "operation handler", - // object = type_name::(), - // operation = type_name::() - // ) - // .entered(); - // let state = unsafe { state.transmute_ref::() }; - - // // Since this is Rust code, we know that the AnyObject and AnyOperation are just boxes - // let object = unsafe { object.transmute_owned::() }; - // let operation = unsafe { operation.transmute_owned::() }; - - // // Cast the callback ptr to ourselves - // let callback: *const F = std::mem::transmute(callback.0); - // let callback = callback.as_ref().unwrap(); - - // let state = state.clone(); - // runtime_state.spawn_future(async move { - // let result = callback(state, *object, *operation).await; - - // match result { - // Ok(success) => unsafe { - // todo!() - // // Ok(AnySuccess::from_raw( - // // FFIBox::from_box(Box::new(success)).untyped(), - // // OperationVTable::new::(), - // // )) - // }, - // Err(err) => match err { - // OperationError::Operation(_) => todo!(), - // OperationError::Internal(_) => todo!(), - // OperationError::Unhandled => todo!(), - // }, - // } - // }) - } - - fn callback_ptr(&self) -> CallbackPtr { - // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } - - todo!() - } -} - -impl IntoPluginOperationHandler for F -where - Fut: Future>>, - F: Fn(S, O, D, A1) -> Fut, - S: Clone + Debug, - O: Debug + GiteratedObject, - D: Debug + GiteratedOperation, -{ - unsafe extern "C" fn handle( - _callback_ptr: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - operation: FfiValueRef, - ) -> RuntimeFuture>> { - todo!() - } - - fn callback_ptr(&self) -> CallbackPtr { - todo!() - } -} - -impl IntoPluginOperationHandler for F -where - Fut: Future>>, - F: Fn(S, O, D, A1, A2) -> Fut, - S: Clone + Debug, - O: Debug + GiteratedObject, - D: Debug + GiteratedOperation, -{ - unsafe extern "C" fn handle( - _callback_ptr: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - operation: FfiValueRef, - ) -> RuntimeFuture>> { - todo!() - } - - fn callback_ptr(&self) -> CallbackPtr { - todo!() - } -} diff --git a/giterated-core/src/callback/setting.rs b/giterated-core/src/callback/setting.rs deleted file mode 100644 index 931ea62..0000000 --- a/giterated-core/src/callback/setting.rs +++ /dev/null @@ -1,150 +0,0 @@ -pub struct SettingGetterCallback(FfiValueUntyped); - -impl Callback for SettingGetterCallback { - type CallbackFunc = unsafe extern "C" fn( - CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - ) -> RuntimeFuture>; -} - -pub trait IntoPluginSettingGetter { - unsafe extern "C" fn get_setting( - callback_ptr: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - ) -> RuntimeFuture>; - - fn callback_ptr(&self) -> CallbackPtr { - // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } - todo!() - } -} - -impl IntoPluginSettingGetter for F -where - Fut: Future>> + Send + Sync + 'static, - S: Clone + Send + Sync + 'static, - O: GiteratedObject + Send + Sync + 'static, - OS: giterated_models::settings::Setting + Send + Sync + 'static, - F: Fn(S, O) -> Fut + Send + Sync + 'static, -{ - unsafe extern "C" fn get_setting( - callback: CallbackPtr, - state: FfiValueMut, - mut object: FfiValueRef, - ) -> RuntimeFuture> { - // let _guard = trace_span!( - // "get_setting handler", - // object = O::object_name(), - // setting = OS::name() - // ) - // .entered(); - // let state = unsafe { state.transmute_ref::() }; - - // let object = unsafe { object.transmute_owned::() }; - - // // Cast the callback ptr to ourselves - // let callback: *const F = std::mem::transmute(callback.0); - // let callback = callback.as_ref().unwrap(); - - // let state = state.clone(); - // runtime_state.spawn_future(async move { - // let result = callback(state, *object).await; - - // match result { - // Ok(success) => unsafe { Ok(NewAnySetting::new(success)) }, - // Err(err) => match err { - // OperationError::Operation(_) => todo!(), - // OperationError::Internal(_) => todo!(), - // OperationError::Unhandled => todo!(), - // }, - // } - - todo!() - // }) - } -} - -pub trait IntoPluginSettingSetter { - unsafe extern "C" fn set_setting( - callback_ptr: CallbackPtr, - state: &PluginState, - object: FfiValueRef, - setting: Setting, - ) -> RuntimeFuture>; - - fn callback_ptr(&self) -> CallbackPtr { - // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } - todo!() - } -} - -impl IntoPluginSettingSetter for F -where - Fut: Future>>, - S: Clone, - O: GiteratedObject, - OS: giterated_models::settings::Setting, - F: Fn(S, O, OS) -> Fut, -{ - unsafe extern "C" fn set_setting( - callback: CallbackPtr, - state: &PluginState, - mut object: FfiValueRef, - _setting: Setting, - ) -> RuntimeFuture> { - // let _guard = trace_span!( - // "get_setting handler", - // object = O::object_name(), - // setting = OS::name() - // ) - // .entered(); - // let _state = unsafe { state.transmute_ref::() }; - - // let _object = unsafe { object.transmute_owned::() }; - - // // Cast the callback ptr to ourselves - // let callback: *const F = std::mem::transmute(callback.0); - // let _callback = callback.as_ref().unwrap(); - - // let result = callback(state.clone(), *object); - - // match result { - // Ok(setting) => Ok(NewAnySetting::new(setting)), - // Err(_) => todo!(), - // } - todo!() - } -} - -pub struct SettingChangeCallback(FfiValueUntyped); - -impl Callback for SettingChangeCallback { - type CallbackFunc = unsafe extern "C" fn( - &PluginState, - object: FfiValueRef, - setting_name: &str, - new_setting: Setting, - ); -} - -pub trait IntoSettingChangeCallback { - unsafe extern "C" fn setting_changed( - state: &PluginState, - object: FfiValueRef, - setting_name: &str, - new_setting: Setting, - ); -} - -impl IntoSettingChangeCallback for F { - unsafe extern "C" fn setting_changed( - _state: &PluginState, - _object: FfiValueRef, - _setting_name: &str, - _new_setting: Setting, - ) { - todo!() - } -} diff --git a/giterated-core/src/callback/value.rs b/giterated-core/src/callback/value.rs deleted file mode 100644 index d9fa84c..0000000 --- a/giterated-core/src/callback/value.rs +++ /dev/null @@ -1,103 +0,0 @@ -#[derive(Copy, Clone)] -pub struct ValueGetterCallback(CallbackPtr); - -impl Callback for ValueGetterCallback { - type CallbackFunc = unsafe extern "C" fn( - CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - ) -> RuntimeFuture>; -} - -pub trait IntoPluginValueGetter { - unsafe extern "C" fn get_value( - callback: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - ) -> RuntimeFuture>; - - fn callback_ptr(&self) -> CallbackPtr; -} - -impl IntoPluginValueGetter for F -where - Fut: Future>> + Send + Sync, - S: Clone + Send + Sync + 'static, - O: GiteratedObject + 'static, - V: GiteratedObjectValue + Send + Sync + 'static, - F: Fn(S, O) -> Fut + Send + Sync + 'static, -{ - unsafe extern "C" fn get_value( - callback: CallbackPtr, - state: FfiValueMut, - mut object: FfiValueRef, - ) -> RuntimeFuture> { - // let _guard = trace_span!( - // "get_value handler", - // object = O::object_name(), - // value = V::value_name() - // ) - // .entered(); - // let state = unsafe { state.transmute_ref::() }; - - // let object = unsafe { object.transmute_owned::() }; - - // // Cast the callback ptr to ourselves - // let callback: *const F = std::mem::transmute(callback.0); - // let callback = callback.as_ref().unwrap(); - - // let state = state.clone(); - // runtime_state.spawn_future(async move { - // let result = callback(state, *object).await; - - // match result { - // Ok(success) => unsafe { Ok(NewAnyValue::new(success)) }, - // Err(err) => match err { - // OperationError::Operation(_) => todo!(), - // OperationError::Internal(_) => todo!(), - // OperationError::Unhandled => todo!(), - // }, - // } - // }) - - todo!() - } - - fn callback_ptr(&self) -> CallbackPtr { - todo!() - // unsafe { CallbackPtr::from_raw(self as *const _ as *const ()) } - } -} - -pub struct ValueChangeCallback(CallbackPtr); - -impl Callback for ValueChangeCallback { - type CallbackFunc = unsafe extern "C" fn( - &PluginState, - object: FfiValueRef, - value_name: FfiSliceRef, - new_value: Value, - ) -> RuntimeFuture<()>; -} - -pub trait IntoValueChangeCallback { - unsafe extern "C" fn value_changed( - callback: CallbackPtr, - state: FfiValueMut, - object: FfiValueRef, - value_name: FfiSliceRef, - new_value: Value, - ) -> RuntimeFuture<()>; -} - -impl IntoValueChangeCallback for F { - unsafe extern "C" fn value_changed( - callback: CallbackPtr, - state: FfiValueMut, - _object: FfiValueRef, - _value_name: FfiSliceRef, - _new_value: Value, - ) -> RuntimeFuture<()> { - todo!() - } -} diff --git a/giterated-core/src/lib.rs b/giterated-core/src/lib.rs index 359f24f..f8b1e37 100644 --- a/giterated-core/src/lib.rs +++ b/giterated-core/src/lib.rs @@ -1,4 +1,3 @@ -mod callback; mod state; mod types; diff --git a/giterated-core/src/types/mod.rs b/giterated-core/src/types/mod.rs index 8ecb3c7..6b7d050 100644 --- a/giterated-core/src/types/mod.rs +++ b/giterated-core/src/types/mod.rs @@ -74,4 +74,3 @@ impl TypeMetadata { ); } } - diff --git a/giterated-macros/src/lib.rs b/giterated-macros/src/lib.rs index f94b617..07ae4cd 100644 --- a/giterated-macros/src/lib.rs +++ b/giterated-macros/src/lib.rs @@ -11,14 +11,18 @@ pub fn plugin(metadata: TokenStream) -> TokenStream { #[proc_macro_attribute] pub fn plugin_init(attribute: TokenStream, item: TokenStream) -> TokenStream { - let mut input = parse_macro_input!(item as ItemFn); + let input = parse_macro_input!(item as ItemFn); + + let func = input.sig.ident.clone(); quote! { #[doc(hidden)] #[no_mangle] unsafe extern "C" fn __plugin_init() { - #input.sig.ident() + #func(&mut ::giterated_plugin::local::PluginStackBuilder::new()).unwrap() } + + #input } .into() } @@ -27,13 +31,13 @@ fn emit_plugin_api() -> impl Into { quote! { #[doc(hidden)] #[no_mangle] - unsafe extern "C" fn __load_runtime_vtable(vtable: &'static ::giterated_abi::VTable) { + unsafe extern "C" fn __load_runtime_vtable(vtable: &'static ::giterated_plugin::abi::vtable::VTable<::giterated_plugin::abi::vtable::runtime::Runtime>) { todo!() } #[doc(hidden)] #[no_mangle] - unsafe extern "C" fn __get_plugin_vtable() -> &'static ::giterated_abi::VTable { + unsafe extern "C" fn __get_plugin_vtable() -> &'static ::giterated_plugin::abi::vtable::VTable<::giterated_plugin::abi::vtable::plugin::Plugin> { todo!() } } diff --git a/giterated-plugin/src/future.rs b/giterated-plugin/src/future.rs index c50447a..10d0a30 100644 --- a/giterated-plugin/src/future.rs +++ b/giterated-plugin/src/future.rs @@ -1,25 +1,29 @@ use futures_util::future::BoxFuture; use futures_util::FutureExt; -use giterated_abi::{FfiFuture, FfiValue, FfiValueMut, RuntimeFuturePoll}; +use giterated_abi::{ + state::State, value_ex::FfiValueUntyped, FfiFuture, FfiValue, FfiValueMut, FfiValueRef, + RuntimeFuturePoll, +}; use std::{ cell::UnsafeCell, future::Future, marker::PhantomData, mem::transmute, + ops::Deref, task::{Context, RawWaker, RawWakerVTable, Waker}, }; #[repr(C)] pub struct RuntimeWakerCallback { - callback: PluginState, - waker_func: unsafe extern "C" fn(PluginState), + callback: FfiValueUntyped, + waker_func: unsafe extern "C" fn(FfiValueUntyped), } pub struct WakerState { waker: Waker, } -unsafe extern "C" fn wake(_waker: PluginState) {} +unsafe extern "C" fn wake(_waker: FfiValueUntyped) {} pub struct LocalRuntimeFuture { inner: BoxFuture<'static, Output>, @@ -44,7 +48,7 @@ pub trait RuntimeFuturesExt { ) -> RuntimeFuture; } -impl RuntimeFuturesExt for RuntimeState { +impl RuntimeFuturesExt for State { fn spawn_future + Send + Sync + 'static>( &self, future: F, @@ -132,8 +136,6 @@ impl Future for RuntimeFuture { waker: cx.waker().clone(), }; - let waker_state = PluginState::from(waker_state); - unsafe { self.0.write_waker(wake_local, waker_state) }; match unsafe { self.0.poll() } { @@ -147,6 +149,11 @@ impl Future for RuntimeFuture { } } -unsafe extern "C" fn wake_local(future: FfiValueMut>, state: FfiValueMut<()>) { - todo!() +/// The function used to wake a local future over FFI +unsafe extern "C" fn wake_local(future: FfiValueMut>) { + if let Some(waker) = &future.waker_state { + let waker: FfiValueRef = unsafe { transmute(waker.deref()) }; + + waker.waker.wake_by_ref(); + } } diff --git a/giterated-plugin/src/lib.rs b/giterated-plugin/src/lib.rs index 638d2ef..1402ead 100644 --- a/giterated-plugin/src/lib.rs +++ b/giterated-plugin/src/lib.rs @@ -1,6 +1,8 @@ #![allow(improper_ctypes_definitions)] pub mod future; +pub mod local; +pub use giterated_abi as abi; #[macro_use] extern crate tracing; diff --git a/giterated-plugin/src/local.rs b/giterated-plugin/src/local.rs index 1ba8196..efb082d 100644 --- a/giterated-plugin/src/local.rs +++ b/giterated-plugin/src/local.rs @@ -1,49 +1,46 @@ -mod local_runtime; - use giterated_abi::{ - callback::{Callback, CallbackPtr}, + callback::{ + operation::IntoPluginOperationHandler, + setting::{IntoPluginSettingGetter, IntoPluginSettingSetter}, + value::IntoPluginValueGetter, + Callback, CallbackPtr, + }, result::FfiError, + state::{State, StateUUID}, value_ex::FfiValueUntyped, vtable::{ operation::{IntoOperationVTable, OperationVTable}, + plugin_initialization::InitializationVTable, IntoObjectVTable, IntoSettingVTable, IntoValueVTable, Object, ObjectVTable, SettingVTable, VTable, ValueVTable, }, - FfiValueRef, + FfiValueMut, FfiValueRef, }; use giterated_models::{ object::GiteratedObject, operation::GiteratedOperation, settings::Setting, value::GiteratedObjectValue, }; -use giterated_plugin::{ - callback::{ - IntoPluginOperationHandler, IntoPluginSettingGetter, IntoPluginSettingSetter, - IntoPluginValueGetter, OperationHandlerCallback, SettingGetterCallback, - ValueGetterCallback, - }, - handle::PluginInitializationState, - new_stack::PluginState, - vtable::Initialization, -}; + use tracing::trace_span; -pub struct PluginStackBuilder { - init_state: *mut PluginInitializationState, - vtable: &'static VTable, - state: S, +pub struct PluginStackBuilder { + init_state: FfiValueUntyped, + vtable: &'static VTable, } -impl PluginStackBuilder { - pub fn new( - plugin_state: S, - state: *mut PluginInitializationState, - vtable: &'static VTable, - ) -> PluginStackBuilder { - PluginStackBuilder { - init_state: state, - vtable, - state: plugin_state, - } +impl PluginStackBuilder { + pub fn new(// state: FfiValueUntyped, + // vtable: &'static VTable, + ) -> PluginStackBuilder { + todo!() + // PluginStackBuilder { + // init_state: state, + // vtable + // } + } + + pub fn insert_state(&mut self, state: S) -> &mut Self { + todo!() } pub fn object(&mut self) -> &mut Self { @@ -51,11 +48,13 @@ impl PluginStackBuilder { let func = self.vtable.register_object; + let mut init_state = self.init_state.pin(); + unsafe { func( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), - O::VTABLE + O::VTABLE, ) }; @@ -69,9 +68,11 @@ impl PluginStackBuilder { { let _guard = trace_span!("register operation").entered(); + let mut init_state = self.init_state.pin(); + unsafe { (self.vtable.register_operation)( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), D::operation_name(), >::VTABLE, @@ -85,14 +86,16 @@ impl PluginStackBuilder { where O: GiteratedObject, OS: IntoSettingVTable + Setting, - HG: IntoPluginSettingGetter, - HS: IntoPluginSettingSetter, + HG: IntoPluginSettingGetter, + HS: IntoPluginSettingSetter, { let _guard = trace_span!("register setting").entered(); + let mut init_state = self.init_state.pin(); + unsafe { (self.vtable.register_setting)( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, @@ -109,9 +112,11 @@ impl PluginStackBuilder { { let _guard = trace_span!("register setting").entered(); + let mut init_state = self.init_state.pin(); + unsafe { (self.vtable.register_setting)( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, @@ -125,13 +130,15 @@ impl PluginStackBuilder { where O: GiteratedObject, V: IntoValueVTable + GiteratedObjectValue, - T: IntoPluginValueGetter, + T: IntoPluginValueGetter, { let _guard = trace_span!("register value").entered(); + let mut init_state = self.init_state.pin(); + unsafe { (self.vtable.register_value)( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), V::value_name(), V::VTABLE, @@ -154,7 +161,7 @@ impl PluginStackBuilder { A, O: GiteratedObject + IntoObjectVTable, D: IntoOperationVTable + GiteratedOperation, - T: IntoPluginOperationHandler, + T: IntoPluginOperationHandler, >( &mut self, handler: T, @@ -206,7 +213,7 @@ impl PluginStackBuilder { where O: GiteratedObject + IntoObjectVTable, OS: Setting + IntoSettingVTable, - T: IntoPluginSettingGetter, + T: IntoPluginSettingGetter, { let _guard = trace_span!("register setting_getter handler").entered(); @@ -221,9 +228,11 @@ impl PluginStackBuilder { self.object::(); + let mut init_state = self.init_state.pin(); + unsafe { (self.vtable.register_setting)( - self.init_state, + unsafe { init_state.grant_mut() }, O::object_name(), OS::name(), OS::VTABLE, @@ -234,22 +243,22 @@ impl PluginStackBuilder { } } -pub trait ValueSettingExt { +pub trait ValueSettingExt { fn value_setting(&mut self, get: HG, set: HS) -> &mut Self where O: GiteratedObject + IntoObjectVTable + 'static, VS: GiteratedObjectValue + IntoValueVTable + Setting + IntoSettingVTable, - HG: IntoPluginSettingGetter, - HS: IntoPluginSettingSetter; + HG: IntoPluginSettingGetter, + HS: IntoPluginSettingSetter; } -impl ValueSettingExt for PluginStackBuilder { +impl ValueSettingExt for PluginStackBuilder { fn value_setting(&mut self, _get: HG, _set: HS) -> &mut Self where O: GiteratedObject + IntoObjectVTable + 'static, VS: GiteratedObjectValue + IntoValueVTable + Setting + IntoSettingVTable, - HG: IntoPluginSettingGetter, - HS: IntoPluginSettingSetter, + HG: IntoPluginSettingGetter, + HS: IntoPluginSettingSetter, { self } @@ -261,30 +270,30 @@ pub struct ValueSettingGetterCallback; impl Callback for ValueSettingGetterCallback { type CallbackFunc = unsafe extern "C" fn( callback: CallbackPtr, - state: &PluginState, + state: FfiValueMut, object: FfiValueRef, ) -> Result; } -pub trait ValueSettingGetter { +pub trait ValueSettingGetter { unsafe extern "C" fn get_value( callback: CallbackPtr, - state: &PluginState, + state: FfiValueMut, object: FfiValueRef, ) -> Result; fn callback_ptr(&self) -> CallbackPtr; } -impl ValueSettingGetter for HG +impl ValueSettingGetter for HG where O: GiteratedObject, VS: GiteratedObjectValue, - HG: IntoPluginSettingGetter, + HG: IntoPluginSettingGetter, { unsafe extern "C" fn get_value( _callback: CallbackPtr, - _state: &PluginState, + _state: FfiValueMut, _object: FfiValueRef, ) -> Result { // let result = HG::get_setting(callback, state, object)?; diff --git a/plugins/example-plugin/src/lib.rs b/plugins/example-plugin/src/lib.rs index 723a922..ba91bcd 100644 --- a/plugins/example-plugin/src/lib.rs +++ b/plugins/example-plugin/src/lib.rs @@ -1,10 +1,15 @@ use anyhow::Error; use giterated_models::{ error::OperationError, + instance::Instance, object::{ObjectRequest, ObjectRequestError, ObjectResponse}, user::{DisplayName, User}, }; -use giterated_plugin::{new_stack::State, plugin}; +use giterated_plugin::{ + abi::state::{StateExtractor, StateUUID}, + local::PluginStackBuilder, + plugin, +}; plugin!( name: "Example Plugin", @@ -18,16 +23,23 @@ plugin!( /// Some kind of global state for the plugin struct PluginState; +impl StateUUID for PluginState { + fn uuid() -> u128 { + 214829528589663836760123667646253464473 + } +} + /// The plugin's initialization function. Ran when the plugin is loaded, used to /// build the plugin's stack. #[plugin::init] pub fn init(builder: &mut PluginStackBuilder) -> Result<(), Error> { - builder.insert_state(State); - - builder.object::().object::(); - - builder.value(value_getter); - builder.setting_getter(setting_getter); + builder + .insert_state(PluginState); + builder + .object::() + .object::(); + builder + .setting_getter(setting_getter); Ok(()) } @@ -35,14 +47,14 @@ pub fn init(builder: &mut PluginStackBuilder) -> Result<(), Error> { async fn handler( object: User, operation: ObjectRequest, - state_extractor: State, + // state_extractor: StateExtractor, ) -> Result> { todo!() } async fn setting_getter( object: User, - state_extractor: State, + // state_extractor: StateExtractor, ) -> Result> { todo!() } diff --git a/plugins/example-plugin/src/main.rs b/plugins/example-plugin/src/main.rs index aa2a8ac..3f20a12 100644 --- a/plugins/example-plugin/src/main.rs +++ b/plugins/example-plugin/src/main.rs @@ -5,7 +5,7 @@ use giterated_models::{ operation::OperationState, user::{DisplayName, User}, }; -use giterated_plugin::{callback::RuntimeState, handle::PluginHandle, new_stack::Runtime}; +use giterated_plugin::abi::state::{Runtime, RuntimeState}; use tracing::{info, Level}; #[tokio::main]