circle-ir 4.19.0 → 4.21.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 (262) hide show
  1. package/dist/analysis/advisories.d.ts +21 -3
  2. package/dist/analysis/advisories.d.ts.map +1 -1
  3. package/dist/analysis/advisories.js +26 -8
  4. package/dist/analysis/advisories.js.map +1 -1
  5. package/dist/analysis/arg-executing-programs.d.ts +16 -0
  6. package/dist/analysis/arg-executing-programs.d.ts.map +1 -0
  7. package/dist/analysis/arg-executing-programs.js +24 -0
  8. package/dist/analysis/arg-executing-programs.js.map +1 -0
  9. package/dist/analysis/config-loader.d.ts.map +1 -1
  10. package/dist/analysis/config-loader.js +27 -3
  11. package/dist/analysis/config-loader.js.map +1 -1
  12. package/dist/analysis/constant-caller-gate.d.ts +3 -1
  13. package/dist/analysis/constant-caller-gate.d.ts.map +1 -1
  14. package/dist/analysis/constant-caller-gate.js +108 -1
  15. package/dist/analysis/constant-caller-gate.js.map +1 -1
  16. package/dist/analysis/constant-propagation/index.d.ts.map +1 -1
  17. package/dist/analysis/constant-propagation/index.js +1 -1
  18. package/dist/analysis/constant-propagation/index.js.map +1 -1
  19. package/dist/analysis/constant-propagation/patterns.d.ts.map +1 -1
  20. package/dist/analysis/constant-propagation/patterns.js +8 -0
  21. package/dist/analysis/constant-propagation/patterns.js.map +1 -1
  22. package/dist/analysis/constant-propagation/propagator.d.ts +47 -2
  23. package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
  24. package/dist/analysis/constant-propagation/propagator.js +422 -57
  25. package/dist/analysis/constant-propagation/propagator.js.map +1 -1
  26. package/dist/analysis/constant-propagation/types.d.ts +2 -0
  27. package/dist/analysis/constant-propagation/types.d.ts.map +1 -1
  28. package/dist/analysis/dependency-versions.d.ts +33 -0
  29. package/dist/analysis/dependency-versions.d.ts.map +1 -1
  30. package/dist/analysis/dependency-versions.js +70 -0
  31. package/dist/analysis/dependency-versions.js.map +1 -1
  32. package/dist/analysis/findings.d.ts.map +1 -1
  33. package/dist/analysis/findings.js +7 -5
  34. package/dist/analysis/findings.js.map +1 -1
  35. package/dist/analysis/interprocedural.d.ts +16 -0
  36. package/dist/analysis/interprocedural.d.ts.map +1 -1
  37. package/dist/analysis/interprocedural.js +545 -197
  38. package/dist/analysis/interprocedural.js.map +1 -1
  39. package/dist/analysis/passes/constant-propagation-pass.d.ts.map +1 -1
  40. package/dist/analysis/passes/constant-propagation-pass.js +1 -0
  41. package/dist/analysis/passes/constant-propagation-pass.js.map +1 -1
  42. package/dist/analysis/passes/cross-file-pass.d.ts.map +1 -1
  43. package/dist/analysis/passes/cross-file-pass.js +61 -0
  44. package/dist/analysis/passes/cross-file-pass.js.map +1 -1
  45. package/dist/analysis/passes/csharp-sanitizer-credit.d.ts.map +1 -1
  46. package/dist/analysis/passes/csharp-sanitizer-credit.js +60 -3
  47. package/dist/analysis/passes/csharp-sanitizer-credit.js.map +1 -1
  48. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts +58 -0
  49. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts.map +1 -0
  50. package/dist/analysis/passes/csrf-samesite-none-session-pass.js +159 -0
  51. package/dist/analysis/passes/csrf-samesite-none-session-pass.js.map +1 -0
  52. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts +15 -0
  53. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts.map +1 -1
  54. package/dist/analysis/passes/deserialization-safety-gate-pass.js +60 -1
  55. package/dist/analysis/passes/deserialization-safety-gate-pass.js.map +1 -1
  56. package/dist/analysis/passes/interprocedural-pass.d.ts +23 -1
  57. package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
  58. package/dist/analysis/passes/interprocedural-pass.js +220 -7
  59. package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
  60. package/dist/analysis/passes/language-sources-pass.d.ts +0 -18
  61. package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
  62. package/dist/analysis/passes/language-sources-pass.js +549 -22
  63. package/dist/analysis/passes/language-sources-pass.js.map +1 -1
  64. package/dist/analysis/passes/param-source-flow-gate-pass.d.ts.map +1 -1
  65. package/dist/analysis/passes/param-source-flow-gate-pass.js +71 -1
  66. package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
  67. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js +1 -1
  68. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js.map +1 -1
  69. package/dist/analysis/passes/resource-leak-pass.d.ts.map +1 -1
  70. package/dist/analysis/passes/resource-leak-pass.js +15 -3
  71. package/dist/analysis/passes/resource-leak-pass.js.map +1 -1
  72. package/dist/analysis/passes/scan-secrets-pass.js +2 -2
  73. package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
  74. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts +66 -0
  75. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts.map +1 -0
  76. package/dist/analysis/passes/sensitive-data-in-log-pass.js +462 -0
  77. package/dist/analysis/passes/sensitive-data-in-log-pass.js.map +1 -0
  78. package/dist/analysis/passes/sink-filter-pass.d.ts +7 -1
  79. package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
  80. package/dist/analysis/passes/sink-filter-pass.js +564 -22
  81. package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
  82. package/dist/analysis/passes/taint-propagation-pass.d.ts +7 -3
  83. package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
  84. package/dist/analysis/passes/taint-propagation-pass.js +464 -27
  85. package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
  86. package/dist/analysis/passes/xml-entity-expansion-pass.d.ts.map +1 -1
  87. package/dist/analysis/passes/xml-entity-expansion-pass.js +50 -6
  88. package/dist/analysis/passes/xml-entity-expansion-pass.js.map +1 -1
  89. package/dist/analysis/pep440-version.d.ts +40 -0
  90. package/dist/analysis/pep440-version.d.ts.map +1 -0
  91. package/dist/analysis/pep440-version.js +149 -0
  92. package/dist/analysis/pep440-version.js.map +1 -0
  93. package/dist/analysis/project-callee-escape-gate.js +1 -1
  94. package/dist/analysis/project-callee-escape-gate.js.map +1 -1
  95. package/dist/analysis/rules.d.ts.map +1 -1
  96. package/dist/analysis/rules.js +15 -0
  97. package/dist/analysis/rules.js.map +1 -1
  98. package/dist/analysis/sbom.d.ts +92 -4
  99. package/dist/analysis/sbom.d.ts.map +1 -1
  100. package/dist/analysis/sbom.js +390 -33
  101. package/dist/analysis/sbom.js.map +1 -1
  102. package/dist/analysis/taint-matcher.d.ts.map +1 -1
  103. package/dist/analysis/taint-matcher.js +124 -19
  104. package/dist/analysis/taint-matcher.js.map +1 -1
  105. package/dist/analysis/taint-propagation.d.ts +1 -0
  106. package/dist/analysis/taint-propagation.d.ts.map +1 -1
  107. package/dist/analysis/taint-propagation.js +32 -0
  108. package/dist/analysis/taint-propagation.js.map +1 -1
  109. package/dist/analysis/test-to-production-taint-gate.d.ts +13 -0
  110. package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -0
  111. package/dist/analysis/test-to-production-taint-gate.js +30 -0
  112. package/dist/analysis/test-to-production-taint-gate.js.map +1 -0
  113. package/dist/analyzer.d.ts +21 -3
  114. package/dist/analyzer.d.ts.map +1 -1
  115. package/dist/analyzer.js +83 -3
  116. package/dist/analyzer.js.map +1 -1
  117. package/dist/browser/circle-ir.js +8118 -2526
  118. package/dist/core/circle-ir-core.cjs +3098 -2280
  119. package/dist/core/circle-ir-core.js +3036 -2214
  120. package/dist/core/extractors/calls.d.ts +10 -1
  121. package/dist/core/extractors/calls.d.ts.map +1 -1
  122. package/dist/core/extractors/calls.js +148 -6
  123. package/dist/core/extractors/calls.js.map +1 -1
  124. package/dist/core/extractors/cfg.d.ts.map +1 -1
  125. package/dist/core/extractors/cfg.js +12 -0
  126. package/dist/core/extractors/cfg.js.map +1 -1
  127. package/dist/core/extractors/dfg.d.ts.map +1 -1
  128. package/dist/core/extractors/dfg.js +151 -18
  129. package/dist/core/extractors/dfg.js.map +1 -1
  130. package/dist/core/extractors/ts-declaration-detail.d.ts +8 -0
  131. package/dist/core/extractors/ts-declaration-detail.d.ts.map +1 -1
  132. package/dist/core/extractors/ts-declaration-detail.js +22 -0
  133. package/dist/core/extractors/ts-declaration-detail.js.map +1 -1
  134. package/dist/core/extractors/types.d.ts +5 -0
  135. package/dist/core/extractors/types.d.ts.map +1 -1
  136. package/dist/core/extractors/types.js +149 -4
  137. package/dist/core/extractors/types.js.map +1 -1
  138. package/dist/core/parser.d.ts.map +1 -1
  139. package/dist/core/parser.js +3 -1
  140. package/dist/core/parser.js.map +1 -1
  141. package/dist/core/ts-grammar-workarounds.d.ts +18 -0
  142. package/dist/core/ts-grammar-workarounds.d.ts.map +1 -0
  143. package/dist/core/ts-grammar-workarounds.js +40 -0
  144. package/dist/core/ts-grammar-workarounds.js.map +1 -0
  145. package/dist/facts/arrow.d.ts +106 -0
  146. package/dist/facts/arrow.d.ts.map +1 -0
  147. package/dist/facts/arrow.js +196 -0
  148. package/dist/facts/arrow.js.map +1 -0
  149. package/dist/facts/facts2.d.ts +68 -0
  150. package/dist/facts/facts2.d.ts.map +1 -0
  151. package/dist/facts/facts2.js +379 -0
  152. package/dist/facts/facts2.js.map +1 -0
  153. package/dist/facts/index.d.ts +22 -0
  154. package/dist/facts/index.d.ts.map +1 -0
  155. package/dist/facts/index.js +15 -0
  156. package/dist/facts/index.js.map +1 -0
  157. package/dist/facts/java-facts.d.ts +56 -0
  158. package/dist/facts/java-facts.d.ts.map +1 -0
  159. package/dist/facts/java-facts.js +250 -0
  160. package/dist/facts/java-facts.js.map +1 -0
  161. package/dist/facts/order.d.ts +39 -0
  162. package/dist/facts/order.d.ts.map +1 -0
  163. package/dist/facts/order.js +120 -0
  164. package/dist/facts/order.js.map +1 -0
  165. package/dist/facts/schema.d.ts +60 -0
  166. package/dist/facts/schema.d.ts.map +1 -0
  167. package/dist/facts/schema.js +126 -0
  168. package/dist/facts/schema.js.map +1 -0
  169. package/dist/facts/schema2.d.ts +47 -0
  170. package/dist/facts/schema2.d.ts.map +1 -0
  171. package/dist/facts/schema2.js +194 -0
  172. package/dist/facts/schema2.js.map +1 -0
  173. package/dist/facts/t6.d.ts +84 -0
  174. package/dist/facts/t6.d.ts.map +1 -0
  175. package/dist/facts/t6.js +226 -0
  176. package/dist/facts/t6.js.map +1 -0
  177. package/dist/facts/table.d.ts +80 -0
  178. package/dist/facts/table.d.ts.map +1 -0
  179. package/dist/facts/table.js +548 -0
  180. package/dist/facts/table.js.map +1 -0
  181. package/dist/graph-facts/dead-code.d.ts +2 -0
  182. package/dist/graph-facts/dead-code.d.ts.map +1 -1
  183. package/dist/graph-facts/dead-code.js +4 -3
  184. package/dist/graph-facts/dead-code.js.map +1 -1
  185. package/dist/graph-facts/index.d.ts +1 -1
  186. package/dist/graph-facts/index.d.ts.map +1 -1
  187. package/dist/graph-facts/index.js +1 -1
  188. package/dist/graph-facts/index.js.map +1 -1
  189. package/dist/graph-facts/reach.d.ts +53 -8
  190. package/dist/graph-facts/reach.d.ts.map +1 -1
  191. package/dist/graph-facts/reach.js +172 -16
  192. package/dist/graph-facts/reach.js.map +1 -1
  193. package/dist/graph-facts/removal-reach.d.ts +2 -0
  194. package/dist/graph-facts/removal-reach.d.ts.map +1 -1
  195. package/dist/graph-facts/removal-reach.js +1 -1
  196. package/dist/graph-facts/removal-reach.js.map +1 -1
  197. package/dist/index.d.ts +15 -7
  198. package/dist/index.d.ts.map +1 -1
  199. package/dist/index.js +19 -4
  200. package/dist/index.js.map +1 -1
  201. package/dist/navigation/index.d.ts +1 -0
  202. package/dist/navigation/index.d.ts.map +1 -1
  203. package/dist/navigation/index.js +1 -0
  204. package/dist/navigation/index.js.map +1 -1
  205. package/dist/navigation/java-graph.d.ts +47 -2
  206. package/dist/navigation/java-graph.d.ts.map +1 -1
  207. package/dist/navigation/java-graph.js +395 -18
  208. package/dist/navigation/java-graph.js.map +1 -1
  209. package/dist/navigation/java-registrations.d.ts +93 -0
  210. package/dist/navigation/java-registrations.d.ts.map +1 -0
  211. package/dist/navigation/java-registrations.js +254 -0
  212. package/dist/navigation/java-registrations.js.map +1 -0
  213. package/dist/navigation/navigation-index.d.ts +74 -3
  214. package/dist/navigation/navigation-index.d.ts.map +1 -1
  215. package/dist/navigation/navigation-index.js +206 -11
  216. package/dist/navigation/navigation-index.js.map +1 -1
  217. package/dist/navigation/ts-call-edges.d.ts +15 -0
  218. package/dist/navigation/ts-call-edges.d.ts.map +1 -1
  219. package/dist/navigation/ts-call-edges.js.map +1 -1
  220. package/dist/navigation/ts-entry-tables.d.ts +1 -1
  221. package/dist/navigation/ts-entry-tables.d.ts.map +1 -1
  222. package/dist/navigation/ts-entry-tables.js.map +1 -1
  223. package/dist/navigation/xml-scan.d.ts +40 -0
  224. package/dist/navigation/xml-scan.d.ts.map +1 -0
  225. package/dist/navigation/xml-scan.js +269 -0
  226. package/dist/navigation/xml-scan.js.map +1 -0
  227. package/dist/resolution/cross-file.d.ts +44 -0
  228. package/dist/resolution/cross-file.d.ts.map +1 -1
  229. package/dist/resolution/cross-file.js +259 -13
  230. package/dist/resolution/cross-file.js.map +1 -1
  231. package/dist/stable-ids/files.d.ts +22 -0
  232. package/dist/stable-ids/files.d.ts.map +1 -0
  233. package/dist/stable-ids/files.js +28 -0
  234. package/dist/stable-ids/files.js.map +1 -0
  235. package/dist/stable-ids/hash.d.ts +16 -4
  236. package/dist/stable-ids/hash.d.ts.map +1 -1
  237. package/dist/stable-ids/hash.js +266 -80
  238. package/dist/stable-ids/hash.js.map +1 -1
  239. package/dist/stable-ids/index.d.ts +6 -5
  240. package/dist/stable-ids/index.d.ts.map +1 -1
  241. package/dist/stable-ids/index.js +19 -6
  242. package/dist/stable-ids/index.js.map +1 -1
  243. package/dist/stable-ids/nodes.d.ts +61 -7
  244. package/dist/stable-ids/nodes.d.ts.map +1 -1
  245. package/dist/stable-ids/nodes.js +134 -21
  246. package/dist/stable-ids/nodes.js.map +1 -1
  247. package/dist/stable-ids/ts-parse-errors.d.ts +5 -1
  248. package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
  249. package/dist/stable-ids/ts-parse-errors.js +55 -3
  250. package/dist/stable-ids/ts-parse-errors.js.map +1 -1
  251. package/dist/stable-ids/typescript.d.ts +5 -0
  252. package/dist/stable-ids/typescript.d.ts.map +1 -1
  253. package/dist/stable-ids/typescript.js +41 -8
  254. package/dist/stable-ids/typescript.js.map +1 -1
  255. package/dist/types/index.d.ts +21 -3
  256. package/dist/types/index.d.ts.map +1 -1
  257. package/dist/types/index.js +1 -0
  258. package/dist/types/index.js.map +1 -1
  259. package/docs/SPEC.md +9 -0
  260. package/package.json +9 -5
  261. package/schemas/facts@1.json +134 -0
  262. package/schemas/facts@2.json +216 -0
