byteflow-actors 0.9.18

Embeddable flow runtime: M:N scheduler, Atomic Hop (Message+Cap), supervisor — use byteflow::
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
//! Native (FFI) table for `Opcode::CallNative`.
//!
//! # The one rule: never block
//!
//! `CallNative` runs **inline** on the worker thread. A native that blocks
//! stalls every other Flow on that worker. Slow I/O belongs in a dedicated
//! Flow (`Send`/`Receive`), not here.

use std::sync::Arc;

use crate::bytecode::{Cap, CapRights, NativeIdx, NativeMask, RevocationCell, Value};

use super::fault::Fault;

pub use super::fault::NativeCallError;

/// Result type for a native function.
pub type NativeResult = Result<Value, Fault>;

/// A host-side function callable from bytecode via `Opcode::CallNative`.
pub type NativeFn = Arc<dyn Fn(&[Value]) -> NativeResult + Send + Sync>;

/// Immutable, indexable set of natives. Gaps left by [`NativeTableBuilder::register_at`]
/// are `None` — calling them faults with [`Fault::BadNative`].
pub struct NativeTable {
    entries: Vec<Option<(String, NativeFn)>>,
}

impl NativeTable {
    pub fn builder() -> NativeTableBuilder {
        NativeTableBuilder {
            entries: Vec::new(),
        }
    }

    /// Empty table — valid for chunks that never emit `CallNative`.
    pub fn empty() -> Arc<NativeTable> {
        Arc::new(NativeTable {
            entries: Vec::new(),
        })
    }

    #[inline]
    pub fn get(&self, index: u32) -> Option<&NativeFn> {
        self.entries
            .get(index as usize)
            .and_then(|slot| slot.as_ref())
            .map(|(_, f)| f)
    }

    pub fn index_of(&self, name: &str) -> Option<u32> {
        self.entries
            .iter()
            .enumerate()
            .find(|(_, slot)| slot.as_ref().is_some_and(|(n, _)| n == name))
            .map(|(i, _)| i as u32)
    }

    /// Slot count including reserved-but-empty gaps (one past the highest
    /// touched index), not the count of registered functions.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.entries
            .iter()
            .filter_map(|slot| slot.as_ref().map(|(n, _)| n.as_str()))
    }
}

/// Highest index [`NativeTableBuilder::register_at`] will allocate.
/// A `u32` index would otherwise resize the slot `Vec` to `index + 1`.
pub const MAX_NATIVE_SLOTS: usize = 4096;

/// Host error while building a [`NativeTable`] (duplicate name, occupied slot,
/// or an index past [`MAX_NATIVE_SLOTS`]).
///
/// Category A: the embedder misconfigured FFI. Never a panic — `std_native_table`
/// and host tables must surface this as `Result`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NativeTableError {
    DuplicateName(String),
    SlotOccupied {
        index: u32,
        name: String,
    },
    /// `register_at` asked for an index at or past [`MAX_NATIVE_SLOTS`].
    IndexTooLarge {
        index: u32,
        limit: u32,
    },
}

impl std::fmt::Display for NativeTableError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NativeTableError::DuplicateName(name) => {
                write!(f, "duplicate native function registered: '{name}'")
            }
            NativeTableError::SlotOccupied { index, name } => {
                write!(
                    f,
                    "native slot {index} already occupied (registering '{name}')"
                )
            }
            NativeTableError::IndexTooLarge { index, limit } => {
                write!(f, "native slot {index} is past the limit of {limit}")
            }
        }
    }
}

impl std::error::Error for NativeTableError {}

/// Fluent builder for a [`NativeTable`].
///
/// - [`Self::register`] — next sequential slot (host builds table + chunk together).
/// - [`Self::register_at`] — fixed ABI slot (MCU / separately flashed bytecode).
pub struct NativeTableBuilder {
    entries: Vec<Option<(String, NativeFn)>>,
}

