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
//! Calls from JS into Rust, and what goes back, as Rust values: no JSON
//! between JS and Rust components, modules or classes.
//!
//! `nsh-js` converts a call's JS arguments into the Rust types in the JS
//! call (the generated decoders, [`super::JsValue::from_js`]) and queues
//! what the call does as an op ([`RustOp`]): a closure holding the typed
//! arguments, which the core runs in order with the UI's ops (a module
//! function with a [`super::ModuleCx`], the start of an `async` function's
//! or member's task, a ref's method). Results are boxed Rust values
//! ([`ToJs`]) the core hands back in its event stream ([`JsEvent`]), in
//! order with the other events, and `nsh-js` converts them when it settles
//! the promise. A Rust component's props cross the same way: decoded in
//! the JS call into an [`AnyValue`], set by the core.
//!
//! Platform native components (the components an embedder registers in
//! its own language) keep JSON (`Prop::ComponentProp`, `EventPayload::Native`,
//! `Command::Native`): their values cross into another language.

use std::{
    any::Any,
    cell::{Cell, RefCell},
    collections::VecDeque,
    fmt,
    rc::Rc,
    sync::Arc,
};

use super::{
    ModuleCx, ModuleFuture, PropError,
    callback::JsCall,
    js::{JsHost, JsOwned},
};
use crate::{EventKind, EventPayload, NodeId};

/// A Rust value of some type, decoded from JS for a component's prop.
/// Cheap to clone; two are equal when they are the same value (the prop
/// keeps its own comparison of what it holds).
#[derive(Clone)]
pub struct AnyValue(Rc<dyn Any>);

impl AnyValue {
    /// `value`, type-erased.
    pub fn new<T: 'static>(value: T) -> Self {
        Self(Rc::new(value))
    }

    /// The value, if it is a `T`.
    pub fn take<T: Clone + 'static>(self) -> Option<T> {
        self.0.downcast::<T>().ok().map(Rc::unwrap_or_clone)
    }
}

impl PartialEq for AnyValue {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl fmt::Debug for AnyValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("AnyValue")
    }
}

/// A Rust value JS gets: converted when JS receives it (a call's result,
/// an event's data).
pub trait ToJs: 'static {
    /// The value as a JS value of `host`.
    fn to_js_value<'a>(self: Box<Self>, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError>;
}

impl<T: super::Ret + 'static> ToJs for T {
    fn to_js_value<'a>(self: Box<Self>, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError> {
        (*self).into_js(host)
    }
}

/// The result of a call JS waits for: the value, or the message its
/// promise rejects with.
pub type JsResult = Result<Box<dyn ToJs>, super::Thrown>;

/// A module function with a context, its arguments decoded: run by the
/// core in order with the UI's ops.
pub type OpCall = Box<dyn FnOnce(&mut ModuleCx) -> JsResult>;

/// The start of an `async` function's or member's task, its arguments
/// decoded: the core starts it in order with the UI's ops.
pub type TaskStart = Box<dyn FnOnce() -> ModuleFuture>;

/// A method of a component's ref, its arguments decoded: called with the
/// API the component registered (`None`: an API of another type).
pub type MethodCall = Box<dyn FnOnce(&dyn Any) -> Option<JsResult>>;

/// What a call JS made into Rust does, carried by an op.
pub enum RustCall {
    /// A module function with a context.
    Op(OpCall),
    /// The start of an `async` function's or member's task.
    Task(TaskStart),
    /// A method of the ref of the component on node `0`.
    Method(NodeId, MethodCall),
    /// The place of a JS call posted during a synchronous call from JS
    /// ([`Posts::park`]): applied (dropped), the call runs after what the
    /// ops before it post.
    Post(ParkedPost),
}

/// A call JS made into Rust ([`crate::Op::Rust`]): what it does, and the
/// number of the promise its result settles (0: none).
pub struct RustOp {
    /// The promise's call number (0: JS does not wait).
    pub call: u32,
    /// What it does.
    pub run: RustCall,
}

impl fmt::Debug for RustOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let kind = match &self.run {
            RustCall::Op(_) => "Op",
            RustCall::Task(_) => "Task",
            RustCall::Method(..) => "Method",
            RustCall::Post(_) => "Post",
        };
        write!(f, "RustOp({kind}, call {})", self.call)
    }
}

/// A Rust component's event JS hears: its name and data.
#[doc(hidden)]
pub trait EventToJs {
    /// The event's name (`press`: JS's `onPress`).
    fn name(&self) -> &'static str;
    /// Its data as a JS value of `host`.
    fn data_js<'a>(&self, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError>;
}

