nsh-core 0.7.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! The graph's scheduling on a plain Rust host: numbers as values,
//! closures as computations.

use std::{
    cell::{Cell, RefCell, UnsafeCell},
    rc::Rc,
};

use super::*;

#[derive(Clone, Copy)]
struct Test;

type Func = Rc<dyn Fn(i64) -> Option<i64>>;

thread_local! {
    static GRAPH: UnsafeCell<Graph<Test>> = UnsafeCell::new(Graph::new(TestData::default()));
}

#[derive(Default)]
struct TestData {
    runaway: bool,
}

// SAFETY: one graph per thread; the module's borrows are short.
unsafe impl Host for Test {
    type Value = i64;
    type Func = Func;
    type Equals = fn(i64, i64) -> bool;
    type Cleanup = Box<dyn FnOnce()>;
    type Context = (u32, i64);
    type Data = TestData;

    unsafe fn graph<'a>(self) -> &'a mut Graph<Self> {
        // SAFETY: the thread's graph lives as long as the thread.
        GRAPH.with(|g| unsafe { &mut *g.get() })
    }
    fn empty() -> i64 {
        0
    }
    fn dup(self, v: &i64) -> i64 {
        *v
    }
    fn free(self, _: i64) {}
    fn dup_func(self, f: &Func) -> Func {
        f.clone()
    }
    fn free_func(self, _: Func) {}
    fn dup_equals(self, e: &Self::Equals) -> Self::Equals {
        *e
    }
    fn free_equals(self, _: Self::Equals) {}
    fn call(self, f: &Func, prev: i64) -> Option<i64> {
        f(prev)
    }
    fn same(self, a: &i64, b: &i64) -> bool {
        a == b
    }
    fn truthy(self, v: &i64) -> bool {
        *v != 0
    }
    fn custom_changed(self, e: &Self::Equals, prev: i64, next: &i64) -> bool {
        !e(prev, *next)
    }
    fn run_cleanup(self, f: Box<dyn FnOnce()>) {
        f();
    }
    fn free_cleanup(self, _: Box<dyn FnOnce()>) {}
    fn runaway(self) {
        // SAFETY: a short borrow.
        unsafe { self.graph() }.data.runaway = true;
    }
}

fn graph() -> &'static mut Graph<Test> {
    // SAFETY: tests take short borrows.
    unsafe { Test.graph() }
}

fn signal(v: i64) -> u32 {
    graph().signal(v, Equals::Default)
}

fn get(id: u32) -> i64 {
    read(Test, id)
}

fn set(id: u32, v: i64) {
    write(Test, id, v);
}

fn func(f: impl Fn(i64) -> i64 + 'static) -> Func {
    Rc::new(move |prev| Some(f(prev)))
}

fn computed(f: impl Fn() -> i64 + 'static) -> u32 {
    memo(Test, func(move |_| f()), 0, Equals::Default, false)
}

fn render_effect(f: impl Fn() + 'static) -> u32 {
    effect(
        Test,
        Kind::Effect,
        func(move |_| {
            f();
            0
        }),
        0,
    )
}

fn user_effect(f: impl Fn() + 'static) -> u32 {
    effect(
        Test,
        Kind::UserEffect,
        func(move |_| {
            f();
            0
        }),
        0,
    )
}

#[test]
fn a_diamond_runs_each_memo_once_per_write() {
    let a = signal(1);
    let runs = Rc::new(Cell::new(0));
    let b = computed(move || get(a) + 1);
    let c = computed(move || get(a) * 2);
    let d = {
        let runs = runs.clone();
        computed(move || {
            runs.set(runs.get() + 1);
            get(b) + get(c)
        })
    };
    let seen = Rc::new(RefCell::new(Vec::new()));
    let log = seen.clone();
    render_effect(move || log.borrow_mut().push(get(d)));
    set(a, 2);
    set(a, 3);
    assert_eq!(*seen.borrow(), [4, 7, 10]);
    assert_eq!(runs.get(), 3, "once at first, once per write");
}

#[test]
fn a_batch_runs_effects_once_at_its_end() {
    let a = signal(1);
    let b = signal(1);
    let runs = Rc::new(Cell::new(0));
    let r = runs.clone();
    render_effect(move || {
        get(a);
        get(b);
        r.set(r.get() + 1);
    });
    batch(Test, || {
        set(a, 2);
        set(b, 2);
    });
    assert_eq!(runs.get(), 2);
}