impl NativeTableBuilder {
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }

    /// Register `f` under `name` at the next sequential slot.
    pub fn register<F>(self, name: impl Into<String>, f: F) -> Result<Self, NativeTableError>
    where
        F: Fn(&[Value]) -> NativeResult + Send + Sync + 'static,
    {
        let index = self.entries.len() as u32;
        self.register_at(index, name, f)
    }

    /// Register `f` under `name` at explicit `index`, padding lower gaps as
    /// unregistered (`get` → `None` → [`Fault::BadNative`]).
    pub fn register_at<F>(
        mut self,
        index: u32,
        name: impl Into<String>,
        f: F,
    ) -> Result<Self, NativeTableError>
    where
        F: Fn(&[Value]) -> NativeResult + Send + Sync + 'static,
    {
        let name = name.into();
        for (n, _) in self.entries.iter().flatten() {
            if n == &name {
                return Err(NativeTableError::DuplicateName(name));
            }
        }
        let index_usize = index as usize;
        if index_usize >= MAX_NATIVE_SLOTS {
            return Err(NativeTableError::IndexTooLarge {
                index,
                limit: MAX_NATIVE_SLOTS as u32,
            });
        }
        if index_usize >= self.entries.len() {
            self.entries.resize_with(index_usize + 1, || None);
        }
        if self.entries[index_usize].is_some() {
            return Err(NativeTableError::SlotOccupied { index, name });
        }
        self.entries[index_usize] = Some((name, Arc::new(f)));
        Ok(self)
    }

    pub fn build(self) -> Arc<NativeTable> {
        Arc::new(NativeTable {
            entries: self.entries,
        })
    }
}

impl Default for NativeTableBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Require `args[index]` to exist.
pub fn expect_arg<'a>(args: &'a [Value], index: usize, fn_name: &str) -> Result<&'a Value, Fault> {
    args.get(index).ok_or(Fault::NativeError(format!(
        "{fn_name}: missing argument {index}"
    )))
}

/// Require `args[index]` to coerce to an int (`Value::as_int`).
pub fn expect_int(args: &[Value], index: usize, fn_name: &str) -> Result<i64, Fault> {
    expect_arg(args, index, fn_name)?
        .as_int()
        .ok_or(Fault::NativeError(format!(
            "{fn_name}: argument {index} is not an int"
        )))
}

/// Require `args[index]` to be a bool (ints: nonzero = true).
pub fn expect_bool(args: &[Value], index: usize, fn_name: &str) -> Result<bool, Fault> {
    let v = expect_arg(args, index, fn_name)?;
    match v {
        Value::Bool(_) | Value::Int(_) => Ok(v.is_truthy()),
        other => Err(Fault::NativeError(format!(
            "{fn_name}: argument {index} is not a bool/int (got {})",
            other.type_name()
        ))),
    }
}

/// Require `args[index]` to be a [`crate::Message`].
///
/// Used by the std `msg_*` natives. A wrong type becomes
/// [`Fault::NativeError`] (category B — Flow fault), not a host panic.
pub fn expect_message(
    args: &[Value],
    index: usize,
    fn_name: &str,
) -> Result<crate::Message, Fault> {
    expect_arg(args, index, fn_name)?
        .as_message()
        .cloned()
        .ok_or(Fault::NativeError(format!(
            "{fn_name}: argument {index} is not a message"
        )))
}

/// Coerce `args[index]` to `u64` from `Int` (≥ 0) or `Pid`.
///
/// Used by `make_msg` for unsigned envelope fields (`request_id`, `tag`).
/// `Bool` is not an integer here. Negative ints are rejected — those fields
/// are unsigned on the wire.
pub fn expect_u64(args: &[Value], index: usize, fn_name: &str) -> Result<u64, Fault> {
    let v = expect_arg(args, index, fn_name)?;
    if let Some(p) = v.as_pid() {
        return Ok(p);
    }
    if let Value::Int(i) = v {
        if *i >= 0 {
            return Ok(*i as u64);
        }
    }
    Err(Fault::NativeError(format!(
        "{fn_name}: argument {index} is not a non-negative int/pid (got {})",
        v.type_name()
    )))
}

/// Per-flow native allowlist snapshot, checked before indexing the table.
#[derive(Clone, Debug)]
pub struct NativeGate {
    has_native: bool,
    mask: NativeMask,
    authority_epoch: u64,
    flow_cell: Arc<RevocationCell>,
    native_epoch: u64,
    native_cell: Arc<RevocationCell>,
}

impl NativeGate {
    /// No native right — unit tests and chunks that never call out.
    pub fn deny(native_count: usize) -> Self {
        Self {
            has_native: false,
            mask: NativeMask::empty(native_count),
            authority_epoch: 0,
            flow_cell: Arc::new(RevocationCell::new()),
            native_epoch: 0,
            native_cell: Arc::new(RevocationCell::new()),
        }
    }

    pub fn from_authority(
        cap: &Cap,
        flow_cell: Arc<RevocationCell>,
        native_cell: Arc<RevocationCell>,
        native_count: usize,
    ) -> Self {
        let mask = match cap.native_mask() {
            Some(m) => m.clone(),
            None => NativeMask::empty(native_count),
        };
        Self {
            has_native: cap.rights().contains(CapRights::NATIVE),
            mask,
            authority_epoch: cap.epoch(),
            flow_cell,
            native_epoch: native_cell.epoch(),
            native_cell,
        }
    }

