Public repository: timo / feel

Reader

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

docs: record ADR-22 as built, flip RD-7/8/9, document Lua bindings

public

docs: record ADR-22 as built, flip RD-7/8/9, document Lua bindings

- ADR-22: from planned to built — mlua 0.11, Lua 5.4 vendored default with
  luajit/luau/module features, the reconsidered library choice (rlua/hlua/piccolo),
  the loadable-module framing, and the value-null vs error-null distinction.
- RD-7/8/9: planned -> implemented (RustRegex, ISO-8601 temporal, lazy LuaForeign,
  with the documented list-vs-context force heuristic).
- "Adding a wrapper" matrix: feel-lua column is now realised.
- README: four targets, crate list, toolchain, and a "lua — module (mlua)" section.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset0b52cfd6f512a5e4832b8c3182d759c53df8a184
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents36da7b3f6c8d
Children
Pushlog
by timo via ssh

modified DECISIONS.md +68 −37

@@ -619,34 +619,56 @@
619619
   all qualify (an older, lifetime-parameterised mlua would not). This is what makes the
620620
   planned feel-lua (ADR-22) a clean fit rather than a fight with the borrow checker.
621621
 
622
-## ADR-22 (planned): Lua bindings (`feel-lua`, mlua)
622
+## ADR-22: Lua bindings (`feel-lua`, mlua)
623623
 
624624
 - **Context:** feel-core already powers three wrappers (feel-pg, feel-wasm, feel-py).
625625
   Lua is a common embedded-scripting and rules host (game logic, Redis, nginx/
626626
   OpenResty, Neovim, application plugins), so a fifth target is a natural next step.
