MCPcopy Create free account
hub / github.com/arktypeio/arktype / precompile

Function precompile

ark/schema/units.ts:91–152  ·  view source on GitHub ↗
(
	references: readonly BaseNode[],
	owningScope?: BaseScope,
	unbound?: UnboundTraversals,
	root?: BaseNode
)

Source from the content-addressed store, hash-verified

89}
90
91export 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)

Callers 2

exportFunction · 0.90
precompileRootFunction · 0.70

Calls 15

canJitFunction · 0.90
createLinkageFunction · 0.85
isLeafInFunction · 0.85
precompileReferencesFunction · 0.85
bindUnitFunction · 0.85
isMethod · 0.80
bindMethod · 0.80
writesInPlaceMethod · 0.80
mapMethod · 0.65
addMethod · 0.45
pushMethod · 0.45
compileMethod · 0.45

Tested by

no test coverage detected