datalogic-rs 5.8.0

High-performance JSONLogic (json-logic) rules engine and sandboxed expression evaluator in Rust — one core, official bindings for Node.js, WASM, Python, Go, Java, .NET, and PHP. Compile once, evaluate in nanoseconds.
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
//! Row adapters: typed rows for fixed-arity operators, the arity gate for
//! `raw[..]` / `iter[..]` rows, and source resolution for `each[..]` rows.
//!
//! A table row such as `eager(Str, Str) string::ends_with` declares the
//! operator's signature as a list of argument extractors. The body is a
//! plain function over the extracted values:
//!
//! ```ignore
//! pub(crate) fn ends_with<'a>(cx: &mut Cx<'_, 'a>, text: &'a str, suffix: &'a str) -> Result<bool>
//! ```
//!
//! and the dispatch arm calls the generic [`call`] adapter below with the
//! extractors as a tuple ([`ExtractList`]) and the function as its
//! [`Body`]. The adapter does the plumbing every such operator used to
//! write by hand (the extractors and result conversions live in
//! [`super::extract`]):
//!
//! 1. **Arity.** The row's `(min, max)` comes from the extractors' spans
//!    ([`Arity::of`]). Too few arguments applies the row's
//!    [`Miss`] policy, too many its [`Extra`] policy, both before anything
//!    is evaluated.
//! 2. **Evaluate, then coerce.** Every present argument is evaluated, in
//!    order, before any is coerced, so a coercion failure on argument 0
//!    never beats an error raised while evaluating argument 1.
//! 3. **Convert the result** through [`IntoValue`].
//!
//! The adapters are generic functions, monomorphised per row, so each
//! row's policy `match` folds against its constant metadata once inlined.

// Which variants, extractors and adapters a build uses depends on which
// operator families it compiles in; with every family on, all of them are
// used and dead code is still reported.
#![cfg_attr(not(feature = "all-operators"), allow(dead_code, unused_imports))]

use bumpalo::Bump;

use super::extract::{Extract, IntoValue};
use super::meta::{Extra, Miss, OpMeta, Singleton};
use crate::arena::{ContextStack, DataValue};
use crate::operators::array::{Items, IterArgKind, ResolvedInput, resolve_iter_input};
use crate::{CompiledNode, Engine, Error, OpCode, Result};

// ---------------------------------------------------------------------------
// The evaluation context an operator body sees
// ---------------------------------------------------------------------------

/// What an operator body works with: the context stack, the engine and
/// the arena, bundled into one value. Built on the stack by the adapter,
/// so with the body inlined it costs nothing.
pub(crate) struct Cx<'c, 'a> {
    pub(crate) ctx: &'c mut ContextStack<'a>,
    pub(crate) engine: &'c Engine,
    pub(crate) arena: &'a Bump,
}

impl<'c, 'a> Cx<'c, 'a> {
    #[inline(always)]
    pub(crate) fn new(ctx: &'c mut ContextStack<'a>, engine: &'c Engine, arena: &'a Bump) -> Self {
        Cx { ctx, engine, arena }
    }

    /// Evaluate a child node.
    #[inline(always)]
    pub(crate) fn eval(&mut self, node: &'a CompiledNode) -> Result<&'a DataValue<'a>> {
        self.engine.dispatch_node(node, self.ctx, self.arena)
    }

    /// Charge `n` operations against the evaluation budget.
    #[inline(always)]
    pub(crate) fn charge(&mut self, n: u64) -> Result<()> {
        self.ctx.charge(n)
    }

    /// Charge the work of scanning `bytes` bytes.
    #[inline(always)]
    pub(crate) fn charge_bytes(&mut self, bytes: usize) -> Result<()> {
        self.ctx.charge_bytes(bytes)
    }

    /// Move a value into the arena.
    #[inline(always)]
    pub(crate) fn alloc<T>(&self, value: T) -> &'a mut T {
        self.arena.alloc(value)
    }

    /// Copy a string into the arena.
    #[inline(always)]
    pub(crate) fn alloc_str(&self, s: &str) -> &'a str {
        self.arena.alloc_str(s)
    }
}

