( references: readonly BaseNode[], owningScope?: BaseScope, unbound?: UnboundTraversals, root?: BaseNode )
| 89 | } |
| 90 | |
| 91 | export const precompile = ( |
| 92 | references: readonly BaseNode[], |
| 93 | owningScope?: BaseScope, |
| 94 | unbound?: UnboundTraversals, |
| 95 | root?: BaseNode |
| 96 | ): Fn => { |
| 97 | const link = createLinkage() |
| 98 | const reused = new Set<BaseNode>() |
| 99 | link.reused = reused |
| 100 | for (const node of references) link.byId[node.id] = node |
| 101 | const declared: BaseNode[] = [] |
| 102 | const pruned = new Set<BaseNode>() |
| 103 | for (const node of references) { |
| 104 | if (node.c.isReusableLeaf && isLeafIn(node, link.byId)) reused.add(node) |
| 105 | else if (root && node !== root && !node.unitFn) { |
| 106 | link.unreached.add(node) |
| 107 | pruned.add(node) |
| 108 | } else if ( |
| 109 | node.unitFn && |
| 110 | (!owningScope || node.$ !== owningScope) && |
| 111 | // compiling an alias can create nodes, so every unit declares its own |
| 112 | !node.is("alias") |
| 113 | ) |
| 114 | link.unreached.add(node) |
| 115 | else declared.push(node) |
| 116 | } |
| 117 | const unitFn = precompileReferences(declared, link).compile() |
| 118 | const traversals = unitFn( |
| 119 | Object.values(link.dependencies).map(([node, kind]) => { |
| 120 | const traverse: Fn = node[`t${kind}`]! |
| 121 | // an uncompiled node's traversal is its method, which reads the node |
| 122 | return node.unitFn ? traverse : traverse.bind(node) |
| 123 | }), |
| 124 | [...link.refs.keys()] |
| 125 | ) |
| 126 | const bound = [...declared] |
| 127 | const boundTraversals = traversals.slice(0, declared.length) |
| 128 | for (let i = 0; i < link.reached.length; i++) { |
| 129 | if (!pruned.has(link.reached[i])) continue |
| 130 | bound.push(link.reached[i]) |
| 131 | boundTraversals.push(traversals[declared.length + i]) |
| 132 | } |
| 133 | const inPlace = $ark.inPlace |
| 134 | if (inPlace) { |
| 135 | for (const reference of references) { |
| 136 | if ( |
| 137 | reference === root || |
| 138 | !reference.tfs || |
| 139 | !reference.isRoot() || |
| 140 | canJit(reference.$.cfg) || |
| 141 | !inPlace.writesInPlace(reference) |
| 142 | ) |
| 143 | continue |
| 144 | unbound ??= new Map() |
| 145 | for (const id in reference.byId) |
| 146 | unbound.set(reference.byId[id], undefined) |
| 147 | } |
| 148 | if (root) unbound?.delete(root) |
no test coverage detected