/// What the core hands JS after a commit, in order: the platform's and the
/// core's events, and the Rust values for JS (a Rust component's events,
/// whether its lazy children are placed, calls' results).
pub enum JsEvent {
    /// An event of a node (layout, scroll, a platform component's).
    Event(NodeId, EventKind, EventPayload),
    /// The initial frame for one newly installed Rust layout listener.
    /// Delivered by the engine to `Core::component_layout`.
    #[doc(hidden)]
    LayoutListener {
        /// The component whose view installed the listener.
        host: NodeId,
        /// Its node.
        id: NodeId,
        /// The newly installed callback.
        listener: super::Listener<EventPayload>,
        /// The node's current frame.
        frame: crate::Bounds<crate::Points>,
    },
    /// An event of the Rust component hosted by `host`.
    Component {
        /// The component's host node.
        host: NodeId,
        /// The event.
        event: Box<dyn EventToJs>,
    },
    /// Whether the Rust component hosted by `host` places its lazy
    /// children now.
    Children {
        /// The component's host node.
        host: NodeId,
        /// Placed.
        wanted: bool,
    },
    /// The Rust component hosted by `host` places the content of its
    /// render prop `prop` (`Option<Renderer<T>>`) for `key` with `value`,
    /// which JS did not render yet: JS calls the app's function with it
    /// and inserts what it returns into the host in a holder view of its
    /// own (`componentRender`: the prop, U+001F, the key), replacing the
    /// one it made for the key before. The core places the holder.
    Render {
        /// The component's host node.
        host: NodeId,
        /// The prop holding the app's function.
        prop: &'static str,
        /// The key, unique among the component's renders of the prop.
        key: super::SharedString,
        /// The value to render.
        value: std::rc::Rc<dyn super::RenderValue>,
        /// The function to render with instead of the prop's, which the
        /// component holds itself: JS calls it once for the key, with
        /// props reading the latest value, and a later `Render` of the key
        /// sets that value in place (no new holder).
        func: Option<std::rc::Rc<dyn super::CallbackHandle>>,
    },
    /// No element of the component's render (`prop`, `key`) is placed any
    /// more ([`Self::Render`]): JS removes its holder and disposes what it
    /// rendered.
    Unrender {
        /// The component's host node.
        host: NodeId,
        /// The prop holding the app's function.
        prop: &'static str,
        /// The key.
        key: super::SharedString,
    },
    /// The `<Each>` rows whose placeholders ([`crate::Op::Placeholder`])
    /// would be made now: JS renders them in their place.
    #[doc(hidden)]
    Rows(Vec<NodeId>),
    /// The result of call `call` (its promise settles).
    Reply {
        /// The call number.
        call: u32,
        /// The value or the rejection's message.
        result: JsResult,
    },
}

impl fmt::Debug for JsEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            JsEvent::Event(id, kind, payload) => write!(f, "Event({id}, {kind:?}, {payload:?})"),
            JsEvent::LayoutListener {
                host, id, frame, ..
            } => {
                write!(f, "LayoutListener({host}, {id}, {frame:?})")
            }
            JsEvent::Component { host, event } => write!(f, "Component({host}, {})", event.name()),
            JsEvent::Children { host, wanted } => write!(f, "Children({host}, {wanted})"),
            JsEvent::Render {
                host, prop, key, ..
            } => write!(f, "Render({host}, {prop}, {key})"),
            JsEvent::Unrender { host, prop, key } => write!(f, "Unrender({host}, {prop}, {key})"),
            JsEvent::Rows(ids) => write!(f, "Rows({ids:?})"),
            JsEvent::Reply { call, result } => write!(f, "Reply({call}, ok: {})", result.is_ok()),
        }
    }
}

// --- Posts ----------------------------------------------------------------------

/// The place in [`Posts`] of a call posted during a synchronous call from
/// JS, which [`RustCall::Post`] carries: dropping it lets the call run
/// there, whichever path drops it (the op applied, or dropped unapplied).
#[doc(hidden)]
pub struct ParkedPost {
    posts: Posts,
    id: u64,
}

impl Drop for ParkedPost {
    fn drop(&mut self) {
        self.posts.release(self.id);
    }
}

impl fmt::Debug for ParkedPost {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ParkedPost({})", self.id)
    }
}

/// The calls of JS functions Rust posts ([`super::Callback::post`],
/// `call_async`) for the next JS turn, in one ordered queue. One per
/// engine, made by it and handed to `nsh-js` and the core. Cheap to clone.
///
/// The calls run in the order they were posted, with one exception that
/// keeps JS's program order: a call posted during a synchronous call from
/// JS is parked in its place until the op JS sent after it is applied
/// ([`RustCall::Post`]), and what the ops before that one post while the
/// core applies them goes before it. So a call a task posts after the
/// synchronous call runs after it, and one an op JS sent before the call
/// posts runs before it. The queue runs up to the first place not
/// released yet; a place is released whichever way its op goes (applied,
/// or dropped unapplied: [`ParkedPost`]), so no lost op stalls it.
#[derive(Clone, Default)]
pub struct Posts(Rc<PostsInner>);