// ---------------------------------------------------------------------------
// Arity
// ---------------------------------------------------------------------------

/// How many arguments an operator reads: at least `min`, at most `max`
/// (`None` = unbounded).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Arity {
    pub min: u8,
    pub max: Option<u8>,
}

impl Arity {
    /// Any number of arguments: a bare `raw` or `iter` row, whose body
    /// takes every count (it may still branch on it).
    pub(crate) const ANY: Arity = Arity { min: 0, max: None };

    /// Exactly `n` (`raw[n]`).
    pub(crate) const fn exactly(n: u8) -> Arity {
        Arity {
            min: n,
            max: Some(n),
        }
    }

    /// `min` or more (`raw[min..]`).
    pub(crate) const fn at_least(min: u8) -> Arity {
        Arity { min, max: None }
    }

    /// `min` to `max` inclusive (`raw[min..=max]`).
    pub(crate) const fn between(min: u8, max: u8) -> Arity {
        assert!(min <= max, "an empty arity range");
        Arity {
            min,
            max: Some(max),
        }
    }

    /// Sum the spans of an `eager` row's extractors. Optional extractors
    /// must come after the required ones, and an unbounded one
    /// ([`super::extract::Rest`]) last, so that a call's argument count
    /// alone decides which positions are present; a row that breaks this
    /// fails to compile.
    pub(crate) const fn of(spans: &[(u8, Option<u8>)]) -> Arity {
        let mut min = 0u8;
        let mut max = Some(0u8);
        let mut optional_seen = false;
        let mut i = 0;
        while i < spans.len() {
            let (lo, hi) = spans[i];
            if lo == 0 {
                optional_seen = true;
            } else if optional_seen {
                panic!("a required extractor follows an optional one");
            }
            min += lo;
            max = match (max, hi) {
                (Some(max), Some(hi)) => Some(max + hi),
                (Some(_), None) if i + 1 == spans.len() => None,
                (Some(_), None) => panic!("an unbounded extractor must be the last one"),
                (None, _) => unreachable!(),
            };
            i += 1;
        }
        Arity { min, max }
    }
}

// ---------------------------------------------------------------------------
// Adapters
// ---------------------------------------------------------------------------

/// Apply the row's arity policy. `Ok(Some(v))` short-circuits with `v`
/// (a [`Miss::Return`] / [`Extra::Return`] row).
#[inline(always)]
fn check_arity(
    len: usize,
    arity: Arity,
    meta: &OpMeta,
) -> Result<Option<&'static DataValue<'static>>> {
    if len < arity.min as usize {
        return match meta.on_missing {
            Miss::InvalidArgs => Err(Error::invalid_args()),
            Miss::Err(msg) => Err(Error::invalid_arguments(msg)),
            Miss::Return(value) => Ok(Some(value())),
        };
    }
    if let Some(max) = arity.max
        && len > max as usize
    {
        return match meta.on_extra {
            Extra::Ignore => Ok(None),
            Extra::InvalidArgs => Err(Error::invalid_args()),
            Extra::Err(msg) => Err(Error::invalid_arguments(msg)),
            Extra::Return(value) => Ok(Some(value())),
        };
    }
    Ok(None)
}

/// An `eager` row's extractors as a tuple, `(Str, Lenient<Int>)`: fetch
/// every argument, in order, then coerce every one, so a coercion failure on
/// argument 0 never beats an error raised while evaluating argument 1.
pub(crate) trait ExtractList<'a> {
    /// Every position's fetched form.
    type Raw;
    /// What the body receives, one element per position.
    type Out;
    fn fetch(args: &'a [CompiledNode], cx: &mut Cx<'_, 'a>) -> Result<Self::Raw>;
    fn coerce(raw: Self::Raw, cx: &mut Cx<'_, 'a>) -> Result<Self::Out>;
}

/// An `eager` row's body for extractors `L`: any
/// `fn(&mut Cx, L0::Out, L1::Out, ...) -> Result<R>`, or one with a bound
/// trailing argument ([`Bound`]).
pub(crate) trait Body<'a, L: ExtractList<'a>, R> {
    fn call(self, cx: &mut Cx<'_, 'a>, args: L::Out) -> Result<R>;
}

