1
0

scope.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. use crate::context::hooks::Hook;
  2. use crate::innerlude::*;
  3. use crate::nodes::VNode;
  4. use bumpalo::Bump;
  5. use generational_arena::Index;
  6. use owning_ref::StableAddress;
  7. use std::{
  8. any::TypeId,
  9. borrow::{Borrow, BorrowMut},
  10. cell::{RefCell, UnsafeCell},
  11. future::Future,
  12. marker::PhantomData,
  13. ops::{Deref, DerefMut},
  14. sync::atomic::AtomicUsize,
  15. todo,
  16. };
  17. /// Every component in Dioxus is represented by a `Scope`.
  18. ///
  19. /// Scopes contain the state for hooks, the component's props, and other lifecycle information.
  20. ///
  21. /// Scopes are allocated in a generational arena. As components are mounted/unmounted, they will replace slots of dead components.
  22. /// The actual contents of the hooks, though, will be allocated with the standard allocator. These should not allocate as frequently.
  23. pub struct Scope {
  24. // pub(crate) struct Scope {
  25. // TODO @Jon
  26. // These hooks are actually references into the hook arena
  27. // These two could be combined with "OwningRef" to remove unsafe usage
  28. // could also use ourborous
  29. pub hooks: RefCell<Vec<*mut Hook>>,
  30. pub hook_arena: typed_arena::Arena<Hook>,
  31. // Map to the parent
  32. pub parent: Option<Index>,
  33. pub frames: ActiveFrame,
  34. // IE Which listeners need to be woken up?
  35. pub listeners: Vec<Box<dyn Fn()>>,
  36. // lying, cheating reference >:(
  37. pub props: Box<dyn std::any::Any>,
  38. // pub props: Box<dyn Properties>,
  39. //
  40. // pub props_type: TypeId,
  41. pub caller: *const (),
  42. }
  43. impl Scope {
  44. // create a new scope from a function
  45. pub fn new<'a, P1, P2: 'static>(
  46. // pub fn new<'a, P: Properties, PFree: P + 'a, PLocked: P + 'static>(
  47. f: FC<P1>,
  48. props: P1,
  49. parent: Option<Index>,
  50. ) -> Self
  51. // where
  52. // PFree: 'a,
  53. // PLocked: 'static,
  54. {
  55. // Capture the props type
  56. // let props_type = TypeId::of::<P>();
  57. let hook_arena = typed_arena::Arena::new();
  58. let hooks = RefCell::new(Vec::new());
  59. // Capture the caller
  60. let caller = f as *const ();
  61. let listeners = Vec::new();
  62. let old_frame = BumpFrame {
  63. bump: Bump::new(),
  64. head_node: VNode::text(""),
  65. };
  66. let new_frame = BumpFrame {
  67. bump: Bump::new(),
  68. head_node: VNode::text(""),
  69. };
  70. let frames = ActiveFrame::from_frames(old_frame, new_frame);
  71. // box the props
  72. let props = Box::new(props);
  73. // erase the lifetime
  74. // we'll manage this with dom lifecycle
  75. let props = unsafe { std::mem::transmute::<_, Box<P2>>(props) };
  76. // todo!()
  77. Self {
  78. hook_arena,
  79. hooks,
  80. // props_type,
  81. caller,
  82. frames,
  83. listeners,
  84. parent,
  85. props,
  86. }
  87. }
  88. /// Update this component's props with a new set of props, remotely
  89. ///
  90. ///
  91. pub(crate) fn update_props<'a, P>(&self, new_props: P) -> crate::error::Result<()> {
  92. Ok(())
  93. }
  94. /// Create a new context and run the component with references from the Virtual Dom
  95. /// This function downcasts the function pointer based on the stored props_type
  96. ///
  97. /// Props is ?Sized because we borrow the props and don't need to know the size. P (sized) is used as a marker (unsized)
  98. pub fn run<'bump, PLocked: Sized + 'static>(&'bump mut self) {
  99. let frame = {
  100. let frame = self.frames.next();
  101. frame.bump.reset();
  102. frame
  103. };
  104. let ctx: Context<'bump> = Context {
  105. arena: &self.hook_arena,
  106. hooks: &self.hooks,
  107. bump: &frame.bump,
  108. idx: 0.into(),
  109. _p: PhantomData {},
  110. };
  111. unsafe {
  112. // we use plocked to be able to remove the borrowed lifetime
  113. // these lifetimes could be very broken, so we need to dynamically manage them
  114. let caller = std::mem::transmute::<*const (), FC<PLocked>>(self.caller);
  115. let props = self.props.downcast_ref::<PLocked>().unwrap();
  116. let nodes: DomTree = caller(ctx, props);
  117. todo!("absorb domtree into self")
  118. // let nodes: VNode<'bump> = caller(ctx, props);
  119. // let unsafe_node = std::mem::transmute::<VNode<'bump>, VNode<'static>>(nodes);
  120. // frame.head_node = unsafe_node;
  121. }
  122. /*
  123. SAFETY ALERT
  124. This particular usage of transmute is outlined in its docs https://doc.rust-lang.org/std/mem/fn.transmute.html
  125. We hide the generic bound on the function item by casting it to raw pointer. When the function is actually called,
  126. we transmute the function back using the props as reference.
  127. we could do a better check to make sure that the TypeID is correct before casting
  128. --
  129. This is safe because we check that the generic type matches before casting.
  130. */
  131. /*
  132. SAFETY ALERT
  133. DO NOT USE THIS VNODE WITHOUT THE APPOPRIATE ACCESSORS.
  134. KEEPING THIS STATIC REFERENCE CAN LEAD TO UB.
  135. Some things to note:
  136. - The VNode itself is bound to the lifetime, but it itself is owned by scope.
  137. - The VNode has a private API and can only be used from accessors.
  138. - Public API cannot drop or destructure VNode
  139. */
  140. }
  141. /// Accessor to get the root node and its children (safely)\
  142. /// Scope is self-referntial, so we are forced to use the 'static lifetime to cheat
  143. pub fn current_root_node<'bump>(&'bump self) -> &'bump VNode<'bump> {
  144. self.frames.current_head_node()
  145. }
  146. pub fn prev_root_node<'bump>(&'bump self) -> &'bump VNode<'bump> {
  147. todo!()
  148. }
  149. }
  150. pub struct BumpFrame {
  151. pub bump: Bump,
  152. pub head_node: VNode<'static>,
  153. }
  154. // todo, do better with the active frame stuff
  155. // somehow build this vnode with a lifetime tied to self
  156. // This root node has "static" lifetime, but it's really not static.
  157. // It's goverened by the oldest of the two frames and is switched every time a new render occurs
  158. // Use this node as if it were static is unsafe, and needs to be fixed with ourborous or owning ref
  159. // ! do not copy this reference are things WILL break !
  160. pub struct ActiveFrame {
  161. pub idx: AtomicUsize,
  162. pub frames: [BumpFrame; 2],
  163. }
  164. impl ActiveFrame {
  165. fn from_frames(a: BumpFrame, b: BumpFrame) -> Self {
  166. Self {
  167. idx: 0.into(),
  168. frames: [a, b],
  169. }
  170. }
  171. fn current_head_node<'b>(&'b self) -> &'b VNode<'b> {
  172. let cur_idx = self.idx.borrow().load(std::sync::atomic::Ordering::Relaxed) % 1;
  173. let raw_node = &self.frames[cur_idx];
  174. unsafe {
  175. let unsafe_head = &raw_node.head_node;
  176. let safe_node = std::mem::transmute::<&VNode<'static>, &VNode<'b>>(unsafe_head);
  177. safe_node
  178. }
  179. }
  180. fn next(&mut self) -> &mut BumpFrame {
  181. self.idx.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
  182. let cur = self.idx.borrow().load(std::sync::atomic::Ordering::Relaxed);
  183. match cur % 1 {
  184. 1 => &mut self.frames[1],
  185. 0 => &mut self.frames[0],
  186. _ => unreachable!("mod cannot by non-zero"),
  187. }
  188. }
  189. }
  190. #[cfg(test)]
  191. mod tests {
  192. use super::*;
  193. #[test]
  194. fn test_scope() {
  195. let example: FC<()> = |ctx, props| {
  196. use crate::builder::*;
  197. ctx.view(|b| div(b).child(text("a")).finish())
  198. };
  199. let props = ();
  200. let parent = None;
  201. let scope = Scope::new::<(), ()>(example, props, parent);
  202. }
  203. #[derive(Debug)]
  204. struct ExampleProps<'src> {
  205. name: &'src String,
  206. }
  207. // impl<'src> Properties<'src> for ExampleProps<'src> {}
  208. #[derive(Debug)]
  209. struct EmptyProps<'src> {
  210. name: &'src String,
  211. }
  212. // impl<'src> Properties<'src> for EmptyProps<'src> {}
  213. use crate::{builder::*, hooks::use_ref};
  214. fn example_fc<'a>(ctx: Context<'a>, props: &'a EmptyProps) -> DomTree {
  215. // fn example_fc<'a>(ctx: Context<'a>, props: &'a EmptyProps<'a>) -> DomTree {
  216. let (content, _): (&'a String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
  217. // let (content, _): (&'a String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
  218. // let (text, set_val) = crate::hooks::use_state(&ctx, || "abcd".to_string());
  219. let childprops: ExampleProps<'a> = ExampleProps { name: content };
  220. // let childprops: ExampleProps<'a> = ExampleProps { name: content };
  221. ctx.view(move |b: &'a Bump| {
  222. div(b)
  223. .child(text(props.name))
  224. // .child(text(props.name))
  225. .child(virtual_child::<ExampleProps>(b, childprops, child_example))
  226. // .child(virtual_child::<ExampleProps<'a>>(b, childprops, CHILD))
  227. // as for<'scope> fn(Context<'_>, &'scope ExampleProps<'scope>) -> DomTree
  228. // |ctx, pops| todo!(),
  229. // .child(virtual_child::<'a>(
  230. // b,
  231. // child_example,
  232. // ExampleProps { name: text },
  233. // ))
  234. .finish()
  235. })
  236. }
  237. fn child_example(ctx: Context, props: &ExampleProps) -> DomTree {
  238. ctx.view(move |b| {
  239. div(b)
  240. .child(text(props.name))
  241. //
  242. .finish()
  243. })
  244. }
  245. static CHILD: FC<ExampleProps> = |ctx, props: &'_ ExampleProps| {
  246. // todo!()
  247. ctx.view(move |b| {
  248. div(b)
  249. .child(text(props.name))
  250. //
  251. .finish()
  252. })
  253. };
  254. #[test]
  255. fn test_borrowed_scope() {
  256. // use crate::builder::*;
  257. let example: FC<EmptyProps> = |ctx, props| {
  258. // render counter
  259. // let mut val = crate::hooks::use_ref(&ctx, || 0);
  260. // val.modify(|f| {
  261. // *f += 1;
  262. // });
  263. // dbg!(val.current());
  264. // only needs to be valid when ran?
  265. // can only borrow from parent?
  266. // props are boxed in parent's scope?
  267. // passing complex structures down to child?
  268. // stored value
  269. // let (text, set_val) = crate::hooks::use_state(&ctx, || "abcd".to_string());
  270. ctx.view(move |b| {
  271. todo!()
  272. // div(b)
  273. // // .child(text(props.name))
  274. // // .child(virtual_child(b, CHILD, ExampleProps { name: val.as_str() }))
  275. // .child(virtual_child(b, CHILD, ExampleProps { name: }))
  276. // .finish()
  277. })
  278. };
  279. let source_text = "abcd123".to_string();
  280. let props = ExampleProps { name: &source_text };
  281. // let parent = None;
  282. // let mut scope =
  283. // Scope::new::<EmptyProps, EmptyProps>(example, EmptyProps { name: &() }, parent);
  284. // scope.run::<ExampleProps>();
  285. // scope.run::<ExampleProps>();
  286. // scope.run::<ExampleProps>();
  287. // scope.run::<ExampleProps>();
  288. // scope.run::<ExampleProps>();
  289. // let nodes = scope.current_root_node();
  290. // dbg!(nodes);
  291. }
  292. }