#[test]
fn equal_writes_and_equal_memos_stop_there() {
    let a = signal(1);
    let parity = computed(move || get(a) % 2);
    let runs = Rc::new(Cell::new(0));
    let r = runs.clone();
    render_effect(move || {
        get(parity);
        r.set(r.get() + 1);
    });
    set(a, 1);
    assert_eq!(runs.get(), 1, "an equal write notifies no one");
    set(a, 3);
    assert_eq!(runs.get(), 1, "a memo that stays equal stops the change");
    set(a, 4);
    assert_eq!(runs.get(), 2);
}

#[test]
fn user_effects_run_after_render_effects() {
    let a = signal(0);
    let order = Rc::new(RefCell::new(Vec::new()));
    let o = order.clone();
    user_effect(move || o.borrow_mut().push(("user", get(a))));
    let o = order.clone();
    render_effect(move || o.borrow_mut().push(("render", get(a))));
    order.borrow_mut().clear();
    set(a, 1);
    assert_eq!(*order.borrow(), [("render", 1), ("user", 1)]);
}

#[test]
fn cleanups_run_before_a_rerun_and_on_dispose() {
    let a = signal(0);
    let log = Rc::new(RefCell::new(Vec::new()));
    let l = log.clone();
    let (root, ()) = with_root(Test, || {
        render_effect(move || {
            let v = get(a);
            let l = l.clone();
            let _ = graph().on_cleanup(Box::new(move || l.borrow_mut().push(v)));
        });
    });
    set(a, 1);
    assert_eq!(*log.borrow(), [0]);
    dispose_owner(Test, root);
    assert_eq!(*log.borrow(), [0, 1]);
    set(a, 2);
    assert_eq!(*log.borrow(), [0, 1], "a disposed effect runs no more");
}

#[test]
fn an_owner_disposes_what_it_made_when_it_runs_again() {
    let show = signal(1);
    let inner = signal(0);
    let inner_runs = Rc::new(Cell::new(0));
    let r = inner_runs.clone();
    render_effect(move || {
        if get(show) != 0 {
            let r = r.clone();
            render_effect(move || {
                get(inner);
                r.set(r.get() + 1);
            });
        }
    });
    set(inner, 1);
    assert_eq!(inner_runs.get(), 2);
    set(show, 0);
    set(inner, 2);
    assert_eq!(inner_runs.get(), 2, "the inner effect went with its run");
}

#[test]
fn untracked_reads_make_no_edge() {
    let a = signal(0);
    let runs = Rc::new(Cell::new(0));
    let r = runs.clone();
    render_effect(move || {
        untracked(Test, || get(a));
        r.set(r.get() + 1);
    });
    set(a, 1);
    assert_eq!(runs.get(), 1);
}

#[test]
fn context_passes_to_what_is_made_under_its_owner() {
    let found = Rc::new(Cell::new(None));
    let f = found.clone();
    with_root(Test, || {
        let _ = graph().provide((7, 42));
        let (_, ()) = with_root(Test, || {
            let mut frame = graph().context();
            while let Some(fr) = frame {
                if fr.data.0 == 7 {
                    f.set(Some(fr.data.1));
                    break;
                }
                frame = fr.parent.clone();
            }
        });
    });
    assert_eq!(found.get(), Some(42));
}

#[test]
fn an_effect_that_keeps_writing_what_it_reads_runs_away() {
    let a = signal(0);
    render_effect(move || {
        let v = get(a);
        set(a, v + 1);
    });
    assert!(graph().data.runaway);
}

#[test]
fn rounds_do_not_add_up_to_a_runaway() {
    let a = signal(0);
    render_effect(move || {
        get(a);
    });
    // Each write is a round of its own: together they run the effect more
    // than `MAX_RUNS` times, none of them runs away.
    for v in 1..=MAX_RUNS as i64 + 10 {
        set(a, v);
    }
    assert!(!graph().data.runaway);
}

#[test]
fn handles_free_signals_and_reuse_slots_with_new_generations() {
    let a = signal(5);
    let generation = graph().generation(a);
    if let Some(held) = graph().drop_handle(a) {
        held.free(Test);
    }
    assert!(!graph().live(a, generation, u32::MAX));
    let b = signal(6);
    assert_eq!(b, a, "the slot is reused");
    assert!(graph().live(b, graph().generation(b), u32::MAX));
    assert_ne!(graph().generation(b), generation);
}