/// A body with one bound trailing argument: the row
/// `eager(StrictNum, Rest<StrictNum>) arithmetic::unary_math(UnaryMathOp::Abs)`
/// calls `unary_math(cx, v0, v1, UnaryMathOp::Abs)`. A struct rather than a
/// closure, so nothing depends on LLVM inlining one.
pub(crate) struct Bound<F, K>(pub(crate) F, pub(crate) K);

/// Implement [`ExtractList`] and [`Body`] for one tuple arity.
macro_rules! arity_impls {
    ($($A:ident $raw:ident $val:ident $i:tt),*) => {
        impl<'a, $($A: Extract<'a>),*> ExtractList<'a> for ($($A,)*) {
            type Raw = ($($A::Raw,)*);
            type Out = ($($A::Out,)*);
            #[inline(always)]
            #[allow(unused_variables)]
            fn fetch(args: &'a [CompiledNode], cx: &mut Cx<'_, 'a>) -> Result<Self::Raw> {
                Ok(($($A::fetch(args, $i, cx)?,)*))
            }
            #[inline(always)]
            #[allow(unused_variables)]
            fn coerce(raw: Self::Raw, cx: &mut Cx<'_, 'a>) -> Result<Self::Out> {
                let ($($raw,)*) = raw;
                Ok(($($A::coerce($raw, cx)?,)*))
            }
        }

        impl<'a, F, R, $($A: Extract<'a>),*> Body<'a, ($($A,)*), R> for F
        where
            F: for<'c> FnOnce(&mut Cx<'c, 'a>, $($A::Out),*) -> Result<R>,
        {
            #[inline(always)]
            fn call(self, cx: &mut Cx<'_, 'a>, args: <($($A,)*) as ExtractList<'a>>::Out) -> Result<R> {
                let ($($val,)*) = args;
                self(cx, $($val),*)
            }
        }

        impl<'a, F, K, R, $($A: Extract<'a>),*> Body<'a, ($($A,)*), R> for Bound<F, K>
        where
            F: for<'c> FnOnce(&mut Cx<'c, 'a>, $($A::Out,)* K) -> Result<R>,
        {
            #[inline(always)]
            fn call(self, cx: &mut Cx<'_, 'a>, args: <($($A,)*) as ExtractList<'a>>::Out) -> Result<R> {
                let ($($val,)*) = args;
                (self.0)(cx, $($val,)* self.1)
            }
        }
    };
}

arity_impls!();
arity_impls!(A0 r0 v0 0);
arity_impls!(A0 r0 v0 0, A1 r1 v1 1);
arity_impls!(A0 r0 v0 0, A1 r1 v1 1, A2 r2 v2 2);
arity_impls!(A0 r0 v0 0, A1 r1 v1 1, A2 r2 v2 2, A3 r3 v3 3);
arity_impls!(A0 r0 v0 0, A1 r1 v1 1, A2 r2 v2 2, A3 r3 v3 3, A4 r4 v4 4);
arity_impls!(A0 r0 v0 0, A1 r1 v1 1, A2 r2 v2 2, A3 r3 v3 3, A4 r4 v4 4, A5 r5 v5 5);

/// The adapter for an `eager(..)` row: apply the row's arity policy,
/// fetch and coerce its arguments through `L`, run the body, and convert
/// the result.
///
/// `OP` is the row's opcode discriminant. The row's metadata and arity are
/// read through it in `const` blocks, so each adapter's policy checks are
/// compile-time constants whether or not the adapter is inlined.
#[inline]
pub(crate) fn call<'a, const OP: u8, L, R, B>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
    body: B,
) -> Result<&'a DataValue<'a>>
where
    L: ExtractList<'a>,
    R: IntoValue<'a>,
    B: Body<'a, L, R>,
{
    let meta: &'static OpMeta = const { OpCode::ALL[OP as usize].meta() };
    let arity: Arity = const { OpCode::ALL[OP as usize].arity() };
    if let Some(early) = check_arity(args.len(), arity, meta)? {
        return Ok(early);
    }
    let mut cx = Cx::new(ctx, engine, arena);
    let raw = L::fetch(args, &mut cx)?;
    let values = L::coerce(raw, &mut cx)?;
    let out = body.call(&mut cx, values)?;
    Ok(out.into_value(&mut cx))
}

