typesea 0.1.0 → 0.2.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 (285) hide show
  1. package/CHANGELOG.md +67 -6
  2. package/README.md +98 -17
  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 +18 -2
  7. package/dist/aot/index.d.ts.map +1 -1
  8. package/dist/aot/index.js +93 -14
  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 +37 -6
  13. package/dist/builders/composite.d.ts.map +1 -1
  14. package/dist/builders/composite.js +84 -10
  15. package/dist/builders/index.d.ts +2 -0
  16. package/dist/builders/index.d.ts.map +1 -1
  17. package/dist/builders/index.js +2 -0
  18. package/dist/builders/modifier.d.ts +30 -5
  19. package/dist/builders/modifier.d.ts.map +1 -1
  20. package/dist/builders/modifier.js +38 -5
  21. package/dist/builders/object/guard.d.ts +18 -22
  22. package/dist/builders/object/guard.d.ts.map +1 -1
  23. package/dist/builders/object/guard.js +26 -26
  24. package/dist/builders/object/index.d.ts +2 -0
  25. package/dist/builders/object/index.d.ts.map +1 -1
  26. package/dist/builders/object/index.js +2 -0
  27. package/dist/builders/object/schema.d.ts +55 -9
  28. package/dist/builders/object/schema.d.ts.map +1 -1
  29. package/dist/builders/object/schema.js +92 -15
  30. package/dist/builders/object/types.d.ts +5 -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/scalar.d.ts +29 -8
  34. package/dist/builders/scalar.d.ts.map +1 -1
  35. package/dist/builders/scalar.js +33 -8
  36. package/dist/builders/table.d.ts +4 -0
  37. package/dist/builders/table.d.ts.map +1 -1
  38. package/dist/builders/table.js +4 -0
  39. package/dist/builders/types.d.ts +14 -4
  40. package/dist/builders/types.d.ts.map +1 -1
  41. package/dist/builders/types.js +2 -0
  42. package/dist/compile/check-composite.d.ts +22 -1
  43. package/dist/compile/check-composite.d.ts.map +1 -1
  44. package/dist/compile/check-composite.js +564 -24
  45. package/dist/compile/check-scalar.d.ts +78 -0
  46. package/dist/compile/check-scalar.d.ts.map +1 -1
  47. package/dist/compile/check-scalar.js +432 -1
  48. package/dist/compile/check.d.ts +12 -0
  49. package/dist/compile/check.d.ts.map +1 -1
  50. package/dist/compile/check.js +37 -0
  51. package/dist/compile/context.d.ts +47 -9
  52. package/dist/compile/context.d.ts.map +1 -1
  53. package/dist/compile/context.js +51 -8
  54. package/dist/compile/graph-predicate.d.ts +4 -2
  55. package/dist/compile/graph-predicate.d.ts.map +1 -1
  56. package/dist/compile/graph-predicate.js +1907 -171
  57. package/dist/compile/guard.d.ts +15 -24
  58. package/dist/compile/guard.d.ts.map +1 -1
  59. package/dist/compile/guard.js +158 -74
  60. package/dist/compile/index.d.ts +3 -1
  61. package/dist/compile/index.d.ts.map +1 -1
  62. package/dist/compile/index.js +2 -0
  63. package/dist/compile/issue.d.ts +110 -0
  64. package/dist/compile/issue.d.ts.map +1 -1
  65. package/dist/compile/issue.js +184 -1
  66. package/dist/compile/names.d.ts +12 -2
  67. package/dist/compile/names.d.ts.map +1 -1
  68. package/dist/compile/names.js +19 -3
  69. package/dist/compile/predicate.d.ts +24 -0
  70. package/dist/compile/predicate.d.ts.map +1 -1
  71. package/dist/compile/predicate.js +131 -5
  72. package/dist/compile/runtime.d.ts +80 -12
  73. package/dist/compile/runtime.d.ts.map +1 -1
  74. package/dist/compile/runtime.js +25 -6
  75. package/dist/compile/source.d.ts +10 -2
  76. package/dist/compile/source.d.ts.map +1 -1
  77. package/dist/compile/source.js +361 -26
  78. package/dist/compile/types.d.ts +20 -0
  79. package/dist/compile/types.d.ts.map +1 -1
  80. package/dist/compile/types.js +2 -0
  81. package/dist/decoder/index.d.ts +32 -46
  82. package/dist/decoder/index.d.ts.map +1 -1
  83. package/dist/decoder/index.js +102 -38
  84. package/dist/evaluate/check-composite.d.ts +59 -0
  85. package/dist/evaluate/check-composite.d.ts.map +1 -1
  86. package/dist/evaluate/check-composite.js +151 -3
  87. package/dist/evaluate/check-scalar.d.ts +16 -0
  88. package/dist/evaluate/check-scalar.d.ts.map +1 -1
  89. package/dist/evaluate/check-scalar.js +32 -0
  90. package/dist/evaluate/check.d.ts +7 -0
  91. package/dist/evaluate/check.d.ts.map +1 -1
  92. package/dist/evaluate/check.js +43 -0
  93. package/dist/evaluate/index.d.ts +2 -0
  94. package/dist/evaluate/index.d.ts.map +1 -1
  95. package/dist/evaluate/index.js +2 -0
  96. package/dist/evaluate/issue.d.ts +11 -1
  97. package/dist/evaluate/issue.d.ts.map +1 -1
  98. package/dist/evaluate/issue.js +15 -1
  99. package/dist/evaluate/predicate.d.ts +16 -5
  100. package/dist/evaluate/predicate.d.ts.map +1 -1
  101. package/dist/evaluate/predicate.js +20 -5
  102. package/dist/evaluate/shared.d.ts +59 -13
  103. package/dist/evaluate/shared.d.ts.map +1 -1
  104. package/dist/evaluate/shared.js +66 -8
  105. package/dist/evaluate/state.d.ts +35 -13
  106. package/dist/evaluate/state.d.ts.map +1 -1
  107. package/dist/evaluate/state.js +35 -2
  108. package/dist/guard/base.d.ts +79 -29
  109. package/dist/guard/base.d.ts.map +1 -1
  110. package/dist/guard/base.js +91 -29
  111. package/dist/guard/error.d.ts +10 -5
  112. package/dist/guard/error.d.ts.map +1 -1
  113. package/dist/guard/error.js +10 -5
  114. package/dist/guard/index.d.ts +2 -0
  115. package/dist/guard/index.d.ts.map +1 -1
  116. package/dist/guard/index.js +2 -0
  117. package/dist/guard/number.d.ts +26 -11
  118. package/dist/guard/number.d.ts.map +1 -1
  119. package/dist/guard/number.js +30 -11
  120. package/dist/guard/props.d.ts +27 -3
  121. package/dist/guard/props.d.ts.map +1 -1
  122. package/dist/guard/props.js +27 -3
  123. package/dist/guard/read.d.ts +62 -9
  124. package/dist/guard/read.d.ts.map +1 -1
  125. package/dist/guard/read.js +83 -10
  126. package/dist/guard/registry.d.ts +12 -2
  127. package/dist/guard/registry.d.ts.map +1 -1
  128. package/dist/guard/registry.js +15 -3
  129. package/dist/guard/string.d.ts +33 -13
  130. package/dist/guard/string.d.ts.map +1 -1
  131. package/dist/guard/string.js +37 -13
  132. package/dist/guard/types.d.ts +92 -40
  133. package/dist/guard/types.d.ts.map +1 -1
  134. package/dist/guard/types.js +2 -0
  135. package/dist/index.d.ts +1 -1
  136. package/dist/index.d.ts.map +1 -1
  137. package/dist/internal/index.d.ts +42 -6
  138. package/dist/internal/index.d.ts.map +1 -1
  139. package/dist/internal/index.js +51 -8
  140. package/dist/ir/builder.d.ts +16 -126
  141. package/dist/ir/builder.d.ts.map +1 -1
  142. package/dist/ir/builder.js +77 -137
  143. package/dist/ir/freeze.d.ts +4 -0
  144. package/dist/ir/freeze.d.ts.map +1 -1
  145. package/dist/ir/freeze.js +59 -0
  146. package/dist/ir/index.d.ts +3 -1
  147. package/dist/ir/index.d.ts.map +1 -1
  148. package/dist/ir/index.js +2 -0
  149. package/dist/ir/regexp.d.ts +2 -0
  150. package/dist/ir/regexp.d.ts.map +1 -1
  151. package/dist/ir/regexp.js +2 -0
  152. package/dist/ir/types.d.ts +90 -55
  153. package/dist/ir/types.d.ts.map +1 -1
  154. package/dist/ir/types.js +2 -0
  155. package/dist/ir/validate.d.ts +8 -1
  156. package/dist/ir/validate.d.ts.map +1 -1
  157. package/dist/ir/validate.js +477 -61
  158. package/dist/issue/index.d.ts +41 -9
  159. package/dist/issue/index.d.ts.map +1 -1
  160. package/dist/issue/index.js +61 -11
  161. package/dist/json-schema/emit-combinator.d.ts +44 -4
  162. package/dist/json-schema/emit-combinator.d.ts.map +1 -1
  163. package/dist/json-schema/emit-combinator.js +44 -4
  164. package/dist/json-schema/emit-composite.d.ts +10 -0
  165. package/dist/json-schema/emit-composite.d.ts.map +1 -1
  166. package/dist/json-schema/emit-composite.js +15 -1
  167. package/dist/json-schema/emit-scalar.d.ts +26 -3
  168. package/dist/json-schema/emit-scalar.d.ts.map +1 -1
  169. package/dist/json-schema/emit-scalar.js +70 -9
  170. package/dist/json-schema/emit-types.d.ts +11 -1
  171. package/dist/json-schema/emit-types.d.ts.map +1 -1
  172. package/dist/json-schema/emit-types.js +2 -0
  173. package/dist/json-schema/emit.d.ts +12 -1
  174. package/dist/json-schema/emit.d.ts.map +1 -1
  175. package/dist/json-schema/emit.js +12 -1
  176. package/dist/json-schema/freeze.d.ts +13 -2
  177. package/dist/json-schema/freeze.d.ts.map +1 -1
  178. package/dist/json-schema/freeze.js +41 -8
  179. package/dist/json-schema/index.d.ts +16 -2
  180. package/dist/json-schema/index.d.ts.map +1 -1
  181. package/dist/json-schema/index.js +23 -3
  182. package/dist/json-schema/issue.d.ts +4 -1
  183. package/dist/json-schema/issue.d.ts.map +1 -1
  184. package/dist/json-schema/issue.js +4 -1
  185. package/dist/json-schema/read.d.ts +24 -3
  186. package/dist/json-schema/read.d.ts.map +1 -1
  187. package/dist/json-schema/read.js +59 -12
  188. package/dist/json-schema/types.d.ts +38 -15
  189. package/dist/json-schema/types.d.ts.map +1 -1
  190. package/dist/json-schema/types.js +2 -0
  191. package/dist/kind/index.d.ts +15 -28
  192. package/dist/kind/index.d.ts.map +1 -1
  193. package/dist/kind/index.js +15 -10
  194. package/dist/lower/index.d.ts +6 -1
  195. package/dist/lower/index.d.ts.map +1 -1
  196. package/dist/lower/index.js +411 -44
  197. package/dist/message/index.d.ts +46 -10
  198. package/dist/message/index.d.ts.map +1 -1
  199. package/dist/message/index.js +88 -17
  200. package/dist/optimize/algebraic.d.ts +54 -0
  201. package/dist/optimize/algebraic.d.ts.map +1 -0
  202. package/dist/optimize/algebraic.js +314 -0
  203. package/dist/optimize/compact.d.ts +8 -1
  204. package/dist/optimize/compact.d.ts.map +1 -1
  205. package/dist/optimize/compact.js +13 -2
  206. package/dist/optimize/domain.d.ts +16 -0
  207. package/dist/optimize/domain.d.ts.map +1 -0
  208. package/dist/optimize/domain.js +615 -0
  209. package/dist/optimize/fold-boolean.d.ts +17 -2
  210. package/dist/optimize/fold-boolean.d.ts.map +1 -1
  211. package/dist/optimize/fold-boolean.js +59 -14
  212. package/dist/optimize/fold-common.d.ts +43 -8
  213. package/dist/optimize/fold-common.d.ts.map +1 -1
  214. package/dist/optimize/fold-common.js +37 -6
  215. package/dist/optimize/fold-constraints.d.ts +33 -0
  216. package/dist/optimize/fold-constraints.d.ts.map +1 -0
  217. package/dist/optimize/fold-constraints.js +484 -0
  218. package/dist/optimize/fold-scalar.d.ts +98 -13
  219. package/dist/optimize/fold-scalar.d.ts.map +1 -1
  220. package/dist/optimize/fold-scalar.js +98 -13
  221. package/dist/optimize/fold.d.ts +8 -1
  222. package/dist/optimize/fold.d.ts.map +1 -1
  223. package/dist/optimize/fold.js +22 -2
  224. package/dist/optimize/index.d.ts +9 -1
  225. package/dist/optimize/index.d.ts.map +1 -1
  226. package/dist/optimize/index.js +18 -3
  227. package/dist/optimize/map-node.d.ts +3 -1
  228. package/dist/optimize/map-node.d.ts.map +1 -1
  229. package/dist/optimize/map-node.js +45 -3
  230. package/dist/optimize/peephole.d.ts +16 -0
  231. package/dist/optimize/peephole.d.ts.map +1 -0
  232. package/dist/optimize/peephole.js +254 -0
  233. package/dist/optimize/remap.d.ts +2 -0
  234. package/dist/optimize/remap.d.ts.map +1 -1
  235. package/dist/optimize/remap.js +2 -0
  236. package/dist/optimize/rewrite.d.ts +13 -8
  237. package/dist/optimize/rewrite.d.ts.map +1 -1
  238. package/dist/optimize/rewrite.js +13 -8
  239. package/dist/plan/cache.d.ts +9 -3
  240. package/dist/plan/cache.d.ts.map +1 -1
  241. package/dist/plan/cache.js +21 -5
  242. package/dist/plan/index.d.ts +2 -0
  243. package/dist/plan/index.d.ts.map +1 -1
  244. package/dist/plan/index.js +2 -0
  245. package/dist/plan/predicate.d.ts +2 -0
  246. package/dist/plan/predicate.d.ts.map +1 -1
  247. package/dist/plan/predicate.js +268 -29
  248. package/dist/plan/schema-predicate.d.ts +6 -0
  249. package/dist/plan/schema-predicate.d.ts.map +1 -1
  250. package/dist/plan/schema-predicate.js +117 -13
  251. package/dist/plan/types.d.ts +2 -0
  252. package/dist/plan/types.d.ts.map +1 -1
  253. package/dist/plan/types.js +2 -0
  254. package/dist/result/index.d.ts +19 -5
  255. package/dist/result/index.d.ts.map +1 -1
  256. package/dist/result/index.js +10 -2
  257. package/dist/schema/common.d.ts +69 -6
  258. package/dist/schema/common.d.ts.map +1 -1
  259. package/dist/schema/common.js +104 -10
  260. package/dist/schema/freeze.d.ts +4 -0
  261. package/dist/schema/freeze.d.ts.map +1 -1
  262. package/dist/schema/freeze.js +18 -0
  263. package/dist/schema/index.d.ts +3 -0
  264. package/dist/schema/index.d.ts.map +1 -1
  265. package/dist/schema/index.js +3 -0
  266. package/dist/schema/lazy.d.ts +4 -0
  267. package/dist/schema/lazy.d.ts.map +1 -1
  268. package/dist/schema/lazy.js +4 -0
  269. package/dist/schema/literal.d.ts +7 -1
  270. package/dist/schema/literal.d.ts.map +1 -1
  271. package/dist/schema/literal.js +7 -1
  272. package/dist/schema/types.d.ts +20 -96
  273. package/dist/schema/types.d.ts.map +1 -1
  274. package/dist/schema/types.js +5 -1
  275. package/dist/schema/undefined.d.ts +17 -0
  276. package/dist/schema/undefined.d.ts.map +1 -0
  277. package/dist/schema/undefined.js +72 -0
  278. package/dist/schema/validate.d.ts +8 -1
  279. package/dist/schema/validate.d.ts.map +1 -1
  280. package/dist/schema/validate.js +146 -55
  281. package/docs/api.md +57 -0
  282. package/docs/assets/benchmark-headline.svg +163 -0
  283. package/docs/engine-notes.md +58 -15
  284. package/docs/index.html +130 -110
  285. package/package.json +65 -65
