Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

feat(feel-lua): implement stable identity for Lua tables to prevent traversal cycles

public

feat(feel-lua): implement stable identity for Lua tables to prevent traversal cycles

Add `identity()` method to `LuaForeign` using `Table::to_pointer()` to provide
stable identity across traversals. Fresh `Arc<LuaForeign>` allocations on each
wrap make Rust address unstable, but the underlying Lua table pointer remains
constant and suitable for visited-set tracking in unbounded descendant
traversals (`e ... name`).

Adds test coverage for:
- List-of-tables collection
- No-match empty results
- Nested table descent
- Cyclic table termination

BREAKING CHANGE: None
Changesetfea1d27abd0f2ff4599b23e786870851fde399ac
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents9628dc752e32
Children
Pushlog
by timo via ssh

modified crates/feel-lua/src/lib.rs +9 −0

@@ -269,6 +269,15 @@
269269
     fn as_any(&self) -> &dyn Any {
270270
         self
271271
     }
272
+
273
+    /// Lua table identity for cycle detection in unbounded traversals (the
274
+    /// descendant `...` operator). feel-lua mints a fresh `Arc<LuaForeign>` per wrap
275
+    /// (`shallow_convert_lua` re-runs on every `force`/`get`), so the Rust allocation
276
+    /// address is *not* stable; the underlying Lua table's pointer (stable across
277
+    /// `Table::clone`, = the table's identity) is. `Table::to_pointer` exposes it.
278
+    fn identity(&self) -> usize {
279
+        self.table.to_pointer() as usize
280
+    }
272281
 }
273282
 
274283
 // --------------------------------------------------------------------------- //

modified crates/feel-lua/tests/feel.rs +54 −0

@@ -324,3 +324,57 @@
324324
         .unwrap();
325325
     assert_eq!((a, b), (2, 42));
326326
 }
327
+
328
+// --------------------------------------------------------------------------- //
329
+// Descendant `e ... name` (DMN 10.3.2.7, grammar rule 68)
330
+// --------------------------------------------------------------------------- //
331
+
332
+#[test]
333
+fn descendant_collects_across_a_list_of_tables() {
334
+    let lua = vm();
335
+    let xs: Vec<i64> = lua
336
+        .load(r#"return feel.evaluate("[{x:1},{x:2}] ... x")"#)
337
+        .eval()
338
+        .unwrap();
339
+    assert_eq!(xs, vec![1, 2]);
340
+}
341
+
342
+#[test]
343
+fn descendant_no_match_is_empty_list() {
344
+    let lua = vm();
345
+    let xs: Vec<i64> = lua
346
+        .load(r#"return feel.evaluate("{a:1} ... z")"#)
347
+        .eval()
348
+        .unwrap();
349
+    assert!(xs.is_empty());
350
+}
351
+
352
+#[test]
353
+fn descendant_descends_into_nested_table() {
354
+    let lua = vm();
355
+    let names: Vec<String> = lua
356
+        .load(
357
+            r#"return feel.evaluate("o ... name", { o = { customer = { name = "Ada" } } })"#,
358
+        )
359
+        .eval()
360
+        .unwrap();
361
+    assert_eq!(names, vec!["Ada".to_string()]);
362
+}
363
+
364
+#[test]
365
+fn descendant_terminates_on_a_cyclic_table() {
366
+    // Without the `Table::to_pointer`-based identity override this would loop
367
+    // forever; with it, the back-edge into the same table is cut after one re-entry.
368
+    let lua = vm();
369
+    let xs: Vec<i64> = lua
370
+        .load(
371
+            r#"
372
+            local c = { value = 42 }
373
+            c.next = c  -- self-referential (cyclic) Lua table
374
+            return feel.evaluate("c ... value", { c = c })
375
+        "#,
376
+        )
377
+        .eval()
378
+        .unwrap();
379
+    assert_eq!(xs, vec![42, 42]);
380
+}