@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,157 @@
1
+ import { existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
2
+ import { dirname, join, relative } from "node:path";
3
+ import { transpileSource } from "./transpile.js";
4
+
5
+ /**
6
+ * Filesystem primitives shared by the injected-package assemblers
7
+ * ({@link ../gi-store.js}, {@link ../jsx-store.js}).
8
+ */
9
+
10
+ /**
11
+ * Fields every injected-package store assembler needs: where the hidden store
12
+ * lives, the visible alias symlink, and the version stamped onto its manifest.
13
+ */
14
+ export type StoreOptions = {
15
+ /** Absolute path to the hidden store directory (e.g. `node_modules/.gtkx/gi`). */
16
+ readonly storeDir: string;
17
+ /** Absolute path to the visible alias symlink (e.g. `node_modules/@gtkx/gi`). */
18
+ readonly linkDir: string;
19
+ /** Version string copied onto the emitted package's `package.json`. */
20
+ readonly version: string;
21
+ };
22
+
23
+ /** A transpilable source destined for the store, addressed by output stem. */
24
+ export type StoreFile = {
25
+ /** Output path without extension, relative to the store root. */
26
+ readonly stem: string;
27
+ /** Source filename TypeScript uses for diagnostics. */
28
+ readonly fileName: string;
29
+ /** The TypeScript source to transpile and write. */
30
+ readonly source: string;
31
+ };
32
+
33
+ /** A symlink to create inside the store's own bundled `node_modules`. */
34
+ export type StoreSymlink = {
35
+ /** Path segments under the store root the symlink is created at. */
36
+ readonly segments: readonly string[];
37
+ /** Absolute symlink target, or `"self"` for the store root itself. */
38
+ readonly target: string | "self";
39
+ };
40
+
41
+ /**
42
+ * Assembles a store in a fresh temp directory and atomically swaps it into
43
+ * place: writes every file pair, the `package.json`, the bundled
44
+ * `node_modules` symlinks, then promotes the temp tree and re-points the
45
+ * visible alias. An optional {@link WriteStoreParams.validate} runs against the
46
+ * temp root before any file is written, so a failure aborts before the store
47
+ * goes live.
48
+ *
49
+ * @param params - {@link WriteStoreParams}
50
+ */
51
+ export const writeStore = (params: WriteStoreParams): void => {
52
+ const tmp = tempStoreFor(params.storeDir);
53
+ params.validate?.(tmp);
54
+ for (const file of params.files) {
55
+ writeFilePair(tmp, file.stem, file.fileName, file.source);
56
+ }
57
+ writePackageJson(tmp, params.manifest);
58
+ for (const raw of params.rawFiles ?? []) {
59
+ writeFileSync(join(tmp, raw.relativePath), raw.content);
60
+ }
61
+ for (const { segments, target } of params.symlinks) {
62
+ symlinkRelative(join(tmp, ...segments), target === "self" ? tmp : target);
63
+ }
64
+ swapStore(tmp, params.storeDir, params.linkDir);
65
+ };
66
+
67
+ /** Inputs for {@link writeStore}. */
68
+ export type WriteStoreParams = {
69
+ /** Absolute hidden store directory. */
70
+ readonly storeDir: string;
71
+ /** Absolute visible alias symlink. */
72
+ readonly linkDir: string;
73
+ /** The source files to transpile and write. */
74
+ readonly files: readonly StoreFile[];
75
+ /** The `package.json` manifest to serialize. */
76
+ readonly manifest: unknown;
77
+ /** The bundled `node_modules` symlinks to create. */
78
+ readonly symlinks: readonly StoreSymlink[];
79
+ /** Verbatim files (e.g. a fingerprint sentinel) written under the store root. */
80
+ readonly rawFiles?: readonly { readonly relativePath: string; readonly content: string }[];
81
+ /** Optional in-memory validation run on the temp root before files are written. */
82
+ readonly validate?: (tmp: string) => void;
83
+ };
84
+
85
+ /**
86
+ * Transpiles `source` and writes the resulting `.js`/`.d.ts` pair at `stem`
87
+ * (relative to `storeDir`), creating parent directories as needed.
88
+ *
89
+ * @param storeDir - Absolute store root the file is written under
90
+ * @param stem - Output path without extension, relative to `storeDir`
91
+ * @param fileName - Source filename TypeScript uses for diagnostics
92
+ * @param source - The TypeScript source to transpile
93
+ */
94
+ export const writeFilePair = (storeDir: string, stem: string, fileName: string, source: string): void => {
95
+ const { js, dts } = transpileSource(fileName, source);
96
+ const jsPath = join(storeDir, `${stem}.js`);
97
+ mkdirSync(dirname(jsPath), { recursive: true });
98
+ writeFileSync(jsPath, js);
99
+ writeFileSync(join(storeDir, `${stem}.d.ts`), dts);
100
+ };
101
+
102
+ /**
103
+ * Creates (or replaces) a relative directory symlink at `linkPath` pointing at
104
+ * `realTarget`. Relative so the link survives the store being moved or cached.
105
+ *
106
+ * @param linkPath - Absolute path of the symlink to create
107
+ * @param realTarget - Absolute path the symlink resolves to
108
+ */
109
+ export const symlinkRelative = (linkPath: string, realTarget: string): void => {
110
+ mkdirSync(dirname(linkPath), { recursive: true });
111
+ rmSync(linkPath, { recursive: true, force: true });
112
+ symlinkSync(relative(dirname(linkPath), realTarget), linkPath, "dir");
113
+ };
114
+
115
+ /**
116
+ * Writes a pretty-printed `package.json` into `storeDir`.
117
+ *
118
+ * @param storeDir - Absolute directory to write `package.json` into
119
+ * @param manifest - The manifest object to serialize
120
+ */
121
+ export const writePackageJson = (storeDir: string, manifest: unknown): void => {
122
+ writeFileSync(join(storeDir, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`);
123
+ };
124
+
125
+ /**
126
+ * Swaps a fully-assembled temp store into place and re-points the visible alias
127
+ * at it.
128
+ *
129
+ * The live store is renamed aside and the temp store renamed in before the old
130
+ * tree is deleted, so the store is unavailable only for the duration of a single
131
+ * rename instead of a full recursive delete. Not safe against a second codegen
132
+ * run swapping the same store concurrently.
133
+ *
134
+ * @param tmp - Absolute temp store directory to promote
135
+ * @param storeDir - Absolute final store directory
136
+ * @param visibleLink - Absolute path of the visible alias symlink
137
+ */
138
+ export const swapStore = (tmp: string, storeDir: string, visibleLink: string): void => {
139
+ const previous = `${storeDir}.old`;
140
+ rmSync(previous, { recursive: true, force: true });
141
+ if (existsSync(storeDir)) {
142
+ renameSync(storeDir, previous);
143
+ }
144
+ renameSync(tmp, storeDir);
145
+ rmSync(previous, { recursive: true, force: true });
146
+ symlinkRelative(visibleLink, storeDir);
147
+ };
148
+
149
+ /**
150
+ * Creates a fresh, per-run temporary store directory next to `storeDir` and
151
+ * returns its path. The random suffix isolates concurrent or repeated runs so
152
+ * they cannot corrupt each other's in-progress trees.
153
+ */
154
+ export const tempStoreFor = (storeDir: string): string => {
155
+ mkdirSync(dirname(storeDir), { recursive: true });
156
+ return mkdtempSync(`${storeDir}.tmp-`);
157
+ };
@@ -0,0 +1,80 @@
1
+ import ts from "typescript";
2
+
3
+ /**
4
+ * Output of a single TypeScript transpile: the stripped `.js` and the
5
+ * isolated-declaration `.d.ts` it produces.
6
+ */
7
+ export type TranspiledFile = {
8
+ /** Stripped-types JavaScript output. */
9
+ readonly js: string;
10
+ /** Generated TypeScript declaration output. */
11
+ readonly dts: string;
12
+ };
13
+
14
+ /**
15
+ * The compiler options shared by the store's two TypeScript passes — the
16
+ * single-file transpile here and the overlay type-check in `typecheck-store.ts`.
17
+ * Each pass spreads these and adds its own emit/check-specific keys.
18
+ */
19
+ export const baseCompilerOptions = (): ts.CompilerOptions => ({
20
+ module: ts.ModuleKind.ESNext,
21
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
22
+ target: ts.ScriptTarget.ESNext,
23
+ skipLibCheck: true,
24
+ jsx: ts.JsxEmit.ReactJSX,
25
+ jsxImportSource: "react",
26
+ });
27
+
28
+ const COMPILER_OPTIONS: ts.CompilerOptions = {
29
+ ...baseCompilerOptions(),
30
+ declaration: true,
31
+ skipDefaultLibCheck: true,
32
+ removeComments: false,
33
+ sourceMap: false,
34
+ declarationMap: false,
35
+ };
36
+
37
+ /**
38
+ * Transpiles a single TypeScript source string to a `.js` / `.d.ts` pair.
39
+ *
40
+ * Uses `ts.transpileModule` for the JS half (type-stripping only, no
41
+ * type checking) and `ts.transpileDeclaration` for the declaration half
42
+ * (single-file declaration emit). Generated sources annotate every export
43
+ * for isolated-declaration emit, so `transpileDeclaration` always
44
+ * succeeds; an error-category diagnostic from it represents a writer bug
45
+ * and is surfaced rather than swallowed.
46
+ *
47
+ * @param fileName - Source filename, used by TS for path resolution
48
+ * @param source - The TypeScript source string
49
+ */
50
+ export const transpileSource = (fileName: string, source: string): TranspiledFile => {
51
+ const jsResult = ts.transpileModule(source, {
52
+ compilerOptions: COMPILER_OPTIONS,
53
+ fileName,
54
+ reportDiagnostics: false,
55
+ });
56
+ const dtsResult = ts.transpileDeclaration(source, {
57
+ compilerOptions: COMPILER_OPTIONS,
58
+ fileName,
59
+ });
60
+ const errorDiagnostics = (dtsResult.diagnostics ?? []).filter(
61
+ (diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
62
+ );
63
+ if (errorDiagnostics.length > 0) {
64
+ const messages = errorDiagnostics
65
+ .slice(0, 5)
66
+ .map((diagnostic) => {
67
+ const text = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
68
+ if (diagnostic.file && diagnostic.start !== undefined) {
69
+ const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
70
+ return `[${line + 1}:${character + 1}] ${text}`;
71
+ }
72
+ return text;
73
+ })
74
+ .join("\n");
75
+ throw new Error(
76
+ `transpileDeclaration produced ${errorDiagnostics.length} diagnostic(s) for ${fileName}:\n${messages}`,
77
+ );
78
+ }
79
+ return { js: jsResult.outputText, dts: dtsResult.outputText };
80
+ };
@@ -0,0 +1,136 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import ts from "typescript";
4
+ import { baseCompilerOptions } from "./transpile.js";
5
+
6
+ /**
7
+ * One in-memory source destined for the injected `@gtkx/gi` store, plus whether
8
+ * it is a hand-written overlay (the type-check target) or a generated module
9
+ * declaration (checked against, but not reported on).
10
+ */
11
+ export type StoreSourceFile = {
12
+ /** Absolute virtual path of the source, under the store root. */
13
+ readonly path: string;
14
+ /** TypeScript source text (overlay `.ts`) or declaration text (generated `.d.ts`). */
15
+ readonly source: string;
16
+ /** Whether this is a hand-written overlay/augment source. */
17
+ readonly overlay: boolean;
18
+ };
19
+
20
+ const GI_SPECIFIER = "@gtkx/gi/";
21
+
22
+ const COMPILER_OPTIONS: ts.CompilerOptions = {
23
+ ...baseCompilerOptions(),
24
+ customConditions: ["source"],
25
+ strict: true,
26
+ noEmit: true,
27
+ types: [],
28
+ };
29
+
30
+ /**
31
+ * Source entries the overlay type-check resolves the runtime packages to.
32
+ */
33
+ export type RuntimeEntries = {
34
+ /** Absolute path to `@gtkx/ffi`'s source entry (`src/index.ts`). */
35
+ readonly ffiEntry: string;
36
+ /** Absolute path to `@gtkx/native`'s declaration entry (`dist/index.d.ts`). */
37
+ readonly nativeEntry: string;
38
+ };
39
+
40
+ const buildCompilerHost = (
41
+ storeRoot: string,
42
+ virtualSources: ReadonlyMap<string, string>,
43
+ entries: RuntimeEntries,
44
+ ): ts.CompilerHost => {
45
+ const readSource = (fileName: string): string | undefined =>
46
+ virtualSources.get(fileName) ?? (existsSync(fileName) ? readFileSync(fileName, "utf8") : undefined);
47
+
48
+ const resolveInjected = (specifier: string): string | undefined => {
49
+ if (specifier === "@gtkx/ffi") return entries.ffiEntry;
50
+ if (specifier === "@gtkx/native") return entries.nativeEntry;
51
+ if (!specifier.startsWith(GI_SPECIFIER)) return undefined;
52
+ const stem = specifier.slice(GI_SPECIFIER.length).replace(/\.js$/, "");
53
+ return [`${stem}.d.ts`, `${stem}.ts`, `${stem}/index.d.ts`, `${stem}/index.ts`]
54
+ .map((candidate) => join(storeRoot, candidate))
55
+ .find((candidate) => virtualSources.has(candidate));
56
+ };
57
+
58
+ const moduleResolutionHost: ts.ModuleResolutionHost = {
59
+ fileExists: (fileName) => virtualSources.has(fileName) || existsSync(fileName),
60
+ readFile: readSource,
61
+ };
62
+
63
+ return {
64
+ getSourceFile: (fileName, languageVersion) => {
65
+ const source = readSource(fileName);
66
+ return source === undefined ? undefined : ts.createSourceFile(fileName, source, languageVersion, true);
67
+ },
68
+ getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
69
+ writeFile: () => {},
70
+ getCurrentDirectory: () => storeRoot,
71
+ getCanonicalFileName: (fileName) => fileName,
72
+ useCaseSensitiveFileNames: () => true,
73
+ getNewLine: () => "\n",
74
+ fileExists: (fileName) => virtualSources.has(fileName) || existsSync(fileName),
75
+ readFile: readSource,
76
+ resolveModuleNameLiterals: (literals, containingFile, _redirectedReference, options) =>
77
+ literals.map((literal) => {
78
+ const injected = resolveInjected(literal.text);
79
+ if (injected !== undefined) {
80
+ return { resolvedModule: { resolvedFileName: injected, extension: ts.Extension.Ts } };
81
+ }
82
+ return {
83
+ resolvedModule: ts.resolveModuleName(literal.text, containingFile, options, moduleResolutionHost)
84
+ .resolvedModule,
85
+ };
86
+ }),
87
+ };
88
+ };
89
+
90
+ const reportOverlayErrors = (program: ts.Program, overlayPaths: readonly string[], storeRoot: string): void => {
91
+ const errors: ts.Diagnostic[] = [];
92
+ for (const path of overlayPaths) {
93
+ const sourceFile = program.getSourceFile(path);
94
+ if (!sourceFile) continue;
95
+ for (const diagnostic of program.getSemanticDiagnostics(sourceFile)) {
96
+ if (diagnostic.category === ts.DiagnosticCategory.Error) errors.push(diagnostic);
97
+ }
98
+ }
99
+ if (errors.length === 0) return;
100
+ const formatted = ts.formatDiagnosticsWithColorAndContext(errors, {
101
+ getCurrentDirectory: () => storeRoot,
102
+ getCanonicalFileName: (fileName) => fileName,
103
+ getNewLine: () => "\n",
104
+ });
105
+ throw new Error(`Overlay type-check failed against the generated @gtkx/gi API:\n${formatted}`);
106
+ };
107
+
108
+ /**
109
+ * Type-checks the hand-written overlay sources against the freshly generated
110
+ * `@gtkx/gi` declarations, entirely in memory.
111
+ *
112
+ * The overlay augments generated classes (e.g. installs `Value.prototype.getBoxed`),
113
+ * so its correctness is only meaningful relative to the exact API codegen just
114
+ * produced. A {@link ts.Program} is assembled over the in-memory generated
115
+ * declarations and overlay sources, resolving `@gtkx/gi/*` to the generated
116
+ * declarations and `@gtkx/ffi` to its package source; every other specifier
117
+ * (`react`, lib files) resolves from the real filesystem. Semantic errors on the
118
+ * overlay sources abort codegen rather than ship broken bindings.
119
+ *
120
+ * @param storeRoot - Absolute root the virtual source paths are rooted at.
121
+ * @param files - The in-memory generated declarations and overlay sources.
122
+ * @param entries - Absolute source entries for `@gtkx/ffi` and `@gtkx/native`.
123
+ * @throws When any overlay source has a semantic type error.
124
+ */
125
+ export const typecheckGiStore = (
126
+ storeRoot: string,
127
+ files: readonly StoreSourceFile[],
128
+ entries: RuntimeEntries,
129
+ ): void => {
130
+ const overlayPaths = files.filter((file) => file.overlay).map((file) => file.path);
131
+ if (overlayPaths.length === 0) return;
132
+ const virtualSources = new Map(files.map((file) => [file.path, file.source]));
133
+ const host = buildCompilerHost(storeRoot, virtualSources, entries);
134
+ const program = ts.createProgram({ rootNames: files.map((file) => file.path), options: COMPILER_OPTIONS, host });
135
+ reportOverlayErrors(program, overlayPaths, storeRoot);
136
+ };
@@ -0,0 +1,19 @@
1
+ import type { ModuleContext } from "../dsl/context.js";
2
+ import { aliasExportName } from "../dsl/identifier.js";
3
+ import type { GirAlias } from "../gir/namespace.js";
4
+ import { renderTsType } from "./ts-type.js";
5
+
6
+ /**
7
+ * Emits an `export type` declaration for a GIR `<alias>`.
8
+ *
9
+ * The alias is exported under {@link aliasExportName} — its GIR `name`, with
10
+ * GObject's `Type` published as `GType`.
11
+ *
12
+ * @param context - The module context
13
+ * @param alias - The alias to emit
14
+ */
15
+ export const emitAlias = (context: ModuleContext, alias: GirAlias): void => {
16
+ const exportName = aliasExportName(context.namespace.name, alias.name);
17
+ const targetType = renderTsType(context, alias.target);
18
+ context.module.appendDeclaration(`export type ${exportName} = ${targetType};`);
19
+ };
@@ -0,0 +1,44 @@
1
+ import type { GirFunction } from "../gir/function.js";
2
+
3
+ /**
4
+ * Detects the start half of a `Gio.AsyncReady` callable pair.
5
+ *
6
+ * Pairs follow two GIR conventions: an explicit `*_async` / `*_finish`
7
+ * suffix (the older style), or a base name paired with a `<root>_finish`
8
+ * (the modern GTK 4 style, e.g. `gtk_file_dialog_open` /
9
+ * `gtk_file_dialog_open_finish`).
10
+ *
11
+ * The runtime convention is to wrap the pair with `promisify(asyncFn,
12
+ * finishFn, cancellable, { leading })`. This detector returns the
13
+ * matching finish counterpart's GIR name when one exists in the same
14
+ * set of callables, so the class writer can wire the wrapper.
15
+ *
16
+ * @param fn - The candidate async-start callable
17
+ * @param siblings - Other callables on the same type to scan for the
18
+ * matching `*_finish`
19
+ * @returns The matching `*_finish` GIR name, or `undefined` when there is
20
+ * no pair
21
+ */
22
+ export const matchAsyncFinishName = (fn: GirFunction, siblings: readonly GirFunction[]): string | undefined => {
23
+ if (fn.name.endsWith("_async")) {
24
+ const root = fn.name.slice(0, -"_async".length);
25
+ const finishName = `${root}_finish`;
26
+ const match = siblings.find((sibling) => sibling.name === finishName);
27
+ return match === undefined ? undefined : finishName;
28
+ }
29
+ if (fn.name.endsWith("_finish")) return undefined;
30
+ const finishName = `${fn.name}_finish`;
31
+ const match = siblings.find((sibling) => sibling.name === finishName);
32
+ if (match === undefined) return undefined;
33
+ if (!hasAsyncReadyCallbackParameter(fn)) return undefined;
34
+ return finishName;
35
+ };
36
+
37
+ const hasAsyncReadyCallbackParameter = (fn: GirFunction): boolean => {
38
+ for (const parameter of fn.parameters) {
39
+ if (parameter.scope !== undefined) return true;
40
+ if (parameter.type?.kind !== "named") continue;
41
+ if (parameter.type.typeName === "AsyncReadyCallback") return true;
42
+ }
43
+ return false;
44
+ };
@@ -0,0 +1,105 @@
1
+ import { quote, toCamelCase, toIdentifier } from "@gtkx/utils";
2
+ import type { ModuleContext } from "../dsl/context.js";
3
+ import { indent } from "../dsl/emit.js";
4
+ import type { GirBoxed } from "../gir/boxed.js";
5
+ import type { GirField } from "../gir/field.js";
6
+ import type { GirTypeRef } from "../gir/type-ref.js";
7
+ import { type BoxedFieldSlot, computeBoxedFieldSlots } from "./boxed-layout.js";
8
+ import { renderTsType } from "./ts-type.js";
9
+ import { renderFfiType } from "./value.js";
10
+
11
+ /**
12
+ * A boxed field that carries a writable value (private, read-only, callback,
13
+ * and type-less fields are not settable at construction).
14
+ */
15
+ type WritableFieldSlot = BoxedFieldSlot & { readonly field: GirField & { readonly type: GirTypeRef } };
16
+
17
+ const isWritableFieldSlot = (entry: BoxedFieldSlot): entry is WritableFieldSlot =>
18
+ !entry.field.private &&
19
+ entry.field.writable &&
20
+ entry.field.callback === undefined &&
21
+ entry.field.type !== undefined;
22
+
23
+ const isOpaque = (boxed: GirBoxed): boolean => boxed.glibGetType === undefined && boxed.disguised;
24
+
25
+ /**
26
+ * Renders the `export interface <Boxed>ConstructorProps` declaration: the
27
+ * camelCase, optional, nullable form of every writable field. Opaque boxed
28
+ * records (disguised, no GType) have no known fields and get an empty bag.
29
+ *
30
+ * @param context - The module context
31
+ * @param boxed - The boxed record
32
+ * @param className - The local PascalCase class name
33
+ */
34
+ export const renderBoxedConstructorPropsInterface = (
35
+ context: ModuleContext,
36
+ boxed: GirBoxed,
37
+ className: string,
38
+ ): string => {
39
+ if (isOpaque(boxed)) return `export interface ${className}ConstructorProps {}`;
40
+ const { slots } = computeBoxedFieldSlots(context, boxed.fields, boxed.isUnion);
41
+ const lines = slots
42
+ .filter(isWritableFieldSlot)
43
+ .map(
44
+ (entry) =>
45
+ `${toIdentifier(toCamelCase(entry.field.name))}?: ${renderTsType(context, entry.field.type, true)};`,
46
+ );
47
+ const body = lines.length === 0 ? "" : `\n${indent(lines.join("\n"), 1)}\n`;
48
+ return `export interface ${className}ConstructorProps {${body}}`;
49
+ };
50
+
51
+ /**
52
+ * Renders a boxed record's constructor.
53
+ *
54
+ * The constructor allocates zero-filled native memory of the struct's size and
55
+ * writes each provided field at its byte offset, threading bitfield members
56
+ * through a read-modify-write into their shared storage word. Opaque records
57
+ * have no known layout, so their constructor throws.
58
+ *
59
+ * @param context - The module context
60
+ * @param boxed - The boxed record
61
+ * @param className - The local PascalCase class name
62
+ */
63
+ export const renderBoxedConstructor = (context: ModuleContext, boxed: GirBoxed, className: string): string => {
64
+ if (isOpaque(boxed)) {
65
+ return `constructor() {\n${indent(`throw new Error(${quote(`Cannot construct ${className}: opaque boxed type with no known layout`)});`, 1)}\n}`;
66
+ }
67
+ context.addNativeImport("alloc");
68
+ context.addRuntimeImport("setHandle");
69
+ const { slots, size } = computeBoxedFieldSlots(context, boxed.fields, boxed.isUnion);
70
+ const statements = [`const handle = alloc(${allocArgs(context, boxed, size).join(", ")});`];
71
+ for (const entry of slots) {
72
+ if (!isWritableFieldSlot(entry)) continue;
73
+ statements.push(renderFieldWrite(context, entry));
74
+ }
75
+ statements.push("setHandle(this, handle);");
76
+ const body = statements.join("\n");
77
+ return `constructor(props: ${className}ConstructorProps = {}) {\n${indent(body, 1)}\n}`;
78
+ };
79
+
80
+ const allocArgs = (context: ModuleContext, boxed: GirBoxed, size: number): readonly string[] => {
81
+ const glibTypeName = boxed.glibTypeName ?? boxed.cType;
82
+ const lib = context.namespace.sharedLibrary;
83
+ const args = [String(size)];
84
+ if (glibTypeName !== undefined || lib !== undefined) {
85
+ args.push(glibTypeName !== undefined ? quote(glibTypeName) : "undefined");
86
+ }
87
+ if (lib !== undefined) args.push(quote(lib));
88
+ return args;
89
+ };
90
+
91
+ const renderFieldWrite = (context: ModuleContext, entry: WritableFieldSlot): string => {
92
+ context.addRuntimeImport("t");
93
+ context.addNativeImport("write");
94
+ const ffiType = renderFfiType(context, entry.field.type, "none");
95
+ const name = toIdentifier(toCamelCase(entry.field.name));
96
+ const offset = entry.slot.byteOffset;
97
+ if (entry.slot.bitWidth === undefined) {
98
+ return `if (props.${name} !== undefined) write(handle, ${ffiType}, ${offset}, props.${name});`;
99
+ }
100
+ context.addNativeImport("read");
101
+ const mask = (1 << entry.slot.bitWidth) - 1;
102
+ const bitOffset = entry.slot.bitOffset ?? 0;
103
+ const merged = `(((read(handle, ${ffiType}, ${offset}) as number) & ~(${mask} << ${bitOffset})) | ((Number(props.${name}) & ${mask}) << ${bitOffset})) >>> 0`;
104
+ return `if (props.${name} !== undefined) write(handle, ${ffiType}, ${offset}, ${merged});`;
105
+ };