@@ -0,0 +1,80 @@
1
+ /**
2
+ * `facts@1` relations as columns: one typed array (or string array) per
3
+ * schema column, never an object per row. The emitter builds these straight
4
+ * from the graph, sorts a row-index permutation once with a comparator over
5
+ * the columns, drops adjacent duplicates (relations are sets) and gathers the
6
+ * kept rows — the shape Arrow wants, so the IPC encoder copies buffers
7
+ * instead of visiting cells. The row form (`FactRow`) stays as the reference
8
+ * for the comparator and for tests; `tableFromRows` / `tableRow` convert.
9
+ */
10
+ import { type FactRow, type FactsColumn, type FactsRelation } from './schema.js';
11
+ /**
12
+ * One column's cells, by schema type: `fixed16` → `Uint8Array` of 16 × rows
13
+ * bytes; `u32` → `Uint32Array`; `utf8` → `string[]`; `bool` → `Uint8Array`
14
+ * of 0/1 per row; `dict` → `Int8Array` of vocabulary indexes.
15
+ */
16
+ export type FactColumnData = Uint8Array | Uint32Array | Int8Array | string[];
17
+ export interface FactTable {
18
+ readonly length: number;
19
+ /** One entry per schema column, in schema order. */
20
+ readonly columns: readonly FactColumnData[];
21
+ }
22
+ /** An empty column of the right shape for `n` rows. */
23
+ export declare function allocColumn(c: FactsColumn, n: number): FactColumnData;
24
+ /** Empty columns for a relation with `n` rows. */
25
+ export declare function allocTable(relation: FactsRelation, n: number): FactColumnData[];
26
+ /** `value → index` for a dictionary column's vocabulary. */
27
+ export declare function dictIndex(c: FactsColumn): Map<string, number>;
28
+ /**
29
+ * Decode an `ids@0.1` id (26 lowercase base32 characters, 130 bits with 2
30
+ * zero padding bits) into 16 bytes at `out[offset..]`, allocating nothing.
31
+ */
32
+ export declare function idBytesInto(id: string, out: Uint8Array, offset: number): void;
33
+ /**
34
+ * Decodes ids into columns through a cache: a snapshot's ids repeat (every
35
+ * call edge names its caller and callee), so an id is decoded once into a
36
+ * pool and copied from there after.
37
+ */
38
+ export declare class IdDecoder {
39
+ private pool;
40
+ private readonly offsets;
41
+ private used;
42
+ /** Write the 16 bytes of `id` at `out[offset..]`. */
43
+ put(id: string, out: Uint8Array, offset: number): void;
44
+ }
45
+ /**
46
+ * Code-point order on two strings, fast path first: equal references, then
47
+ * the first differing UTF-16 unit, corrected only where a surrogate meets a
48
+ * non-surrogate (the one case where unit order and code-point order differ).
49
+ */
50
+ export declare function compareStrings(a: string, b: string): number;
51
+ type IndexComparator = (a: number, b: number) => number;
52
+ /**
53
+ * The relation's canonical order as a comparator on row indexes of `columns`:
54
+ * key columns, then every remaining column in schema order. Total over the
55
+ * cells, so `0` means the two rows are equal cell for cell.
56
+ */
57
+ export declare function tableComparator(relation: FactsRelation, columns: readonly FactColumnData[]): IndexComparator;
58
+ /**
59
+ * Canonical form of a relation's rows: sorted in the total order, exact
60
+ * duplicates dropped (relations are sets) and counted. Sorts a permutation
61
+ * once and gathers; the input columns are not modified.
62
+ */
63
+ export declare function canonicalTable(relation: FactsRelation, columns: readonly FactColumnData[], length: number): {
64
+ table: FactTable;
65
+ duplicatesDropped: number;
66
+ };
67
+ /** Adjacent rows of a canonical table that are equal on the key columns alone (what a store's key check would refuse). */
68
+ export declare function keyCollisions(relation: FactsRelation, table: FactTable): number;
69
+ /** Row `i` of a table as cells in schema order. */
70
+ export declare function tableRow(relation: FactsRelation, table: FactTable, i: number): FactRow;
71
+ /** Every row of a table, in table order. */
72
+ export declare function tableRows(relation: FactsRelation, table: FactTable): FactRow[];
73
+ /**
74
+ * Columns from rows in the row form, cells checked against their column
75
+ * (`null` is refused: `facts@1` has no nullable column). The rows keep their
76
+ * order; pass the result through `canonicalTable` for the canonical form.
77
+ */
78
+ export declare function tableFromRows(relation: FactsRelation, rows: readonly FactRow[]): FactTable;
79
+ export {};
80
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/facts/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAkB,KAAK,OAAO,EAAkB,KAAK,WAAW,EAAwB,KAAK,aAAa,EAAe,MAAM,aAAa,CAAC;AAGpJ;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,WAAW,GAAG,SAAS,GAAG,MAAM,EAAE,CAAC;AAE7E,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oDAAoD;IACpD,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAC;CAC7C;AAED,uDAAuD;AACvD,wBAAgB,WAAW,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,cAAc,CAQrE;AAED,kDAAkD;AAClD,wBAAgB,UAAU,CAAC,QAAQ,EAAE,aAAa,EAAE,CAAC,EAAE,MAAM,GAAG,cAAc,EAAE,CAE/E;AAED,4DAA4D;AAC5D,wBAAgB,SAAS,CAAC,CAAC,EAAE,WAAW,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAE7D;AAKD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAkB7E;AAED;;;;GAIG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,IAAI,CAAyC;IACrD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6B;IACrD,OAAO,CAAC,IAAI,CAAK;IAEjB,qDAAqD;IACrD,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAerD;CACF;AAWD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAY3D;AAED,KAAK,eAAe,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;AAiCxD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,cAAc,EAAE,GAAG,eAAe,CAU5G;AA8PD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,cAAc,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,iBAAiB,EAAE,MAAM,CAAA;CAAE,CAiB3J;AAED,0HAA0H;AAC1H,wBAAgB,aAAa,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAQ/E;AAED,mDAAmD;AACnD,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAYtF;AAED,4CAA4C;AAC5C,wBAAgB,SAAS,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO,EAAE,CAE9E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,OAAO,EAAE,GAAG,SAAS,CAsC1F"}
@@ -0,0 +1,548 @@
1
+ /**
2
+ * `facts@1` relations as columns: one typed array (or string array) per
3
+ * schema column, never an object per row. The emitter builds these straight
4
+ * from the graph, sorts a row-index permutation once with a comparator over
5
+ * the columns, drops adjacent duplicates (relations are sets) and gathers the
6
+ * kept rows — the shape Arrow wants, so the IPC encoder copies buffers
7
+ * instead of visiting cells. The row form (`FactRow`) stays as the reference
8
+ * for the comparator and for tests; `tableFromRows` / `tableRow` convert.
9
+ */
10
+ import { FACTS_ID_WIDTH, sortColumns } from './schema.js';
11
+ import { compareCodePoints } from './order.js';
12
+ /** An empty column of the right shape for `n` rows. */
13
+ export function allocColumn(c, n) {
14
+ switch (c.type) {
15
+ case 'fixed16': return new Uint8Array(n * FACTS_ID_WIDTH);
16
+ case 'u32': return new Uint32Array(n);
17
+ case 'utf8': return new Array(n);
18
+ case 'bool': return new Uint8Array(n);
19
+ case 'dict': return new Int8Array(n);
20
+ }
21
+ }
22
+ /** Empty columns for a relation with `n` rows. */
23
+ export function allocTable(relation, n) {
24
+ return relation.columns.map(c => allocColumn(c, n));
25
+ }
26
+ /** `value → index` for a dictionary column's vocabulary. */
27
+ export function dictIndex(c) {
28
+ return new Map((c.values ?? []).map((v, i) => [v, i]));
29
+ }
30
+ const BASE32_VALUE = new Int8Array(128).fill(-1);
31
+ for (let i = 0; i < 32; i++)
32
+ BASE32_VALUE['abcdefghijklmnopqrstuvwxyz234567'.charCodeAt(i)] = i;
33
+ /**
34
+ * Decode an `ids@0.1` id (26 lowercase base32 characters, 130 bits with 2
35
+ * zero padding bits) into 16 bytes at `out[offset..]`, allocating nothing.
36
+ */
37
+ export function idBytesInto(id, out, offset) {
38
+ if (id.length !== 26)
39
+ throw new Error(`facts@1: not an ids@0.1 id: ${JSON.stringify(id)}`);
40
+ let buffer = 0;
41
+ let bits = 0;
42
+ let n = offset;
43
+ for (let i = 0; i < 26; i++) {
44
+ const code = id.charCodeAt(i);
45
+ const v = code < 128 ? BASE32_VALUE[code] : -1;
46
+ if (v < 0)
47
+ throw new Error(`facts@1: not an ids@0.1 id: ${JSON.stringify(id)}`);
48
+ buffer = (buffer << 5) | v;
49
+ bits += 5;
50
+ if (bits >= 8) {
51
+ out[n++] = (buffer >>> (bits - 8)) & 0xff;
52
+ bits -= 8;
53
+ buffer &= (1 << bits) - 1;
54
+ }
55
+ }
56
+ if (bits !== 2 || buffer !== 0)
57
+ throw new Error(`facts@1: not an ids@0.1 id: ${JSON.stringify(id)}`);
58
+ }
59
+ /**
60
+ * Decodes ids into columns through a cache: a snapshot's ids repeat (every
61
+ * call edge names its caller and callee), so an id is decoded once into a
62
+ * pool and copied from there after.
63
+ */
64
+ export class IdDecoder {
65
+ pool = new Uint8Array(FACTS_ID_WIDTH * 1024);
66
+ offsets = new Map();
67
+ used = 0;
68
+ /** Write the 16 bytes of `id` at `out[offset..]`. */
69
+ put(id, out, offset) {
70
+ let at = this.offsets.get(id);
71
+ if (at === undefined) {
72
+ if (this.used + FACTS_ID_WIDTH > this.pool.length) {
73
+ const bigger = new Uint8Array(this.pool.length * 2);
74
+ bigger.set(this.pool);
75
+ this.pool = bigger;
76
+ }
77
+ at = this.used;
78
+ idBytesInto(id, this.pool, at);
79
+ this.used += FACTS_ID_WIDTH;
80
+ this.offsets.set(id, at);
81
+ }
82
+ const pool = this.pool;
83
+ for (let k = 0; k < FACTS_ID_WIDTH; k++)
84
+ out[offset + k] = pool[at + k];
85
+ }
86
+ }
87
+ /** Dictionary index → rank in code-point order of the vocabulary (the order the contract sorts dict columns by). */
88
+ function dictRanks(c) {
89
+ const vocab = c.values ?? [];
90
+ const sorted = [...vocab].sort(compareCodePoints);
91
+ const ranks = new Int8Array(vocab.length);
92
+ vocab.forEach((v, i) => { ranks[i] = sorted.indexOf(v); });
93
+ return ranks;
94
+ }
95
+ /**
96
+ * Code-point order on two strings, fast path first: equal references, then
97
+ * the first differing UTF-16 unit, corrected only where a surrogate meets a
98
+ * non-surrogate (the one case where unit order and code-point order differ).
99
+ */
100
+ export function compareStrings(a, b) {
101
+ if (a === b)
102
+ return 0;
103
+ const n = Math.min(a.length, b.length);
104
+ let i = 0;
105
+ while (i < n && a.charCodeAt(i) === b.charCodeAt(i))
106
+ i++;
107
+ if (i === n)
108
+ return a.length - b.length;
109
+ const ca = a.charCodeAt(i);
110
+ const cb = b.charCodeAt(i);
111
+ const sa = ca >= 0xd800 && ca < 0xe000;
112
+ const sb = cb >= 0xd800 && cb < 0xe000;
113
+ if (sa === sb)
114
+ return ca - cb;
115
+ return a.codePointAt(i) - b.codePointAt(i);
116
+ }
117
+ function columnComparator(c, data) {
118
+ switch (c.type) {
119
+ case 'fixed16': {
120
+ const d = data;
121
+ return (a, b) => {
122
+ let x = a * FACTS_ID_WIDTH;
123
+ let y = b * FACTS_ID_WIDTH;
124
+ for (let k = 0; k < FACTS_ID_WIDTH; k++, x++, y++) {
125
+ const diff = d[x] - d[y];
126
+ if (diff !== 0)
127
+ return diff;
128
+ }
129
+ return 0;
130
+ };
131
+ }
132
+ case 'u32':
133
+ case 'bool': {
134
+ const d = data;
135
+ return (a, b) => d[a] - d[b];
136
+ }
137
+ case 'utf8': {
138
+ const d = data;
139
+ return (a, b) => compareStrings(d[a], d[b]);
140
+ }
141
+ case 'dict': {
142
+ const d = data;
143
+ const ranks = dictRanks(c);
144
+ return (a, b) => ranks[d[a]] - ranks[d[b]];
145
+ }
146
+ }
147
+ }
148
+ /**
149
+ * The relation's canonical order as a comparator on row indexes of `columns`:
150
+ * key columns, then every remaining column in schema order. Total over the
151
+ * cells, so `0` means the two rows are equal cell for cell.
152
+ */
153
+ export function tableComparator(relation, columns) {
154
+ const cmps = sortColumns(relation).map(i => columnComparator(relation.columns[i], columns[i]));
155
+ const n = cmps.length;
156
+ return (a, b) => {
157
+ for (let k = 0; k < n; k++) {
158
+ const c = cmps[k](a, b);
159
+ if (c !== 0)
160
+ return c;
161
+ }
162
+ return 0;
163
+ };
164
+ }
165
+ /** The rows `idx` names, gathered into new columns in that order. */
166
+ function gather(relation, columns, idx) {
167
+ const n = idx.length;
168
+ return relation.columns.map((c, ci) => {
169
+ const src = columns[ci];
170
+ const dst = allocColumn(c, n);
171
+ if (c.type === 'fixed16') {
172
+ const s = src;
173
+ const d = dst;
174
+ for (let i = 0, to = 0; i < n; i++) {
175
+ // A plain copy loop: `set(subarray())` would allocate a view per cell.
176
+ for (let from = idx[i] * FACTS_ID_WIDTH, end = from + FACTS_ID_WIDTH; from < end; from++, to++)
177
+ d[to] = s[from];
178
+ }
179
+ }
180
+ else if (c.type === 'utf8') {
181
+ const s = src;
182
+ const d = dst;
183
+ for (let i = 0; i < n; i++)
184
+ d[i] = s[idx[i]];
185
+ }
186
+ else {
187
+ const s = src;
188
+ const d = dst;
189
+ for (let i = 0; i < n; i++)
190
+ d[i] = s[idx[i]];
191
+ }
192
+ return dst;
193
+ });
194
+ }
195
+ const INDEX_BITS = 24;
196
+ const INDEX_RANGE = 2 ** INDEX_BITS;
197
+ const KEY_BITS = 29;
198
+ const KEY_RANGE = 2 ** KEY_BITS;
199
+ /**
200
+ * The sort key of a row as a byte string, for the native sort: the key
201
+ * columns then the remaining columns in schema order, each column's bytes in
202
+ * an order that agrees with the comparator — a `fixed16` as its 16 bytes,
203
+ * a `u32` big-endian, a `dict` as its code-point rank, a `bool` as 0/1 — up
204
+ * to the first `utf8` column, which has no fixed width and ends the string.
205
+ * `bitsAt(level)` reads 29-bit slices of it, so a relation whose leading
206
+ * column is an id (`flow`, `call`, `decl`, …) sorts natively level after
207
+ * level instead of falling back to the comparator for every tie on the first
208
+ * 29 bits — and on real data the ties are the rule: one `call` caller has
209
+ * many edges, one `flow` definition many uses.
210
+ */
211
+ function copyFixed16(d, buf, at, stride, rows) {
212
+ for (let row = 0, o = at, f = 0; row < rows; row++, o += stride, f += FACTS_ID_WIDTH) {
213
+ for (let k = 0; k < FACTS_ID_WIDTH; k++)
214
+ buf[o + k] = d[f + k];
215
+ }
216
+ }
217
+ function copyU32(d, buf, at, stride, rows) {
218
+ for (let row = 0, o = at; row < rows; row++, o += stride) {
219
+ const v = d[row];
220
+ buf[o] = v >>> 24;
221
+ buf[o + 1] = (v >>> 16) & 0xff;
222
+ buf[o + 2] = (v >>> 8) & 0xff;
223
+ buf[o + 3] = v & 0xff;
224
+ }
225
+ }
226
+ function copyRanks(d, ranks, buf, at, stride, rows) {
227
+ for (let row = 0, o = at; row < rows; row++, o += stride)
228
+ buf[o] = ranks[d[row]];
229
+ }
230
+ function copyBytes(d, buf, at, stride, rows) {
231
+ for (let row = 0, o = at; row < rows; row++, o += stride)
232
+ buf[o] = d[row];
233
+ }
234
+ class KeyBytes {
235
+ rows;
236
+ /** Total key bytes before the first `utf8` column. */
237
+ length;
238
+ /** Row-major key strings, `length` bytes per row. */
239
+ buf;
240
+ cache = new Map();
241
+ constructor(relation, columns, rows) {
242
+ this.rows = rows;
243
+ const parts = [];
244
+ let length = 0;
245
+ for (const i of sortColumns(relation)) {
246
+ const c = relation.columns[i];
247
+ if (c.type === 'utf8')
248
+ break;
249
+ const width = c.type === 'fixed16' ? FACTS_ID_WIDTH : c.type === 'u32' ? 4 : 1;
250
+ parts.push({ i, type: c.type, width });
251
+ length += width;
252
+ }
253
+ this.length = length;
254
+ this.complete = parts.length === relation.columns.length;
255
+ const buf = new Uint8Array(rows * length);
256
+ let at = 0;
257
+ for (const { i, type, width } of parts) {
258
+ const c = relation.columns[i];
259
+ if (type === 'fixed16')
260
+ copyFixed16(columns[i], buf, at, length, rows);
261
+ else if (type === 'u32')
262
+ copyU32(columns[i], buf, at, length, rows);
263
+ else if (type === 'dict')
264
+ copyRanks(columns[i], dictRanks(c), buf, at, length, rows);
265
+ else
266
+ copyBytes(columns[i], buf, at, length, rows);
267
+ at += width;
268
+ }
269
+ this.buf = buf;
270
+ }
271
+ /** True when the key string covers every column (no `utf8`), so equal key strings are equal rows. */
272
+ complete;
273
+ /** Rows `a` and `b` have the same key string. */
274
+ equal(a, b) {
275
+ const { buf, length } = this;
276
+ for (let x = a * length, y = b * length, end = x + length; x < end; x++, y++)
277
+ if (buf[x] !== buf[y])
278
+ return false;
279
+ return true;
280
+ }
281
+ /** Number of 29-bit levels the key string holds. */
282
+ get levels() {
283
+ return Math.ceil((this.length * 8) / KEY_BITS);
284
+ }
285
+ /** Bits `[29·level, 29·level + 29)` of every row's key string (zero past its end), or `undefined` when the level is past the end. */
286
+ bitsAt(level) {
287
+ if (level >= this.levels)
288
+ return undefined;
289
+ let bits = this.cache.get(level);
290
+ if (bits)
291
+ return bits;
292
+ bits = new Uint32Array(this.rows);
293
+ const bitStart = level * KEY_BITS;
294
+ const q = bitStart >> 3;
295
+ const r = bitStart & 7;
296
+ // Five bytes hold any 29-bit window at any bit offset; exact as a double (< 2^40).
297
+ const shift = 2 ** (40 - r - KEY_BITS);
298
+ const { buf, length } = this;
299
+ const b = (o, k) => (k < length ? buf[o + k] : 0);
300
+ for (let row = 0, o = 0; row < this.rows; row++, o += length) {
301
+ const v = b(o, q) * 2 ** 32 + b(o, q + 1) * 2 ** 24 + b(o, q + 2) * 2 ** 16 + b(o, q + 3) * 2 ** 8 + b(o, q + 4);
302
+ bits[row] = Math.floor(v / shift) % KEY_RANGE;
303
+ }
304
+ this.cache.set(level, bits);
305
+ return bits;
306
+ }
307
+ }
308
+ /**
309
+ * Up to 29 bits per row that agree with the leading sort column(s) when the
310
+ * leading column is a `u32`: the leading `u32` with the next `u32` key
311
+ * packed below it when both are small enough (`file_id` < 2^13 and the line
312
+ * < 2^16), else the leading `u32` alone when it is under 2^29. `undefined`
313
+ * when a value does not fit. (An id-led relation reads `KeyBytes` instead.)
314
+ */
315
+ function u32KeyBits(relation, columns, length) {
316
+ const order = sortColumns(relation);
317
+ const a = columns[order[0]];
318
+ const second = order.length > 1 && relation.columns[order[1]].type === 'u32' ? columns[order[1]] : undefined;
319
+ let maxA = 0;
320
+ let maxB = 0;
321
+ for (let i = 0; i < length; i++) {
322
+ if (a[i] > maxA)
323
+ maxA = a[i];
324
+ if (second && second[i] > maxB)
325
+ maxB = second[i];
326
+ }
327
+ const keys = new Uint32Array(length);
328
+ if (second && maxA < 0x2000 && maxB < 0x10000) {
329
+ for (let i = 0; i < length; i++)
330
+ keys[i] = (a[i] << 16) | second[i];
331
+ return keys;
332
+ }
333
+ if (maxA < KEY_RANGE) {
334
+ keys.set(a);
335
+ return keys;
336
+ }
337
+ return undefined;
338
+ }
339
+ /** Sort `idx[start, end)` with the comparator, in place: insertion for a short run, a native sort of a copy otherwise. */
340
+ function sortRun(idx, start, end, cmp) {
341
+ const n = end - start;
342
+ if (n === 2) {
343
+ if (cmp(idx[start], idx[start + 1]) > 0) {
344
+ const t = idx[start];
345
+ idx[start] = idx[start + 1];
346
+ idx[start + 1] = t;
347
+ }
348
+ }
349
+ else if (n <= 16) {
350
+ for (let a = start + 1; a < end; a++) {
351
+ const v = idx[a];
352
+ let b = a - 1;
353
+ while (b >= start && cmp(idx[b], v) > 0) {
354
+ idx[b + 1] = idx[b];
355
+ b--;
356
+ }
357
+ idx[b + 1] = v;
358
+ }
359
+ }
360
+ else {
361
+ const sorted = idx.slice(start, end).sort(cmp);
362
+ for (let k = 0; k < n; k++)
363
+ idx[start + k] = sorted[k];
364
+ }
365
+ }
366
+ /**
367
+ * The canonical permutation of the rows: sorted by the total order. Rows are
368
+ * packed as `keyBits · 2^24 + index` into a `Float64Array` (53 bits, exact)
369
+ * and sorted natively — no comparator call per comparison. Rows that tie on
370
+ * those bits are sorted the same way on the next 29 bits of the key string
371
+ * (`KeyBytes`), level after level, in place on a scratch array; only runs
372
+ * that are still tied when the key string ends (a `utf8` column, or equal
373
+ * rows) go to the comparator. Falls back to a comparator sort when the rows
374
+ * do not fit the packing.
375
+ */
376
+ function canonicalPermutation(relation, columns, length, cmp) {
377
+ const idx = new Array(length);
378
+ const first = relation.columns[sortColumns(relation)[0]];
379
+ const keys = first.type === 'fixed16' || first.type === 'dict' || first.type === 'bool' ? new KeyBytes(relation, columns, length) : undefined;
380
+ const bits0 = length < INDEX_RANGE ? (keys ? keys.bitsAt(0) : first.type === 'u32' ? u32KeyBits(relation, columns, length) : undefined) : undefined;
381
+ if (!bits0) {
382
+ for (let i = 0; i < length; i++)
383
+ idx[i] = i;
384
+ idx.sort(cmp);
385
+ return { idx, keys };
386
+ }
387
+ const packed = new Float64Array(length);
388
+ for (let i = 0; i < length; i++)
389
+ packed[i] = bits0[i] * INDEX_RANGE + i;
390
+ packed.sort();
391
+ for (let i = 0; i < length; i++)
392
+ idx[i] = packed[i] % INDEX_RANGE;
393
+ // Refine `idx[start, end)` — rows equal on levels < `level` — by the bits at `level`.
394
+ const refine = (start, end, level) => {
395
+ const bits = keys?.bitsAt(level);
396
+ if (!bits) {
397
+ sortRun(idx, start, end, cmp);
398
+ return;
399
+ }
400
+ const n = end - start;
401
+ if (n <= 16) {
402
+ // Insertion sort on the bits, in place: most runs are one id used a few times.
403
+ for (let a = start + 1; a < end; a++) {
404
+ const v = idx[a];
405
+ const bv = bits[v];
406
+ let b = a - 1;
407
+ while (b >= start && bits[idx[b]] > bv) {
408
+ idx[b + 1] = idx[b];
409
+ b--;
410
+ }
411
+ idx[b + 1] = v;
412
+ }
413
+ }
414
+ else {
415
+ const scratch = packed.subarray(0, n);
416
+ for (let k = 0; k < n; k++)
417
+ scratch[k] = bits[idx[start + k]] * INDEX_RANGE + idx[start + k];
418
+ scratch.sort();
419
+ for (let k = 0; k < n; k++)
420
+ idx[start + k] = scratch[k] % INDEX_RANGE;
421
+ }
422
+ let runStart = start;
423
+ for (let i = start + 1; i <= end; i++) {
424
+ if (i < end && bits[idx[i]] === bits[idx[runStart]])
425
+ continue;
426
+ if (i - runStart > 1)
427
+ refine(runStart, i, level + 1);
428
+ runStart = i;
429
+ }
430
+ };
431
+ // `packed` is reused as the scratch below: the sorted order is in `idx` by now.
432
+ let runStart = 0;
433
+ for (let i = 1; i <= length; i++) {
434
+ if (i < length && bits0[idx[i]] === bits0[idx[runStart]])
435
+ continue;
436
+ if (i - runStart > 1)
437
+ refine(runStart, i, 1);
438
+ runStart = i;
439
+ }
440
+ return { idx, keys };
441
+ }
442
+ /**
443
+ * Canonical form of a relation's rows: sorted in the total order, exact
444
+ * duplicates dropped (relations are sets) and counted. Sorts a permutation
445
+ * once and gathers; the input columns are not modified.
446
+ */
447
+ export function canonicalTable(relation, columns, length) {
448
+ if (columns.length !== relation.columns.length)
449
+ throw new Error(`facts@1: ${columns.length} columns for a relation of ${relation.columns.length}`);
450
+ const cmp = tableComparator(relation, columns);
451
+ const { idx, keys } = canonicalPermutation(relation, columns, length, cmp);
452
+ // Adjacent equal rows: same key string (and, when a utf8 column lies past it, equal under the comparator too).
453
+ const same = keys
454
+ ? keys.complete
455
+ ? (a, b) => keys.equal(a, b)
456
+ : (a, b) => keys.equal(a, b) && cmp(a, b) === 0
457
+ : (a, b) => cmp(a, b) === 0;
458
+ let kept = 0;
459
+ for (let i = 0; i < length; i++) {
460
+ if (kept > 0 && same(idx[kept - 1], idx[i]))
461
+ continue;
462
+ idx[kept++] = idx[i];
463
+ }
464
+ idx.length = kept;
465
+ return { table: { length: kept, columns: gather(relation, columns, idx) }, duplicatesDropped: length - kept };
466
+ }
467
+ /** Adjacent rows of a canonical table that are equal on the key columns alone (what a store's key check would refuse). */
468
+ export function keyCollisions(relation, table) {
469
+ const keyIdx = relation.key.map(k => relation.columns.findIndex(c => c.name === k));
470
+ const cmps = keyIdx.map(i => columnComparator(relation.columns[i], table.columns[i]));
471
+ let collisions = 0;
472
+ for (let i = 1; i < table.length; i++) {
473
+ if (cmps.every(c => c(i - 1, i) === 0))
474
+ collisions++;
475
+ }
476
+ return collisions;
477
+ }
478
+ /** Row `i` of a table as cells in schema order. */
479
+ export function tableRow(relation, table, i) {
480
+ if (i < 0 || i >= table.length)
481
+ throw new RangeError(`facts@1: row ${i} of ${table.length}`);
482
+ return relation.columns.map((c, ci) => {
483
+ const d = table.columns[ci];
484
+ switch (c.type) {
485
+ case 'fixed16': return d.slice(i * FACTS_ID_WIDTH, (i + 1) * FACTS_ID_WIDTH);
486
+ case 'u32': return d[i];
487
+ case 'utf8': return d[i];
488
+ case 'bool': return d[i] !== 0;
489
+ case 'dict': return c.values[d[i]];
490
+ }
491
+ });
492
+ }
493
+ /** Every row of a table, in table order. */
494
+ export function tableRows(relation, table) {
495
+ return Array.from({ length: table.length }, (_, i) => tableRow(relation, table, i));
496
+ }
497
+ /**
498
+ * Columns from rows in the row form, cells checked against their column
499
+ * (`null` is refused: `facts@1` has no nullable column). The rows keep their
500
+ * order; pass the result through `canonicalTable` for the canonical form.
501
+ */
502
+ export function tableFromRows(relation, rows) {
503
+ const n = rows.length;
504
+ const columns = allocTable(relation, n);
505
+ const dicts = relation.columns.map(c => (c.type === 'dict' ? dictIndex(c) : undefined));
506
+ for (let i = 0; i < n; i++) {
507
+ const r = rows[i];
508
+ if (r.length !== relation.columns.length)
509
+ throw new Error(`facts@1: a row has ${r.length} cells, the relation ${relation.columns.length} columns`);
510
+ for (let ci = 0; ci < relation.columns.length; ci++) {
511
+ const c = relation.columns[ci];
512
+ const x = r[ci];
513
+ if (x === null)
514
+ throw new Error(`facts@1: null in non-nullable column ${c.name}`);
515
+ switch (c.type) {
516
+ case 'fixed16':
517
+ if (!(x instanceof Uint8Array) || x.length !== FACTS_ID_WIDTH)
518
+ throw new Error(`facts@1: column ${c.name} expects ${FACTS_ID_WIDTH} id bytes`);
519
+ columns[ci].set(x, i * FACTS_ID_WIDTH);
520
+ break;
521
+ case 'u32':
522
+ if (typeof x !== 'number' || !Number.isInteger(x) || x < 0 || x > 0xffffffff)
523
+ throw new Error(`facts@1: column ${c.name} expects an unsigned 32-bit integer, got ${String(x)}`);
524
+ columns[ci][i] = x;
525
+ break;
526
+ case 'utf8':
527
+ if (typeof x !== 'string')
528
+ throw new Error(`facts@1: column ${c.name} expects a string`);
529
+ columns[ci][i] = x;
530
+ break;
531
+ case 'bool':
532
+ if (typeof x !== 'boolean')
533
+ throw new Error(`facts@1: column ${c.name} expects a boolean`);
534
+ columns[ci][i] = x ? 1 : 0;
535
+ break;
536
+ case 'dict': {
537
+ const k = typeof x === 'string' ? dicts[ci].get(x) : undefined;
538
+ if (k === undefined)
539
+ throw new Error(`facts@1: ${JSON.stringify(x)} is not in the ${c.name} vocabulary`);
540
+ columns[ci][i] = k;
541
+ break;
542
+ }
543
+ }
544
+ }
545
+ }
546
+ return { length: n, columns };
547
+ }
548
+ //# sourceMappingURL=table.js.map