findPath.js (1836B)
1 load(libdir + "match.js") 2 3 // At the moment, findPath just returns the names as provided by ubi::Node, 4 // which just uses js::TraceChildren for now. However, we have various plans 5 // to improve the quality of ubi::Node's metadata, to improve the precision 6 // and clarity of the results here. 7 8 var o = { w: { x: { y: { z: {} } } } }; 9 Match.Pattern([{node: {}, edge: "w"}, 10 {node: {}, edge: "x"}, 11 {node: {}, edge: "y"}, 12 {node: {}, edge: "z"}]) 13 .assert(findPath(o, o.w.x.y.z)); 14 print(JSON.stringify(findPath(o, o.w.x.y.z))); 15 16 var a = [ , o ]; 17 Match.Pattern([{node: {}, edge: "objectElements[1]"}]) 18 .assert(findPath(a, o)); 19 print(JSON.stringify(findPath(a, o))); 20 21 function C() {} 22 C.prototype.obj = {}; 23 var c = new C; 24 Match.Pattern([{node: {}, edge: "shape"}, 25 {node: Match.Pattern.ANY, edge: "base"}, 26 {node: Match.Pattern.ANY, edge: "baseshape_proto"}, 27 {node: { constructor: Match.Pattern.ANY }, edge: "obj"}]) 28 .assert(findPath(c, c.obj)); 29 print(JSON.stringify(findPath(c, c.obj))); 30 31 function f(x) { return function g(y) { return x+y; }; } 32 var o = {} 33 var gc = f(o); 34 Match.Pattern([{node: gc, edge: "**UNKNOWN SLOT 1**"}, 35 {node: Match.Pattern.ANY, edge: "x"}]) 36 .assert(findPath(gc, o)); 37 print(JSON.stringify(findPath(gc, o))); 38 39 Match.Pattern([{node: {}, edge: "shape"}, 40 {node: Match.Pattern.ANY, edge: "base"}, 41 {node: Match.Pattern.ANY, edge: "baseshape_global"}, 42 {node: {}, edge: "o"}]) 43 .assert(findPath(o, o)); 44 print(findPath(o, o).map((e) => e.edge).toString()); 45 46 // Check that we can generate ubi::Nodes for Symbols. 47 var so = { sym: Symbol() }; 48 Match.Pattern([{node: {}, edge: "sym" }]) 49 .assert(findPath(so, so.sym)); 50 print(findPath(so, so.sym).map((e) => e.edge).toString());