1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use crate::context::hooks::Hook;
use crate::innerlude::*;
use crate::nodes::VNode;
use bumpalo::Bump;
use generational_arena::Index;

use std::{
    any::TypeId,
    borrow::{Borrow, BorrowMut},
    cell::{RefCell, UnsafeCell},
    future::Future,
    marker::PhantomData,
    ops::{Deref, DerefMut},
    sync::atomic::AtomicUsize,
    sync::atomic::Ordering,
    todo,
};

/// Every component in Dioxus is represented by a `Scope`.
///
/// Scopes contain the state for hooks, the component's props, and other lifecycle information.
///
/// Scopes are allocated in a generational arena. As components are mounted/unmounted, they will replace slots of dead components.
/// The actual contents of the hooks, though, will be allocated with the standard allocator. These should not allocate as frequently.
pub struct Scope {
    // pub(crate) struct Scope {
    // TODO @Jon
    // These hooks are actually references into the hook arena
    // These two could be combined with "OwningRef" to remove unsafe usage
    // could also use ourborous
    pub hooks: RefCell<Vec<*mut Hook>>,
    pub hook_arena: typed_arena::Arena<Hook>,

    // Map to the parent
    pub parent: Option<Index>,

    pub frames: ActiveFrame,

    // List of listeners generated when CTX is evaluated
    pub listeners: Vec<*const dyn Fn(crate::events::VirtualEvent)>,
    // pub listeners: Vec<*const dyn Fn(crate::events::VirtualEvent)>,
    // pub listeners: Vec<Box<dyn Fn(crate::events::VirtualEvent)>>,

    // lying, cheating reference >:(
    pub props: Box<dyn std::any::Any>,

    // pub props_type: TypeId,
    pub caller: *const (),
}

impl Scope {
    // create a new scope from a function
    pub fn new<'a, P1, P2: 'static>(f: FC<P1>, props: P1, parent: Option<Index>) -> Self {
        let hook_arena = typed_arena::Arena::new();
        let hooks = RefCell::new(Vec::new());

        // Capture the caller
        let caller = f as *const ();

        let listeners = vec![];
        // let listeners: Vec<Box<dyn Fn(crate::events::VirtualEvent)>> = vec![];

        let old_frame = BumpFrame {
            bump: Bump::new(),
            head_node: VNode::text(""),
        };

        let new_frame = BumpFrame {
            bump: Bump::new(),
            head_node: VNode::text(""),
        };

        let frames = ActiveFrame::from_frames(old_frame, new_frame);

        // box the props
        let props = Box::new(props);

        let props = unsafe { std::mem::transmute::<_, Box<P2>>(props) };

        Self {
            hook_arena,
            hooks,
            caller,
            frames,
            listeners,
            parent,
            props,
        }
    }

    /// Create a new context and run the component with references from the Virtual Dom
    /// This function downcasts the function pointer based on the stored props_type
    ///
    /// Props is ?Sized because we borrow the props and don't need to know the size. P (sized) is used as a marker (unsized)
    pub fn run<'bump, PLocked: Sized + 'static>(&'bump mut self) {
        let frame = {
            let frame = self.frames.next();
            frame.bump.reset();
            log::debug!("Rednering into frame {:?}", frame as *const _);
            frame
        };

        let node_slot = std::rc::Rc::new(RefCell::new(None));
        let ctx: Context<'bump> = Context {
            arena: &self.hook_arena,
            hooks: &self.hooks,
            bump: &frame.bump,
            idx: 0.into(),
            _p: PhantomData {},
            final_nodes: node_slot.clone(),
        };