#[derive(Default)]
struct PostsInner {
    queue: RefCell<VecDeque<Entry>>,
    next_parked: Cell<u64>,
    /// The core is applying an op JS sent ([`Posts::in_op`]).
    in_op: Cell<u32>,
    notify: RefCell<Option<Arc<dyn Fn() + Send + Sync>>>,
    /// JS stopped for good: calls are dropped (their replies say so).
    closed: Cell<bool>,
}

enum Entry {
    Ready(JsCall),
    /// Parked as `id`; released (runnable) once its op goes.
    Parked {
        id: u64,
        call: JsCall,
        released: bool,
    },
}

impl fmt::Debug for Posts {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Posts")
            .field("queued", &self.0.queue.borrow().len())
            .finish()
    }
}

impl Posts {
    /// An empty queue.
    pub fn new() -> Self {
        Self::default()
    }

    /// Calls `notify` when a call becomes runnable (it wakes the engine).
    pub fn set_notify(&self, notify: Arc<dyn Fn() + Send + Sync>) {
        *self.0.notify.borrow_mut() = Some(notify);
    }

    fn wake(&self) {
        let notify = self.0.notify.borrow().clone();
        if let Some(notify) = notify {
            notify();
        }
    }

    /// Queues `call` and wakes the engine.
    #[doc(hidden)]
    pub fn post(&self, call: JsCall) {
        if self.0.closed.get() {
            // Dropped: a reply waiting for it is told JS stopped.
            drop(call);
            return;
        }
        {
            let mut queue = self.0.queue.borrow_mut();
            // An op's post goes before the places of the calls JS posted
            // after sending it (parked, not released: their ops come
            // later).
            let at = (self.0.in_op.get() > 0)
                .then(|| {
                    queue.iter().position(|e| {
                        matches!(
                            e,
                            Entry::Parked {
                                released: false,
                                ..
                            }
                        )
                    })
                })
                .flatten();
            match at {
                Some(at) => queue.insert(at, Entry::Ready(call)),
                None => queue.push_back(Entry::Ready(call)),
            }
        }
        self.wake();
    }

    /// Runs `f`, the work of an op JS sent: what it posts goes before the
    /// calls JS posted after the op.
    pub(crate) fn in_op<R>(&self, f: impl FnOnce() -> R) -> R {
        struct Raised<'a>(&'a Cell<u32>);
        impl Drop for Raised<'_> {
            fn drop(&mut self) {
                self.0.set(self.0.get() - 1);
            }
        }
        self.0.in_op.set(self.0.in_op.get() + 1);
        let _raised = Raised(&self.0.in_op);
        f()
    }

    /// Queues `call` parked in its place until the place it returns is
    /// dropped: when its op is applied, or dropped by any other path (the
    /// call then runs in its place, or with the queue when it closes), so
    /// a lost op never holds back the calls after it.
    #[doc(hidden)]
    pub fn park(&self, call: JsCall) -> ParkedPost {
        let id = self.0.next_parked.get() + 1;
        self.0.next_parked.set(id);
        if self.0.closed.get() {
            drop(call);
        } else {
            self.0.queue.borrow_mut().push_back(Entry::Parked {
                id,
                call,
                released: false,
            });
        }
        ParkedPost {
            posts: self.clone(),
            id,
        }
    }

    /// Lets the call parked as `id` run in its place.
    fn release(&self, id: u64) {
        let found = self.0.queue.borrow_mut().iter_mut().any(|e| match e {
            Entry::Parked {
                id: parked,
                released,
                ..
            } if *parked == id => {
                *released = true;
                true
            }
            _ => false,
        });
        if found {
            self.wake();
        }
    }

    /// No JS runs any more (the VM stopped or went): the queued calls are
    /// dropped, and so are those posted later (their replies say so).
    pub fn close(&self) {
        self.0.closed.set(true);
        let queue = std::mem::take(&mut *self.0.queue.borrow_mut());
        drop(queue);
    }

    /// Whether a call can run now.
    pub fn has_calls(&self) -> bool {
        matches!(
            self.0.queue.borrow().front(),
            Some(Entry::Ready(_) | Entry::Parked { released: true, .. })
        )
    }

    /// The calls that can run now, in order: up to the first parked one not
    /// released yet.
    #[doc(hidden)]
    pub fn take_calls(&self) -> Vec<JsCall> {
        let mut queue = self.0.queue.borrow_mut();
        let mut out = Vec::new();
        while let Some(entry) = queue.front() {
            if matches!(
                entry,
                Entry::Parked {
                    released: false,
                    ..
                }
            ) {
                break;
            }
            match queue.pop_front() {
                Some(Entry::Ready(call) | Entry::Parked { call, .. }) => out.push(call),
                None => break,
            }
        }
        out
    }
}