627
-  This entry records the decision and its constraints **before** the crate is built —
628
-  in the spirit of ADR-5 — so the next increment starts from a decision, not a blank
629
-  page. *Not yet built.*
630
-- **Decision (planned):** a new `crates/feel-lua`, a wrapper over the same core
631
-  (ADR-1/I1 holds — `mlua` lives only here), built with **mlua ≥ 0.10**. The 0.10 line
632
-  dropped the `'lua` lifetime parameter, so `Table`/`Value`/`Function` are owned and
633
-  `'static` and fit the `Arc<dyn ForeignObject + 'static>` seam (ADR-21) directly; an
634
-  older lifetime-parameterised mlua would not. Like feel-py (PyO3) and feel-pg (pgrx),
635
-  feel-lua needs a Lua *link context*, so it goes in the workspace `members` but
636
-  **not** `default-members` (a bare `cargo build` keeps building only the portable
637
-  members). The three boundary sub-decisions are recorded ahead as **RD-7** (regex),
638
-  **RD-8** (temporal) and **RD-9** (context mapping); the seven reusable seams are the
639
-  "Adding a wrapper" contract below.
640
-- **Hazard to plan for:** Lua signals errors by `longjmp` (like PostgreSQL — the RD-1
641
-  hazard), but mlua already wraps every C-API call and surfaces them as
642
-  `Result<_, mlua::Error>`. So `LuaForeign::get`/`force` map an `Err` (a raised
643
-  `__index` metamethod, a bad access) to a value-null — the same defensive pattern
644
-  feel-py uses around `getattr` (ADR-21). The longjmp never unwinds through Rust
645
-  frames; mlua catches it at the boundary.
646
-- **Consequence (anticipated):** a fifth target on the same core. `Value` stays
647
-  `!Send` (ADR-21), which *matches* mlua's non-`Send` values rather than fighting them.
648
-  I2 (ADR-1) relaxes here exactly as it does elsewhere — no Lua regex (RD-7), no native
649
-  Lua temporal (RD-8) — both loud and documented, neither a surprise.
627
+  This entry was recorded **before** the crate was built (in the spirit of ADR-5) and
628
+  is now realised; the three boundary sub-decisions stand as **RD-7** (regex),
629
+  **RD-8** (temporal) and **RD-9** (context mapping), and the seven reusable seams are
630
+  the "Adding a wrapper" contract below. *Built 2026-06-17.*
631
+- **Library (reconsidered, not just inherited):** the candidates were re-checked rather
632
+  than taking the original note at face value. **rlua** is deprecated (v0.20 is a thin
633
+  re-export of mlua) — a dead end; **hlua** is effectively unmaintained; **piccolo** (a
634
+  pure-Rust stackless VM) is attractive (no C, sandboxing-focused) but pre-1.0 with an
635
+  incomplete stdlib, *and* its values are `gc-arena`-bound with a `'gc` lifetime — the
636
+  opposite of what the `Arc<dyn ForeignObject + 'static>` seam needs, reintroducing
637
+  exactly the lifetime fight an old lifetime-parameterised mlua would. **mlua** (0.11.6)
638
+  is actively maintained, covers Lua 5.1–5.5 + LuaJIT + Luau, has first-class loadable-
639
+  module support, and — since 0.10 — owned `'static` `Table`/`Function`/`Value`. So mlua
640
+  wins on the merits, not by default.
641
+- **Decision:** a new `crates/feel-lua`, a wrapper over the same core (ADR-1/I1 holds —
642
+  `mlua` lives only here), built with **mlua 0.11**. It ships as a **Lua-loadable C
643
+  module** (`#[mlua::lua_module] fn feel → luaopen_feel`, exposing
644
+  `feel.evaluate`/`evaluate_strict`/`compile`) — the analogue of feel-py's Python module
645
+  and feel-wasm's JS module. The module-building logic is factored into a plain
646
+  `build_module(&Lua)` so the Rust integration tests drive it against an embedded VM.
647
+  Like feel-py and feel-pg, feel-lua needs a Lua *link context*, so it sits in the
648
+  workspace `members` but **not** `default-members` (a bare `cargo build` keeps building
649
+  only the portable members). Cargo features select the Lua flavour/mode: `default =
650
+  ["lua54", "vendored"]` (the test/embed build, where `Lua::new()` works), with
651
+  `luajit`/`luau` and a `module` feature (link the host's Lua, build the loadable `.so`:
652
+  `cargo build -p feel-lua --no-default-features --features lua54,module`). `vendored`
653
+  and `module` are practically exclusive — one embeds Lua, the other expects the host's.
654
+- **Hazard handled:** Lua signals errors by `longjmp` (like PostgreSQL — the RD-1
655
+  hazard), but mlua wraps every C-API call and surfaces them as `Result<_, mlua::Error>`,
656
+  so the longjmp never unwinds through Rust frames. Two distinct mappings: a failed
657
+  **member access** (`LuaForeign::get`/`force`: a raised `__index`, a bad access) is a
658
+  **value-null** (the defensive feel-py `getattr` pattern, ADR-21); a failed **host call**
659
+  (`LuaForeignFn::call`: a raised Lua error) is an **error-null** (`Value::Error`,
660
+  ADR-14/ADR-23), distinct from a callable that simply `return`s `nil` (value-null).
661
+- **Named arguments:** Lua has no kwargs, so — exactly like feel-wasm's options object —
662
+  FEEL named arguments become a single trailing table `{name = value}`. A returned host
663
+  callable/table passes back **by identity** (`as_any` downcast). Argument marshalling
664
+  uses mlua's `IntoLua` so the conversion runs with the `&Lua` that `Function::call`
665
+  already holds (`ForeignFunction::call` is handed no state).
666
+- **Consequence:** a fifth target on the same core, verified two ways (per the
667
+  user-chosen "both"): 21 Rust integration tests embedding a vendored VM, and a
668
+  `smoke.lua` run end-to-end through the loadable module under the system `lua5.4`.
669
+  `Value` stays `!Send` (ADR-21), which *matches* mlua's non-`Send` values rather than
670
+  fighting them. I2 (ADR-1) relaxes here exactly as elsewhere — no Lua regex (RD-7), no
671
+  native Lua temporal (RD-8) — both loud and documented, neither a surprise.
650672
 
651673
 ## ADR-23: User-defined functions — FEEL closures and host callables
652674
 
@@ -734,7 +756,7 @@
734756
 
