Log
a4a29f172176
feat(wasm): add WeakMap-based cycle detection for descendant traversals in JavaScript
public
feat(wasm): add WeakMap-based cycle detection for descendant traversals in JavaScript
Implements cycle-safe unbounded descendant traversal (`e ... name`) in the feel-wasm
crate using a thread-local WeakMap<object, number> to derive stable object identities.
JavaScript lacks native object address primitives, so the previous Arc allocation
guard would fail to detect cycles when the same object is re-wrapped across force/get
operations.
The ForeignObject::identity() implementation now returns a stable integer key from
the WeakMap, enabling safe traversal of cyclic data structures while preserving the
descendant operator's specification (DMN §10.3.2.7).
Adds smoke test coverage for:
- Basic descendant traversal cases
- Empty result handling
- Cyclic object termination
- Nested object descent
BREAKING CHANGE: None
modified
crates/feel-wasm/smoke.mjs
+31 −0
@@ -115,6 +115,37 @@
115 115 ok = false ; 116 116 } 117 117 118 +// Descendant `e ... name` (DMN 10.3.2.7): recursively collect every value bound to 119 +// `name` in `e` and all nested contexts. Unlike the lazy `.`, it is an *unbounded* 120 +// traversal, so a cyclic object would loop forever without the WeakMap-based 121 +// identity (JS exposes no cheap object address, so object identity is derived via a 122 +// WeakMap<object, number>). 123 +const descendantCases = [ 124 + ["{a: {b: {b: 1}}} ... b" , {}, [{ b: 1 }, 1 ]], 125 + ["[{x:1},{x:2}] ... x" , {}, [1 , 2 ]], 126 + ["{a:1} ... z" , {}, []], 127 + ["o ... name" , { o: { customer: { name: "Ada" } } }, ["Ada" ]], 128 +]; 129 +for (const [expr, ctx, expected] of descendantCases) { 130 + const got = evaluate(expr, ctx); 131 + console.log(` ${ expr} => ${ JSON .stringify(got)} ` ); 132 + if (! eq(got, expected)) { 133 + console.error(` FAIL: ${ expr} expected ${ JSON .stringify(expected)} , got ${ JSON .stringify(got)} ` ); 134 + ok = false ; 135 + } 136 +} 137 +// The descendant traversal over a *cyclic* object must terminate: the back-edge into 138 +// the same object (same WeakMap id) is cut after the first re-entry. `cyc2.next` 139 +// points back to `cyc2`, so `value` is collected twice before the cycle is broken. 140 +const cyc2 = { value: 42 }; 141 +cyc2.next = cyc2; 142 +const cycDesc = evaluate("c ... value" , { c: cyc2 }); 143 +console.log(` c ... value (cyclic) => ${ JSON .stringify(cycDesc)} ` ); 144 +if (! eq(cycDesc, [42 , 42 ])) { 145 + console.error(` FAIL: descendant over a cyclic object should terminate with [42,42], got ${ JSON .stringify(cycDesc)} ` ); 146 + ok = false ; 147 +} 148 + 118 149 // Boundary policy (ADR-14): a type error is a lenient `null`, but `evaluate_strict` 119 150 // surfaces it as a thrown exception instead of letting it pass silently as `null`. 120 151 const threw = (fn) => {
modified
crates/feel-wasm/src/lib.rs
+37 −0
@@ -16,6 +16,7 @@
16 16 //! A finite expression can only navigate a finite path, so cyclic objects need no 17 17 //! cycle guard (the previous `js_to_value` guard is gone). 18 18 19 +use std::cell::RefCell; 19 20 use std::collections::BTreeMap; 20 21 use std::sync::Arc; 21 22
@@ -341,6 +342,42 @@
341 342 fn as_any (& self ) -> & dyn std::any::Any { 342 343 self 343 344 } 345 + 346 + /// JS object identity for cycle detection in unbounded traversals (the 347 + /// descendant `...` operator). feel-wasm mints a fresh `Arc<JsForeign>` per wrap 348 + /// (`shallow_convert_js` re-runs on every `force`/`get`), and a `JsValue` handle 349 + /// index is NOT object identity across different reads of the same object, so the 350 + /// default (allocation address) guard would miss a cycle. We map each distinct 351 + /// object to a stable integer via a process-wide `WeakMap` ([`js_identity`]). 352 + fn identity (& self ) -> usize { 353 + js_identity(& self .obj) 354 + } 355 +} 356 + 357 +/// Assign each distinct JS object a stable integer identity via a process-wide 358 +/// `WeakMap`. Its keys are weakly referenced, so it neither pins objects nor grows 359 +/// unboundedly with live data — entries are GC'd once the object is unreferenced 360 +/// elsewhere. This is the JS analogue of feel-py's `id()` / feel-lua's table 361 +/// pointer: JS exposes no cheap object address, so a `WeakMap<object, number>` is 362 +/// the standard way to derive a hashable, stable identity. 363 +fn js_identity (obj: & JsValue ) -> usize { 364 + thread_local! { 365 + static IDS: RefCell < (js_sys::WeakMap, u32 )> = 366 + RefCell::new((js_sys::WeakMap::new(), 0 )); 367 + } 368 + IDS.with(| cell| { 369 + let (map, next) = & mut * cell.borrow_mut(); 370 + // A Foreign always wraps an object; `unchecked_ref` is the zero-cost view. 371 + let key: & js_sys ::Object = obj.unchecked_ref(); 372 + if let Some (v) = map.get_checked(key) { 373 + v.as_f64().map(| n| n as usize ).unwrap_or(0 ) 374 + } else { 375 + let n = * next; 376 + * next + = 1 ; 377 + let _ = map.set(key, & JsValue::from(n)); 378 + n as usize 379 + } 380 + }) 344 381 } 345 382 346 383 // --------------------------------------------------------------------------- //