typesea 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/CHANGELOG.md +85 -6
  2. package/README.md +143 -28
  3. package/dist/adapters/index.d.ts +50 -8
  4. package/dist/adapters/index.d.ts.map +1 -1
  5. package/dist/adapters/index.js +169 -48
  6. package/dist/aot/index.d.ts +19 -3
  7. package/dist/aot/index.d.ts.map +1 -1
  8. package/dist/aot/index.js +115 -17
  9. package/dist/async/index.d.ts +28 -56
  10. package/dist/async/index.d.ts.map +1 -1
  11. package/dist/async/index.js +94 -37
  12. package/dist/builders/composite.d.ts +43 -9
  13. package/dist/builders/composite.d.ts.map +1 -1
  14. package/dist/builders/composite.js +100 -17
  15. package/dist/builders/index.d.ts +8 -5
  16. package/dist/builders/index.d.ts.map +1 -1
  17. package/dist/builders/index.js +7 -4
  18. package/dist/builders/modifier.d.ts +36 -5
  19. package/dist/builders/modifier.d.ts.map +1 -1
  20. package/dist/builders/modifier.js +52 -5
  21. package/dist/builders/object/guard.d.ts +72 -24
  22. package/dist/builders/object/guard.d.ts.map +1 -1
  23. package/dist/builders/object/guard.js +139 -29
  24. package/dist/builders/object/index.d.ts +4 -2
  25. package/dist/builders/object/index.d.ts.map +1 -1
  26. package/dist/builders/object/index.js +3 -1
  27. package/dist/builders/object/schema.d.ts +88 -11
  28. package/dist/builders/object/schema.d.ts.map +1 -1
  29. package/dist/builders/object/schema.js +290 -23
  30. package/dist/builders/object/types.d.ts +20 -31
  31. package/dist/builders/object/types.d.ts.map +1 -1
  32. package/dist/builders/object/types.js +2 -0
  33. package/dist/builders/runtime.d.ts +40 -0
  34. package/dist/builders/runtime.d.ts.map +1 -0
  35. package/dist/builders/runtime.js +150 -0
  36. package/dist/builders/scalar.d.ts +49 -9
  37. package/dist/builders/scalar.d.ts.map +1 -1
  38. package/dist/builders/scalar.js +87 -9
  39. package/dist/builders/table.d.ts +35 -5
  40. package/dist/builders/table.d.ts.map +1 -1
  41. package/dist/builders/table.js +35 -5
  42. package/dist/builders/types.d.ts +20 -4
  43. package/dist/builders/types.d.ts.map +1 -1
  44. package/dist/builders/types.js +2 -0
  45. package/dist/compile/check-composite.d.ts +25 -2
  46. package/dist/compile/check-composite.d.ts.map +1 -1
  47. package/dist/compile/check-composite.js +699 -27
  48. package/dist/compile/check-scalar.d.ts +88 -0
  49. package/dist/compile/check-scalar.d.ts.map +1 -1
  50. package/dist/compile/check-scalar.js +570 -3
  51. package/dist/compile/check.d.ts +12 -0
  52. package/dist/compile/check.d.ts.map +1 -1
  53. package/dist/compile/check.js +62 -3
  54. package/dist/compile/context.d.ts +47 -9
  55. package/dist/compile/context.d.ts.map +1 -1
  56. package/dist/compile/context.js +53 -8
  57. package/dist/compile/first.d.ts +26 -0
  58. package/dist/compile/first.d.ts.map +1 -0
  59. package/dist/compile/first.js +850 -0
  60. package/dist/compile/graph-predicate.d.ts +4 -2
  61. package/dist/compile/graph-predicate.d.ts.map +1 -1
  62. package/dist/compile/graph-predicate.js +2272 -165
  63. package/dist/compile/guard.d.ts +16 -24
  64. package/dist/compile/guard.d.ts.map +1 -1
  65. package/dist/compile/guard.js +202 -72
  66. package/dist/compile/index.d.ts +3 -1
  67. package/dist/compile/index.d.ts.map +1 -1
  68. package/dist/compile/index.js +2 -0
  69. package/dist/compile/issue.d.ts +110 -0
  70. package/dist/compile/issue.d.ts.map +1 -1
  71. package/dist/compile/issue.js +184 -1
  72. package/dist/compile/names.d.ts +12 -2
  73. package/dist/compile/names.d.ts.map +1 -1
  74. package/dist/compile/names.js +19 -3
  75. package/dist/compile/predicate.d.ts +24 -0
  76. package/dist/compile/predicate.d.ts.map +1 -1
  77. package/dist/compile/predicate.js +287 -10
  78. package/dist/compile/runtime.d.ts +100 -13
  79. package/dist/compile/runtime.d.ts.map +1 -1
  80. package/dist/compile/runtime.js +56 -6
  81. package/dist/compile/source.d.ts +10 -2
  82. package/dist/compile/source.d.ts.map +1 -1
  83. package/dist/compile/source.js +385 -26
  84. package/dist/compile/types.d.ts +22 -0
  85. package/dist/compile/types.d.ts.map +1 -1
  86. package/dist/compile/types.js +2 -0
  87. package/dist/decoder/index.d.ts +92 -46
  88. package/dist/decoder/index.d.ts.map +1 -1
  89. package/dist/decoder/index.js +266 -39
  90. package/dist/evaluate/check-composite.d.ts +111 -2
  91. package/dist/evaluate/check-composite.d.ts.map +1 -1
  92. package/dist/evaluate/check-composite.js +343 -8
  93. package/dist/evaluate/check-scalar.d.ts +25 -0
  94. package/dist/evaluate/check-scalar.d.ts.map +1 -1
  95. package/dist/evaluate/check-scalar.js +124 -3
  96. package/dist/evaluate/check.d.ts +7 -0
  97. package/dist/evaluate/check.d.ts.map +1 -1
  98. package/dist/evaluate/check.js +62 -4
  99. package/dist/evaluate/index.d.ts +2 -0
  100. package/dist/evaluate/index.d.ts.map +1 -1
  101. package/dist/evaluate/index.js +2 -0
  102. package/dist/evaluate/issue.d.ts +11 -1
  103. package/dist/evaluate/issue.d.ts.map +1 -1
  104. package/dist/evaluate/issue.js +15 -1
  105. package/dist/evaluate/predicate.d.ts +16 -5
  106. package/dist/evaluate/predicate.d.ts.map +1 -1
  107. package/dist/evaluate/predicate.js +20 -5
  108. package/dist/evaluate/shared.d.ts +78 -13
  109. package/dist/evaluate/shared.d.ts.map +1 -1
  110. package/dist/evaluate/shared.js +101 -8
  111. package/dist/evaluate/state.d.ts +35 -13
  112. package/dist/evaluate/state.d.ts.map +1 -1
  113. package/dist/evaluate/state.js +35 -2
  114. package/dist/guard/array.d.ts +48 -0
  115. package/dist/guard/array.d.ts.map +1 -0
  116. package/dist/guard/array.js +84 -0
  117. package/dist/guard/base.d.ts +111 -31
  118. package/dist/guard/base.d.ts.map +1 -1
  119. package/dist/guard/base.js +165 -32
  120. package/dist/guard/date.d.ts +34 -0
  121. package/dist/guard/date.d.ts.map +1 -0
  122. package/dist/guard/date.js +60 -0
  123. package/dist/guard/error.d.ts +10 -5
  124. package/dist/guard/error.d.ts.map +1 -1
  125. package/dist/guard/error.js +10 -5
  126. package/dist/guard/index.d.ts +4 -0
  127. package/dist/guard/index.d.ts.map +1 -1
  128. package/dist/guard/index.js +4 -0
  129. package/dist/guard/number.d.ts +86 -11
  130. package/dist/guard/number.d.ts.map +1 -1
  131. package/dist/guard/number.js +159 -11
  132. package/dist/guard/props.d.ts +27 -3
  133. package/dist/guard/props.d.ts.map +1 -1
  134. package/dist/guard/props.js +27 -3
  135. package/dist/guard/read.d.ts +115 -10
  136. package/dist/guard/read.d.ts.map +1 -1
  137. package/dist/guard/read.js +185 -10
  138. package/dist/guard/registry.d.ts +12 -2
  139. package/dist/guard/registry.d.ts.map +1 -1
  140. package/dist/guard/registry.js +15 -3
  141. package/dist/guard/string.d.ts +115 -13
  142. package/dist/guard/string.d.ts.map +1 -1
  143. package/dist/guard/string.js +250 -13
  144. package/dist/guard/types.d.ts +110 -40
  145. package/dist/guard/types.d.ts.map +1 -1
  146. package/dist/guard/types.js +2 -0
  147. package/dist/index.d.ts +5 -5
  148. package/dist/index.d.ts.map +1 -1
  149. package/dist/index.js +4 -4
  150. package/dist/internal/index.d.ts +42 -6
  151. package/dist/internal/index.d.ts.map +1 -1
  152. package/dist/internal/index.js +51 -8
  153. package/dist/ir/builder.d.ts +17 -127
  154. package/dist/ir/builder.d.ts.map +1 -1
  155. package/dist/ir/builder.js +80 -137
  156. package/dist/ir/freeze.d.ts +4 -0
  157. package/dist/ir/freeze.d.ts.map +1 -1
  158. package/dist/ir/freeze.js +66 -0
  159. package/dist/ir/index.d.ts +3 -1
  160. package/dist/ir/index.d.ts.map +1 -1
  161. package/dist/ir/index.js +2 -0
  162. package/dist/ir/regexp.d.ts +2 -0
  163. package/dist/ir/regexp.d.ts.map +1 -1
  164. package/dist/ir/regexp.js +2 -0
  165. package/dist/ir/types.d.ts +94 -56
  166. package/dist/ir/types.d.ts.map +1 -1
  167. package/dist/ir/types.js +2 -0
  168. package/dist/ir/validate.d.ts +8 -1
  169. package/dist/ir/validate.d.ts.map +1 -1
  170. package/dist/ir/validate.js +511 -61
  171. package/dist/issue/index.d.ts +42 -10
  172. package/dist/issue/index.d.ts.map +1 -1
  173. package/dist/issue/index.js +65 -11
  174. package/dist/json-schema/emit-combinator.d.ts +44 -4
  175. package/dist/json-schema/emit-combinator.d.ts.map +1 -1
  176. package/dist/json-schema/emit-combinator.js +44 -4
  177. package/dist/json-schema/emit-composite.d.ts +16 -2
  178. package/dist/json-schema/emit-composite.d.ts.map +1 -1
  179. package/dist/json-schema/emit-composite.js +81 -13
  180. package/dist/json-schema/emit-scalar.d.ts +26 -3
  181. package/dist/json-schema/emit-scalar.d.ts.map +1 -1
  182. package/dist/json-schema/emit-scalar.js +124 -10
  183. package/dist/json-schema/emit-types.d.ts +11 -1
  184. package/dist/json-schema/emit-types.d.ts.map +1 -1
  185. package/dist/json-schema/emit-types.js +2 -0
  186. package/dist/json-schema/emit.d.ts +12 -1
  187. package/dist/json-schema/emit.d.ts.map +1 -1
  188. package/dist/json-schema/emit.js +23 -3
  189. package/dist/json-schema/freeze.d.ts +13 -2
  190. package/dist/json-schema/freeze.d.ts.map +1 -1
  191. package/dist/json-schema/freeze.js +41 -8
  192. package/dist/json-schema/index.d.ts +16 -2
  193. package/dist/json-schema/index.d.ts.map +1 -1
  194. package/dist/json-schema/index.js +23 -3
  195. package/dist/json-schema/issue.d.ts +4 -1
  196. package/dist/json-schema/issue.d.ts.map +1 -1
  197. package/dist/json-schema/issue.js +4 -1
  198. package/dist/json-schema/read.d.ts +24 -3
  199. package/dist/json-schema/read.d.ts.map +1 -1
  200. package/dist/json-schema/read.js +59 -12
  201. package/dist/json-schema/types.d.ts +45 -16
  202. package/dist/json-schema/types.d.ts.map +1 -1
  203. package/dist/json-schema/types.js +2 -0
  204. package/dist/kind/index.d.ts +40 -28
  205. package/dist/kind/index.d.ts.map +1 -1
  206. package/dist/kind/index.js +41 -13
  207. package/dist/lower/index.d.ts +6 -1
  208. package/dist/lower/index.d.ts.map +1 -1
  209. package/dist/lower/index.js +462 -46
  210. package/dist/message/index.d.ts +64 -10
  211. package/dist/message/index.d.ts.map +1 -1
  212. package/dist/message/index.js +155 -17
  213. package/dist/optimize/algebraic.d.ts +54 -0
  214. package/dist/optimize/algebraic.d.ts.map +1 -0
  215. package/dist/optimize/algebraic.js +314 -0
  216. package/dist/optimize/compact.d.ts +8 -1
  217. package/dist/optimize/compact.d.ts.map +1 -1
  218. package/dist/optimize/compact.js +13 -2
  219. package/dist/optimize/domain.d.ts +16 -0
  220. package/dist/optimize/domain.d.ts.map +1 -0
  221. package/dist/optimize/domain.js +619 -0
  222. package/dist/optimize/fold-boolean.d.ts +17 -2
  223. package/dist/optimize/fold-boolean.d.ts.map +1 -1
  224. package/dist/optimize/fold-boolean.js +59 -14
  225. package/dist/optimize/fold-common.d.ts +43 -8
  226. package/dist/optimize/fold-common.d.ts.map +1 -1
  227. package/dist/optimize/fold-common.js +37 -6
  228. package/dist/optimize/fold-constraints.d.ts +33 -0
  229. package/dist/optimize/fold-constraints.d.ts.map +1 -0
  230. package/dist/optimize/fold-constraints.js +484 -0
  231. package/dist/optimize/fold-scalar.d.ts +98 -13
  232. package/dist/optimize/fold-scalar.d.ts.map +1 -1
  233. package/dist/optimize/fold-scalar.js +98 -13
  234. package/dist/optimize/fold.d.ts +8 -1
  235. package/dist/optimize/fold.d.ts.map +1 -1
  236. package/dist/optimize/fold.js +22 -2
  237. package/dist/optimize/index.d.ts +9 -1
  238. package/dist/optimize/index.d.ts.map +1 -1
  239. package/dist/optimize/index.js +18 -3
  240. package/dist/optimize/map-node.d.ts +3 -1
  241. package/dist/optimize/map-node.d.ts.map +1 -1
  242. package/dist/optimize/map-node.js +48 -3
  243. package/dist/optimize/peephole.d.ts +16 -0
  244. package/dist/optimize/peephole.d.ts.map +1 -0
  245. package/dist/optimize/peephole.js +254 -0
  246. package/dist/optimize/remap.d.ts +2 -0
  247. package/dist/optimize/remap.d.ts.map +1 -1
  248. package/dist/optimize/remap.js +2 -0
  249. package/dist/optimize/rewrite.d.ts +13 -8
  250. package/dist/optimize/rewrite.d.ts.map +1 -1
  251. package/dist/optimize/rewrite.js +13 -8
  252. package/dist/plan/cache.d.ts +9 -3
  253. package/dist/plan/cache.d.ts.map +1 -1
  254. package/dist/plan/cache.js +34 -6
  255. package/dist/plan/index.d.ts +2 -0
  256. package/dist/plan/index.d.ts.map +1 -1
  257. package/dist/plan/index.js +2 -0
  258. package/dist/plan/predicate.d.ts +2 -0
  259. package/dist/plan/predicate.d.ts.map +1 -1
  260. package/dist/plan/predicate.js +298 -29
  261. package/dist/plan/schema-predicate.d.ts +6 -0
  262. package/dist/plan/schema-predicate.d.ts.map +1 -1
  263. package/dist/plan/schema-predicate.js +382 -19
  264. package/dist/plan/types.d.ts +2 -0
  265. package/dist/plan/types.d.ts.map +1 -1
  266. package/dist/plan/types.js +2 -0
  267. package/dist/result/index.d.ts +19 -5
  268. package/dist/result/index.d.ts.map +1 -1
  269. package/dist/result/index.js +10 -2
  270. package/dist/schema/common.d.ts +69 -6
  271. package/dist/schema/common.d.ts.map +1 -1
  272. package/dist/schema/common.js +104 -10
  273. package/dist/schema/freeze.d.ts +4 -0
  274. package/dist/schema/freeze.d.ts.map +1 -1
  275. package/dist/schema/freeze.js +40 -0
  276. package/dist/schema/index.d.ts +5 -2
  277. package/dist/schema/index.d.ts.map +1 -1
  278. package/dist/schema/index.js +4 -1
  279. package/dist/schema/lazy.d.ts +4 -0
  280. package/dist/schema/lazy.d.ts.map +1 -1
  281. package/dist/schema/lazy.js +4 -0
  282. package/dist/schema/literal.d.ts +7 -1
  283. package/dist/schema/literal.d.ts.map +1 -1
  284. package/dist/schema/literal.js +7 -1
  285. package/dist/schema/types.d.ts +109 -100
  286. package/dist/schema/types.d.ts.map +1 -1
  287. package/dist/schema/types.js +13 -2
  288. package/dist/schema/undefined.d.ts +17 -0
  289. package/dist/schema/undefined.d.ts.map +1 -0
  290. package/dist/schema/undefined.js +77 -0
  291. package/dist/schema/validate.d.ts +8 -1
  292. package/dist/schema/validate.d.ts.map +1 -1
  293. package/dist/schema/validate.js +255 -57
  294. package/docs/api.md +128 -8
  295. package/docs/assets/benchmark-headline.svg +163 -0
  296. package/docs/engine-notes.md +62 -15
  297. package/docs/index.html +1340 -702
  298. package/docs/ko/api.md +375 -0
  299. package/docs/ko/engine-notes.md +156 -0
  300. package/docs/ko/readme.md +378 -0
  301. package/package.json +66 -65