        unsafe {
            /*
            SAFETY ALERT

            This particular usage of transmute is outlined in its docs https://doc.rust-lang.org/std/mem/fn.transmute.html
            We hide the generic bound on the function item by casting it to raw pointer. When the function is actually called,
            we transmute the function back using the props as reference.

            we could do a better check to make sure that the TypeID is correct before casting
            --
            This is safe because we check that the generic type matches before casting.
            */
            // we use plocked to be able to remove the borrowed lifetime
            // these lifetimes could be very broken, so we need to dynamically manage them
            let caller = std::mem::transmute::<*const (), FC<PLocked>>(self.caller);
            let props = self.props.downcast_ref::<PLocked>().unwrap();

            // Note that the actual modification of the vnode head element occurs during this call
            let _nodes: DomTree = caller(ctx, props);

            /*
            SAFETY ALERT

            DO NOT USE THIS VNODE WITHOUT THE APPOPRIATE ACCESSORS.
            KEEPING THIS STATIC REFERENCE CAN LEAD TO UB.

            Some things to note:
            - The VNode itself is bound to the lifetime, but it itself is owned by scope.
            - The VNode has a private API and can only be used from accessors.
            - Public API cannot drop or destructure VNode
            */
            // the nodes we care about have been unsafely extended to a static lifetime in context
            frame.head_node = node_slot
                .deref()
                .borrow_mut()
                .take()
                .expect("Viewing did not happen");

            // todo:
            // make this so we dont have to iterate through the vnodes to get its listener
            let mut listeners = vec![];
            retrieve_listeners(&frame.head_node, &mut listeners);
            self.listeners = listeners
                .into_iter()
                .map(|f| {
                    let g = f.callback;
                    g as *const _
                })
                .collect();

            // consume the listeners from the head_node into a list of boxed ref listeners
        }
    }

    /// Accessor to get the root node and its children (safely)\
    /// Scope is self-referntial, so we are forced to use the 'static lifetime to cheat
    pub fn new_frame<'bump>(&'bump self) -> &'bump VNode<'bump> {
        self.frames.current_head_node()
    }

    pub fn old_frame<'bump>(&'bump self) -> &'bump VNode<'bump> {
        self.frames.prev_head_node()
    }
}

fn retrieve_listeners(node: &VNode<'static>, listeners: &mut Vec<&Listener>) {
    if let VNode::Element(el) = *node {
        for listener in el.listeners {
            // let g = listener as *const Listener;
            listeners.push(listener);
        }
        for child in el.children {
            retrieve_listeners(child, listeners);
        }
    }
}

// todo, do better with the active frame stuff
// somehow build this vnode with a lifetime tied to self
// This root node has  "static" lifetime, but it's really not static.
// It's goverened by the oldest of the two frames and is switched every time a new render occurs
// Use this node as if it were static is unsafe, and needs to be fixed with ourborous or owning ref
// ! do not copy this reference are things WILL break !
pub struct ActiveFrame {
    pub idx: AtomicUsize,
    pub frames: [BumpFrame; 2],
}

pub struct BumpFrame {
    pub bump: Bump,
    pub head_node: VNode<'static>,
}

impl ActiveFrame {
    fn from_frames(a: BumpFrame, b: BumpFrame) -> Self {
        Self {
            idx: 0.into(),
            frames: [a, b],
        }
    }

    fn current_head_node<'b>(&'b self) -> &'b VNode<'b> {
        let raw_node = match self.idx.borrow().load(Ordering::Relaxed) & 1 == 0 {
            true => &self.frames[0],
            false => &self.frames[1],
        };
        unsafe {
            let unsafe_head = &raw_node.head_node;
            let safe_node = std::mem::transmute::<&VNode<'static>, &VNode<'b>>(unsafe_head);
            safe_node
        }
    }

    fn prev_head_node<'b>(&'b self) -> &'b VNode<'b> {
        let raw_node = match self.idx.borrow().load(Ordering::Relaxed) & 1 != 0 {
            true => &self.frames[0],
            false => &self.frames[1],
        };

        unsafe {
            let unsafe_head = &raw_node.head_node;
            let safe_node = std::mem::transmute::<&VNode<'static>, &VNode<'b>>(unsafe_head);
            safe_node
        }
    }

    fn next(&mut self) -> &mut BumpFrame {
        self.idx.fetch_add(1, Ordering::Relaxed);
        let cur = self.idx.borrow().load(Ordering::Relaxed);
        log::debug!("Next frame! {}", cur);

        if cur % 2 == 0 {
            log::debug!("Chosing frame 0");
            &mut self.frames[0]
        } else {
            log::debug!("Chosing frame 1");
            &mut self.frames[1]
        }
        // match cur % 1 {
        //     0 => {
        //     }
        //     1 => {
        //     }
        //     _ => unreachable!("mod cannot by non-zero"),
        // }
    }
}