/// The graph's links hold: every edge has its back edge, no edge names a
/// freed slot, only computations have sources, and no slot is free twice.
fn check_links() {
    let g = graph();
    for (id, n) in g.nodes.iter().enumerate() {
        if n.kind == Kind::Free {
            continue;
        }
        let id = id as u32;
        assert!(
            n.is_computation() || n.sources.is_empty(),
            "{:?} {id} has sources",
            n.kind
        );
        for (i, &(s, slot)) in n.sources.iter().enumerate() {
            let back = g.nodes[s as usize].observers.get(slot as usize);
            assert_eq!(back, Some(&(id, i as u32)), "{id} reads {s}");
        }
        for (i, &(o, slot)) in n.observers.iter().enumerate() {
            assert_ne!(
                g.nodes[o as usize].kind,
                Kind::Free,
                "{id} is read by freed {o}"
            );
            let back = g.nodes[o as usize].sources.get(slot as usize);
            assert_eq!(back, Some(&(id, i as u32)), "{id} is read by {o}");
        }
    }
    let mut free = g.free.clone();
    free.sort_unstable();
    free.dedup();
    assert_eq!(free.len(), g.free.len(), "a slot freed twice: {:?}", g.free);
}

#[test]
fn a_computation_disposed_while_it_runs_is_left_with_no_edges() {
    let show = signal(1);
    let (a, b, c) = (signal(0), signal(0), signal(0));
    // Another reader of `c`, before the effect below in its list.
    render_effect(move || {
        get(c);
    });
    with_root(Test, || {
        computed(move || {
            if get(show) != 0 {
                render_effect(move || {
                    let _ = graph().on_cleanup(Box::new(move || {
                        get(a);
                        get(b);
                        get(c);
                    }));
                    // Closes the branch it runs in: it is disposed, its
                    // cleanup reads while it is still the listener.
                    set(show, 0);
                });
            }
            0
        });
    });
    // Its slot is reused, and the first effect leaves `c`'s readers.
    signal(0);
    set(c, 1);
    check_links();
}

#[test]
fn a_memo_read_while_it_is_disposed_does_not_run_again() {
    let s = signal(0);
    let other = signal(0);
    let runs = Rc::new(Cell::new(0));
    let r = runs.clone();
    with_root(Test, || {
        computed(move || {
            get(s);
            let me = Rc::new(Cell::new(NONE));
            let m = me.clone();
            let r = r.clone();
            let inner = memo(
                Test,
                func(move |_| {
                    r.set(r.get() + 1);
                    get(s);
                    get(other);
                    let m = m.clone();
                    render_effect(move || {
                        let m = m.clone();
                        // Reads the memo that owns it when it goes.
                        let _ = graph().on_cleanup(Box::new(move || {
                            get(m.get());
                        }));
                    });
                    0
                }),
                0,
                Equals::Default,
                false,
            );
            me.set(inner);
            0
        });
    });
    // The outer memo runs first and disposes the stale inner one.
    set(s, 1);
    assert_eq!(runs.get(), 2, "once at first, once made again");
    check_links();
}

#[test]
fn an_owner_disposed_again_by_its_cleanups_is_freed_once() {
    let close = signal(0);
    let root = Rc::new(Cell::new(NONE));
    let r = root.clone();
    render_effect(move || {
        if get(close) != 0 {
            dispose_owner(Test, r.get());
        }
    });
    let (id, ()) = with_root(Test, || {
        render_effect(move || {
            let _ = graph().on_cleanup(Box::new(move || set(close, 1)));
        });
    });
    root.set(id);
    // Outside a round: the cleanup's write runs the effect above at once.
    dispose_owner(Test, id);
    check_links();
}

#[test]
fn a_memo_whose_handle_goes_while_it_is_disposed_is_freed_once() {
    let a = signal(0);
    let me = Rc::new(Cell::new(NONE));
    let m = me.clone();
    let (root, ()) = with_root(Test, || {
        let id = memo(
            Test,
            func(move |_| {
                get(a);
                let m = m.clone();
                // A JS memo's object is finalized when a cleanup frees the
                // last reference to it.
                let _ = graph().on_cleanup(Box::new(move || {
                    if let Some(held) = graph().drop_handle(m.get()) {
                        held.free(Test);
                    }
                }));
                0
            }),
            0,
            Equals::Default,
            true,
        );
        me.set(id);
    });
    dispose_owner(Test, root);
    check_links();
}

