@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,146 @@
1
+ import type { GirNamespace } from "../gir/namespace.js";
2
+ import type { GirRepository } from "../gir/repository.js";
3
+ import { ModuleBuilder } from "./module.js";
4
+
5
+ /**
6
+ * Shared context handed to every writer for a single generated FFI module.
7
+ *
8
+ * Bundles the namespace the writer is emitting for, the broader repository
9
+ * (for cross-namespace lookups), and the in-progress {@link ModuleBuilder}.
10
+ * Writers also call back into this context to register cross-namespace
11
+ * runtime imports so individual writers do not need to know the relative
12
+ * path layout of the generated output.
13
+ *
14
+ * Generated bindings live in the `@gtkx/gi` package at `<ns>/<ns>.{js,d.ts}`,
15
+ * with a per-namespace barrel at `<ns>/index.js` and the augment overlay at
16
+ * `<ns>/augment/*.js`. The hand-written runtime is reached through the single
17
+ * `@gtkx/ffi` barrel; sibling generated namespaces are reached relative to the
18
+ * gi package root.
19
+ */
20
+ export class ModuleContext {
21
+ public readonly module = new ModuleBuilder();
22
+
23
+ /**
24
+ * @param namespace - The namespace this module corresponds to
25
+ * @param repository - The repository containing every loaded namespace
26
+ */
27
+ constructor(
28
+ public readonly namespace: GirNamespace,
29
+ public readonly repository: GirRepository,
30
+ ) {}
31
+
32
+ /**
33
+ * Adds a named import from the `@gtkx/ffi` runtime barrel.
34
+ */
35
+ addRuntimeImport(name: string): void {
36
+ this.module.imports.addNamed("@gtkx/ffi", name);
37
+ }
38
+
39
+ /**
40
+ * Adds a type-only named import from the `@gtkx/ffi` runtime barrel
41
+ * (`import { type Name }`), erased from the emitted `.js`.
42
+ */
43
+ addRuntimeTypeImport(name: string): void {
44
+ this.module.imports.addNamed("@gtkx/ffi", name, true);
45
+ }
46
+
47
+ /**
48
+ * Adds a named import from the low-level `@gtkx/native` runtime.
49
+ *
50
+ * The transport primitives (`alloc`, `call`, `read`, `write`) live in
51
+ * `@gtkx/native`; generated bindings reach them directly so `@gtkx/ffi`
52
+ * stays the home of higher-level runtime helpers only.
53
+ */
54
+ addNativeImport(name: string): void {
55
+ this.module.imports.addNamed("@gtkx/native", name);
56
+ }
57
+
58
+ /**
59
+ * Adds a type-only named import from `@gtkx/native`
60
+ * (`import { type Name }`), erased from the emitted `.js`.
61
+ */
62
+ addNativeTypeImport(name: string): void {
63
+ this.module.imports.addNamed("@gtkx/native", name, true);
64
+ }
65
+
66
+ /** Adds the canonical `constructGObjectInstance` import from `@gtkx/ffi`. */
67
+ addConstructGObjectInstanceImport(): void {
68
+ this.module.imports.addNamed("@gtkx/ffi", "constructGObjectInstance");
69
+ }
70
+
71
+ /** Adds the `valueFromFfi` import from `@gtkx/ffi`. */
72
+ addValueFromFfiImport(): void {
73
+ this.module.imports.addNamed("@gtkx/ffi", "valueFromFfi");
74
+ }
75
+
76
+ /** Adds the `valueFromFfiOptional` import from `@gtkx/ffi`. */
77
+ addValueFromFfiOptionalImport(): void {
78
+ this.module.imports.addNamed("@gtkx/ffi", "valueFromFfiOptional");
79
+ }
80
+
81
+ /**
82
+ * Adds the canonical side-effect imports for `gobject/object.js` and
83
+ * `gobject/value.js`.
84
+ *
85
+ * Skipped for the `GObject` namespace itself (those modules import its
86
+ * generated classes) and for `GLib`, which lives below GObject in the
87
+ * import graph. Letting GLib bootstrap creates a runtime cycle:
88
+ * `gobject.js` imports `glib.js` for `GLib.Variant` return-wrapping,
89
+ * `glib.js`'s bootstrap re-enters `gobject/value.js`, and `value.ts`
90
+ * touches `Value.prototype` before `gobject.js` has finished defining
91
+ * `Value`. Other namespaces install the override on first import.
92
+ */
93
+ addGObjectBootstrapImports(): void {
94
+ if (this.namespace.name === "GObject") return;
95
+ if (this.namespace.name === "GLib") return;
96
+ this.module.imports.addSideEffect("../gobject/augment/object.js");
97
+ this.module.imports.addSideEffect("../gobject/augment/value.js");
98
+ }
99
+
100
+ /**
101
+ * Adds a wildcard import for another namespace and returns the local
102
+ * alias used.
103
+ *
104
+ * Most namespaces are routed through their hand-written `index.js` so any
105
+ * prototype augmentations (Cairo `Context` methods, Gdk `RGBA`, Graphene
106
+ * structs, …) load whenever a generated module references the namespace.
107
+ * The foundational `gobject` and `glib` namespaces are routed straight to
108
+ * their generated module instead, because their indexes pull in modules
109
+ * that depend on values still mid-initialization, which would deadlock the
110
+ * import graph.
111
+ *
112
+ * For non-foundational namespaces a side-effect import is recorded
113
+ * alongside the wildcard so the foreign module loads even when its alias
114
+ * appears only in type positions. The wildcard alias is type-stripped out
115
+ * of the emitted `.js` whenever it is never read as a value, which would
116
+ * otherwise drop the foreign module from the runtime graph and skip its
117
+ * eager `*_get_type` registrations. The side-effect import survives type
118
+ * stripping and pins the module in place. The foundational `gobject` and
119
+ * `glib` wildcards are always read as values (enums, base classes) and so
120
+ * are never stripped, needing no side-effect pin.
121
+ *
122
+ * @param namespaceName - The other namespace (e.g. `"GLib"`)
123
+ */
124
+ addCrossNamespaceImport(namespaceName: string): string {
125
+ if (namespaceName === this.namespace.name) return namespaceName;
126
+ const directory = namespaceName.toLowerCase();
127
+ const isFoundational = directory === "gobject" || directory === "glib";
128
+ const path = isFoundational ? `../${directory}/${directory}.js` : `../${directory}/index.js`;
129
+ if (!isFoundational) this.module.imports.addSideEffect(`../${directory}/${directory}.js`);
130
+ this.module.imports.addNamespace(path, namespaceName);
131
+ return namespaceName;
132
+ }
133
+
134
+ /**
135
+ * Renders a namespace-qualified reference to `typeName`: the bare name when
136
+ * it lives in this module's own namespace, otherwise `Alias.Name` with the
137
+ * foreign namespace imported on demand via {@link addCrossNamespaceImport}.
138
+ *
139
+ * @param namespaceName - The namespace the referenced type lives in
140
+ * @param typeName - The local type or export name within that namespace
141
+ */
142
+ qualify(namespaceName: string, typeName: string): string {
143
+ if (namespaceName === this.namespace.name) return typeName;
144
+ return `${this.addCrossNamespaceImport(namespaceName)}.${typeName}`;
145
+ }
146
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Tiny string-builder helpers used by the writers to assemble TypeScript
3
+ * source. The DSL deliberately stops short of modelling expressions or
4
+ * statements — writers compose strings directly using these primitives.
5
+ */
6
+
7
+ /**
8
+ * Indents each line of `block` by `level` four-space units.
9
+ *
10
+ * Lines that are empty stay empty (no trailing whitespace).
11
+ *
12
+ * @param block - The multi-line source fragment
13
+ * @param level - Number of indentation steps to apply
14
+ */
15
+ export const indent = (block: string, level: number): string => {
16
+ if (level <= 0) return block;
17
+ const prefix = " ".repeat(level);
18
+ return block
19
+ .split("\n")
20
+ .map((line) => (line.length === 0 ? line : `${prefix}${line}`))
21
+ .join("\n");
22
+ };
23
+
24
+ /**
25
+ * Joins `parts` with commas, dropping `undefined` entries.
26
+ */
27
+ export const joinArgs = (parts: ReadonlyArray<string | undefined>): string =>
28
+ parts.filter((part): part is string => part !== undefined).join(", ");
29
+
30
+ /**
31
+ * Renders an array literal multi-line, indenting each element four spaces
32
+ * and adding a trailing comma.
33
+ *
34
+ * Empty input renders as `[]`.
35
+ *
36
+ * @param elements - Already-rendered element expressions
37
+ */
38
+ export const arrayLiteral = (elements: readonly string[]): string => {
39
+ if (elements.length === 0) return "[]";
40
+ const lines = elements.map((element) => ` ${element},`);
41
+ return `[\n${lines.join("\n")}\n]`;
42
+ };
@@ -0,0 +1,52 @@
1
+ import { toCamelCase, toIdentifier } from "@gtkx/utils";
2
+
3
+ /**
4
+ * Derives the public camelCase export name for a namespace-level callable.
5
+ *
6
+ * The GIR `name` attribute is camelCased (`quark_from_string` →
7
+ * `quarkFromString`); for a shadowing function it is the shadowed short name,
8
+ * so `g_idle_add_full` (which shadows `idle_add`) exports as `idleAdd`. When the
9
+ * `name` is empty the C identifier is used instead, with the namespace's
10
+ * `c:symbol-prefixes` value stripped (e.g. `g_` from `g_quark_from_string`).
11
+ * The result is escaped so it never collides with a reserved word.
12
+ *
13
+ * @param cIdentifier - The C symbol identifier from GIR
14
+ * @param girName - The GIR `name` attribute (shadow-resolved)
15
+ * @param symbolPrefixes - The namespace's `c:symbol-prefixes` values
16
+ */
17
+ export const namespaceFunctionExportName = (
18
+ cIdentifier: string,
19
+ girName: string,
20
+ symbolPrefixes: readonly string[],
21
+ ): string => {
22
+ if (girName.length > 0) {
23
+ return toIdentifier(toCamelCase(girName));
24
+ }
25
+ const stripped = stripLongestPrefix(cIdentifier, symbolPrefixes);
26
+ return toIdentifier(toCamelCase(stripped));
27
+ };
28
+
29
+ /**
30
+ * Derives the exported identifier for a GIR `<alias>`.
31
+ *
32
+ * Aliases are surfaced under their GIR `name` (e.g. `Quark`, `Pid`,
33
+ * `Allocation`), matching the `@girs` convention. GObject's `Type` alias is the
34
+ * one exception: it is published as `GType` so it lines up with the GObject
35
+ * runtime's `GType` type and the references that resolve through it.
36
+ *
37
+ * @param namespaceName - The namespace the alias is declared in
38
+ * @param aliasName - The alias's GIR `name`
39
+ */
40
+ export const aliasExportName = (namespaceName: string, aliasName: string): string =>
41
+ namespaceName === "GObject" && aliasName === "Type" ? "GType" : aliasName;
42
+
43
+ const stripLongestPrefix = (input: string, prefixes: readonly string[]): string => {
44
+ let best = "";
45
+ for (const prefix of prefixes) {
46
+ const candidate = `${prefix}_`;
47
+ if (input.startsWith(candidate) && candidate.length > best.length) {
48
+ best = candidate;
49
+ }
50
+ }
51
+ return best.length === 0 ? input : input.slice(best.length);
52
+ };
@@ -0,0 +1,109 @@
1
+ import { quote } from "@gtkx/utils";
2
+
3
+ /**
4
+ * Tracks imports for a single generated module.
5
+ *
6
+ * The DSL is intentionally tiny: it covers what is awkward to do with
7
+ * string concatenation (deduplicating named imports, sorting them, mixing
8
+ * side-effect and named imports from the same specifier) and leaves
9
+ * everything else to raw TS strings the writers compose themselves.
10
+ */
11
+ export class ImportsBuilder {
12
+ private readonly named = new Map<string, Map<string, boolean>>();
13
+ private readonly defaultNames = new Map<string, string>();
14
+ private readonly namespaces = new Map<string, string>();
15
+ private readonly sideEffects = new Set<string>();
16
+
17
+ /**
18
+ * Records a named import: `import { foo, bar } from "lib"`.
19
+ *
20
+ * Repeated calls deduplicate; the final emit alphabetises within the
21
+ * braces. A name is emitted with the inline `type` modifier only when
22
+ * every recording of it is type-only — a single value import of the same
23
+ * name keeps it a value import.
24
+ *
25
+ * @param specifier - The module specifier (e.g. `"@gtkx/ffi"`)
26
+ * @param name - The identifier to import
27
+ * @param isType - Whether this recording is type-only (`import { type X }`)
28
+ */
29
+ addNamed(specifier: string, name: string, isType = false): void {
30
+ let bucket = this.named.get(specifier);
31
+ if (bucket === undefined) {
32
+ bucket = new Map();
33
+ this.named.set(specifier, bucket);
34
+ }
35
+ bucket.set(name, (bucket.get(name) ?? true) && isType);
36
+ }
37
+
38
+ /**
39
+ * Records a namespace import: `import * as Alias from "lib"`.
40
+ *
41
+ * The first call wins if a different alias is reused for the same
42
+ * specifier.
43
+ *
44
+ * @param specifier - The module specifier
45
+ * @param alias - The local alias bound by the import
46
+ */
47
+ addNamespace(specifier: string, alias: string): void {
48
+ if (!this.namespaces.has(specifier)) {
49
+ this.namespaces.set(specifier, alias);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Records a default import: `import Alias from "lib"`. Subsequent calls
55
+ * with a different alias are ignored.
56
+ *
57
+ * @param specifier - The module specifier
58
+ * @param alias - The local name bound to the default export
59
+ */
60
+ addDefault(specifier: string, alias: string): void {
61
+ if (!this.defaultNames.has(specifier)) {
62
+ this.defaultNames.set(specifier, alias);
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Records a side-effect-only import: `import "lib"`.
68
+ *
69
+ * @param specifier - The module specifier
70
+ */
71
+ addSideEffect(specifier: string): void {
72
+ this.sideEffects.add(specifier);
73
+ }
74
+
75
+ /**
76
+ * Renders all imports as a single string ending with a trailing
77
+ * newline. Side-effect imports come first (in insertion order),
78
+ * followed by named/namespace/default imports sorted by specifier.
79
+ */
80
+ toSource(): string {
81
+ const lines: string[] = [];
82
+ for (const specifier of this.sideEffects) {
83
+ lines.push(`import ${quote(specifier)};`);
84
+ }
85
+ const specifiers = new Set<string>([
86
+ ...this.named.keys(),
87
+ ...this.namespaces.keys(),
88
+ ...this.defaultNames.keys(),
89
+ ]);
90
+ const sortedSpecifiers = [...specifiers].sort((a, b) => a.localeCompare(b));
91
+ for (const specifier of sortedSpecifiers) {
92
+ const defaultAlias = this.defaultNames.get(specifier);
93
+ const namespaceAlias = this.namespaces.get(specifier);
94
+ const namedNames = this.named.get(specifier);
95
+ const parts: string[] = [];
96
+ if (defaultAlias !== undefined) parts.push(defaultAlias);
97
+ if (namespaceAlias !== undefined) parts.push(`* as ${namespaceAlias}`);
98
+ if (namedNames !== undefined && namedNames.size > 0) {
99
+ const sortedNames = [...namedNames.entries()]
100
+ .sort(([a], [b]) => a.localeCompare(b))
101
+ .map(([name, isType]) => (isType ? `type ${name}` : name));
102
+ parts.push(`{ ${sortedNames.join(", ")} }`);
103
+ }
104
+ if (parts.length === 0) continue;
105
+ lines.push(`import ${parts.join(", ")} from ${quote(specifier)};`);
106
+ }
107
+ return lines.length === 0 ? "" : `${lines.join("\n")}\n`;
108
+ }
109
+ }
@@ -0,0 +1,75 @@
1
+ import { ImportsBuilder } from "./imports.js";
2
+
3
+ /**
4
+ * Accumulator for one generated TypeScript module's source.
5
+ *
6
+ * Writers append code in three logical phases:
7
+ *
8
+ * 1. *Bindings* — the `const fn_name = t.fn(...)` block at the top.
9
+ * 2. *Declarations* — the class bodies, free function exports, enums,
10
+ * constants.
11
+ * 3. *Registrations* — the trailing `registerNativeClass(...)` block.
12
+ *
13
+ * Keeping the three buckets independent removes ordering constraints
14
+ * between writers and lets {@link emit} stitch a final source string with
15
+ * the same shape every generated file uses today.
16
+ */
17
+ export class ModuleBuilder {
18
+ /** Imports manifest the writers feed into. */
19
+ public readonly imports = new ImportsBuilder();
20
+ private readonly bindings: string[] = [];
21
+ private readonly bindingNames = new Set<string>();
22
+ private readonly declarations: string[] = [];
23
+ private readonly registrations: string[] = [];
24
+
25
+ /**
26
+ * Appends a top-level FFI binding (`const foo = t.fn(...)`).
27
+ *
28
+ * Duplicates are dropped when a `name` is supplied that matches a
29
+ * previously appended binding; this is the common case for GIR
30
+ * callables that appear both as a namespace function and as a
31
+ * constructor or static on a class.
32
+ *
33
+ * @param code - The source fragment, without a trailing newline
34
+ * @param name - Optional binding identifier used for deduplication
35
+ */
36
+ appendBinding(code: string, name?: string): void {
37
+ if (name !== undefined) {
38
+ if (this.bindingNames.has(name)) return;
39
+ this.bindingNames.add(name);
40
+ }
41
+ this.bindings.push(code);
42
+ }
43
+
44
+ /**
45
+ * Appends a declaration (class, function, enum, constant).
46
+ *
47
+ * @param code - The source fragment, without a trailing newline
48
+ */
49
+ appendDeclaration(code: string): void {
50
+ this.declarations.push(code);
51
+ }
52
+
53
+ /**
54
+ * Appends a trailing registration statement (`registerNativeClass(…)`).
55
+ *
56
+ * @param code - The source fragment, without a trailing newline
57
+ */
58
+ appendRegistration(code: string): void {
59
+ this.registrations.push(code);
60
+ }
61
+
62
+ /**
63
+ * Renders the full TypeScript source: imports, bindings, declarations,
64
+ * registrations — separated by a single blank line.
65
+ */
66
+ toSource(): string {
67
+ const sections: string[] = [];
68
+ const importsBlock = this.imports.toSource();
69
+ if (importsBlock.length > 0) sections.push(importsBlock.trimEnd());
70
+ if (this.bindings.length > 0) sections.push(this.bindings.join("\n\n"));
71
+ if (this.declarations.length > 0) sections.push(this.declarations.join("\n\n"));
72
+ if (this.registrations.length > 0) sections.push(this.registrations.join("\n\n"));
73
+ return `${sections.join("\n\n")}\n`;
74
+ }
75
+ }
@@ -0,0 +1,113 @@
1
+ import { ModuleContext } from "../dsl/context.js";
2
+ import type { GirClass } from "../gir/class.js";
3
+ import type { GirNamespace } from "../gir/namespace.js";
4
+ import type { GirRepository } from "../gir/repository.js";
5
+ import { emitAlias } from "../writers/alias.js";
6
+ import { emitBoxed } from "../writers/boxed.js";
7
+ import { emitCallback } from "../writers/callback.js";
8
+ import { emitClass } from "../writers/class.js";
9
+ import { emitConstant } from "../writers/constant.js";
10
+ import { emitEnum } from "../writers/enum.js";
11
+ import { emitNamespaceBootstrap, emitNamespaceFunction } from "../writers/function.js";
12
+ import { emitInterface } from "../writers/interface.js";
13
+
14
+ /**
15
+ * Generates the TypeScript source for one FFI namespace module.
16
+ *
17
+ * Walks the namespace's declared enums, boxeds, classes, interfaces,
18
+ * callbacks, functions, constants, and aliases, dispatching to the
19
+ * per-construct writers in `writers/`: bindings first, declarations second,
20
+ * registrations trailing.
21
+ *
22
+ * @param namespace - The namespace to emit
23
+ * @param repository - The full repository (for cross-namespace lookups)
24
+ * @returns The relative output path and the TypeScript source string
25
+ */
26
+ export const generateNamespaceModule = (
27
+ namespace: GirNamespace,
28
+ repository: GirRepository,
29
+ ): { readonly path: string; readonly source: string } => {
30
+ const context = new ModuleContext(namespace, repository);
31
+ context.addGObjectBootstrapImports();
32
+
33
+ for (const enumeration of namespace.enums) {
34
+ emitEnum(context, enumeration);
35
+ }
36
+ for (const boxed of namespace.boxeds) {
37
+ emitBoxed(context, boxed);
38
+ }
39
+ for (const klass of topologicalClassOrder(namespace.classes, namespace.name)) {
40
+ emitClass(context, klass);
41
+ }
42
+ for (const iface of namespace.interfaces) {
43
+ emitInterface(context, iface);
44
+ }
45
+ for (const callback of namespace.callbacks) {
46
+ emitCallback(context, callback);
47
+ }
48
+ for (const fn of namespace.functions) {
49
+ emitNamespaceFunction(context, fn);
50
+ }
51
+ emitNamespaceBootstrap(context, namespace);
52
+ for (const constant of namespace.constants) {
53
+ emitConstant(context, constant);
54
+ }
55
+ for (const alias of namespace.aliases) {
56
+ emitAlias(context, alias);
57
+ }
58
+
59
+ const directory = namespace.name.toLowerCase();
60
+ return {
61
+ path: `${directory}/${directory}.ts`,
62
+ source: context.module.toSource(),
63
+ };
64
+ };
65
+
66
+ /**
67
+ * Returns the namespace's classes ordered so each class is preceded by every
68
+ * same-namespace ancestor it extends.
69
+ *
70
+ * Generated JS classes use `extends`, which is a runtime reference: a child
71
+ * class declaration that names its parent before the parent's class body runs
72
+ * hits a temporal dead-zone error. GIR file order is source order, which does
73
+ * not match the inheritance order for namespaces where a leaf type (e.g.
74
+ * `GObject.Binding`) is declared earlier in the file than its base (e.g.
75
+ * `GObject.Object`). The codegen sorts by inheritance instead.
76
+ *
77
+ * Cross-namespace parents are imported by the writer and do not participate in
78
+ * the sort.
79
+ *
80
+ * @param classes - The classes to order
81
+ * @param namespaceName - The namespace these classes live in
82
+ */
83
+ const topologicalClassOrder = (classes: readonly GirClass[], namespaceName: string): readonly GirClass[] => {
84
+ const byLocalName = new Map<string, GirClass>();
85
+ for (const klass of classes) byLocalName.set(klass.name, klass);
86
+ const result: GirClass[] = [];
87
+ const placed = new Set<GirClass>();
88
+ const visiting = new Set<GirClass>();
89
+ const visit = (klass: GirClass): void => {
90
+ if (placed.has(klass)) return;
91
+ if (visiting.has(klass)) return;
92
+ visiting.add(klass);
93
+ const parent = sameNamespaceParent(klass, namespaceName, byLocalName);
94
+ if (parent !== undefined) visit(parent);
95
+ visiting.delete(klass);
96
+ placed.add(klass);
97
+ result.push(klass);
98
+ };
99
+ for (const klass of classes) visit(klass);
100
+ return result;
101
+ };
102
+
103
+ const sameNamespaceParent = (
104
+ klass: GirClass,
105
+ namespaceName: string,
106
+ byLocalName: ReadonlyMap<string, GirClass>,
107
+ ): GirClass | undefined => {
108
+ if (klass.parent === undefined) return undefined;
109
+ const dot = klass.parent.indexOf(".");
110
+ if (dot === -1) return byLocalName.get(klass.parent);
111
+ if (klass.parent.slice(0, dot) !== namespaceName) return undefined;
112
+ return byLocalName.get(klass.parent.slice(dot + 1));
113
+ };
@@ -0,0 +1,50 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import { createRequire } from "node:module";
4
+
5
+ const require = createRequire(import.meta.url);
6
+
7
+ /** Filename of the staleness sentinel written into the `@gtkx/gi` store root. */
8
+ export const FINGERPRINT_FILENAME = ".codegen-fingerprint.json";
9
+
10
+ /**
11
+ * The `@gtkx/codegen` package version, mixed into the store fingerprint so a
12
+ * codegen upgrade invalidates a previously-generated store.
13
+ */
14
+ export const CODEGEN_VERSION: string = (require("../package.json") as { version: string }).version;
15
+
16
+ /**
17
+ * The staleness sentinel written into the generated `@gtkx/gi` store: the
18
+ * fingerprint value plus the inputs that produced it, so a consumer can
19
+ * recompute and compare without reloading the GIR repository.
20
+ */
21
+ export type CodegenFingerprint = {
22
+ /** The fingerprint hash of {@link CODEGEN_VERSION}, libraries, and GIR contents. */
23
+ readonly value: string;
24
+ /** Absolute paths of every loaded `.gir` file, in load order. */
25
+ readonly girFiles: readonly string[];
26
+ /** The resolved `Name-Version` library identifiers the store was built for. */
27
+ readonly libraries: readonly string[];
28
+ };
29
+
30
+ /**
31
+ * Computes a content fingerprint over the codegen version, the resolved library
32
+ * set, and the contents of every loaded GIR file. A change to any of these
33
+ * means the generated bindings are stale and must be regenerated.
34
+ *
35
+ * @param girFiles - Absolute paths of the loaded `.gir` files
36
+ * @param libraries - The resolved `Name-Version` library identifiers
37
+ */
38
+ export const computeFingerprint = (girFiles: readonly string[], libraries: readonly string[]): string => {
39
+ const hash = createHash("sha256");
40
+ hash.update(CODEGEN_VERSION);
41
+ hash.update("\n");
42
+ hash.update([...libraries].sort((a, b) => a.localeCompare(b)).join(","));
43
+ for (const file of [...girFiles].sort((a, b) => a.localeCompare(b))) {
44
+ hash.update("\n");
45
+ hash.update(file);
46
+ hash.update("\0");
47
+ hash.update(readFileSync(file));
48
+ }
49
+ return hash.digest("hex");
50
+ };