735757
 Per-wrapper choices at a glance (the detail, with revisit triggers, is in RD-1…9):
736758
 
737
-| Seam | feel-pg | feel-wasm | feel-py | **feel-lua (planned)** |
759
+| Seam | feel-pg | feel-wasm | feel-py | **feel-lua** |
738760
 | --- | --- | --- | --- | --- |
739761
 | Input mapping | eager `jsonb` | lazy `JsForeign` | lazy `PyForeign` | lazy `LuaForeign` (RD-9) |
740762
 | Regex dialect | `RustRegex` (RD-1) | `JsRegex` (ADR-16) | `PyRegex`/`re` (RD-4) | `RustRegex` (RD-7) |
@@ -900,9 +922,11 @@
900922
   off the table; the realistic options are (a) keep the bundled `RustRegex` (parity with
901923
   feel-core/TCK, like feel-pg RD-1) or (b) bind a C regex library (`lrexlib`/PCRE) — real
902924
   FFI weight for a marginal gain.
903
-- **Decision (planned):** feel-lua keeps the default `RustRegex` provider — same dialect
904
-  as feel-core and the TCK fixtures (the validated reference), and no native dependency.
905
-  The `evaluate_with_env` seam leaves the door open.
925
+- **Decision (2026-06-17, implemented):** feel-lua keeps the default `RustRegex` provider
926
+  — same dialect as feel-core and the TCK fixtures (the validated reference), and no native
927
+  dependency. Because the feel-core dependency keeps its default features, no regex feature
928
+  flag is needed here (simpler than feel-wasm/feel-py). The `evaluate_with_env` seam leaves
929
+  the door open.
906930
 - **Revisit trigger:** a concrete need for FEEL regex in Lua to match a host PCRE binding
907931
   users already author against.
908932
 - **If flipped:** implement `struct LuaRegex` over `lrexlib`/PCRE as a
@@ -916,10 +940,12 @@
916940
   `{year=, month=, …}` tables). Options for the five FEEL temporal types: ISO-8601 strings
917941
   (as feel-wasm leaves durations), `os.date`-style tables, or userdata from a Lua datetime
918942
   library.
919
-- **Decision (planned):** map every temporal type to/from a **canonical ISO-8601 string** —
920
-  the portable boundary ADR-18 already produces, with zero extra dependency and no lossy
921
-  conversion. A convenience `os.date`-style table form (`{year=, month=, day=, …}`) on input
922
-  is a possible later addition, not the default.
943
+- **Decision (2026-06-17, implemented):** map every temporal **result** to a **canonical
944
+  ISO-8601 string** (via `temporal::to_iso`) — the portable boundary ADR-18 already
945
+  produces, with zero extra dependency and no lossy conversion. On input there is no native
946
+  Lua temporal type, so a date/time is just a string the FEEL `date()`/`time()`/`duration()`
947
+  builtins parse. A convenience `os.date`-style table form (`{year=, month=, day=, …}`) on
948
+  input is a possible later addition, not the default.
923949
 - **Revisit trigger:** users want first-class Lua date objects (integration with a specific
924950
   Lua datetime lib) rather than ISO strings.
