diff --git a/Cargo.toml b/Cargo.toml index 91955b533..af1f57201 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ router = ["dioxus-router"] tui = ["dioxus-tui"] liveview = ["dioxus-liveview"] autofmt = ["dioxus-autofmt"] -hot_reload = ["dioxus-core-macro/hot_reload"] +hot_reload = ["dioxus-core-macro/hot_reload", "dioxus-rsx-interperter"] [workspace] members = [ @@ -85,6 +85,7 @@ criterion = "0.3.5" thiserror = "1.0.30" env_logger = "0.9.0" + [[bench]] name = "create" harness = false diff --git a/packages/core-macro/src/lib.rs b/packages/core-macro/src/lib.rs index f1dd28a60..55249c846 100644 --- a/packages/core-macro/src/lib.rs +++ b/packages/core-macro/src/lib.rs @@ -1,7 +1,5 @@ use proc_macro::TokenStream; -use quote::ToTokens; -use rsx::{BodyNode, Component, ContentField, ElementAttr, IfmtInput}; -use std::collections::HashMap; +use quote::{quote, ToTokens}; use syn::parse_macro_input; mod inlineprops; @@ -190,7 +188,22 @@ pub fn rsx(s: TokenStream) -> TokenStream { use dioxus_rsx_interperter::captuered_context::CapturedContextBuilder; let captured = CapturedContextBuilder::from_call_body(body); - captured.to_token_stream().into() + quote! { + { + let line_num = get_line_num(); + LazyNodes::new(|factory|{ + let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap(); + let read = rsx_text_index.read(); + let text = read.get(line_num).unwrap(); + interpert_rsx( + factory, + &text, + #captured + ) + }) + } + } + .into() } #[cfg(not(feature = "hot_reload"))] body.to_token_stream().into() diff --git a/packages/core/Cargo.toml b/packages/core/Cargo.toml index 480c3904d..4b10afbca 100644 --- a/packages/core/Cargo.toml +++ b/packages/core/Cargo.toml @@ -49,4 +49,3 @@ backtrace = "0.3" default = [] serialize = ["serde"] debug_vdom = [] - diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index eabec8983..f31e2e328 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -85,8 +85,6 @@ pub mod prelude { fc_to_builder, Attributes, Component, DioxusElement, Element, EventHandler, Fragment, LazyNodes, NodeFactory, Properties, Scope, ScopeId, ScopeState, VNode, VirtualDom, }; - #[cfg(feature = "hot_reload")] - pub use dioxus_rsx_interperter::captuered_context::{CapturedContext, IfmtArgs}; } pub mod exports { diff --git a/packages/rsx_interperter/src/captuered_context.rs b/packages/rsx_interperter/src/captuered_context.rs index f3df4f341..21683e004 100644 --- a/packages/rsx_interperter/src/captuered_context.rs +++ b/packages/rsx_interperter/src/captuered_context.rs @@ -6,10 +6,10 @@ use syn::Expr; #[derive(Default)] pub struct CapturedContextBuilder { - attributes: HashMap, - text: Vec, - components: Vec, - iterators: Vec, + pub attributes: HashMap, + pub text: Vec, + pub components: Vec, + pub iterators: Vec, } impl CapturedContextBuilder { @@ -89,7 +89,7 @@ impl ToTokens for CapturedContextBuilder { tokens.append_all(quote! { CapturedContext { captured: IfmtArgs{ - named_args: &'static [#((#captured_names, #captured_expr)),*] + named_args: &[#((#captured_names, #captured_expr)),*] }, components: vec![#(#components),*], iterators: vec![#(#iterators),*], @@ -105,11 +105,11 @@ struct CapturedComponentBuilder { pub struct CapturedContext<'a> { // map of the attribute name to the formated value - captured: IfmtArgs, + pub captured: IfmtArgs, // the only thing we can update in component is the children - components: Vec>, + pub components: Vec>, // we can't reasonably interpert iterators, so they are staticly inserted - iterators: Vec>, + pub iterators: Vec>, } pub struct IfmtArgs { diff --git a/packages/rsx_interperter/src/lib.rs b/packages/rsx_interperter/src/lib.rs index 0e9826509..e041e52f2 100644 --- a/packages/rsx_interperter/src/lib.rs +++ b/packages/rsx_interperter/src/lib.rs @@ -1,6 +1,10 @@ use crate::interperter::build; -use dioxus_core::LazyNodes; +use dioxus_core::{LazyNodes, VNode}; use dioxus_rsx::CallBody; +use std::collections::HashMap; +use std::panic::Location; +use std::rc::Rc; +use std::sync::{RwLock, RwLockReadGuard}; use syn::{parse_str, Result}; mod attributes; @@ -8,6 +12,30 @@ pub mod captuered_context; mod elements; mod interperter; -pub fn rsx_to_html(text: &str, context: &captuered_context::CapturedContext) -> { +pub fn interpert_rsx<'a, 'b>( + factory: dioxus_core::NodeFactory<'a>, + text: &str, + context: captuered_context::CapturedContext, +) -> VNode<'a> { panic!() } + +#[track_caller] +pub fn get_line_num() -> &'static Location<'static> { + Location::caller() +} + +#[derive(Debug, Default, Clone)] +pub struct RsxTextIndex { + hm: Rc, String>>>, +} + +impl RsxTextIndex { + pub fn insert(&self, loc: &'static Location<'static>, text: String) { + self.hm.write().unwrap().insert(loc, text); + } + + pub fn read(&self) -> RwLockReadGuard, String>> { + self.hm.read().unwrap() + } +} diff --git a/src/lib.rs b/src/lib.rs index 787a185d2..60f7128f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,4 +53,10 @@ pub mod prelude { #[cfg(feature = "fermi")] pub use fermi::{use_atom_ref, use_init_atom_root, use_read, use_set, Atom, AtomRef}; + + #[cfg(feature = "hot_reload")] + pub use dioxus_rsx_interperter::{ + captuered_context::{CapturedContext, IfmtArgs}, + get_line_num, interpert_rsx, RsxTextIndex, + }; }