@gtkx/codegen 0.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 (340) hide show
  1. package/LICENSE +373 -0
  2. package/dist/dsl/context.d.ts +104 -0
  3. package/dist/dsl/context.d.ts.map +1 -0
  4. package/dist/dsl/context.js +140 -0
  5. package/dist/dsl/context.js.map +1 -0
  6. package/dist/dsl/emit.d.ts +28 -0
  7. package/dist/dsl/emit.d.ts.map +1 -0
  8. package/dist/dsl/emit.js +41 -0
  9. package/dist/dsl/emit.js.map +1 -0
  10. package/dist/dsl/identifier.d.ts +28 -0
  11. package/dist/dsl/identifier.d.ts.map +1 -0
  12. package/dist/dsl/identifier.js +45 -0
  13. package/dist/dsl/identifier.js.map +1 -0
  14. package/dist/dsl/imports.d.ts +58 -0
  15. package/dist/dsl/imports.d.ts.map +1 -0
  16. package/dist/dsl/imports.js +107 -0
  17. package/dist/dsl/imports.js.map +1 -0
  18. package/dist/dsl/module.d.ts +53 -0
  19. package/dist/dsl/module.d.ts.map +1 -0
  20. package/dist/dsl/module.js +76 -0
  21. package/dist/dsl/module.js.map +1 -0
  22. package/dist/ffi/pipeline.d.ts +19 -0
  23. package/dist/ffi/pipeline.d.ts.map +1 -0
  24. package/dist/ffi/pipeline.js +107 -0
  25. package/dist/ffi/pipeline.js.map +1 -0
  26. package/dist/fingerprint.d.ts +30 -0
  27. package/dist/fingerprint.d.ts.map +1 -0
  28. package/dist/fingerprint.js +33 -0
  29. package/dist/fingerprint.js.map +1 -0
  30. package/dist/gi-store.d.ts +34 -0
  31. package/dist/gi-store.d.ts.map +1 -0
  32. package/dist/gi-store.js +154 -0
  33. package/dist/gi-store.js.map +1 -0
  34. package/dist/gir/boxed.d.ts +66 -0
  35. package/dist/gir/boxed.d.ts.map +1 -0
  36. package/dist/gir/boxed.js +49 -0
  37. package/dist/gir/boxed.js.map +1 -0
  38. package/dist/gir/callback.d.ts +15 -0
  39. package/dist/gir/callback.d.ts.map +1 -0
  40. package/dist/gir/callback.js +17 -0
  41. package/dist/gir/callback.js.map +1 -0
  42. package/dist/gir/class.d.ts +49 -0
  43. package/dist/gir/class.d.ts.map +1 -0
  44. package/dist/gir/class.js +42 -0
  45. package/dist/gir/class.js.map +1 -0
  46. package/dist/gir/enum.d.ts +39 -0
  47. package/dist/gir/enum.d.ts.map +1 -0
  48. package/dist/gir/enum.js +24 -0
  49. package/dist/gir/enum.js.map +1 -0
  50. package/dist/gir/field.d.ts +27 -0
  51. package/dist/gir/field.d.ts.map +1 -0
  52. package/dist/gir/field.js +19 -0
  53. package/dist/gir/field.js.map +1 -0
  54. package/dist/gir/function.d.ts +47 -0
  55. package/dist/gir/function.d.ts.map +1 -0
  56. package/dist/gir/function.js +28 -0
  57. package/dist/gir/function.js.map +1 -0
  58. package/dist/gir/namespace.d.ts +65 -0
  59. package/dist/gir/namespace.d.ts.map +1 -0
  60. package/dist/gir/namespace.js +60 -0
  61. package/dist/gir/namespace.js.map +1 -0
  62. package/dist/gir/parameter.d.ts +82 -0
  63. package/dist/gir/parameter.d.ts.map +1 -0
  64. package/dist/gir/parameter.js +66 -0
  65. package/dist/gir/parameter.js.map +1 -0
  66. package/dist/gir/parse.d.ts +78 -0
  67. package/dist/gir/parse.d.ts.map +1 -0
  68. package/dist/gir/parse.js +155 -0
  69. package/dist/gir/parse.js.map +1 -0
  70. package/dist/gir/primitives.d.ts +36 -0
  71. package/dist/gir/primitives.d.ts.map +1 -0
  72. package/dist/gir/primitives.js +109 -0
  73. package/dist/gir/primitives.js.map +1 -0
  74. package/dist/gir/property.d.ts +29 -0
  75. package/dist/gir/property.d.ts.map +1 -0
  76. package/dist/gir/property.js +16 -0
  77. package/dist/gir/property.js.map +1 -0
  78. package/dist/gir/qualified-name.d.ts +37 -0
  79. package/dist/gir/qualified-name.d.ts.map +1 -0
  80. package/dist/gir/qualified-name.js +39 -0
  81. package/dist/gir/qualified-name.js.map +1 -0
  82. package/dist/gir/qualify.d.ts +32 -0
  83. package/dist/gir/qualify.d.ts.map +1 -0
  84. package/dist/gir/qualify.js +60 -0
  85. package/dist/gir/qualify.js.map +1 -0
  86. package/dist/gir/repository.d.ts +82 -0
  87. package/dist/gir/repository.d.ts.map +1 -0
  88. package/dist/gir/repository.js +122 -0
  89. package/dist/gir/repository.js.map +1 -0
  90. package/dist/gir/signal.d.ts +22 -0
  91. package/dist/gir/signal.d.ts.map +1 -0
  92. package/dist/gir/signal.js +20 -0
  93. package/dist/gir/signal.js.map +1 -0
  94. package/dist/gir/size.d.ts +67 -0
  95. package/dist/gir/size.d.ts.map +1 -0
  96. package/dist/gir/size.js +87 -0
  97. package/dist/gir/size.js.map +1 -0
  98. package/dist/gir/type-ref.d.ts +79 -0
  99. package/dist/gir/type-ref.d.ts.map +1 -0
  100. package/dist/gir/type-ref.js +101 -0
  101. package/dist/gir/type-ref.js.map +1 -0
  102. package/dist/index.d.ts +12 -0
  103. package/dist/index.d.ts.map +1 -0
  104. package/dist/index.js +12 -0
  105. package/dist/index.js.map +1 -0
  106. package/dist/jsx-store.d.ts +22 -0
  107. package/dist/jsx-store.d.ts.map +1 -0
  108. package/dist/jsx-store.js +40 -0
  109. package/dist/jsx-store.js.map +1 -0
  110. package/dist/react/compounds-meta.d.ts +18 -0
  111. package/dist/react/compounds-meta.d.ts.map +1 -0
  112. package/dist/react/compounds-meta.js +93 -0
  113. package/dist/react/compounds-meta.js.map +1 -0
  114. package/dist/react/compounds.d.ts +27 -0
  115. package/dist/react/compounds.d.ts.map +1 -0
  116. package/dist/react/compounds.js +164 -0
  117. package/dist/react/compounds.js.map +1 -0
  118. package/dist/react/internal.d.ts +14 -0
  119. package/dist/react/internal.d.ts.map +1 -0
  120. package/dist/react/internal.js +79 -0
  121. package/dist/react/internal.js.map +1 -0
  122. package/dist/react/jsx.d.ts +22 -0
  123. package/dist/react/jsx.d.ts.map +1 -0
  124. package/dist/react/jsx.js +134 -0
  125. package/dist/react/jsx.js.map +1 -0
  126. package/dist/react/pipeline.d.ts +13 -0
  127. package/dist/react/pipeline.d.ts.map +1 -0
  128. package/dist/react/pipeline.js +25 -0
  129. package/dist/react/pipeline.js.map +1 -0
  130. package/dist/react/props.d.ts +38 -0
  131. package/dist/react/props.d.ts.map +1 -0
  132. package/dist/react/props.js +231 -0
  133. package/dist/react/props.js.map +1 -0
  134. package/dist/react/slot-props.d.ts +10 -0
  135. package/dist/react/slot-props.d.ts.map +1 -0
  136. package/dist/react/slot-props.js +56 -0
  137. package/dist/react/slot-props.js.map +1 -0
  138. package/dist/react/widgets.d.ts +83 -0
  139. package/dist/react/widgets.d.ts.map +1 -0
  140. package/dist/react/widgets.js +152 -0
  141. package/dist/react/widgets.js.map +1 -0
  142. package/dist/runner.d.ts +55 -0
  143. package/dist/runner.d.ts.map +1 -0
  144. package/dist/runner.js +60 -0
  145. package/dist/runner.js.map +1 -0
  146. package/dist/store-fs.d.ts +109 -0
  147. package/dist/store-fs.d.ts.map +1 -0
  148. package/dist/store-fs.js +98 -0
  149. package/dist/store-fs.js.map +1 -0
  150. package/dist/transpile.d.ts +32 -0
  151. package/dist/transpile.d.ts.map +1 -0
  152. package/dist/transpile.js +63 -0
  153. package/dist/transpile.js.map +1 -0
  154. package/dist/typecheck-store.d.ts +41 -0
  155. package/dist/typecheck-store.d.ts.map +1 -0
  156. package/dist/typecheck-store.js +102 -0
  157. package/dist/typecheck-store.js.map +1 -0
  158. package/dist/writers/alias.d.ts +13 -0
  159. package/dist/writers/alias.d.ts.map +1 -0
  160. package/dist/writers/alias.js +17 -0
  161. package/dist/writers/alias.js.map +1 -0
  162. package/dist/writers/async.d.ts +22 -0
  163. package/dist/writers/async.d.ts.map +1 -0
  164. package/dist/writers/async.js +48 -0
  165. package/dist/writers/async.js.map +1 -0
  166. package/dist/writers/boxed-constructor.d.ts +26 -0
  167. package/dist/writers/boxed-constructor.d.ts.map +1 -0
  168. package/dist/writers/boxed-constructor.js +85 -0
  169. package/dist/writers/boxed-constructor.js.map +1 -0
  170. package/dist/writers/boxed-field-accessor.d.ts +18 -0
  171. package/dist/writers/boxed-field-accessor.d.ts.map +1 -0
  172. package/dist/writers/boxed-field-accessor.js +260 -0
  173. package/dist/writers/boxed-field-accessor.js.map +1 -0
  174. package/dist/writers/boxed-layout.d.ts +27 -0
  175. package/dist/writers/boxed-layout.d.ts.map +1 -0
  176. package/dist/writers/boxed-layout.js +128 -0
  177. package/dist/writers/boxed-layout.js.map +1 -0
  178. package/dist/writers/boxed.d.ts +20 -0
  179. package/dist/writers/boxed.d.ts.map +1 -0
  180. package/dist/writers/boxed.js +71 -0
  181. package/dist/writers/boxed.js.map +1 -0
  182. package/dist/writers/callables.d.ts +124 -0
  183. package/dist/writers/callables.d.ts.map +1 -0
  184. package/dist/writers/callables.js +283 -0
  185. package/dist/writers/callables.js.map +1 -0
  186. package/dist/writers/callback.d.ts +16 -0
  187. package/dist/writers/callback.d.ts.map +1 -0
  188. package/dist/writers/callback.js +38 -0
  189. package/dist/writers/callback.js.map +1 -0
  190. package/dist/writers/class-struct-record.d.ts +31 -0
  191. package/dist/writers/class-struct-record.d.ts.map +1 -0
  192. package/dist/writers/class-struct-record.js +67 -0
  193. package/dist/writers/class-struct-record.js.map +1 -0
  194. package/dist/writers/class-struct.d.ts +22 -0
  195. package/dist/writers/class-struct.d.ts.map +1 -0
  196. package/dist/writers/class-struct.js +85 -0
  197. package/dist/writers/class-struct.js.map +1 -0
  198. package/dist/writers/class.d.ts +16 -0
  199. package/dist/writers/class.d.ts.map +1 -0
  200. package/dist/writers/class.js +307 -0
  201. package/dist/writers/class.js.map +1 -0
  202. package/dist/writers/constant.d.ts +15 -0
  203. package/dist/writers/constant.d.ts.map +1 -0
  204. package/dist/writers/constant.js +23 -0
  205. package/dist/writers/constant.js.map +1 -0
  206. package/dist/writers/constants.d.ts +15 -0
  207. package/dist/writers/constants.d.ts.map +1 -0
  208. package/dist/writers/constants.js +23 -0
  209. package/dist/writers/constants.js.map +1 -0
  210. package/dist/writers/constructor-props.d.ts +32 -0
  211. package/dist/writers/constructor-props.d.ts.map +1 -0
  212. package/dist/writers/constructor-props.js +110 -0
  213. package/dist/writers/constructor-props.js.map +1 -0
  214. package/dist/writers/enum.d.ts +21 -0
  215. package/dist/writers/enum.d.ts.map +1 -0
  216. package/dist/writers/enum.js +48 -0
  217. package/dist/writers/enum.js.map +1 -0
  218. package/dist/writers/function.d.ts +40 -0
  219. package/dist/writers/function.d.ts.map +1 -0
  220. package/dist/writers/function.js +127 -0
  221. package/dist/writers/function.js.map +1 -0
  222. package/dist/writers/gtype-binding.d.ts +19 -0
  223. package/dist/writers/gtype-binding.d.ts.map +1 -0
  224. package/dist/writers/gtype-binding.js +40 -0
  225. package/dist/writers/gtype-binding.js.map +1 -0
  226. package/dist/writers/inheritance.d.ts +48 -0
  227. package/dist/writers/inheritance.d.ts.map +1 -0
  228. package/dist/writers/inheritance.js +90 -0
  229. package/dist/writers/inheritance.js.map +1 -0
  230. package/dist/writers/interface.d.ts +16 -0
  231. package/dist/writers/interface.d.ts.map +1 -0
  232. package/dist/writers/interface.js +155 -0
  233. package/dist/writers/interface.js.map +1 -0
  234. package/dist/writers/method.d.ts +150 -0
  235. package/dist/writers/method.d.ts.map +1 -0
  236. package/dist/writers/method.js +510 -0
  237. package/dist/writers/method.js.map +1 -0
  238. package/dist/writers/param-classify.d.ts +93 -0
  239. package/dist/writers/param-classify.d.ts.map +1 -0
  240. package/dist/writers/param-classify.js +163 -0
  241. package/dist/writers/param-classify.js.map +1 -0
  242. package/dist/writers/property-accessor.d.ts +19 -0
  243. package/dist/writers/property-accessor.d.ts.map +1 -0
  244. package/dist/writers/property-accessor.js +117 -0
  245. package/dist/writers/property-accessor.js.map +1 -0
  246. package/dist/writers/registration.d.ts +27 -0
  247. package/dist/writers/registration.d.ts.map +1 -0
  248. package/dist/writers/registration.js +30 -0
  249. package/dist/writers/registration.js.map +1 -0
  250. package/dist/writers/return-wrap.d.ts +39 -0
  251. package/dist/writers/return-wrap.d.ts.map +1 -0
  252. package/dist/writers/return-wrap.js +152 -0
  253. package/dist/writers/return-wrap.js.map +1 -0
  254. package/dist/writers/runtime-override.d.ts +18 -0
  255. package/dist/writers/runtime-override.d.ts.map +1 -0
  256. package/dist/writers/runtime-override.js +44 -0
  257. package/dist/writers/runtime-override.js.map +1 -0
  258. package/dist/writers/signal.d.ts +21 -0
  259. package/dist/writers/signal.d.ts.map +1 -0
  260. package/dist/writers/signal.js +260 -0
  261. package/dist/writers/signal.js.map +1 -0
  262. package/dist/writers/ts-type.d.ts +50 -0
  263. package/dist/writers/ts-type.d.ts.map +1 -0
  264. package/dist/writers/ts-type.js +75 -0
  265. package/dist/writers/ts-type.js.map +1 -0
  266. package/dist/writers/types-ts.d.ts +27 -0
  267. package/dist/writers/types-ts.d.ts.map +1 -0
  268. package/dist/writers/types-ts.js +97 -0
  269. package/dist/writers/types-ts.js.map +1 -0
  270. package/dist/writers/value.d.ts +89 -0
  271. package/dist/writers/value.d.ts.map +1 -0
  272. package/dist/writers/value.js +402 -0
  273. package/dist/writers/value.js.map +1 -0
  274. package/package.json +55 -0
  275. package/src/dsl/context.ts +146 -0
  276. package/src/dsl/emit.ts +42 -0
  277. package/src/dsl/identifier.ts +52 -0
  278. package/src/dsl/imports.ts +109 -0
  279. package/src/dsl/module.ts +75 -0
  280. package/src/ffi/pipeline.ts +113 -0
  281. package/src/fingerprint.ts +50 -0
  282. package/src/gi-store.ts +199 -0
  283. package/src/gir/boxed.ts +94 -0
  284. package/src/gir/callback.ts +26 -0
  285. package/src/gir/class.ts +79 -0
  286. package/src/gir/enum.ts +55 -0
  287. package/src/gir/field.ts +40 -0
  288. package/src/gir/function.ts +67 -0
  289. package/src/gir/namespace.ts +118 -0
  290. package/src/gir/parameter.ts +120 -0
  291. package/src/gir/parse.ts +171 -0
  292. package/src/gir/primitives.ts +137 -0
  293. package/src/gir/property.ts +39 -0
  294. package/src/gir/qualified-name.ts +55 -0
  295. package/src/gir/qualify.ts +64 -0
  296. package/src/gir/repository.ts +149 -0
  297. package/src/gir/signal.ts +36 -0
  298. package/src/gir/size.ts +129 -0
  299. package/src/gir/type-ref.ts +180 -0
  300. package/src/index.ts +16 -0
  301. package/src/jsx-store.ts +53 -0
  302. package/src/react/compounds-meta.ts +99 -0
  303. package/src/react/compounds.ts +204 -0
  304. package/src/react/internal.ts +91 -0
  305. package/src/react/jsx.ts +155 -0
  306. package/src/react/pipeline.ts +29 -0
  307. package/src/react/props.ts +307 -0
  308. package/src/react/slot-props.ts +57 -0
  309. package/src/react/widgets.ts +185 -0
  310. package/src/runner.ts +89 -0
  311. package/src/store-fs.ts +157 -0
  312. package/src/transpile.ts +80 -0
  313. package/src/typecheck-store.ts +136 -0
  314. package/src/writers/alias.ts +19 -0
  315. package/src/writers/async.ts +44 -0
  316. package/src/writers/boxed-constructor.ts +105 -0
  317. package/src/writers/boxed-field-accessor.ts +306 -0
  318. package/src/writers/boxed-layout.ts +166 -0
  319. package/src/writers/boxed.ts +77 -0
  320. package/src/writers/callables.ts +339 -0
  321. package/src/writers/callback.ts +40 -0
  322. package/src/writers/class-struct-record.ts +75 -0
  323. package/src/writers/class-struct.ts +96 -0
  324. package/src/writers/class.ts +384 -0
  325. package/src/writers/constant.ts +29 -0
  326. package/src/writers/constructor-props.ts +130 -0
  327. package/src/writers/enum.ts +53 -0
  328. package/src/writers/function.ts +136 -0
  329. package/src/writers/gtype-binding.ts +48 -0
  330. package/src/writers/inheritance.ts +104 -0
  331. package/src/writers/interface.ts +168 -0
  332. package/src/writers/method.ts +678 -0
  333. package/src/writers/param-classify.ts +178 -0
  334. package/src/writers/property-accessor.ts +139 -0
  335. package/src/writers/registration.ts +42 -0
  336. package/src/writers/return-wrap.ts +180 -0
  337. package/src/writers/runtime-override.ts +59 -0
  338. package/src/writers/signal.ts +318 -0
  339. package/src/writers/ts-type.ts +115 -0
  340. package/src/writers/value.ts +485 -0