925951
 - **If flipped:** add a table/userdata mapping in the wrapper boundary (`to_iso`/
@@ -931,11 +957,16 @@
931957
   deep table resolves one dot at a time. A Lua table can carry an `__index` metamethod (a
932958
   computed/lazy field) exactly like a Python `@property`, so the lazy seam fits; an eager
933959
   alternative would snapshot and miss metamethod semantics.
934
-- **Decision (planned):** wrap an `mlua::Table` (or userdata) in a `LuaForeign` implementing
935
-  `ForeignObject`: `get` via `Table::get` (which fires `__index`, the on-demand load), an
936
-  `Err` → value-null; `force` enumerates the table's own pairs one level; `as_any` recovers
937
-  the handle for identity pass-through. Scalars and temporals (RD-8) stay eager. Mirrors
938
-  feel-py's `PyForeign` and feel-wasm's `JsForeign`.
960
+- **Decision (2026-06-17, implemented):** wrap an `mlua::Table` in a `LuaForeign`
961
+  implementing `ForeignObject`: `get` via `Table::get` (which fires `__index`, the
962
+  on-demand load), an `Err` → value-null; `force` materialises one level — a sequence
963
+  (`#t >= 1`) becomes a `Value::List` of `t[1..=#t]`, any other table a `Value::Context` of
964
+  its string-keyed pairs; `as_any` recovers the handle for identity pass-through. Scalars
965
+  and temporals (RD-8) stay eager. Mirrors feel-py's `PyForeign` and feel-wasm's
966
+  `JsForeign`. **Documented simplification:** a Lua table is ambiguous (it is both array
967
+  and map), so the sequence/map split is a heuristic — a *mixed* table's hash part is
968
+  dropped when a sequence border exists, non-string map keys are dropped, and an empty
969
+  table forces to an empty context (not an empty list).
939970
 - **Revisit trigger:** a need to forbid metamethod side effects entirely during evaluation
940971
   (a hard "no `__index` runs" guarantee).
941972
 - **If flipped:** offer an opt-in eager snapshot (force the whole subtree at the boundary)

modified README.md +63 −10

@@ -1,17 +1,18 @@
1
-# rust-s-feel — a dual-target (S-)FEEL engine slice
1
+# rust-s-feel — a multi-target (S-)FEEL engine slice
22
 
33
 A deliberately thin vertical slice of an (S-)FEEL expression engine in Rust. The
44
 point is **not** FEEL completeness — it is to prove a multi-target architecture:
5
-one pure core crate compiled into **three** targets (PostgreSQL via pgrx, WASM via
6
-wasm-bindgen, Python via PyO3) that produce the **same result** for the same
7
-expression, plus a closed ingestion loop from the official DMN-TCK test suite into
8
-core unit tests.
5
+one pure core crate compiled into **four** targets (PostgreSQL via pgrx, WASM via
6
+wasm-bindgen, Python via PyO3, Lua via mlua) that produce the **same result** for
7
+the same expression, plus a closed ingestion loop from the official DMN-TCK test
8
+suite into core unit tests.
99
 
1010
 ```text
1111
 crates/feel-core   pure engine (lalrpop grammar + AST + evaluator); no target deps
1212
 crates/feel-pg     pgrx wrapper:  feel_eval(text, jsonb) RETURNS jsonb
1313
 crates/feel-wasm   wasm-bindgen wrapper:  evaluate(expr, context) -> JS value
1414
 crates/feel-py     PyO3 wrapper:  evaluate(expr, context) -> Python value
15
+crates/feel-lua    mlua wrapper:  feel.evaluate(expr, context) -> Lua value
1516
 tools/ingest_tck.py  DMN-TCK literal-expression -> feel-core fixture (with deviations)
1617
 ```
1718
 
@@ -32,9 +33,10 @@
3233
   ADR-15/ADR-17), callable positionally or with named arguments
3334
   (`substring(string: "foobar", start position: 3)`; ADR-23).
3435
 - **User-defined functions:** FEEL `function(a, b) a + b` closures, and
35
-  host-provided functions — a Python callable or JS function passed in the context
36
-  (or an ambient `functions` registry) and invoked from the expression, with
37
-  positional / named / mixed arguments (Python kwargs, JS options object; ADR-23).
36
+  host-provided functions — a Python callable, JS function or Lua function passed in
37
+  the context (or an ambient `functions` registry) and invoked from the expression,
38
+  with positional / named / mixed arguments (Python kwargs, JS/Lua options object;
39
+  ADR-23).
3840
 - **Temporal types:** `date`, `time`, `date and time`, and the two durations,
3941
   with `@"…"` literals, arithmetic / comparison / component access (`.year`,
4042
   `.time offset`, …), the temporal constructor and component built-ins
@@ -56,8 +58,7 @@
5658
 
5759
 ## Architecture invariant (Cardinal Rule)
5860
 
59
-`feel-core` must never depend on pgrx, wasm-bindgen or pyo3 (nor, once the planned
60
-Lua wrapper lands, mlua). Verify:
61
+`feel-core` must never depend on pgrx, wasm-bindgen, pyo3 or mlua. Verify:
6162
 