@@ -1,11 +1,20 @@
1
1
  /**
2
2
  * @file fold-scalar.ts
3
3
  * @brief Constant folding for scalar graph nodes.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { NodeTag } from "../kind/index.js";
6
8
  import { ensureConst, isFiniteNumber, isPlainRecord, keep, readConst, replace } from "./fold-common.js";
7
9
  /**
8
- * @brief fold get prop.
10
+ * @brief Fold property reads whose receiver is already a literal constant.
11
+ * @details Lowered graphs never materialize object literals as safe Const data,
12
+ * so a constant receiver cannot provide a trustworthy property slot. The node
13
+ * folds to undefined instead of executing user accessors during optimization.
14
+ * @param node Property-read node to fold.
15
+ * @param nodes Mutable graph node table for constant interning.
16
+ * @param aliases Mutable alias table updated when a constant is inserted.
17
+ * @returns Fold result for the property read.
9
18
  */
10
19
  export function foldGetProp(node, nodes, aliases) {
11
20
  const value = readConst(nodes, node.object);
@@ -15,7 +24,14 @@ export function foldGetProp(node, nodes, aliases) {
15
24
  return keep(node);
16
25
  }
17
26
  /**
18
- * @brief fold unary.
27
+ * @brief Fold unary predicate nodes when their subject is constant.
28
+ * @details Each predicate mirrors the runtime validator's exact primitive test,
29
+ * including finite-number and plain-record semantics, so compile-time folding
30
+ * cannot widen the accepted value set.
31
+ * @param node Unary predicate node to fold.
32
+ * @param nodes Mutable graph node table for constant interning.
33
+ * @param aliases Mutable alias table updated when a constant is inserted.
34
+ * @returns Fold result for the predicate.
19
35
  */
20
36
  export function foldUnary(node, nodes, aliases) {
21
37
  const value = readConst(nodes, node.value);
@@ -48,7 +64,13 @@ export function foldUnary(node, nodes, aliases) {
48
64
  }
49
65
  }
50
66
  /**
51
- * @brief fold equals.
67
+ * @brief Fold literal equality when both operands are constants.
68
+ * @details `Object.is` is used so NaN and negative zero follow TypeSea literal
69
+ * equality semantics instead of JavaScript `===` corner cases.
70
+ * @param node Equality node to fold.
71
+ * @param nodes Mutable graph node table for constant interning.
72
+ * @param aliases Mutable alias table updated when a constant is inserted.
73
+ * @returns Fold result for the equality node.
52
74
  */
53
75
  export function foldEquals(node, nodes, aliases) {
54
76
  const left = readConst(nodes, node.left);
@@ -59,7 +81,13 @@ export function foldEquals(node, nodes, aliases) {
59
81
  return replace(node, ensureConst(nodes, aliases, Object.is(left.value, right.value)));
60
82
  }
61
83
  /**
62
- * @brief fold numeric.
84
+ * @brief Fold numeric comparisons with two constant operands.
85
+ * @details Non-number operands fold to false, matching the runtime predicate
86
+ * path where numeric schema checks require number values before comparison.
87
+ * @param node Numeric comparison node to fold.
88
+ * @param nodes Mutable graph node table for constant interning.
89
+ * @param aliases Mutable alias table updated when a constant is inserted.
90
+ * @returns Fold result for the comparison node.
63
91
  */
64
92
  export function foldNumeric(node, nodes, aliases) {
65
93
  const left = readConst(nodes, node.left);
@@ -74,7 +102,13 @@ export function foldNumeric(node, nodes, aliases) {
74
102
  return replace(node, ensureConst(nodes, aliases, result));
75
103
  }
76
104
  /**
77
- * @brief fold string bound.
105
+ * @brief Fold string length predicates with a constant subject.
106
+ * @details Non-string subjects fold to false because the separate string guard
107
+ * may later be removed or reordered by boolean simplification.
108
+ * @param node String bound node to fold.
109
+ * @param nodes Mutable graph node table for constant interning.
110
+ * @param aliases Mutable alias table updated when a constant is inserted.
111
+ * @returns Fold result for the string bound node.
78
112
  */
79
113
  export function foldStringBound(node, nodes, aliases) {
80
114
  const value = readConst(nodes, node.value);
@@ -87,7 +121,13 @@ export function foldStringBound(node, nodes, aliases) {
87
121
  return replace(node, ensureConst(nodes, aliases, result));
88
122
  }
89
123
  /**
90
- * @brief fold regex.
124
+ * @brief Fold regular expression checks with a constant string subject.
125
+ * @details `lastIndex` is reset before and after the test so global or sticky
126
+ * expressions cannot leak state across optimizer and runtime use.
127
+ * @param node Regex node to fold.
128
+ * @param nodes Mutable graph node table for constant interning.
129
+ * @param aliases Mutable alias table updated when a constant is inserted.
130
+ * @returns Fold result for the regex node.
91
131
  */
92
132
  export function foldRegex(node, nodes, aliases) {
93
133
  const value = readConst(nodes, node.value);
@@ -103,7 +143,14 @@ export function foldRegex(node, nodes, aliases) {
103
143
  return replace(node, ensureConst(nodes, aliases, result));
104
144
  }
105
145
  /**
106
- * @brief fold has own.
146
+ * @brief Fold own-property presence checks on constant receivers.
147
+ * @details Const nodes do not carry safe object identity, so the optimizer never
148
+ * inspects properties here. A constant receiver means the object check cannot be
149
+ * satisfied through this IR path and folds to false.
150
+ * @param node Own-property check node to fold.
151
+ * @param nodes Mutable graph node table for constant interning.
152
+ * @param aliases Mutable alias table updated when a constant is inserted.
153
+ * @returns Fold result for the own-property check.
107
154
  */
108
155
  export function foldHasOwn(node, nodes, aliases) {
109
156
  const value = readConst(nodes, node.object);
@@ -113,7 +160,13 @@ export function foldHasOwn(node, nodes, aliases) {
113
160
  return keep(node);
114
161
  }
115
162
  /**
116
- * @brief fold has own data.
163
+ * @brief Fold own-data-property checks on constant receivers.
164
+ * @details The optimizer refuses to execute descriptors or getters. Constant
165
+ * receivers therefore fold to false instead of probing runtime object state.
166
+ * @param node Own-data-property check node to fold.
167
+ * @param nodes Mutable graph node table for constant interning.
168
+ * @param aliases Mutable alias table updated when a constant is inserted.
169
+ * @returns Fold result for the data-property check.
117
170
  */
118
171
  export function foldHasOwnData(node, nodes, aliases) {
119
172
  const value = readConst(nodes, node.object);
@@ -123,7 +176,14 @@ export function foldHasOwnData(node, nodes, aliases) {
123
176
  return keep(node);
124
177
  }
125
178
  /**
126
- * @brief fold strict keys.
179
+ * @brief Fold strict-key checks on constant receivers.
180
+ * @details Strict-key validation depends on own property keys. The optimizer
181
+ * does not enumerate runtime objects, so constant receivers fold to false and
182
+ * non-constant receivers stay intact.
183
+ * @param node Strict-key node to fold.
184
+ * @param nodes Mutable graph node table for constant interning.
185
+ * @param aliases Mutable alias table updated when a constant is inserted.
186
+ * @returns Fold result for the strict-key node.
127
187
  */
128
188
  export function foldStrictKeys(node, nodes, aliases) {
129
189
  const value = readConst(nodes, node.object);
@@ -133,7 +193,14 @@ export function foldStrictKeys(node, nodes, aliases) {
133
193
  return keep(node);
134
194
  }
135
195
  /**
136
- * @brief fold array every.
196
+ * @brief Fold array iteration nodes with constant receivers.
197
+ * @details Iteration semantics require reading runtime indexes and child
198
+ * schemas. Constants cannot safely stand in for arrays here, so the node folds
199
+ * to false only when the receiver is already constant.
200
+ * @param node Array iteration node to fold.
201
+ * @param nodes Mutable graph node table for constant interning.
202
+ * @param aliases Mutable alias table updated when a constant is inserted.
203
+ * @returns Fold result for the array iteration node.
137
204
  */
138
205
  export function foldArrayEvery(node, nodes, aliases) {
139
206
  const value = readConst(nodes, node.value);
@@ -143,7 +210,13 @@ export function foldArrayEvery(node, nodes, aliases) {
143
210
  return keep(node);
144
211
  }
145
212
  /**
146
- * @brief fold tuple items.
213
+ * @brief Fold tuple item checks with constant receivers.
214
+ * @details Tuple validation depends on array length and descriptor-backed item
215
+ * reads, so the optimizer does not simulate it for constant receivers.
216
+ * @param node Tuple item node to fold.
217
+ * @param nodes Mutable graph node table for constant interning.
218
+ * @param aliases Mutable alias table updated when a constant is inserted.
219
+ * @returns Fold result for the tuple item node.
147
220
  */
148
221
  export function foldTupleItems(node, nodes, aliases) {
149
222
  const value = readConst(nodes, node.value);
@@ -153,7 +226,13 @@ export function foldTupleItems(node, nodes, aliases) {
153
226
  return keep(node);
154
227
  }
155
228
  /**
156
- * @brief fold record every.
229
+ * @brief Fold record iteration nodes with constant receivers.
230
+ * @details Record validation must enumerate own keys at runtime. The optimizer
231
+ * keeps non-constant records intact and folds constant receivers closed.
232
+ * @param node Record iteration node to fold.
233
+ * @param nodes Mutable graph node table for constant interning.
234
+ * @param aliases Mutable alias table updated when a constant is inserted.
235
+ * @returns Fold result for the record iteration node.
157
236
  */
158
237
  export function foldRecordEvery(node, nodes, aliases) {
159
238
  const value = readConst(nodes, node.value);
@@ -163,7 +242,13 @@ export function foldRecordEvery(node, nodes, aliases) {
163
242
  return keep(node);
164
243
  }
165
244
  /**
166
- * @brief fold discriminant dispatch.
245
+ * @brief Fold discriminant dispatch nodes with constant receivers.
246
+ * @details Dispatch requires an object tag read, which the optimizer does not
247
+ * perform on constants. Non-constant receivers remain available for codegen.
248
+ * @param node Discriminant dispatch node to fold.
249
+ * @param nodes Mutable graph node table for constant interning.
250
+ * @param aliases Mutable alias table updated when a constant is inserted.
251
+ * @returns Fold result for the dispatch node.
167
252
  */
168
253
  export function foldDiscriminantDispatch(node, nodes, aliases) {
169
254
  const value = readConst(nodes, node.value);
@@ -1,10 +1,17 @@
1
1
  /**
2
2
  * @file fold.ts
3
3
  * @brief Constant folding pass driver.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import type { Graph } from "../ir/index.js";
6
8
  /**
7
- * @brief fold constants.
9
+ * @brief Run the constant and local simplification pass over one graph.
10
+ * @details The pass keeps an alias table instead of deleting nodes mid-scan.
11
+ * That gives every later node a stable source id while dependencies are
12
+ * rewritten to their canonical replacements.
13
+ * @param graph Input graph to simplify.
14
+ * @returns Graph with folded nodes and resolved dependency aliases.
8
15
  */
9
16
  export declare function foldConstants(graph: Graph): Graph;
10
17
  //# sourceMappingURL=fold.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/optimize/fold.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,KAAK,EAGN,MAAM,gBAAgB,CAAC;AAoBxB;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAsCjD"}
1
+ {"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/optimize/fold.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EACR,KAAK,EAGR,MAAM,gBAAgB,CAAC;AAoBxB;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAsCjD"}
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file fold.ts
3
3
  * @brief Constant folding pass driver.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { NodeTag } from "../kind/index.js";
6
8
  import { foldAnd, foldOr } from "./fold-boolean.js";
@@ -8,7 +10,12 @@ import { keep } from "./fold-common.js";
8
10
  import { foldArrayEvery, foldDiscriminantDispatch, foldEquals, foldGetProp, foldHasOwn, foldHasOwnData, foldNumeric, foldRecordEvery, foldRegex, foldStrictKeys, foldStringBound, foldTupleItems, foldUnary } from "./fold-scalar.js";
9
11
  import { resolveAlias, rewriteNodeDeps } from "./rewrite.js";
10
12
  /**
11
- * @brief fold constants.
13
+ * @brief Run the constant and local simplification pass over one graph.
14
+ * @details The pass keeps an alias table instead of deleting nodes mid-scan.
15
+ * That gives every later node a stable source id while dependencies are
16
+ * rewritten to their canonical replacements.
17
+ * @param graph Input graph to simplify.
18
+ * @returns Graph with folded nodes and resolved dependency aliases.
12
19
  */
13
20
  export function foldConstants(graph) {
14
21
  const originalLength = graph.nodes.length;
@@ -46,7 +53,14 @@ export function foldConstants(graph) {
46
53
  };
47
54
  }
48
55
  /**
49
- * @brief fold node.
56
+ * @brief Dispatch one graph node to the local folding rule for its tag.
57
+ * @details Structural nodes that cannot be simplified locally are kept intact.
58
+ * Boolean nodes delegate to algebraic and constraint passes because their
59
+ * simplification depends on the whole value vector.
60
+ * @param node Node whose dependencies have already been rewritten.
61
+ * @param nodes Mutable node table for this folding pass.
62
+ * @param aliases Mutable alias table for replacement nodes.
63
+ * @returns Fold result containing the updated node and optional alias.
50
64
  */
51
65
  function foldNode(node, nodes, aliases) {
52
66
  switch (node.tag) {
@@ -93,6 +107,12 @@ function foldNode(node, nodes, aliases) {
93
107
  return foldRecordEvery(node, nodes, aliases);
94
108
  case NodeTag.DiscriminantDispatch:
95
109
  return foldDiscriminantDispatch(node, nodes, aliases);
110
+ case NodeTag.ObjectShape:
111
+ return keep(node);
112
+ case NodeTag.UnionDispatch:
113
+ return keep(node);
114
+ case NodeTag.PrimitiveUnion:
115
+ return keep(node);
96
116
  case NodeTag.SchemaCheck:
97
117
  return keep(node);
98
118
  case NodeTag.And:
@@ -1,10 +1,18 @@
1
1
  /**
2
2
  * @file index.ts
3
3
  * @brief Public graph optimizer pipeline.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { type Graph } from "../ir/index.js";
6
8
  /**
7
- * @brief optimize graph.
9
+ * @brief Run the public graph optimization pipeline.
10
+ * @details Pass order is intentionally conservative: constant folding exposes
11
+ * local identities, peephole rewriting removes adjacent graph noise, domain
12
+ * specialization reasons about repeated validation loops, and compaction drops
13
+ * nodes made unreachable by earlier replacements.
14
+ * @param graph Frozen or mutable TypeSea graph to optimize.
15
+ * @returns Frozen graph after all optimizer passes have completed.
8
16
  */
9
17
  export declare function optimizeGraph(graph: Graph): Graph;
10
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/optimize/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAGL,KAAK,KAAK,EACX,MAAM,gBAAgB,CAAC;AAIxB;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAGjD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/optimize/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAGH,KAAK,KAAK,EACb,MAAM,gBAAgB,CAAC;AAMxB;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAGjD"}
@@ -1,19 +1,34 @@
1
1
  /**
2
2
  * @file index.ts
3
3
  * @brief Public graph optimizer pipeline.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { freezeGraph, isGraphValue } from "../ir/index.js";
6
8
  import { compactReachable } from "./compact.js";
9
+ import { specializeDomains } from "./domain.js";
7
10
  import { foldConstants } from "./fold.js";
11
+ import { peepholeGraph } from "./peephole.js";
8
12
  /**
9
- * @brief optimize graph.
13
+ * @brief Run the public graph optimization pipeline.
14
+ * @details Pass order is intentionally conservative: constant folding exposes
15
+ * local identities, peephole rewriting removes adjacent graph noise, domain
16
+ * specialization reasons about repeated validation loops, and compaction drops
17
+ * nodes made unreachable by earlier replacements.
18
+ * @param graph Frozen or mutable TypeSea graph to optimize.
19
+ * @returns Frozen graph after all optimizer passes have completed.
10
20
  */
11
21
  export function optimizeGraph(graph) {
12
22
  const input = readGraph(graph);
13
- return freezeGraph(compactReachable(foldConstants(input)));
23
+ return freezeGraph(compactReachable(specializeDomains(peepholeGraph(foldConstants(input)))));
14
24
  }
15
25
  /**
16
- * @brief read graph.
26
+ * @brief Validate optimizer input before mutating pass-local copies.
27
+ * @details Public callers may pass arbitrary values through JavaScript module
28
+ * boundaries. The optimizer fails at the edge rather than letting malformed IR
29
+ * reach passes that assume node ids and dependency arrays are well formed.
30
+ * @param value Candidate graph value.
31
+ * @returns Frozen graph value accepted by the IR validator.
17
32
  */
18
33
  function readGraph(value) {
19
34
  if (!isGraphValue(value)) {
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * @file map-node.ts
3
3
  * @brief Shared graph node-id mapping utility.
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 node id mapper.
9
+ * @brief Rewrite callback for arena-local node ids.
8
10
  * @details Converts one graph node id into the id visible in a rewritten graph.
9
11
  * @invariant The caller owns range validation for the returned id.
10
12
  */
@@ -1 +1 @@
1
- {"version":3,"file":"map-node.d.ts","sourceRoot":"","sources":["../../src/optimize/map-node.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,SAAS,EACT,MAAM,EACP,MAAM,gBAAgB,CAAC;AAExB;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;AAErD;;;;;;;GAOG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,YAAY,EACnB,EAAE,EAAE,MAAM,GACT,SAAS,CA+LX"}
1
+ {"version":3,"file":"map-node.d.ts","sourceRoot":"","sources":["../../src/optimize/map-node.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EACR,SAAS,EACT,MAAM,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;AAErD;;;;;;;GAOG;AACH,wBAAgB,UAAU,CACtB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,YAAY,EACnB,EAAE,EAAE,MAAM,GACX,SAAS,CAuOX"}
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @file map-node.ts
3
3
  * @brief Shared graph node-id mapping utility.
4
+ * @details Optimizer helpers preserve graph equivalence while shrinking redundant nodes
5
+ * before code generation consumes the graph.
4
6
  */
5
7
  import { NodeTag } from "../kind/index.js";
6
8
  /**
@@ -134,7 +136,8 @@ export function mapNodeIds(node, mapId, id) {
134
136
  tag: node.tag,
135
137
  deps: [value],
136
138
  value,
137
- item: node.item
139
+ item: node.item,
140
+ itemGraph: node.itemGraph
138
141
  };
139
142
  }
140
143
  case NodeTag.TupleItems: {
@@ -144,7 +147,8 @@ export function mapNodeIds(node, mapId, id) {
144
147
  tag: node.tag,
145
148
  deps: [value],
146
149
  value,
147
- items: node.items
150
+ items: node.items,
151
+ itemGraphs: node.itemGraphs
148
152
  };
149
153
  }
150
154
  case NodeTag.RecordEvery: {
@@ -154,7 +158,8 @@ export function mapNodeIds(node, mapId, id) {
154
158
  tag: node.tag,
155
159
  deps: [value],
156
160
  value,
157
- item: node.item
161
+ item: node.item,
162
+ itemGraph: node.itemGraph
158
163
  };
159
164
  }
160
165
  case NodeTag.DiscriminantDispatch: {
@@ -167,9 +172,46 @@ export function mapNodeIds(node, mapId, id) {
167
172
  key: node.key,
168
173
  literals: node.literals,
169
174
  schemas: node.schemas,
175
+ graphs: node.graphs,
170
176
  lookup: node.lookup
171
177
  };
172
178
  }
179
+ case NodeTag.ObjectShape: {
180
+ const value = mapId(node.value);
181
+ return {
182
+ id,
183
+ tag: node.tag,
184
+ deps: [value],
185
+ value,
186
+ entries: node.entries,
187
+ keys: node.keys,
188
+ mode: node.mode,
189
+ allRequired: node.allRequired
190
+ };
191
+ }
192
+ case NodeTag.UnionDispatch: {
193
+ const value = mapId(node.value);
194
+ return {
195
+ id,
196
+ tag: node.tag,
197
+ deps: [value],
198
+ value,
199
+ options: node.options,
200
+ graphs: node.graphs,
201
+ masks: node.masks
202
+ };
203
+ }
204
+ case NodeTag.PrimitiveUnion: {
205
+ const value = mapId(node.value);
206
+ return {
207
+ id,
208
+ tag: node.tag,
209
+ deps: [value],
210
+ value,
211
+ graphs: node.graphs,
212
+ masks: node.masks
213
+ };
214
+ }
173
215
  case NodeTag.SchemaCheck: {
174
216
  const value = mapId(node.value);
175
217
  return {
@@ -0,0 +1,16 @@
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 type { Graph } from "../ir/index.js";
8
+ /**
9
+ * @brief Run local dispatch compaction over one graph.
10
+ * @details This pass removes child arms that already fold to false and rewrites
11
+ * aliases after each replacement so later nodes see the simplified ids.
12
+ * @param graph Graph to optimize.
13
+ * @returns Graph with compacted dispatch nodes and resolved aliases.
14
+ */
15
+ export declare function peepholeGraph(graph: Graph): Graph;
16
+ //# sourceMappingURL=peephole.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peephole.d.ts","sourceRoot":"","sources":["../../src/optimize/peephole.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAGR,KAAK,EAKR,MAAM,gBAAgB,CAAC;AAKxB;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CA+CjD"}