@@ -0,0 +1,199 @@
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { type CodegenFingerprint, FINGERPRINT_FILENAME } from "./fingerprint.js";
5
+ import { type StoreOptions, writeStore } from "./store-fs.js";
6
+ import { transpileSource } from "./transpile.js";
7
+ import { type StoreSourceFile, typecheckGiStore } from "./typecheck-store.js";
8
+
9
+ /**
10
+ * Absolute path to the hand-written augment overlay shipped with `@gtkx/codegen`.
11
+ *
12
+ * Resolves the same way whether codegen runs from `src/` (tsx) or `dist/`,
13
+ * since `overlay/` sits at the package root next to both.
14
+ */
15
+ const OVERLAY_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..", "overlay");
16
+
17
+ /**
18
+ * Options for assembling the injected `@gtkx/gi` package.
19
+ */
20
+ export type GiStoreOptions = StoreOptions & {
21
+ /** Real (symlink-resolved) directory of the installed `@gtkx/ffi`. */
22
+ readonly realFfiDir: string;
23
+ /** Real (symlink-resolved) directory of the installed `@gtkx/native`. */
24
+ readonly realNativeDir: string;
25
+ };
26
+
27
+ /** Per-namespace inputs the store assembler turns into gi modules. */
28
+ export type GiNamespaceInput = {
29
+ /** Lowercased namespace directory name (e.g. `"gtk"`). */
30
+ readonly directory: string;
31
+ /** Transpilable raw module source, or `null` for augment-only namespaces (gl). */
32
+ readonly rawSource: string | null;
33
+ };
34
+
35
+ /** Returns the augment `.ts` files for a namespace, or `[]` when it is pure pass-through. */
36
+ const overlayFiles = (directory: string): readonly string[] => {
37
+ const dir = join(OVERLAY_ROOT, directory);
38
+ if (!existsSync(dir)) return [];
39
+ return readdirSync(dir).filter((name) => name.endsWith(".ts") && name !== "index.ts");
40
+ };
41
+
42
+ const isAugmented = (directory: string): boolean => existsSync(join(OVERLAY_ROOT, directory, "index.ts"));
43
+
44
+ /**
45
+ * Whether an augmented namespace's barrel re-exports a generated module (and so
46
+ * may only be emitted when that namespace is generated), as opposed to a
47
+ * standalone overlay like `gl` that has no GIR backing.
48
+ */
49
+ const barrelNeedsGenerated = (directory: string): boolean =>
50
+ readFileSync(join(OVERLAY_ROOT, directory, "index.ts"), "utf8").includes("../generated/");
51
+
52
+ /**
53
+ * Retargets a barrel-template specifier from its source layout (under
54
+ * `overlay/<ns>/`) to its emitted layout (under `gi/<ns>/`): the namespace's
55
+ * own generated module becomes a sibling, augment files move under `augment/`,
56
+ * and relative runtime modules collapse to the `@gtkx/ffi` barrel. The
57
+ * `@gtkx/native` specifier passes through so low-level primitives resolve
58
+ * straight to the native package.
59
+ */
60
+ const retargetBarrelSpecifier = (spec: string, directory: string): string => {
61
+ const generated = spec.match(/^\.\.\/generated\/[a-z0-9]+\/([a-z0-9]+)\.js$/);
62
+ if (generated) return `./${generated[1]}.js`;
63
+ if (spec.startsWith("../")) return "@gtkx/ffi";
64
+ const sibling = spec.match(/^\.\/([a-z0-9-]+)\.js$/);
65
+ if (sibling) {
66
+ return existsSync(join(OVERLAY_ROOT, directory, `${sibling[1]}.ts`))
67
+ ? `./augment/${sibling[1]}.js`
68
+ : "@gtkx/ffi";
69
+ }
70
+ return spec;
71
+ };
72
+
73
+ const barrelSource = (directory: string): string => {
74
+ if (!isAugmented(directory)) {
75
+ return `export * from "./${directory}.js";\n`;
76
+ }
77
+ const template = readFileSync(join(OVERLAY_ROOT, directory, "index.ts"), "utf8");
78
+ return template.replace(/"([^"]+)"/g, (_match, spec) => `"${retargetBarrelSpecifier(spec, directory)}"`);
79
+ };
80
+
81
+ type CollectedFile = {
82
+ readonly stem: string;
83
+ readonly fileName: string;
84
+ readonly source: string;
85
+ readonly overlay: boolean;
86
+ };
87
+
88
+ /**
89
+ * Gathers the per-namespace generated module, augment overlay, and barrel
90
+ * sources into a flat file list, building the package `exports` map alongside.
91
+ */
92
+ const collectStoreSources = (
93
+ directories: ReadonlySet<string>,
94
+ standaloneSet: ReadonlySet<string>,
95
+ rawByDirectory: ReadonlyMap<string, string | null>,
96
+ ): { collected: CollectedFile[]; exportsMap: Record<string, unknown> } => {
97
+ const exportsMap: Record<string, unknown> = { "./package.json": "./package.json" };
98
+ const collected: CollectedFile[] = [];
99
+ for (const directory of directories) {
100
+ const rawSource = standaloneSet.has(directory) ? null : (rawByDirectory.get(directory) ?? null);
101
+ if (rawSource !== null) {
102
+ collected.push({
103
+ stem: `${directory}/${directory}`,
104
+ fileName: `${directory}/${directory}.ts`,
105
+ source: rawSource,
106
+ overlay: false,
107
+ });
108
+ exportsMap[`./${directory}/${directory}.js`] = {
109
+ types: `./${directory}/${directory}.d.ts`,
110
+ default: `./${directory}/${directory}.js`,
111
+ };
112
+ }
113
+ for (const file of overlayFiles(directory)) {
114
+ collected.push({
115
+ stem: `${directory}/augment/${file.slice(0, -".ts".length)}`,
116
+ fileName: `${directory}/augment/${file}`,
117
+ source: readFileSync(join(OVERLAY_ROOT, directory, file), "utf8"),
118
+ overlay: true,
119
+ });
120
+ }
121
+ collected.push({
122
+ stem: `${directory}/index`,
123
+ fileName: `${directory}/index.ts`,
124
+ source: barrelSource(directory),
125
+ overlay: false,
126
+ });
127
+ exportsMap[`./${directory}`] = {
128
+ types: `./${directory}/index.d.ts`,
129
+ default: `./${directory}/index.js`,
130
+ };
131
+ }
132
+ return { collected, exportsMap };
133
+ };
134
+
135
+ /**
136
+ * Assembles the self-contained injected `@gtkx/gi` package.
137
+ *
138
+ * Writes every namespace's raw generated module, its augment overlay, and its
139
+ * barrel into a temporary store, emits the `package.json` and gi's own
140
+ * `node_modules/@gtkx/{ffi,gi}` symlinks — the self-referential `@gtkx/gi` link
141
+ * lets augment modules resolve sibling namespaces without relying on the
142
+ * project's top-level alias — swaps it into place, and points the visible
143
+ * `node_modules/@gtkx/gi` alias at it.
144
+ *
145
+ * @param options - Resolved store/link/dependency paths
146
+ * @param namespaces - Per-namespace raw module inputs
147
+ * @param fingerprint - Staleness sentinel written into the store, when provided
148
+ */
149
+ export const writeGiStore = (
150
+ options: GiStoreOptions,
151
+ namespaces: readonly GiNamespaceInput[],
152
+ fingerprint?: CodegenFingerprint,
153
+ ): void => {
154
+ const standaloneOverlays = readdirSync(OVERLAY_ROOT).filter(
155
+ (name) => isAugmented(name) && !barrelNeedsGenerated(name),
156
+ );
157
+ const standaloneSet = new Set(standaloneOverlays);
158
+ const directories = new Set<string>([...namespaces.map((n) => n.directory), ...standaloneOverlays]);
159
+ const rawByDirectory = new Map(namespaces.map((n) => [n.directory, n.rawSource]));
160
+
161
+ const { collected, exportsMap } = collectStoreSources(directories, standaloneSet, rawByDirectory);
162
+
163
+ writeStore({
164
+ storeDir: options.storeDir,
165
+ linkDir: options.linkDir,
166
+ files: collected.map((file) => ({ stem: file.stem, fileName: file.fileName, source: file.source })),
167
+ manifest: {
168
+ name: "@gtkx/gi",
169
+ type: "module",
170
+ version: options.version,
171
+ sideEffects: true,
172
+ exports: exportsMap,
173
+ },
174
+ rawFiles:
175
+ fingerprint === undefined
176
+ ? []
177
+ : [{ relativePath: FINGERPRINT_FILENAME, content: `${JSON.stringify(fingerprint, null, 2)}\n` }],
178
+ symlinks: [
179
+ { segments: ["node_modules", "@gtkx", "ffi"], target: options.realFfiDir },
180
+ { segments: ["node_modules", "@gtkx", "native"], target: options.realNativeDir },
181
+ { segments: ["node_modules", "@gtkx", "gi"], target: "self" },
182
+ ],
183
+ validate: (tmp) => {
184
+ const sources: StoreSourceFile[] = collected.map((file) =>
185
+ file.overlay
186
+ ? { path: join(tmp, file.fileName), source: file.source, overlay: true }
187
+ : {
188
+ path: join(tmp, `${file.stem}.d.ts`),
189
+ source: transpileSource(file.fileName, file.source).dts,
190
+ overlay: false,
191
+ },
192
+ );
193
+ typecheckGiStore(tmp, sources, {
194
+ ffiEntry: join(options.realFfiDir, "src", "index.ts"),
195
+ nativeEntry: join(options.realNativeDir, "dist", "index.d.ts"),
196
+ });
197
+ },
198
+ });
199
+ };
@@ -0,0 +1,94 @@
1
+ import { fieldFromNode, type GirField } from "./field.js";
2
+ import { functionFromNode, type GirFunction } from "./function.js";
3
+ import { attr, attrBool, childrenOf, GIR_CONSTRUCTOR_TAG, type RawNode } from "./parse.js";
4
+
5
+ /**
6
+ * Discriminator for the different shapes a `<record>` (or `<glib:boxed>`)
7
+ * can take in GIR.
8
+ *
9
+ * - `boxed` — a record with a `glib:get-type`; registers as a GObject boxed.
10
+ * - `plain-struct` — a record without GType; passed by pointer using
11
+ * `t.struct(ownership)`.
12
+ * - `vtable` — a record carrying `glib:is-gtype-struct-for`; its fields are
13
+ * function pointers consumed by the class struct registration.
14
+ * - `glib-boxed` — a top-level `<glib:boxed>` element with no struct body.
15
+ */
16
+ export type BoxedKind = "boxed" | "plain-struct" | "vtable" | "glib-boxed";
17
+
18
+ /** A `<record>`, `<glib:boxed>`, or `<union>` declaration. */
19
+ export type GirBoxed = {
20
+ readonly kind: BoxedKind;
21
+ /** Local name inside the namespace (no prefix). */
22
+ readonly name: string;
23
+ readonly cType: string | undefined;
24
+ /** GLib type name (e.g. `"GtkBorder"`); absent on plain structs. */
25
+ readonly glibTypeName: string | undefined;
26
+ /** GLib get-type C symbol; absent on plain structs. */
27
+ readonly glibGetType: string | undefined;
28
+ /** Class name this record is the vtable for (`glib:is-gtype-struct-for`). */
29
+ readonly glibIsGTypeStructFor: string | undefined;
30
+ /** GLib ref/unref/etc. function names on fundamentals. */
31
+ readonly glibRefFunc: string | undefined;
32
+ readonly glibUnrefFunc: string | undefined;
33
+ readonly glibSetValueFunc: string | undefined;
34
+ readonly glibGetValueFunc: string | undefined;
35
+ /** `copy-function`/`free-function` — the ref/unref pair for records that are reference-counted rather than g_boxed-copyable. */
36
+ readonly copyFunc: string | undefined;
37
+ readonly freeFunc: string | undefined;
38
+ /** `disguised="1"` — record has no introspectable fields. */
39
+ readonly disguised: boolean;
40
+ /** `opaque="1"` — record is opaque to callers. */
41
+ readonly opaque: boolean;
42
+ readonly introspectable: boolean;
43
+ readonly fields: readonly GirField[];
44
+ readonly methods: readonly GirFunction[];
45
+ readonly constructors: readonly GirFunction[];
46
+ readonly functions: readonly GirFunction[];
47
+ /** `true` when the source element was `<union>` rather than `<record>`. */
48
+ readonly isUnion: boolean;
49
+ };
50
+
51
+ /**
52
+ * Builds a {@link GirBoxed} from a `<record>`, `<union>`, or `<glib:boxed>` element.
53
+ *
54
+ * @param node - The XML element
55
+ * @param kind - The kind inferred from element name and attribute set
56
+ * @param isUnion - `true` when the source element was `<union>`
57
+ */
58
+ export const boxedFromNode = (node: RawNode, kind: BoxedKind, isUnion: boolean): GirBoxed => ({
59
+ kind,
60
+ name: attr(node, "name") ?? attr(node, "glib:name") ?? "",
61
+ cType: attr(node, "c:type"),
62
+ glibTypeName: attr(node, "glib:type-name"),
63
+ glibGetType: attr(node, "glib:get-type"),
64
+ glibIsGTypeStructFor: attr(node, "glib:is-gtype-struct-for"),
65
+ glibRefFunc: attr(node, "glib:ref-func"),
66
+ glibUnrefFunc: attr(node, "glib:unref-func"),
67
+ glibSetValueFunc: attr(node, "glib:set-value-func"),
68
+ glibGetValueFunc: attr(node, "glib:get-value-func"),
69
+ copyFunc: attr(node, "copy-function"),
70
+ freeFunc: attr(node, "free-function"),
71
+ disguised: attrBool(node, "disguised"),
72
+ opaque: attrBool(node, "opaque"),
73
+ introspectable: attr(node, "introspectable") !== "0",
74
+ fields: childrenOf(node, "field").map(fieldFromNode),
75
+ methods: childrenOf(node, "method").map((method) => functionFromNode(method, "method")),
76
+ constructors: childrenOf(node, GIR_CONSTRUCTOR_TAG).map((ctor) => functionFromNode(ctor, "constructor")),
77
+ functions: childrenOf(node, "function").map((function_) => functionFromNode(function_, "function")),
78
+ isUnion,
79
+ });
80
+
81
+ /**
82
+ * Decides which {@link BoxedKind} applies to a `<record>` or `<union>` element.
83
+ *
84
+ * Vtable records (`glib:is-gtype-struct-for`) take precedence; otherwise a
85
+ * `glib:get-type` flips the record to a registered boxed; otherwise it's a
86
+ * plain struct.
87
+ *
88
+ * @param node - The `<record>` or `<union>` element
89
+ */
90
+ export const boxedKind = (node: RawNode): BoxedKind => {
91
+ if (attr(node, "glib:is-gtype-struct-for") !== undefined) return "vtable";
92
+ if (attr(node, "glib:get-type") !== undefined) return "boxed";
93
+ return "plain-struct";
94
+ };
@@ -0,0 +1,26 @@
1
+ import { type GirParameter, type GirReturnValue, parameterFromNode, returnValueFromNode } from "./parameter.js";
2
+ import { attr, childOf, childrenOf, type RawNode } from "./parse.js";
3
+
4
+ /** A `<callback>` declaration (top-level inside a namespace or nested in a field). */
5
+ export type GirCallback = {
6
+ readonly name: string;
7
+ readonly cType: string | undefined;
8
+ readonly parameters: readonly GirParameter[];
9
+ readonly returnValue: GirReturnValue;
10
+ readonly introspectable: boolean;
11
+ };
12
+
13
+ /**
14
+ * Builds a {@link GirCallback} from a `<callback>` element.
15
+ */
16
+ export const callbackFromNode = (node: RawNode): GirCallback => {
17
+ const parametersNode = childOf(node, "parameters");
18
+ const parameterNodes = childrenOf(parametersNode, "parameter");
19
+ return {
20
+ name: attr(node, "name") ?? "",
21
+ cType: attr(node, "c:type"),
22
+ parameters: parameterNodes.map((parameter) => parameterFromNode(parameter, false)),
23
+ returnValue: returnValueFromNode(childOf(node, "return-value")),
24
+ introspectable: attr(node, "introspectable") !== "0",
25
+ };
26
+ };
@@ -0,0 +1,79 @@
1
+ import { fieldFromNode, type GirField } from "./field.js";
2
+ import { functionFromNode, type GirFunction } from "./function.js";
3
+ import { attr, attrBool, childrenOf, GIR_CONSTRUCTOR_TAG, type RawNode } from "./parse.js";
4
+ import { type GirProperty, propertyFromNode } from "./property.js";
5
+ import { type GirSignal, signalFromNode } from "./signal.js";
6
+
7
+ /** A `<class>` or `<interface>` declaration. */
8
+ export type GirClass = {
9
+ /** Local name inside the namespace (no prefix). */
10
+ readonly name: string;
11
+ readonly cType: string | undefined;
12
+ /** GIR `parent`, possibly cross-namespace (e.g. `"GObject.InitiallyUnowned"`). */
13
+ readonly parent: string | undefined;
14
+ /** GLib type name (e.g. `"GtkWidget"`). */
15
+ readonly glibTypeName: string | undefined;
16
+ /** C symbol that returns the GType integer (e.g. `"gtk_widget_get_type"`). */
17
+ readonly glibGetType: string | undefined;
18
+ /** Local record name that holds this class's vtable (`glib:type-struct`). */
19
+ readonly glibTypeStruct: string | undefined;
20
+ /** Glib ref/unref/etc. function names on fundamentals; not used for GObject subclasses. */
21
+ readonly glibRefFunc: string | undefined;
22
+ readonly glibUnrefFunc: string | undefined;
23
+ readonly glibSetValueFunc: string | undefined;
24
+ readonly glibGetValueFunc: string | undefined;
25
+ /** `glib:fundamental="1"` — a non-GObject fundamental type (e.g. `GdkEvent`, `GskRenderNode`). */
26
+ readonly fundamental: boolean;
27
+ readonly abstract: boolean;
28
+ readonly final: boolean;
29
+ readonly isInterface: boolean;
30
+ readonly introspectable: boolean;
31
+ /** Names of interfaces this class implements (cross-namespace possible). */
32
+ readonly implements: readonly string[];
33
+ /** Names of interfaces a `<interface>` requires (`<prerequisite>` elements). */
34
+ readonly prerequisites: readonly string[];
35
+ readonly methods: readonly GirFunction[];
36
+ readonly constructors: readonly GirFunction[];
37
+ readonly functions: readonly GirFunction[];
38
+ readonly virtualMethods: readonly GirFunction[];
39
+ readonly properties: readonly GirProperty[];
40
+ readonly signals: readonly GirSignal[];
41
+ readonly fields: readonly GirField[];
42
+ };
43
+
44
+ /**
45
+ * Builds a {@link GirClass} from a `<class>` or `<interface>` element.
46
+ *
47
+ * @param node - The XML element
48
+ * @param isInterface - `true` when the source element was `<interface>`
49
+ */
50
+ export const classFromNode = (node: RawNode, isInterface: boolean): GirClass => ({
51
+ name: attr(node, "name") ?? "",
52
+ cType: attr(node, "c:type"),
53
+ parent: attr(node, "parent"),
54
+ glibTypeName: attr(node, "glib:type-name"),
55
+ glibGetType: attr(node, "glib:get-type"),
56
+ glibTypeStruct: attr(node, "glib:type-struct"),
57
+ glibRefFunc: attr(node, "glib:ref-func"),
58
+ glibUnrefFunc: attr(node, "glib:unref-func"),
59
+ glibSetValueFunc: attr(node, "glib:set-value-func"),
60
+ glibGetValueFunc: attr(node, "glib:get-value-func"),
61
+ fundamental: attrBool(node, "glib:fundamental"),
62
+ abstract: attrBool(node, "abstract"),
63
+ final: attrBool(node, "final"),
64
+ isInterface,
65
+ introspectable: attr(node, "introspectable") !== "0",
66
+ implements: childrenOf(node, "implements")
67
+ .map((implement) => attr(implement, "name"))
68
+ .filter((name): name is string => name !== undefined),
69
+ prerequisites: childrenOf(node, "prerequisite")
70
+ .map((prerequisite) => attr(prerequisite, "name"))
71
+ .filter((name): name is string => name !== undefined),
72
+ methods: childrenOf(node, "method").map((method) => functionFromNode(method, "method")),
73
+ constructors: childrenOf(node, GIR_CONSTRUCTOR_TAG).map((ctor) => functionFromNode(ctor, "constructor")),
74
+ functions: childrenOf(node, "function").map((function_) => functionFromNode(function_, "function")),
75
+ virtualMethods: childrenOf(node, "virtual-method").map((vmethod) => functionFromNode(vmethod, "virtual-method")),
76
+ properties: childrenOf(node, "property").map(propertyFromNode),
77
+ signals: childrenOf(node, "glib:signal").map(signalFromNode),
78
+ fields: childrenOf(node, "field").map(fieldFromNode),
79
+ });
@@ -0,0 +1,55 @@
1
+ import { functionFromNode, type GirFunction } from "./function.js";
2
+ import { attr, childrenOf, type RawNode } from "./parse.js";
3
+
4
+ /** A single `<member>` of an `<enumeration>` or `<bitfield>`. */
5
+ type EnumMember = {
6
+ /** GIR `name`, snake_case lowercase (e.g. `"fill"`). */
7
+ readonly name: string;
8
+ /** GIR `c:identifier`, SCREAMING_SNAKE_CASE (e.g. `"GTK_ALIGN_FILL"`). */
9
+ readonly cIdentifier: string | undefined;
10
+ /** Numeric value as a string; preserved verbatim from GIR. */
11
+ readonly value: string;
12
+ };
13
+
14
+ /** Discriminator for enums vs bitfields. */
15
+ export type EnumKind = "enumeration" | "bitfield";
16
+
17
+ /** An `<enumeration>` or `<bitfield>` declaration. */
18
+ export type GirEnum = {
19
+ readonly kind: EnumKind;
20
+ /** Local name within the namespace (no namespace prefix). */
21
+ readonly name: string;
22
+ readonly cType: string | undefined;
23
+ /** GLib type name (e.g. `"GtkAlign"`); absent for unregistered enums. */
24
+ readonly glibTypeName: string | undefined;
25
+ /** GLib get-type C symbol; absent for unregistered enums. */
26
+ readonly glibGetType: string | undefined;
27
+ /** Optional `glib:error-domain` marker; presence flips emission to `createErrorDomain`. */
28
+ readonly errorDomain: string | undefined;
29
+ readonly introspectable: boolean;
30
+ readonly members: readonly EnumMember[];
31
+ /** Free functions nested inside the enum (e.g. error quark accessors). */
32
+ readonly functions: readonly GirFunction[];
33
+ };
34
+
35
+ /**
36
+ * Builds a {@link GirEnum} from an `<enumeration>` or `<bitfield>` element.
37
+ *
38
+ * @param node - The XML element
39
+ * @param kind - `"enumeration"` or `"bitfield"`, matching the element name
40
+ */
41
+ export const enumFromNode = (node: RawNode, kind: EnumKind): GirEnum => ({
42
+ kind,
43
+ name: attr(node, "name") ?? "",
44
+ cType: attr(node, "c:type"),
45
+ glibTypeName: attr(node, "glib:type-name"),
46
+ glibGetType: attr(node, "glib:get-type"),
47
+ errorDomain: attr(node, "glib:error-domain"),
48
+ introspectable: attr(node, "introspectable") !== "0",
49
+ members: childrenOf(node, "member").map((member) => ({
50
+ name: attr(member, "name") ?? "",
51
+ cIdentifier: attr(member, "c:identifier"),
52
+ value: attr(member, "value") ?? "0",
53
+ })),
54
+ functions: childrenOf(node, "function").map((function_) => functionFromNode(function_, "function")),
55
+ });
@@ -0,0 +1,40 @@
1
+ import { attr, attrBool, childOf, type RawNode } from "./parse.js";
2
+ import { type GirTypeRef, typeRefFromSlot } from "./type-ref.js";
3
+
4
+ /**
5
+ * A `<field>` inside a class, interface, record, or union.
6
+ *
7
+ * Vtable slots (fields holding `<callback>` children inside a
8
+ * `glib:is-gtype-struct-for` record) keep `callback` populated; ordinary
9
+ * data fields keep `type`. The two are mutually exclusive.
10
+ */
11
+ export type GirField = {
12
+ /** GIR field name, snake_case. */
13
+ readonly name: string;
14
+ /** Field type, or `undefined` when the field is a vtable callback slot. */
15
+ readonly type: GirTypeRef | undefined;
16
+ /** Inline callback for vtable slots; `undefined` for data fields. */
17
+ readonly callback: RawNode | undefined;
18
+ readonly readable: boolean;
19
+ readonly writable: boolean;
20
+ readonly private: boolean;
21
+ /** GIR `bits` attribute width when the field is a bitfield member. */
22
+ readonly bits: number | undefined;
23
+ };
24
+
25
+ /**
26
+ * Builds a {@link GirField} from a `<field>` element.
27
+ */
28
+ export const fieldFromNode = (node: RawNode): GirField => {
29
+ const callback = childOf(node, "callback");
30
+ const bitsAttr = attr(node, "bits");
31
+ return {
32
+ name: attr(node, "name") ?? "",
33
+ type: callback === undefined ? typeRefFromSlot(node) : undefined,
34
+ callback,
35
+ readable: attrBool(node, "readable", true),
36
+ writable: attrBool(node, "writable", false),
37
+ private: attrBool(node, "private", false),
38
+ bits: bitsAttr === undefined ? undefined : Number.parseInt(bitsAttr, 10),
39
+ };
40
+ };
@@ -0,0 +1,67 @@
1
+ import { type GirParameter, type GirReturnValue, parameterFromNode, returnValueFromNode } from "./parameter.js";
2
+ import { attr, attrBool, childOf, childrenOf, type RawNode } from "./parse.js";
3
+
4
+ /**
5
+ * Whether this callable is a free function, a static method on a type, an
6
+ * instance method, a constructor, or a virtual method.
7
+ *
8
+ * The kind controls call-site shape in the writers: instance methods pass
9
+ * `self` as the first FFI argument, constructors return a fresh native
10
+ * handle, etc.
11
+ */
12
+ export type FunctionKind = "function" | "method" | "constructor" | "virtual-method";
13
+
14
+ /**
15
+ * A callable declaration: `<function>`, `<method>`, `<constructor>`, or
16
+ * `<virtual-method>`.
17
+ */
18
+ export type GirFunction = {
19
+ readonly kind: FunctionKind;
20
+ /** GIR `name`, snake_case (e.g. `"set_label"`). */
21
+ readonly name: string;
22
+ /** C symbol identifier passed to `t.fn(library, symbol, …)`. */
23
+ readonly cIdentifier: string | undefined;
24
+ /** `throws="1"` — appends an implicit `GError**` out-parameter. */
25
+ readonly throws: boolean;
26
+ /** `introspectable="0"` — the function is skipped in JS output. */
27
+ readonly introspectable: boolean;
28
+ /** Indicates the function is the preferred binding for the named method. */
29
+ readonly shadows: string | undefined;
30
+ /** Indicates a different function is the preferred binding (the shadower). */
31
+ readonly shadowedBy: string | undefined;
32
+ /** Marker that re-homes a namespace function onto a type method. */
33
+ readonly movedTo: string | undefined;
34
+ /** The instance parameter for methods; `undefined` otherwise. */
35
+ readonly instance: GirParameter | undefined;
36
+ /** Regular parameters (no instance and no implicit GError). */
37
+ readonly parameters: readonly GirParameter[];
38
+ readonly returnValue: GirReturnValue;
39
+ /** Linked invoker method when this is a `<virtual-method>`. */
40
+ readonly invoker: string | undefined;
41
+ };
42
+
43
+ /**
44
+ * Builds a {@link GirFunction} from a callable element.
45
+ *
46
+ * @param node - The XML element
47
+ * @param kind - The callable kind matching the element name
48
+ */
49
+ export const functionFromNode = (node: RawNode, kind: FunctionKind): GirFunction => {
50
+ const parametersNode = childOf(node, "parameters");
51
+ const instanceNode = childOf(parametersNode, "instance-parameter");
52
+ const parameterNodes = childrenOf(parametersNode, "parameter");
53
+ return {
54
+ kind,
55
+ name: attr(node, "shadows") ?? attr(node, "name") ?? "",
56
+ cIdentifier: attr(node, "c:identifier"),
57
+ throws: attrBool(node, "throws"),
58
+ introspectable: attr(node, "introspectable") !== "0",
59
+ shadows: attr(node, "shadows"),
60
+ shadowedBy: attr(node, "shadowed-by"),
61
+ movedTo: attr(node, "moved-to"),
62
+ instance: instanceNode === undefined ? undefined : parameterFromNode(instanceNode, true),
63
+ parameters: parameterNodes.map((parameter) => parameterFromNode(parameter, false)),
64
+ returnValue: returnValueFromNode(childOf(node, "return-value")),
65
+ invoker: attr(node, "invoker"),
66
+ };
67
+ };