arena.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use crate::{
  2. factory::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, AttributeValue, DynamicNode,
  3. ScopeId,
  4. };
  5. use bumpalo::boxed::Box as BumpBox;
  6. #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
  7. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
  8. pub struct ElementId(pub usize);
  9. pub(crate) struct ElementRef {
  10. // the pathway of the real element inside the template
  11. pub path: ElementPath,
  12. // The actual template
  13. pub template: *const VNode<'static>,
  14. }
  15. #[derive(Clone, Copy)]
  16. pub enum ElementPath {
  17. Deep(&'static [u8]),
  18. Root(usize),
  19. }
  20. impl ElementRef {
  21. pub(crate) fn null() -> Self {
  22. Self {
  23. template: std::ptr::null_mut(),
  24. path: ElementPath::Root(0),
  25. }
  26. }
  27. }
  28. impl VirtualDom {
  29. pub(crate) fn next_element(&mut self, template: &VNode, path: &'static [u8]) -> ElementId {
  30. let entry = self.elements.vacant_entry();
  31. let id = entry.key();
  32. entry.insert(ElementRef {
  33. template: template as *const _ as *mut _,
  34. path: ElementPath::Deep(path),
  35. });
  36. ElementId(id)
  37. }
  38. pub(crate) fn next_root(&mut self, template: &VNode, path: usize) -> ElementId {
  39. let entry = self.elements.vacant_entry();
  40. let id = entry.key();
  41. entry.insert(ElementRef {
  42. template: template as *const _ as *mut _,
  43. path: ElementPath::Root(path),
  44. });
  45. ElementId(id)
  46. }
  47. pub(crate) fn reclaim(&mut self, el: ElementId) {
  48. self.try_reclaim(el)
  49. .unwrap_or_else(|| panic!("cannot reclaim {:?}", el));
  50. }
  51. pub(crate) fn try_reclaim(&mut self, el: ElementId) -> Option<ElementRef> {
  52. if el.0 == 0 {
  53. panic!(
  54. "Invalid element set to 0 - {:#?}",
  55. std::backtrace::Backtrace::force_capture()
  56. )
  57. }
  58. println!("reclaiming {:?}", el);
  59. self.elements.try_remove(el.0)
  60. }
  61. pub(crate) fn update_template(&mut self, el: ElementId, node: &VNode) {
  62. let node: *const VNode = node as *const _;
  63. self.elements[el.0].template = unsafe { std::mem::transmute(node) };
  64. }
  65. // Drop a scope and all its children
  66. pub(crate) fn drop_scope(&mut self, id: ScopeId) {
  67. let scope = self.scopes.get(id.0).unwrap();
  68. if let Some(root) = scope.as_ref().try_root_node() {
  69. let root = unsafe { root.extend_lifetime_ref() };
  70. if let RenderReturn::Sync(Ok(node)) = root {
  71. self.drop_scope_inner(node)
  72. }
  73. }
  74. let scope = self.scopes.get_mut(id.0).unwrap();
  75. scope.props.take();
  76. // Drop all the hooks once the children are dropped
  77. // this means we'll drop hooks bottom-up
  78. for hook in scope.hook_list.get_mut().drain(..) {
  79. println!("dropping hook !");
  80. drop(unsafe { BumpBox::from_raw(hook) });
  81. println!("hook dropped !");
  82. }
  83. }
  84. fn drop_scope_inner(&mut self, node: &VNode) {
  85. for attr in node.dynamic_attrs {
  86. if let AttributeValue::Listener(l) = &attr.value {
  87. l.borrow_mut().take();
  88. }
  89. }
  90. for (idx, _) in node.template.roots.iter().enumerate() {
  91. match node.dynamic_root(idx) {
  92. Some(DynamicNode::Component(c)) => self.drop_scope(c.scope.get().unwrap()),
  93. Some(DynamicNode::Fragment(nodes)) => {
  94. for node in *nodes {
  95. self.drop_scope_inner(node);
  96. }
  97. }
  98. _ => {}
  99. }
  100. }
  101. }
  102. }
  103. impl ElementPath {
  104. pub(crate) fn is_ascendant(&self, big: &&[u8]) -> bool {
  105. match *self {
  106. ElementPath::Deep(small) => small.len() <= big.len() && small == &big[..small.len()],
  107. ElementPath::Root(r) => big.len() == 1 && big[0] == r as u8,
  108. }
  109. }
  110. }
  111. impl PartialEq<&[u8]> for ElementPath {
  112. fn eq(&self, other: &&[u8]) -> bool {
  113. match *self {
  114. ElementPath::Deep(deep) => deep.eq(*other),
  115. ElementPath::Root(r) => other.len() == 1 && other[0] == r as u8,
  116. }
  117. }
  118. }