// #[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::bumpalo;
    // use crate::prelude::bumpalo::collections::string::String;
    use crate::prelude::format_args_f;

    static ListenerTest: FC<()> = |ctx, props| {
        ctx.view(html! {
            <div onclick={|_| println!("Hell owlrld")}>
                "hello"
            </div>
        })
    };

    #[test]
    fn check_listeners() -> Result<()> {
        let mut scope = Scope::new::<(), ()>(ListenerTest, (), None);
        scope.run::<()>();

        let nodes = scope.new_frame();
        dbg!(nodes);

        Ok(())
    }

    #[test]
    fn test_scope() {
        let example: FC<()> = |ctx, props| {
            use crate::builder::*;
            ctx.view(|b| div(b).child(text("a")).finish())
        };

        let props = ();
        let parent = None;
        let scope = Scope::new::<(), ()>(example, props, parent);
    }

    #[derive(Debug)]
    struct ExampleProps<'src> {
        name: &'src String,
    }
    // impl<'src> Properties<'src> for ExampleProps<'src> {}

    #[derive(Debug)]
    struct EmptyProps<'src> {
        name: &'src String,
    }
    // impl<'src> Properties<'src> for EmptyProps<'src> {}

    use crate::{builder::*, hooks::use_ref};

    fn example_fc<'a>(ctx: Context<'a>, props: &'a EmptyProps) -> DomTree {
        // fn example_fc<'a>(ctx: Context<'a>, props: &'a EmptyProps<'a>) -> DomTree {
        let (content, _): (&'a String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
        // let (content, _): (&'a String, _) = crate::hooks::use_state(&ctx, || "abcd".to_string());
        // let (text, set_val) = crate::hooks::use_state(&ctx, || "abcd".to_string());

        let childprops: ExampleProps<'a> = ExampleProps { name: content };
        // let childprops: ExampleProps<'a> = ExampleProps { name: content };
        ctx.view(move |b: &'a Bump| {
            div(b)
                .child(text(props.name))
                // .child(text(props.name))
                .child(virtual_child::<ExampleProps>(b, childprops, child_example))
                // .child(virtual_child::<ExampleProps<'a>>(b, childprops, CHILD))
                // as for<'scope> fn(Context<'_>, &'scope ExampleProps<'scope>) -> DomTree
                // |ctx, pops| todo!(),
                // .child(virtual_child::<'a>(
                //     b,
                //     child_example,
                //     ExampleProps { name: text },
                // ))
                .finish()
        })
    }

    fn child_example<'b>(ctx: Context<'b>, props: &'b ExampleProps) -> DomTree {
        ctx.view(move |b| {
            div(b)
                .child(text(props.name))
                //
                .finish()
        })
    }

    static CHILD: FC<ExampleProps> = |ctx, props: &'_ ExampleProps| {
        // todo!()
        ctx.view(move |b| {
            div(b)
                .child(text(props.name))
                //
                .finish()
        })
    };
    #[test]
    fn test_borrowed_scope() {
        // use crate::builder::*;

        let example: FC<EmptyProps> = |ctx, props| {
            // render counter
            // let mut val = crate::hooks::use_ref(&ctx, || 0);
            // val.modify(|f| {
            //     *f += 1;
            // });
            // dbg!(val.current());
            // only needs to be valid when ran?
            // can only borrow from parent?
            // props are boxed in parent's scope?
            // passing complex structures down to child?
            // stored value
            // let (text, set_val) = crate::hooks::use_state(&ctx, || "abcd".to_string());

            ctx.view(move |b| {
                todo!()
                // div(b)
                //     // .child(text(props.name))
                //     // .child(virtual_child(b, CHILD, ExampleProps { name: val.as_str() }))
                //     .child(virtual_child(b, CHILD, ExampleProps { name:  }))
                //     .finish()
            })
        };

        let source_text = "abcd123".to_string();
        let props = ExampleProps { name: &source_text };

        // let parent = None;
        // let mut scope =
        //     Scope::new::<EmptyProps, EmptyProps>(example, EmptyProps { name: &() }, parent);
        // scope.run::<ExampleProps>();
        // scope.run::<ExampleProps>();
        // scope.run::<ExampleProps>();
        // scope.run::<ExampleProps>();
        // scope.run::<ExampleProps>();
        // let nodes = scope.current_root_node();
        // dbg!(nodes);
    }
}