#[test]
fn a_computation_disposed_in_a_nested_run_tracks_nothing_after() {
    let items = signal(1);
    let other = signal(0);
    let row = Rc::new(Cell::new(NONE));
    with_root(Test, || {
        // A list's memo: a row (a root) for its item, the last row goes.
        computed(move || {
            let n = get(items);
            dispose_owner(Test, row.get());
            // The row's item and index signals.
            signal(n);
            signal(0);
            let row = row.clone();
            with_root(Test, || {
                row.set(graph().owner());
                if n == 1 {
                    render_effect(move || {
                        // The list runs again in this write and disposes
                        // the row; the effect is the listener again after,
                        // and its slot holds the new row's index signal.
                        set(items, 2);
                        get(other);
                    });
                }
            });
            0
        });
    });
    check_links();
}

#[test]
fn compacting_gives_back_the_slots_of_a_tree_that_is_gone() {
    let kept = signal(1);
    let many: Vec<u32> = (0..5000).map(signal).collect();
    let last = *many.last().unwrap();
    let last_generation = graph().generation(last);
    // One in the middle stays: the slots after it go, those before stay free.
    let middle = many[100];
    for &id in &many {
        if id != middle
            && let Some(held) = graph().drop_handle(id)
        {
            held.free(Test);
        }
    }
    graph().compact();
    assert_eq!(graph().slots(), (2, middle as usize + 1));
    assert_eq!(get(kept), 1);
    assert_eq!(get(middle), 100);
    // A handle of a dropped slot never names the node made in its place.
    assert!(!graph().live(last, last_generation, u32::MAX));
    // Freed slots are reused lowest first.
    let next = signal(7);
    assert!(next < middle);
    if let Some(held) = graph().drop_handle(middle) {
        held.free(Test);
    }
    graph().compact();
    let mut made: Vec<u32> = (0..(last as i64 + 1)).map(signal).collect();
    made.sort_unstable();
    let again = made.iter().copied().find(|&id| id == last).unwrap();
    assert_ne!(graph().generation(again), last_generation);
    assert!(!graph().live(last, last_generation, u32::MAX));
    check_links();
}

/// A root made under an owner that is disposed before it keeps naming the
/// owner's slot; once the slot is reused by a node made under the root, the
/// root's `owner` link closes a loop. A write then must not hang `runTop`.
#[test]
fn a_root_whose_owner_went_does_not_loop_through_its_reused_slot() {
    let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let flag = done.clone();
    std::thread::spawn(move || {
        let tick = signal(0);
        let mut root = NONE;
        // The owner: a computation the root is made under (not owned by it).
        let owner = render_effect(|| {});
        with_owner(Test, owner, || {
            root = with_root(Test, || ()).0;
        });
        // The owner goes; the root does not.
        dispose(Test, owner);
        // A computation made under the root takes the owner's freed slot.
        let effect = with_owner(Test, root, || {
            render_effect(move || {
                get(tick);
            })
        });
        assert_eq!(effect, owner, "the freed slot is reused");
        assert_eq!(
            graph().parent(effect),
            root,
            "the slot's new node is under the root"
        );
        set(tick, 1);
        flag.store(true, std::sync::atomic::Ordering::SeqCst);
    });
    for _ in 0..50 {
        if done.load(std::sync::atomic::Ordering::SeqCst) {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
    panic!("runTop looped through the root's stale owner");
}

/// A memo whose function fails, as an effect's: what reads it hears of the
/// next change of what it read, and keeps the last value meanwhile.
#[test]
fn a_memo_that_failed_once_still_tells_its_readers_of_changes() {
    let a = signal(0);
    let m = memo(
        Test,
        Rc::new(move |_| {
            let v = get(a);
            (v != 1).then_some(v * 10)
        }),
        0,
        Equals::Default,
        false,
    );
    let seen = Rc::new(RefCell::new(Vec::new()));
    let log = seen.clone();
    render_effect(move || log.borrow_mut().push(get(m)));
    set(a, 1);
    assert_eq!(get(m), 0, "a failed run keeps the last value");
    set(a, 2);
    set(a, 3);
    assert_eq!(*seen.borrow(), vec![0, 20, 30]);
}

/// The same through a memo that reads the failed one.
#[test]
fn a_memo_that_failed_once_still_updates_the_memos_that_read_it() {
    let a = signal(0);
    let m = memo(
        Test,
        Rc::new(move |_| {
            let v = get(a);
            (v != 1).then_some(v)
        }),
        0,
        Equals::Default,
        false,
    );
    let n = computed(move || get(m) + 100);
    let seen = Rc::new(RefCell::new(Vec::new()));
    let log = seen.clone();
    user_effect(move || log.borrow_mut().push(get(n)));
    set(a, 1);
    set(a, 2);
    assert_eq!(*seen.borrow(), vec![100, 102]);
}