@@ -0,0 +1,254 @@
1
+ /**
2
+ * @file peephole.ts
3
+ * @brief Local graph rewrites that remove dead dispatch arms.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
6
+ */
7
+ import { NodeTag } from "../kind/index.js";
8
+ import { ensureConst, keep, replace } from "./fold-common.js";
9
+ import { resolveAlias, rewriteNodeDeps } from "./rewrite.js";
10
+ /**
11
+ * @brief Run local dispatch compaction over one graph.
12
+ * @details This pass removes child arms that already fold to false and rewrites
13
+ * aliases after each replacement so later nodes see the simplified ids.
14
+ * @param graph Graph to optimize.
15
+ * @returns Graph with compacted dispatch nodes and resolved aliases.
16
+ */
17
+ export function peepholeGraph(graph) {
18
+ const originalLength = graph.nodes.length;
19
+ const nodes = graph.nodes.slice();
20
+ const aliases = new Array(nodes.length);
21
+ for (let index = 0; index < aliases.length; index += 1) {
22
+ aliases[index] = index;
23
+ }
24
+ for (let index = 0; index < originalLength; index += 1) {
25
+ const node = nodes[index];
26
+ if (node === undefined) {
27
+ continue;
28
+ }
29
+ /*
30
+ * Each node is rewritten through aliases before the peephole rule runs.
31
+ * That lets a removal near the front of the graph feed later dispatch
32
+ * compaction in the same pass.
33
+ */
34
+ const rewritten = rewriteNodeDeps(node, aliases);
35
+ const result = peepholeNode(rewritten, nodes, aliases);
36
+ nodes[index] = result.node;
37
+ if (result.replacement !== undefined) {
38
+ aliases[index] = result.replacement;
39
+ }
40
+ }
41
+ for (let index = 0; index < aliases.length; index += 1) {
42
+ /*
43
+ * Aliases may point through other aliases. Resolve the chain once before
44
+ * rebuilding the graph so every dependency points at the final target.
45
+ */
46
+ aliases[index] = resolveAlias(index, aliases);
47
+ }
48
+ const rewritten = new Array(nodes.length);
49
+ for (let index = 0; index < nodes.length; index += 1) {
50
+ const node = nodes[index];
51
+ if (node !== undefined) {
52
+ rewritten[index] = rewriteNodeDeps(node, aliases);
53
+ }
54
+ }
55
+ return {
56
+ nodes: rewritten,
57
+ entry: resolveAlias(graph.entry, aliases),
58
+ result: resolveAlias(graph.result, aliases)
59
+ };
60
+ }
61
+ /**
62
+ * @brief Apply the local peephole rule for one node.
63
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
64
+ * before code generation consumes the graph.
65
+ * @param node Node with dependencies already alias-rewritten.
66
+ * @param nodes Mutable graph node table.
67
+ * @param aliases Alias table updated by replacements.
68
+ * @returns Fold decision for the node.
69
+ */
70
+ function peepholeNode(node, nodes, aliases) {
71
+ switch (node.tag) {
72
+ case NodeTag.DiscriminantDispatch:
73
+ return compactDiscriminantDispatch(node, nodes, aliases);
74
+ case NodeTag.UnionDispatch:
75
+ return compactUnionDispatch(node, nodes, aliases);
76
+ case NodeTag.PrimitiveUnion:
77
+ return compactPrimitiveUnion(node, nodes, aliases);
78
+ default:
79
+ return keep(node);
80
+ }
81
+ }
82
+ /**
83
+ * @brief Remove impossible discriminant dispatch arms.
84
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
85
+ * before code generation consumes the graph.
86
+ * @param node Discriminant dispatch node to compact.
87
+ * @param nodes Mutable graph node table used for replacement constants.
88
+ * @param aliases Alias table updated by replacements.
89
+ * @returns Compacted dispatch node or constant false when no arm remains.
90
+ */
91
+ function compactDiscriminantDispatch(node, nodes, aliases) {
92
+ const literals = [];
93
+ const schemas = [];
94
+ const graphs = [];
95
+ let changed = false;
96
+ for (let index = 0; index < node.graphs.length; index += 1) {
97
+ const graph = node.graphs[index];
98
+ const literal = node.literals[index];
99
+ const schema = node.schemas[index];
100
+ if (graph === undefined || literal === undefined || schema === undefined ||
101
+ readGraphResultBoolean(graph) === false) {
102
+ /*
103
+ * A dispatch arm whose child graph is already false can never accept
104
+ * input for that discriminant literal, so it is removed from lookup.
105
+ */
106
+ changed = true;
107
+ continue;
108
+ }
109
+ literals.push(literal);
110
+ schemas.push(schema);
111
+ graphs.push(graph);
112
+ changed = changed || literals.length - 1 !== index;
113
+ }
114
+ if (graphs.length === 0) {
115
+ return replace(node, ensureConst(nodes, aliases, false));
116
+ }
117
+ if (!changed) {
118
+ return keep(node);
119
+ }
120
+ return keep({
121
+ id: node.id,
122
+ tag: node.tag,
123
+ deps: node.deps,
124
+ value: node.value,
125
+ key: node.key,
126
+ literals,
127
+ schemas,
128
+ graphs,
129
+ lookup: makeDiscriminantLookup(literals)
130
+ });
131
+ }
132
+ /**
133
+ * @brief Remove impossible general union dispatch arms.
134
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
135
+ * before code generation consumes the graph.
136
+ * @param node Union dispatch node to compact.
137
+ * @param nodes Mutable graph node table used for replacement constants.
138
+ * @param aliases Alias table updated by replacements.
139
+ * @returns Compacted dispatch node or constant false when no arm remains.
140
+ */
141
+ function compactUnionDispatch(node, nodes, aliases) {
142
+ const options = [];
143
+ const graphs = [];
144
+ const masks = [];
145
+ let changed = false;
146
+ for (let index = 0; index < node.graphs.length; index += 1) {
147
+ const graph = node.graphs[index];
148
+ const option = node.options[index];
149
+ const mask = node.masks[index];
150
+ if (graph === undefined || option === undefined || mask === undefined ||
151
+ mask === 0 || readGraphResultBoolean(graph) === false) {
152
+ /*
153
+ * A zero mask or false child graph means the arm has no reachable
154
+ * runtime domain after earlier folds.
155
+ */
156
+ changed = true;
157
+ continue;
158
+ }
159
+ options.push(option);
160
+ graphs.push(graph);
161
+ masks.push(mask);
162
+ changed = changed || graphs.length - 1 !== index;
163
+ }
164
+ if (graphs.length === 0) {
165
+ return replace(node, ensureConst(nodes, aliases, false));
166
+ }
167
+ if (!changed) {
168
+ return keep(node);
169
+ }
170
+ return keep({
171
+ id: node.id,
172
+ tag: node.tag,
173
+ deps: node.deps,
174
+ value: node.value,
175
+ options,
176
+ graphs,
177
+ masks
178
+ });
179
+ }
180
+ /**
181
+ * @brief Remove impossible primitive-union dispatch arms.
182
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
183
+ * before code generation consumes the graph.
184
+ * @param node Primitive union dispatch node to compact.
185
+ * @param nodes Mutable graph node table used for replacement constants.
186
+ * @param aliases Alias table updated by replacements.
187
+ * @returns Compacted dispatch node or constant false when no arm remains.
188
+ */
189
+ function compactPrimitiveUnion(node, nodes, aliases) {
190
+ const graphs = [];
191
+ const masks = [];
192
+ let changed = false;
193
+ for (let index = 0; index < node.graphs.length; index += 1) {
194
+ const graph = node.graphs[index];
195
+ const mask = node.masks[index];
196
+ if (graph === undefined || mask === undefined ||
197
+ mask === 0 || readGraphResultBoolean(graph) === false) {
198
+ changed = true;
199
+ continue;
200
+ }
201
+ graphs.push(graph);
202
+ masks.push(mask);
203
+ changed = changed || graphs.length - 1 !== index;
204
+ }
205
+ if (graphs.length === 0) {
206
+ return replace(node, ensureConst(nodes, aliases, false));
207
+ }
208
+ if (!changed) {
209
+ return keep(node);
210
+ }
211
+ return keep({
212
+ id: node.id,
213
+ tag: node.tag,
214
+ deps: node.deps,
215
+ value: node.value,
216
+ graphs,
217
+ masks
218
+ });
219
+ }
220
+ /**
221
+ * @brief Read whether a graph's result is a constant boolean.
222
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
223
+ * before code generation consumes the graph.
224
+ * @param graph Child graph to inspect.
225
+ * @returns Constant boolean result, or undefined for non-constant graphs.
226
+ */
227
+ function readGraphResultBoolean(graph) {
228
+ const result = graph.nodes[graph.result];
229
+ if (result?.tag !== NodeTag.Return) {
230
+ return undefined;
231
+ }
232
+ const value = graph.nodes[result.value];
233
+ if (value?.tag === NodeTag.Const && typeof value.value === "boolean") {
234
+ return value.value;
235
+ }
236
+ return undefined;
237
+ }
238
+ /**
239
+ * @brief Build a literal-to-arm lookup for discriminant dispatch.
240
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
241
+ * before code generation consumes the graph.
242
+ * @param literals Remaining discriminant literals in arm order.
243
+ * @returns Frozen null-prototype lookup from literal to compacted arm index.
244
+ */
245
+ function makeDiscriminantLookup(literals) {
246
+ const lookup = Object.create(null);
247
+ for (let index = 0; index < literals.length; index += 1) {
248
+ const literal = literals[index];
249
+ if (literal !== undefined) {
250
+ lookup[literal] = index;
251
+ }
252
+ }
253
+ return Object.freeze(lookup);
254
+ }
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file optimize-remap.ts
3
3
  * @brief Dense node-id remapping helpers for graph compaction.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import type { GraphNode, NodeId } from "../ir/index.js";