    pub fn is_live(&self) -> bool {
        self.flow_cell.epoch() == self.authority_epoch
            && self.native_cell.epoch() == self.native_epoch
    }
}

/// Host-side check against a [`Cap`] (not the interpreter path).
///
/// The VM uses [`check_native_gate`] on the per-flow snapshot. This helper
/// is for embedders that hold a live `Cap` and want the same verdict.
pub fn check_native_call(
    cap: &Cap,
    table: &NativeTable,
    idx: NativeIdx,
) -> Result<(), NativeCallError> {
    if (idx as usize) >= table.len() {
        return Err(NativeCallError::IndexOutOfRange(idx));
    }
    if !cap.rights().contains(CapRights::NATIVE) {
        return Err(NativeCallError::NoNativeRight);
    }
    match cap.native_mask() {
        Some(mask) if mask.allows(idx) => Ok(()),
        _ => Err(NativeCallError::IndexNotAllowlisted(idx)),
    }
}

pub fn check_native_gate(
    gate: &NativeGate,
    table: &NativeTable,
    idx: NativeIdx,
) -> Result<(), NativeCallError> {
    if (idx as usize) >= table.len() {
        return Err(NativeCallError::IndexOutOfRange(idx));
    }
    if !gate.is_live() {
        return Err(NativeCallError::Revoked);
    }
    if !gate.has_native {
        return Err(NativeCallError::NoNativeRight);
    }
    if gate.mask.allows(idx) {
        Ok(())
    } else {
        Err(NativeCallError::IndexNotAllowlisted(idx))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn expect_u64_rejects_bool_and_keeps_non_negative_ints() {
        let flag = [Value::Bool(true)];
        assert!(expect_u64(&flag, 0, "make_msg").is_err());
        let negative = [Value::Int(-1)];
        assert!(expect_u64(&negative, 0, "make_msg").is_err());
        let zero = [Value::Int(0)];
        assert_eq!(expect_u64(&zero, 0, "make_msg").ok(), Some(0));
        let pid = [Value::Pid(7)];
        assert_eq!(expect_u64(&pid, 0, "make_msg").ok(), Some(7));
    }

    #[test]
    fn register_at_leaves_holes_as_none() -> Result<(), Box<dyn std::error::Error>> {
        let table = NativeTable::builder()
            .register_at(10, "answer", |_| Ok(Value::Int(42)))?
            .build();
        assert_eq!(table.len(), 11);
        assert_eq!(table.index_of("answer"), Some(10));
        let f = table.get(10).ok_or("missing native")?;
        assert!(matches!(f(&[])?, Value::Int(42)));
        assert!(table.get(2).is_none());
        Ok(())
    }

    #[test]
    fn register_at_rejects_an_index_past_the_slot_ceiling() {
        let result = NativeTable::builder().register_at(u32::MAX, "huge", |_| Ok(Value::Unit));
        assert_eq!(
            result.map(|builder| builder.build().len()),
            Err(NativeTableError::IndexTooLarge {
                index: u32::MAX,
                limit: MAX_NATIVE_SLOTS as u32,
            })
        );
    }

    #[test]
    fn register_at_errors_on_duplicate_slot() {
        let result = NativeTable::builder()
            .register_at(3, "a", |_| Ok(Value::Unit))
            .and_then(|b| b.register_at(3, "b", |_| Ok(Value::Unit)));
        assert!(matches!(
            result,
            Err(NativeTableError::SlotOccupied { index: 3, .. })
        ));
    }

    #[test]
    fn register_errors_on_duplicate_name() {
        let result = NativeTable::builder()
            .register("x", |_| Ok(Value::Unit))
            .and_then(|b| b.register("x", |_| Ok(Value::Unit)));
        assert!(matches!(result, Err(NativeTableError::DuplicateName(_))));
    }

    #[test]
    fn denies_unlisted_index_even_with_native_right() {
        use crate::bytecode::{CapTarget, RevocationCell};

        let cell = RevocationCell::new();
        let mask = NativeMask::from_indices(16, &[2, 4]);
        let cap = Cap::root(CapTarget::Flow(1), CapRights::NATIVE, Some(mask), &cell);
        let table = NativeTable {
            entries: Vec::new(),
        };
        assert!(matches!(
            check_native_call(&cap, &table, 4),
            Err(NativeCallError::IndexOutOfRange(_))
        ));
    }
}