/// The adapter for a `raw[..]` row: apply the row's declared arity and
/// policy, then run the body, which evaluates its own arguments and may
/// index any position below the declared minimum.
#[inline]
pub(crate) fn raw<'a, const OP: u8, F>(
    args: &'a [CompiledNode],
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
    body: F,
) -> Result<&'a DataValue<'a>>
where
    F: for<'c> FnOnce(
        &'a [CompiledNode],
        &'c mut ContextStack<'a>,
        &'c Engine,
        &'a Bump,
    ) -> Result<&'a DataValue<'a>>,
{
    let meta: &'static OpMeta = const { OpCode::ALL[OP as usize].meta() };
    let arity: Arity = const { OpCode::ALL[OP as usize].arity() };
    if let Some(early) = check_arity(args.len(), arity, meta)? {
        return Ok(early);
    }
    body(args, ctx, engine, arena)
}

/// The adapter for an `iter[..]` row: [`raw`], passing the iteration-source
/// classification through.
#[inline]
pub(crate) fn iter<'a, const OP: u8, F>(
    args: &'a [CompiledNode],
    iter_arg_kind: IterArgKind,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
    body: F,
) -> Result<&'a DataValue<'a>>
where
    F: for<'c> FnOnce(
        &'a [CompiledNode],
        IterArgKind,
        &'c mut ContextStack<'a>,
        &'c Engine,
        &'a Bump,
    ) -> Result<&'a DataValue<'a>>,
{
    let meta: &'static OpMeta = const { OpCode::ALL[OP as usize].meta() };
    let arity: Arity = const { OpCode::ALL[OP as usize].arity() };
    if let Some(early) = check_arity(args.len(), arity, meta)? {
        return Ok(early);
    }
    body(args, iter_arg_kind, ctx, engine, arena)
}

/// The adapter for an `each[..]` row: apply the declared arity, resolve
/// `args[0]` as the iteration source, answer a null, missing or empty-array
/// source with the row's `on_empty_source`, and hand the body the rest.
///
/// The source is resolved (and its items charged) before the body runs,
/// exactly as the iterator bodies did themselves.
#[inline]
pub(crate) fn each<'a, const OP: u8, F>(
    args: &'a [CompiledNode],
    iter_arg_kind: IterArgKind,
    ctx: &mut ContextStack<'a>,
    engine: &Engine,
    arena: &'a Bump,
    body: F,
) -> Result<&'a DataValue<'a>>
where
    F: for<'c> FnOnce(
        Items<'a>,
        &'a [CompiledNode],
        &'c mut ContextStack<'a>,
        &'c Engine,
        &'a Bump,
    ) -> Result<&'a DataValue<'a>>,
{
    let meta: &'static OpMeta = const { OpCode::ALL[OP as usize].meta() };
    let arity: Arity = const {
        let arity = OpCode::ALL[OP as usize].arity();
        assert!(arity.min >= 1, "an `each` row reads at least its source");
        arity
    };
    let on_empty: Singleton = const {
        match OpCode::ALL[OP as usize].meta().on_empty_source {
            Some(value) => value,
            None => panic!("an `each` row must declare `on_empty_source`"),
        }
    };
    if let Some(early) = check_arity(args.len(), arity, meta)? {
        return Ok(early);
    }
    let items = match resolve_iter_input(&args[0], iter_arg_kind, ctx, engine, arena)? {
        ResolvedInput::Iterable(src) if !src.is_empty() => Items::Array(src),
        ResolvedInput::Iterable(_) | ResolvedInput::Empty => return Ok(on_empty()),
        ResolvedInput::Bridge(DataValue::Object(pairs)) => Items::Object(pairs),
        ResolvedInput::Bridge(value) => Items::Scalar(value),
    };
    body(items, args, ctx, engine, arena)
}