chore: improve debugability of fcptrs and vcomponents

this commit changes the fc slot type from a thin pointer to a c pointer.
this lets us provide frame inspection of components using backtrace.
This commit is contained in:
Jonathan Kelley 2022-01-31 03:22:34 -05:00
parent 0820e18d3d
commit 120ee18368
3 changed files with 16 additions and 9 deletions

View file

@ -4,7 +4,7 @@
//! cheap and *very* fast to construct - building a full tree should be quick. //! cheap and *very* fast to construct - building a full tree should be quick.
use crate::{ use crate::{
innerlude::{Element, Properties, Scope, ScopeId, ScopeState}, innerlude::{ComponentPtr, Element, Properties, Scope, ScopeId, ScopeState},
lazynodes::LazyNodes, lazynodes::LazyNodes,
AnyEvent, Component, AnyEvent, Component,
}; };
@ -177,11 +177,18 @@ impl Debug for VNode<'_> {
.field("children", &el.children) .field("children", &el.children)
.finish(), .finish(),
VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text), VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text),
VNode::Placeholder(_) => write!(s, "VNode::VPlaceholder"), VNode::Placeholder(t) => write!(s, "VNode::VPlaceholder {{ id: {:?} }}", t.id),
VNode::Fragment(frag) => { VNode::Fragment(frag) => {
write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children) write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children)
} }
VNode::Component(comp) => write!(s, "VNode::VComponent {{ fc: {:?}}}", comp.user_fc), VNode::Component(comp) => s
.debug_struct("VNode::VComponent")
.field("name", &comp.fn_name)
.field("fnptr", &comp.user_fc)
.field("key", &comp.key)
.field("scope", &comp.scope)
.field("originator", &comp.originator)
.finish(),
} }
} }
} }
@ -384,7 +391,7 @@ pub struct VComponent<'src> {
pub originator: ScopeId, pub originator: ScopeId,
pub scope: Cell<Option<ScopeId>>, pub scope: Cell<Option<ScopeId>>,
pub can_memoize: bool, pub can_memoize: bool,
pub user_fc: *const (), pub user_fc: ComponentPtr,
pub fn_name: &'static str, pub fn_name: &'static str,
pub props: RefCell<Option<Box<dyn AnyProps + 'src>>>, pub props: RefCell<Option<Box<dyn AnyProps + 'src>>>,
} }
@ -559,7 +566,7 @@ impl<'a> NodeFactory<'a> {
key: key.map(|f| self.raw_text(f).0), key: key.map(|f| self.raw_text(f).0),
scope: Default::default(), scope: Default::default(),
can_memoize: P::IS_STATIC, can_memoize: P::IS_STATIC,
user_fc: component as *const (), user_fc: component as ComponentPtr,
originator: self.scope.scope_id(), originator: self.scope.scope_id(),
fn_name, fn_name,
props: RefCell::new(Some(Box::new(VComponentProps { props: RefCell::new(Some(Box::new(VComponentProps {

View file

@ -13,7 +13,7 @@ use std::{
rc::Rc, rc::Rc,
}; };
pub(crate) type FcSlot = *const (); pub(crate) type ComponentPtr = *mut std::os::raw::c_void;
pub(crate) struct Heuristic { pub(crate) struct Heuristic {
hook_arena_size: usize, hook_arena_size: usize,
@ -28,7 +28,7 @@ pub(crate) struct ScopeArena {
pub scope_gen: Cell<usize>, pub scope_gen: Cell<usize>,
pub bump: Bump, pub bump: Bump,
pub scopes: RefCell<FxHashMap<ScopeId, *mut ScopeState>>, pub scopes: RefCell<FxHashMap<ScopeId, *mut ScopeState>>,
pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>, pub heuristics: RefCell<FxHashMap<ComponentPtr, Heuristic>>,
pub free_scopes: RefCell<Vec<*mut ScopeState>>, pub free_scopes: RefCell<Vec<*mut ScopeState>>,
pub nodes: RefCell<Slab<*const VNode<'static>>>, pub nodes: RefCell<Slab<*const VNode<'static>>>,
pub tasks: Rc<TaskQueue>, pub tasks: Rc<TaskQueue>,
@ -82,7 +82,7 @@ impl ScopeArena {
pub(crate) fn new_with_key( pub(crate) fn new_with_key(
&self, &self,
fc_ptr: *const (), fc_ptr: ComponentPtr,
vcomp: Box<dyn AnyProps>, vcomp: Box<dyn AnyProps>,
parent_scope: Option<ScopeId>, parent_scope: Option<ScopeId>,
container: ElementId, container: ElementId,

View file

@ -212,7 +212,7 @@ impl VirtualDom {
let scopes = ScopeArena::new(channel.0.clone()); let scopes = ScopeArena::new(channel.0.clone());
scopes.new_with_key( scopes.new_with_key(
root as *const _, root as ComponentPtr,
Box::new(VComponentProps { Box::new(VComponentProps {
props: root_props, props: root_props,
memo: |_a, _b| unreachable!("memo on root will neve be run"), memo: |_a, _b| unreachable!("memo on root will neve be run"),