6263
 ```sh
6364
 cargo tree -p feel-core --edges normal,build | grep -E 'pgrx|wasm-bindgen|pyo3|mlua' && echo VIOLATION || echo OK
@@ -73,6 +74,9 @@
7374
   (PyO3 builds the extension module; one abi3 wheel covers all CPython ≥ 3.9).
7475
 - For the PostgreSQL target: PostgreSQL 13–18 with `pg_config`, plus `libclang`
7576
   (pgrx uses bindgen), and `cargo-pgrx`.
77
+- For the Lua target: nothing extra to run the Rust tests (Lua 5.4 is compiled in via
78
+  mlua's `vendored` feature). To build the loadable `.so` and run `smoke.lua`, a Lua
79
+  5.4 interpreter + dev headers (`lua5.4`, `pkg-config`).
7680
 
7781
 ## Build everything
7882
 
@@ -280,6 +284,55 @@
280284
 maturin develop --features xpath-regex
281285
 ```
282286
 
287
+## lua — module (mlua)
288
+
289
+Self-contained Rust integration tests embed a vendored Lua 5.4 and drive the module
290
+from Lua scripts — no external interpreter needed:
291
+
292
+```sh
293
+cargo test -p feel-lua                    # 21 tests, Lua 5.4 compiled in (vendored)
294
+```
295
+
296
+To use it from a real Lua interpreter, build the loadable C module and run the
297
+end-to-end smoke (the analogue of feel-wasm's `smoke.mjs`):
298
+
299
+```sh
300
+cargo build -p feel-lua --no-default-features --features lua54,module
301
+LUA_CPATH='target/debug/lib?.so;;' lua5.4 crates/feel-lua/smoke.lua
302
+```
303
+
304
+(`lib?.so` expands `?` → `feel`, i.e. `libfeel.so`, so no rename is needed. Swap
305
+`lua54` for `luajit`/`luau` to target those.) The context and the result are native
306
+Lua values; parse/eval errors become Lua errors, and `evaluate_strict` also errors on
307
+a null that arose from a type/domain error.
308
+
309
+```lua
310
+local feel = require("feel")
311
+
312
+feel.evaluate("1 + 1")                                  -- => 2  (integer, not 2.0)
313
+feel.evaluate("person.age", { person = { age = 42 } })  -- => 42
314
+feel.evaluate("1 + true")                               -- => nil   (lenient)
315
+
316
+-- Host functions: bound in the context, or via an ambient `functions` registry.
317
+-- Lua has no kwargs, so FEEL named arguments arrive as a trailing options table.
318
+feel.evaluate("f(2, 3)", { f = function(a, b) return a + b end })          -- => 5
319
+feel.evaluate("f(2, b: 3)", { f = function(a, opts) return a + opts.b end })-- => 5
320
+feel.evaluate("add(2, 3)", nil, { add = function(a, b) return a + b end })  -- => 5
321
+
322
+-- Parse once, evaluate many times:
323
+local prog = feel.compile("a + 1")
324
+prog:evaluate({ a = 1 })                                -- => 2
325
+prog:evaluate({ a = 41 })                               -- => 42
326
+```
327
+
328
+Like feel-py/feel-wasm, a context table is mapped **lazily** (DECISIONS.md ADR-21/
329
+RD-9): `o.customer.address` resolves one hop at a time via `table[key]` (firing an
330
+`__index` metamethod on demand), and a table/function that comes back as the result is
331
+the *same* value, not a snapshot. A raised host-callable error is an **error-null** (→
332
+`nil` lenient, an error under `evaluate_strict`), distinct from a callable that simply
333
+`return`s `nil` (a value-null). The regex builtins use the bundled Rust dialect (no Lua
334
+pattern engine; RD-7) and temporal values cross out as ISO-8601 strings (RD-8).
335
+
283336
 ## benchmarks
284337
 
285338
 Two harnesses quantify the parse-once / evaluate-many win and a cross-engine