6
8
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"remap.d.ts","sourceRoot":"","sources":["../../src/optimize/remap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACP,MAAM,gBAAgB,CAAC;AAGxB;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB,SAAS,CAMX;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAMvE;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAStF"}
1
+ {"version":3,"file":"remap.d.ts","sourceRoot":"","sources":["../../src/optimize/remap.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACR,SAAS,EACT,MAAM,EACT,MAAM,gBAAgB,CAAC;AAGxB;;;;;;GAMG;AACH,wBAAgB,SAAS,CACrB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,MAAM,EAAE,GACzB,SAAS,CAMX;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAMvE;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAStF"}
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file optimize-remap.ts
3
3
  * @brief Dense node-id remapping helpers for graph compaction.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { mapNodeIds } from "./map-node.js";
6
8
  /**
@@ -1,21 +1,26 @@
1
1
  /**
2
2
  * @file rewrite.ts
3
3
  * @brief Node dependency rewrite utilities.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import type { GraphNode, NodeId } from "../ir/index.js";
6
8
  /**
7
- * @brief rewrite node deps.
8
- * @details Resolves dependency aliases while preserving the node's own id.
9
- * @param node Borrowed graph node to rewrite.
10
- * @param aliases Borrowed alias table produced by the folding pass.
9
+ * @brief Rewrite dependency ids through the current folding alias table.
10
+ * @details The node keeps its original id so the folding pass can continue a
11
+ * stable arena scan while all dependency edges point at canonical replacements.
12
+ * @param node Graph node to rewrite.
13
+ * @param aliases Alias table produced by the folding pass.
11
14
  * @returns Graph node with dependency ids rewritten through the alias table.
12
15
  */
13
16
  export declare function rewriteNodeDeps(node: GraphNode, aliases: readonly NodeId[]): GraphNode;
14
17
  /**
15
- * @brief resolve alias.
16
- * @details Follows an alias chain until it reaches a stable node id.
17
- * @param value Borrowed node id to resolve.
18
- * @param aliases Borrowed alias table.
18
+ * @brief Resolve one node id through a chain of folding aliases.
19
+ * @details Alias chains appear when a node folds to another node that later
20
+ * folds again. Resolving to the final stable id keeps dependency rewrites
21
+ * single-valued before compaction.
22
+ * @param value Node id to resolve.
23
+ * @param aliases Alias table produced by the folding pass.
19
24
  * @returns Canonical node id for the current folding pass.
20
25
  */
21
26
  export declare function resolveAlias(value: NodeId, aliases: readonly NodeId[]): NodeId;
@@ -1 +1 @@
1
- {"version":3,"file":"rewrite.d.ts","sourceRoot":"","sources":["../../src/optimize/rewrite.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACP,MAAM,gBAAgB,CAAC;AAGxB;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,SAAS,EACf,OAAO,EAAE,SAAS,MAAM,EAAE,GACzB,SAAS,CAMX;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAW9E"}
1
+ {"version":3,"file":"rewrite.d.ts","sourceRoot":"","sources":["../../src/optimize/rewrite.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACR,SAAS,EACT,MAAM,EACT,MAAM,gBAAgB,CAAC;AAGxB;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC3B,IAAI,EAAE,SAAS,EACf,OAAO,EAAE,SAAS,MAAM,EAAE,GAC3B,SAAS,CAMX;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAW9E"}
@@ -1,23 +1,28 @@
1
1
  /**
2
2
  * @file rewrite.ts
3
3
  * @brief Node dependency rewrite utilities.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { mapNodeIds } from "./map-node.js";
6
8
  /**
7
- * @brief rewrite node deps.
8
- * @details Resolves dependency aliases while preserving the node's own id.
9
- * @param node Borrowed graph node to rewrite.
10
- * @param aliases Borrowed alias table produced by the folding pass.
9
+ * @brief Rewrite dependency ids through the current folding alias table.
10
+ * @details The node keeps its original id so the folding pass can continue a
11
+ * stable arena scan while all dependency edges point at canonical replacements.
12
+ * @param node Graph node to rewrite.
13
+ * @param aliases Alias table produced by the folding pass.
11
14
  * @returns Graph node with dependency ids rewritten through the alias table.
12
15
  */
13
16
  export function rewriteNodeDeps(node, aliases) {
14
17
  return mapNodeIds(node, (value) => resolveAlias(value, aliases), node.id);
15
18
  }
16
19
  /**
17
- * @brief resolve alias.
18
- * @details Follows an alias chain until it reaches a stable node id.
19
- * @param value Borrowed node id to resolve.
20
- * @param aliases Borrowed alias table.
20
+ * @brief Resolve one node id through a chain of folding aliases.
21
+ * @details Alias chains appear when a node folds to another node that later
22
+ * folds again. Resolving to the final stable id keeps dependency rewrites
23
+ * single-valued before compaction.
24
+ * @param value Node id to resolve.
25
+ * @param aliases Alias table produced by the folding pass.
21
26
  * @returns Canonical node id for the current folding pass.
22
27
  */
23
28
  export function resolveAlias(value, aliases) {
@@ -1,19 +1,25 @@
1
1
  /**
2
2
  * @file cache.ts
3
3
  * @brief Validation plan cache.
4
+ * @details Plan helpers keep schema-specialized execution aligned with optimized IR while
5
+ * preserving interpreter parity.
4
6
  */
5
7
  import type { Schema } from "../schema/index.js";
6
8
  import type { ValidationPlan } from "./types.js";
7
9
  /**
8
- * @brief make validation plan.
10
+ * @brief Build or reuse the optimized validation plan for a schema identity.
9
11
  * @details Lowers a schema into Sea-of-Nodes IR, runs the optimizer, and caches
10
- * the resulting graph for all runtime users of the schema.
12
+ * the resulting graph for all runtime users of the schema. Schema objects are
13
+ * immutable after construction, so identity is a stable cache key.
14
+ * @param schema Schema whose execution plan is requested.
11
15
  * @returns Cached or freshly built validation plan for the schema identity.
12
16
  */
13
17
  export declare function makeValidationPlan(schema: Schema): ValidationPlan;
14
18
  /**
15
- * @brief schema requires tracking.
19
+ * @brief Decide whether validation needs active schema/value pair tracking.
16
20
  * @details Returns true only for schemas that can recurse through `lazy`.
21
+ * Non-recursive schemas can skip the WeakMap bookkeeping on the hot path.
22
+ * @param schema Schema being analyzed.
17
23
  * @returns True when active schema/value pair tracking is required.
18
24
  */
19
25
  export declare function schemaRequiresTracking(schema: Schema): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/plan/cache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAgBjD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAYjE;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAQ9D"}
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/plan/cache.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAgBjD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAYjE;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAQ9D"}
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file cache.ts
3
3
  * @brief Validation plan cache.
4
+ * @details Plan helpers keep schema-specialized execution aligned with optimized IR while
5
+ * preserving interpreter parity.
4
6
  */
5
7
  import { lowerSchema } from "../lower/index.js";
6
8
  import { SchemaTag } from "../kind/index.js";
@@ -18,9 +20,11 @@ const planCache = new WeakMap();
18
20
  */
19
21
  const trackingCache = new WeakMap();
20
22
  /**
21
- * @brief make validation plan.
23
+ * @brief Build or reuse the optimized validation plan for a schema identity.
22
24
  * @details Lowers a schema into Sea-of-Nodes IR, runs the optimizer, and caches
23
- * the resulting graph for all runtime users of the schema.
25
+ * the resulting graph for all runtime users of the schema. Schema objects are
26
+ * immutable after construction, so identity is a stable cache key.
27
+ * @param schema Schema whose execution plan is requested.
24
28
  * @returns Cached or freshly built validation plan for the schema identity.
25
29
  */
26
30
  export function makeValidationPlan(schema) {
@@ -37,8 +41,10 @@ export function makeValidationPlan(schema) {
37
41
  return plan;
38
42
  }
39
43
  /**
40
- * @brief schema requires tracking.
44
+ * @brief Decide whether validation needs active schema/value pair tracking.
41
45
  * @details Returns true only for schemas that can recurse through `lazy`.
46
+ * Non-recursive schemas can skip the WeakMap bookkeeping on the hot path.
47
+ * @param schema Schema being analyzed.
42
48
  * @returns True when active schema/value pair tracking is required.
43
49
  */
44
50
  export function schemaRequiresTracking(schema) {
@@ -51,7 +57,12 @@ export function schemaRequiresTracking(schema) {
51
57
  return result;
52
58
  }
53
59
  /**
54
- * @brief schema requires tracking inner.
60
+ * @brief Walk a schema tree looking for lazy recursion boundaries.
61
+ * @details Plan helpers keep schema-specialized execution aligned with optimized IR while
62
+ * preserving interpreter parity.
63
+ * @param schema Current schema node.
64
+ * @param seen Schema identities already visited in this walk.
65
+ * @returns True when a reachable node can re-enter validation recursively.
55
66
  */
56
67
  function schemaRequiresTrackingInner(schema, seen) {
57
68
  if (seen.has(schema)) {
@@ -64,9 +75,19 @@ function schemaRequiresTrackingInner(schema, seen) {
64
75
  case SchemaTag.Array:
65
76
  return schemaRequiresTrackingInner(schema.item, seen);
66
77
  case SchemaTag.Tuple:
67
- return schemaArrayRequiresTracking(schema.items, seen);
78
+ return schemaArrayRequiresTracking(schema.items, seen) ||
79
+ (schema.rest !== undefined &&
80
+ schemaRequiresTrackingInner(schema.rest, seen));
68
81
  case SchemaTag.Record:
69
82
  return schemaRequiresTrackingInner(schema.value, seen);
83
+ case SchemaTag.Map:
84
+ return schemaRequiresTrackingInner(schema.key, seen) ||
85
+ schemaRequiresTrackingInner(schema.value, seen);
86
+ case SchemaTag.Set:
87
+ return schemaRequiresTrackingInner(schema.item, seen);
88
+ case SchemaTag.Property:
89
+ return schemaRequiresTrackingInner(schema.base, seen) ||
90
+ schemaRequiresTrackingInner(schema.value, seen);
70
91
  case SchemaTag.Object:
71
92
  for (let index = 0; index < schema.entries.length; index += 1) {
72
93
  const entry = schema.entries[index];
@@ -101,6 +122,8 @@ function schemaRequiresTrackingInner(schema, seen) {
101
122
  case SchemaTag.Never:
102
123
  case SchemaTag.String:
103
124
  case SchemaTag.Number:
125
+ case SchemaTag.Date:
126
+ case SchemaTag.InstanceOf:
104
127
  case SchemaTag.BigInt:
105
128
  case SchemaTag.Symbol:
106
129
  case SchemaTag.Boolean:
@@ -109,7 +132,12 @@ function schemaRequiresTrackingInner(schema, seen) {
109
132
  }
110
133
  }
111
134
  /**
112
- * @brief schema array requires tracking.
135
+ * @brief Scan a vector of child schemas for recursion tracking needs.
136
+ * @details Plan helpers keep schema-specialized execution aligned with optimized IR while
137
+ * preserving interpreter parity.
138
+ * @param schemas Child schema vector.
139
+ * @param seen Schema identities already visited in this walk.
140
+ * @returns True when at least one child requires active-pair tracking.
113
141
  */
114
142
  function schemaArrayRequiresTracking(schemas, seen) {
115
143
  for (let index = 0; index < schemas.length; index += 1) {
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file index.ts
3
3
  * @brief Validation plan module aggregation.
4
+ * @details This barrel keeps public import paths stable while implementation files remain
5
+ * split by responsibility.
4
6
  */
5
7
  export { makeValidationPlan, schemaRequiresTracking } from "./cache.js";
6
8
  export { executeGraphPredicate, executeSchemaPredicate, executeSchemaPredicateWithState } from "./predicate.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plan/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,+BAA+B,EAChC,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plan/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EACH,qBAAqB,EACrB,sBAAsB,EACtB,+BAA+B,EAClC,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file index.ts
3
3
  * @brief Validation plan module aggregation.
4
+ * @details This barrel keeps public import paths stable while implementation files remain
5
+ * split by responsibility.
4
6
  */
5
7
  export { makeValidationPlan, schemaRequiresTracking } from "./cache.js";
6
8
  export { executeGraphPredicate, executeSchemaPredicate, executeSchemaPredicateWithState } from "./predicate.js";
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file predicate.ts
3
3
  * @brief Sea-of-Nodes predicate executor.
4
+ * @details Plan helpers keep schema-specialized execution aligned with optimized IR while
5
+ * preserving interpreter parity.
4
6
  */
5
7
  import type { Graph } from "../ir/index.js";
6
8
  import { type Schema } from "../schema/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"predicate.d.ts","sourceRoot":"","sources":["../../src/plan/predicate.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,KAAK,EAGN,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEL,KAAK,MAAM,EACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAKL,KAAK,eAAe,EACrB,MAAM,sBAAsB,CAAC;AAI9B;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,GACb,OAAO,CAET;AAED;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,eAAe,GACrB,OAAO,CAcT;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,eAAe,GACrB,OAAO,CAaT"}
1
+ {"version":3,"file":"predicate.d.ts","sourceRoot":"","sources":["../../src/plan/predicate.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,OAAO,KAAK,EACR,KAAK,EAIR,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAIH,KAAK,MAAM,EACd,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAKH,KAAK,eAAe,EACvB,MAAM,sBAAsB,CAAC;AAI9B;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAClC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,GACf,OAAO,CAET;AAED;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC3C,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,eAAe,GACvB,OAAO,CAcT;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACjC,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,eAAe,GACvB,OAAO,CAaT"}