@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.
- package/LICENSE +373 -0
- package/dist/dsl/context.d.ts +104 -0
- package/dist/dsl/context.d.ts.map +1 -0
- package/dist/dsl/context.js +140 -0
- package/dist/dsl/context.js.map +1 -0
- package/dist/dsl/emit.d.ts +28 -0
- package/dist/dsl/emit.d.ts.map +1 -0
- package/dist/dsl/emit.js +41 -0
- package/dist/dsl/emit.js.map +1 -0
- package/dist/dsl/identifier.d.ts +28 -0
- package/dist/dsl/identifier.d.ts.map +1 -0
- package/dist/dsl/identifier.js +45 -0
- package/dist/dsl/identifier.js.map +1 -0
- package/dist/dsl/imports.d.ts +58 -0
- package/dist/dsl/imports.d.ts.map +1 -0
- package/dist/dsl/imports.js +107 -0
- package/dist/dsl/imports.js.map +1 -0
- package/dist/dsl/module.d.ts +53 -0
- package/dist/dsl/module.d.ts.map +1 -0
- package/dist/dsl/module.js +76 -0
- package/dist/dsl/module.js.map +1 -0
- package/dist/ffi/pipeline.d.ts +19 -0
- package/dist/ffi/pipeline.d.ts.map +1 -0
- package/dist/ffi/pipeline.js +107 -0
- package/dist/ffi/pipeline.js.map +1 -0
- package/dist/fingerprint.d.ts +30 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/fingerprint.js +33 -0
- package/dist/fingerprint.js.map +1 -0
- package/dist/gi-store.d.ts +34 -0
- package/dist/gi-store.d.ts.map +1 -0
- package/dist/gi-store.js +154 -0
- package/dist/gi-store.js.map +1 -0
- package/dist/gir/boxed.d.ts +66 -0
- package/dist/gir/boxed.d.ts.map +1 -0
- package/dist/gir/boxed.js +49 -0
- package/dist/gir/boxed.js.map +1 -0
- package/dist/gir/callback.d.ts +15 -0
- package/dist/gir/callback.d.ts.map +1 -0
- package/dist/gir/callback.js +17 -0
- package/dist/gir/callback.js.map +1 -0
- package/dist/gir/class.d.ts +49 -0
- package/dist/gir/class.d.ts.map +1 -0
- package/dist/gir/class.js +42 -0
- package/dist/gir/class.js.map +1 -0
- package/dist/gir/enum.d.ts +39 -0
- package/dist/gir/enum.d.ts.map +1 -0
- package/dist/gir/enum.js +24 -0
- package/dist/gir/enum.js.map +1 -0
- package/dist/gir/field.d.ts +27 -0
- package/dist/gir/field.d.ts.map +1 -0
- package/dist/gir/field.js +19 -0
- package/dist/gir/field.js.map +1 -0
- package/dist/gir/function.d.ts +47 -0
- package/dist/gir/function.d.ts.map +1 -0
- package/dist/gir/function.js +28 -0
- package/dist/gir/function.js.map +1 -0
- package/dist/gir/namespace.d.ts +65 -0
- package/dist/gir/namespace.d.ts.map +1 -0
- package/dist/gir/namespace.js +60 -0
- package/dist/gir/namespace.js.map +1 -0
- package/dist/gir/parameter.d.ts +82 -0
- package/dist/gir/parameter.d.ts.map +1 -0
- package/dist/gir/parameter.js +66 -0
- package/dist/gir/parameter.js.map +1 -0
- package/dist/gir/parse.d.ts +78 -0
- package/dist/gir/parse.d.ts.map +1 -0
- package/dist/gir/parse.js +155 -0
- package/dist/gir/parse.js.map +1 -0
- package/dist/gir/primitives.d.ts +36 -0
- package/dist/gir/primitives.d.ts.map +1 -0
- package/dist/gir/primitives.js +109 -0
- package/dist/gir/primitives.js.map +1 -0
- package/dist/gir/property.d.ts +29 -0
- package/dist/gir/property.d.ts.map +1 -0
- package/dist/gir/property.js +16 -0
- package/dist/gir/property.js.map +1 -0
- package/dist/gir/qualified-name.d.ts +37 -0
- package/dist/gir/qualified-name.d.ts.map +1 -0
- package/dist/gir/qualified-name.js +39 -0
- package/dist/gir/qualified-name.js.map +1 -0
- package/dist/gir/qualify.d.ts +32 -0
- package/dist/gir/qualify.d.ts.map +1 -0
- package/dist/gir/qualify.js +60 -0
- package/dist/gir/qualify.js.map +1 -0
- package/dist/gir/repository.d.ts +82 -0
- package/dist/gir/repository.d.ts.map +1 -0
- package/dist/gir/repository.js +122 -0
- package/dist/gir/repository.js.map +1 -0
- package/dist/gir/signal.d.ts +22 -0
- package/dist/gir/signal.d.ts.map +1 -0
- package/dist/gir/signal.js +20 -0
- package/dist/gir/signal.js.map +1 -0
- package/dist/gir/size.d.ts +67 -0
- package/dist/gir/size.d.ts.map +1 -0
- package/dist/gir/size.js +87 -0
- package/dist/gir/size.js.map +1 -0
- package/dist/gir/type-ref.d.ts +79 -0
- package/dist/gir/type-ref.d.ts.map +1 -0
- package/dist/gir/type-ref.js +101 -0
- package/dist/gir/type-ref.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-store.d.ts +22 -0
- package/dist/jsx-store.d.ts.map +1 -0
- package/dist/jsx-store.js +40 -0
- package/dist/jsx-store.js.map +1 -0
- package/dist/react/compounds-meta.d.ts +18 -0
- package/dist/react/compounds-meta.d.ts.map +1 -0
- package/dist/react/compounds-meta.js +93 -0
- package/dist/react/compounds-meta.js.map +1 -0
- package/dist/react/compounds.d.ts +27 -0
- package/dist/react/compounds.d.ts.map +1 -0
- package/dist/react/compounds.js +164 -0
- package/dist/react/compounds.js.map +1 -0
- package/dist/react/internal.d.ts +14 -0
- package/dist/react/internal.d.ts.map +1 -0
- package/dist/react/internal.js +79 -0
- package/dist/react/internal.js.map +1 -0
- package/dist/react/jsx.d.ts +22 -0
- package/dist/react/jsx.d.ts.map +1 -0
- package/dist/react/jsx.js +134 -0
- package/dist/react/jsx.js.map +1 -0
- package/dist/react/pipeline.d.ts +13 -0
- package/dist/react/pipeline.d.ts.map +1 -0
- package/dist/react/pipeline.js +25 -0
- package/dist/react/pipeline.js.map +1 -0
- package/dist/react/props.d.ts +38 -0
- package/dist/react/props.d.ts.map +1 -0
- package/dist/react/props.js +231 -0
- package/dist/react/props.js.map +1 -0
- package/dist/react/slot-props.d.ts +10 -0
- package/dist/react/slot-props.d.ts.map +1 -0
- package/dist/react/slot-props.js +56 -0
- package/dist/react/slot-props.js.map +1 -0
- package/dist/react/widgets.d.ts +83 -0
- package/dist/react/widgets.d.ts.map +1 -0
- package/dist/react/widgets.js +152 -0
- package/dist/react/widgets.js.map +1 -0
- package/dist/runner.d.ts +55 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +60 -0
- package/dist/runner.js.map +1 -0
- package/dist/store-fs.d.ts +109 -0
- package/dist/store-fs.d.ts.map +1 -0
- package/dist/store-fs.js +98 -0
- package/dist/store-fs.js.map +1 -0
- package/dist/transpile.d.ts +32 -0
- package/dist/transpile.d.ts.map +1 -0
- package/dist/transpile.js +63 -0
- package/dist/transpile.js.map +1 -0
- package/dist/typecheck-store.d.ts +41 -0
- package/dist/typecheck-store.d.ts.map +1 -0
- package/dist/typecheck-store.js +102 -0
- package/dist/typecheck-store.js.map +1 -0
- package/dist/writers/alias.d.ts +13 -0
- package/dist/writers/alias.d.ts.map +1 -0
- package/dist/writers/alias.js +17 -0
- package/dist/writers/alias.js.map +1 -0
- package/dist/writers/async.d.ts +22 -0
- package/dist/writers/async.d.ts.map +1 -0
- package/dist/writers/async.js +48 -0
- package/dist/writers/async.js.map +1 -0
- package/dist/writers/boxed-constructor.d.ts +26 -0
- package/dist/writers/boxed-constructor.d.ts.map +1 -0
- package/dist/writers/boxed-constructor.js +85 -0
- package/dist/writers/boxed-constructor.js.map +1 -0
- package/dist/writers/boxed-field-accessor.d.ts +18 -0
- package/dist/writers/boxed-field-accessor.d.ts.map +1 -0
- package/dist/writers/boxed-field-accessor.js +260 -0
- package/dist/writers/boxed-field-accessor.js.map +1 -0
- package/dist/writers/boxed-layout.d.ts +27 -0
- package/dist/writers/boxed-layout.d.ts.map +1 -0
- package/dist/writers/boxed-layout.js +128 -0
- package/dist/writers/boxed-layout.js.map +1 -0
- package/dist/writers/boxed.d.ts +20 -0
- package/dist/writers/boxed.d.ts.map +1 -0
- package/dist/writers/boxed.js +71 -0
- package/dist/writers/boxed.js.map +1 -0
- package/dist/writers/callables.d.ts +124 -0
- package/dist/writers/callables.d.ts.map +1 -0
- package/dist/writers/callables.js +283 -0
- package/dist/writers/callables.js.map +1 -0
- package/dist/writers/callback.d.ts +16 -0
- package/dist/writers/callback.d.ts.map +1 -0
- package/dist/writers/callback.js +38 -0
- package/dist/writers/callback.js.map +1 -0
- package/dist/writers/class-struct-record.d.ts +31 -0
- package/dist/writers/class-struct-record.d.ts.map +1 -0
- package/dist/writers/class-struct-record.js +67 -0
- package/dist/writers/class-struct-record.js.map +1 -0
- package/dist/writers/class-struct.d.ts +22 -0
- package/dist/writers/class-struct.d.ts.map +1 -0
- package/dist/writers/class-struct.js +85 -0
- package/dist/writers/class-struct.js.map +1 -0
- package/dist/writers/class.d.ts +16 -0
- package/dist/writers/class.d.ts.map +1 -0
- package/dist/writers/class.js +307 -0
- package/dist/writers/class.js.map +1 -0
- package/dist/writers/constant.d.ts +15 -0
- package/dist/writers/constant.d.ts.map +1 -0
- package/dist/writers/constant.js +23 -0
- package/dist/writers/constant.js.map +1 -0
- package/dist/writers/constants.d.ts +15 -0
- package/dist/writers/constants.d.ts.map +1 -0
- package/dist/writers/constants.js +23 -0
- package/dist/writers/constants.js.map +1 -0
- package/dist/writers/constructor-props.d.ts +32 -0
- package/dist/writers/constructor-props.d.ts.map +1 -0
- package/dist/writers/constructor-props.js +110 -0
- package/dist/writers/constructor-props.js.map +1 -0
- package/dist/writers/enum.d.ts +21 -0
- package/dist/writers/enum.d.ts.map +1 -0
- package/dist/writers/enum.js +48 -0
- package/dist/writers/enum.js.map +1 -0
- package/dist/writers/function.d.ts +40 -0
- package/dist/writers/function.d.ts.map +1 -0
- package/dist/writers/function.js +127 -0
- package/dist/writers/function.js.map +1 -0
- package/dist/writers/gtype-binding.d.ts +19 -0
- package/dist/writers/gtype-binding.d.ts.map +1 -0
- package/dist/writers/gtype-binding.js +40 -0
- package/dist/writers/gtype-binding.js.map +1 -0
- package/dist/writers/inheritance.d.ts +48 -0
- package/dist/writers/inheritance.d.ts.map +1 -0
- package/dist/writers/inheritance.js +90 -0
- package/dist/writers/inheritance.js.map +1 -0
- package/dist/writers/interface.d.ts +16 -0
- package/dist/writers/interface.d.ts.map +1 -0
- package/dist/writers/interface.js +155 -0
- package/dist/writers/interface.js.map +1 -0
- package/dist/writers/method.d.ts +150 -0
- package/dist/writers/method.d.ts.map +1 -0
- package/dist/writers/method.js +510 -0
- package/dist/writers/method.js.map +1 -0
- package/dist/writers/param-classify.d.ts +93 -0
- package/dist/writers/param-classify.d.ts.map +1 -0
- package/dist/writers/param-classify.js +163 -0
- package/dist/writers/param-classify.js.map +1 -0
- package/dist/writers/property-accessor.d.ts +19 -0
- package/dist/writers/property-accessor.d.ts.map +1 -0
- package/dist/writers/property-accessor.js +117 -0
- package/dist/writers/property-accessor.js.map +1 -0
- package/dist/writers/registration.d.ts +27 -0
- package/dist/writers/registration.d.ts.map +1 -0
- package/dist/writers/registration.js +30 -0
- package/dist/writers/registration.js.map +1 -0
- package/dist/writers/return-wrap.d.ts +39 -0
- package/dist/writers/return-wrap.d.ts.map +1 -0
- package/dist/writers/return-wrap.js +152 -0
- package/dist/writers/return-wrap.js.map +1 -0
- package/dist/writers/runtime-override.d.ts +18 -0
- package/dist/writers/runtime-override.d.ts.map +1 -0
- package/dist/writers/runtime-override.js +44 -0
- package/dist/writers/runtime-override.js.map +1 -0
- package/dist/writers/signal.d.ts +21 -0
- package/dist/writers/signal.d.ts.map +1 -0
- package/dist/writers/signal.js +260 -0
- package/dist/writers/signal.js.map +1 -0
- package/dist/writers/ts-type.d.ts +50 -0
- package/dist/writers/ts-type.d.ts.map +1 -0
- package/dist/writers/ts-type.js +75 -0
- package/dist/writers/ts-type.js.map +1 -0
- package/dist/writers/types-ts.d.ts +27 -0
- package/dist/writers/types-ts.d.ts.map +1 -0
- package/dist/writers/types-ts.js +97 -0
- package/dist/writers/types-ts.js.map +1 -0
- package/dist/writers/value.d.ts +89 -0
- package/dist/writers/value.d.ts.map +1 -0
- package/dist/writers/value.js +402 -0
- package/dist/writers/value.js.map +1 -0
- package/package.json +55 -0
- package/src/dsl/context.ts +146 -0
- package/src/dsl/emit.ts +42 -0
- package/src/dsl/identifier.ts +52 -0
- package/src/dsl/imports.ts +109 -0
- package/src/dsl/module.ts +75 -0
- package/src/ffi/pipeline.ts +113 -0
- package/src/fingerprint.ts +50 -0
- package/src/gi-store.ts +199 -0
- package/src/gir/boxed.ts +94 -0
- package/src/gir/callback.ts +26 -0
- package/src/gir/class.ts +79 -0
- package/src/gir/enum.ts +55 -0
- package/src/gir/field.ts +40 -0
- package/src/gir/function.ts +67 -0
- package/src/gir/namespace.ts +118 -0
- package/src/gir/parameter.ts +120 -0
- package/src/gir/parse.ts +171 -0
- package/src/gir/primitives.ts +137 -0
- package/src/gir/property.ts +39 -0
- package/src/gir/qualified-name.ts +55 -0
- package/src/gir/qualify.ts +64 -0
- package/src/gir/repository.ts +149 -0
- package/src/gir/signal.ts +36 -0
- package/src/gir/size.ts +129 -0
- package/src/gir/type-ref.ts +180 -0
- package/src/index.ts +16 -0
- package/src/jsx-store.ts +53 -0
- package/src/react/compounds-meta.ts +99 -0
- package/src/react/compounds.ts +204 -0
- package/src/react/internal.ts +91 -0
- package/src/react/jsx.ts +155 -0
- package/src/react/pipeline.ts +29 -0
- package/src/react/props.ts +307 -0
- package/src/react/slot-props.ts +57 -0
- package/src/react/widgets.ts +185 -0
- package/src/runner.ts +89 -0
- package/src/store-fs.ts +157 -0
- package/src/transpile.ts +80 -0
- package/src/typecheck-store.ts +136 -0
- package/src/writers/alias.ts +19 -0
- package/src/writers/async.ts +44 -0
- package/src/writers/boxed-constructor.ts +105 -0
- package/src/writers/boxed-field-accessor.ts +306 -0
- package/src/writers/boxed-layout.ts +166 -0
- package/src/writers/boxed.ts +77 -0
- package/src/writers/callables.ts +339 -0
- package/src/writers/callback.ts +40 -0
- package/src/writers/class-struct-record.ts +75 -0
- package/src/writers/class-struct.ts +96 -0
- package/src/writers/class.ts +384 -0
- package/src/writers/constant.ts +29 -0
- package/src/writers/constructor-props.ts +130 -0
- package/src/writers/enum.ts +53 -0
- package/src/writers/function.ts +136 -0
- package/src/writers/gtype-binding.ts +48 -0
- package/src/writers/inheritance.ts +104 -0
- package/src/writers/interface.ts +168 -0
- package/src/writers/method.ts +678 -0
- package/src/writers/param-classify.ts +178 -0
- package/src/writers/property-accessor.ts +139 -0
- package/src/writers/registration.ts +42 -0
- package/src/writers/return-wrap.ts +180 -0
- package/src/writers/runtime-override.ts +59 -0
- package/src/writers/signal.ts +318 -0
- package/src/writers/ts-type.ts +115 -0
- package/src/writers/value.ts +485 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const RUNTIME_OVERRIDES = new Map([
|
|
2
|
+
[
|
|
3
|
+
"g_value_get_boxed",
|
|
4
|
+
{
|
|
5
|
+
generics: "<T = unknown>",
|
|
6
|
+
signature: "",
|
|
7
|
+
returnType: "T",
|
|
8
|
+
body: 'throw new Error("g_value_get_boxed: runtime override not installed");',
|
|
9
|
+
},
|
|
10
|
+
],
|
|
11
|
+
[
|
|
12
|
+
"g_value_set_boxed",
|
|
13
|
+
{
|
|
14
|
+
signature: "boxed: object | null",
|
|
15
|
+
returnType: "void",
|
|
16
|
+
body: 'throw new Error("g_value_set_boxed: runtime override not installed");',
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Renders the method body for a callable whose marshaling is provided by a
|
|
22
|
+
* hand-written runtime override.
|
|
23
|
+
*
|
|
24
|
+
* `gpointer` exchanges (`GValue` boxed accessors and similar) cannot be
|
|
25
|
+
* marshaled from GIR alone; the FFI runtime replaces them on the prototype
|
|
26
|
+
* at module load. The codegen emits a stub with the override's signature
|
|
27
|
+
* so the consumer types resolve to the runtime shape and the runtime's
|
|
28
|
+
* `prototype` assignment type-checks.
|
|
29
|
+
*
|
|
30
|
+
* Returns `undefined` for callables without a registered override.
|
|
31
|
+
*
|
|
32
|
+
* @param callable - The callable to consider
|
|
33
|
+
* @param memberName - The camelCase JS method name
|
|
34
|
+
*/
|
|
35
|
+
export const renderRuntimeOverride = (callable, memberName) => {
|
|
36
|
+
if (callable.cIdentifier === undefined)
|
|
37
|
+
return undefined;
|
|
38
|
+
const override = RUNTIME_OVERRIDES.get(callable.cIdentifier);
|
|
39
|
+
if (override === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
const generics = override.generics ?? "";
|
|
42
|
+
return `${memberName}${generics}(${override.signature}): ${override.returnType} {\n ${override.body}\n}`;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=runtime-override.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-override.js","sourceRoot":"","sources":["../../src/writers/runtime-override.ts"],"names":[],"mappings":"AAiBA,MAAM,iBAAiB,GAAyC,IAAI,GAAG,CAAC;IACpE;QACI,mBAAmB;QACnB;YACI,QAAQ,EAAE,eAAe;YACzB,SAAS,EAAE,EAAE;YACb,UAAU,EAAE,GAAG;YACf,IAAI,EAAE,uEAAuE;SAChF;KACJ;IACD;QACI,mBAAmB;QACnB;YACI,SAAS,EAAE,sBAAsB;YACjC,UAAU,EAAE,MAAM;YAClB,IAAI,EAAE,uEAAuE;SAChF;KACJ;CACJ,CAAC,CAAC;AAEH;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,QAAqB,EAAE,UAAkB,EAAsB,EAAE;IACnG,IAAI,QAAQ,CAAC,WAAW,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACzD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7D,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC7C,MAAM,QAAQ,GAAG,QAAQ,CAAC,QAAQ,IAAI,EAAE,CAAC;IACzC,OAAO,GAAG,UAAU,GAAG,QAAQ,IAAI,QAAQ,CAAC,SAAS,MAAM,QAAQ,CAAC,UAAU,WAAW,QAAQ,CAAC,IAAI,KAAK,CAAC;AAChH,CAAC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ModuleContext } from "../dsl/context.js";
|
|
2
|
+
import type { GirClass } from "../gir/class.js";
|
|
3
|
+
/**
|
|
4
|
+
* Renders the `connect` and `emit` instance methods for a class that owns or
|
|
5
|
+
* inherits-by-interface at least one signal.
|
|
6
|
+
*
|
|
7
|
+
* Each method is a `switch` over the type's own signals. `connect` resolves the
|
|
8
|
+
* per-signal trampoline, wraps the handler, and hands it to the thin
|
|
9
|
+
* `connectSignal` wrapper around the non-introspectable `g_signal_connect_data`.
|
|
10
|
+
* `emit` marshals the arguments into `GValue`s with `valueFromFfi` and calls the
|
|
11
|
+
* generated `signalLookup` / `signalEmitv` directly. Unknown signals route up
|
|
12
|
+
* the class chain via `super.connect` / `super.emit`; the root `GObject.Object`
|
|
13
|
+
* throws because it bottoms out the hierarchy.
|
|
14
|
+
*
|
|
15
|
+
* Returns an empty array when the class contributes no signals of its own.
|
|
16
|
+
*
|
|
17
|
+
* @param context - The module context
|
|
18
|
+
* @param klass - The class whose signal methods to render
|
|
19
|
+
*/
|
|
20
|
+
export declare const renderSignalMembers: (context: ModuleContext, klass: GirClass) => readonly string[];
|
|
21
|
+
//# sourceMappingURL=signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../../src/writers/signal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AA+BhD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,aAAa,EAAE,OAAO,QAAQ,KAAG,SAAS,MAAM,EA0B5F,CAAC"}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { quote, toCamelCase } from "@gtkx/utils";
|
|
2
|
+
import { indent } from "../dsl/emit.js";
|
|
3
|
+
import { isOutParameter } from "../gir/parameter.js";
|
|
4
|
+
import { qualifyTypeRef } from "../gir/qualify.js";
|
|
5
|
+
import { forEachAncestor, resolveImplementedInterface } from "./inheritance.js";
|
|
6
|
+
import { planTrampolineArgs, renderTupleWriteback } from "./method.js";
|
|
7
|
+
import { isHandlePassing } from "./param-classify.js";
|
|
8
|
+
import { isCellInout, renderFfiType } from "./value.js";
|
|
9
|
+
const SIGNAL_HANDLER_TYPE = "(...args: any[]) => any";
|
|
10
|
+
/**
|
|
11
|
+
* Renders the `connect` and `emit` instance methods for a class that owns or
|
|
12
|
+
* inherits-by-interface at least one signal.
|
|
13
|
+
*
|
|
14
|
+
* Each method is a `switch` over the type's own signals. `connect` resolves the
|
|
15
|
+
* per-signal trampoline, wraps the handler, and hands it to the thin
|
|
16
|
+
* `connectSignal` wrapper around the non-introspectable `g_signal_connect_data`.
|
|
17
|
+
* `emit` marshals the arguments into `GValue`s with `valueFromFfi` and calls the
|
|
18
|
+
* generated `signalLookup` / `signalEmitv` directly. Unknown signals route up
|
|
19
|
+
* the class chain via `super.connect` / `super.emit`; the root `GObject.Object`
|
|
20
|
+
* throws because it bottoms out the hierarchy.
|
|
21
|
+
*
|
|
22
|
+
* Returns an empty array when the class contributes no signals of its own.
|
|
23
|
+
*
|
|
24
|
+
* @param context - The module context
|
|
25
|
+
* @param klass - The class whose signal methods to render
|
|
26
|
+
*/
|
|
27
|
+
export const renderSignalMembers = (context, klass) => {
|
|
28
|
+
if (klass.glibGetType === undefined)
|
|
29
|
+
return [];
|
|
30
|
+
const signals = collectClassSignals(context, klass);
|
|
31
|
+
if (signals.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
const isRootObject = context.namespace.name === "GObject" && klass.name === "Object";
|
|
34
|
+
context.addRuntimeImport("connectSignal");
|
|
35
|
+
context.addRuntimeImport("signalBaseName");
|
|
36
|
+
context.addRuntimeImport("t");
|
|
37
|
+
const connectCases = signals.map((collected) => renderConnectCase(context, collected));
|
|
38
|
+
const emitCases = signals.map((collected) => renderEmitCase(context, collected));
|
|
39
|
+
const connectDefault = isRootObject
|
|
40
|
+
? `default:\n throw new globalThis.Error("Unknown signal '" + signal + "'");`
|
|
41
|
+
: "default:\n return super.connect(signal, handler, after);";
|
|
42
|
+
const emitDefault = isRootObject
|
|
43
|
+
? `default:\n throw new globalThis.Error("Unknown signal '" + sigName + "'");`
|
|
44
|
+
: "default:\n super.emit(sigName, ...args);";
|
|
45
|
+
const connectSwitch = `switch (signalBaseName(signal)) {\n${indent([...connectCases, connectDefault].join("\n"), 1)}\n}`;
|
|
46
|
+
const emitSwitch = `switch (sigName) {\n${indent([...emitCases, emitDefault].join("\n"), 1)}\n}`;
|
|
47
|
+
return [
|
|
48
|
+
`connect(signal: string, handler: ${SIGNAL_HANDLER_TYPE}, after?: boolean): number {\n${indent(connectSwitch, 1)}\n}`,
|
|
49
|
+
`emit(sigName: string, ...args: unknown[]): void {\n${indent(emitSwitch, 1)}\n}`,
|
|
50
|
+
];
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Renders one `connect` switch case: it resolves the signal's typed trampoline,
|
|
54
|
+
* wraps the user handler with the per-signal in-parameter marshalling closure,
|
|
55
|
+
* and dispatches `g_signal_connect_data` through {@link connectSignal} with the
|
|
56
|
+
* full detailed signal name.
|
|
57
|
+
*/
|
|
58
|
+
const renderConnectCase = (context, collected) => {
|
|
59
|
+
const { signal } = collected;
|
|
60
|
+
const { trampoline, invoke } = renderTrampolineAndInvoke(context, collected);
|
|
61
|
+
const body = [
|
|
62
|
+
`const invoke = ${invoke};`,
|
|
63
|
+
"const handlerWrapper = (...args: unknown[]): unknown => invoke(handler, args);",
|
|
64
|
+
`return connectSignal(this, signal, ${trampoline}, handlerWrapper, after ?? false);`,
|
|
65
|
+
].join("\n");
|
|
66
|
+
return `case ${quote(signal.name)}: {\n${indent(body, 1)}\n}`;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Renders one `emit` switch case: it marshals the emitting instance and each
|
|
70
|
+
* signal parameter into a `GValue` with `valueFromFfi`, resolves the signal id
|
|
71
|
+
* from the instance's runtime `GType`, and dispatches `g_signal_emitv`. Signals
|
|
72
|
+
* with a non-void return supply an initialized return `GValue`.
|
|
73
|
+
*/
|
|
74
|
+
const renderEmitCase = (context, collected) => {
|
|
75
|
+
const { signal, namespaceName } = collected;
|
|
76
|
+
const params = signal.parameters.filter((parameter) => !parameter.isVarargs);
|
|
77
|
+
const paramFfi = params.map((parameter) => renderFfiType(context, qualifyTypeRef(parameter.type, namespaceName), parameter.transferOwnership));
|
|
78
|
+
context.addValueFromFfiImport();
|
|
79
|
+
const refs = gobjectRefs(context);
|
|
80
|
+
const valueArgs = [
|
|
81
|
+
'valueFromFfi(t.object("full"), this)',
|
|
82
|
+
...paramFfi.map((ffi, index) => `valueFromFfi(${ffi}, args[${index}])`),
|
|
83
|
+
];
|
|
84
|
+
const values = `[${valueArgs.join(", ")}]`;
|
|
85
|
+
const lookup = `${refs.signalLookup}(${quote(signal.name)}, this.__gtype__)`;
|
|
86
|
+
const returnRef = qualifyTypeRef(signal.returnValue.type, namespaceName);
|
|
87
|
+
const emitStatements = returnRef === undefined || isVoidRef(returnRef)
|
|
88
|
+
? `${refs.signalEmitv}(${values}, ${lookup}, 0);`
|
|
89
|
+
: [
|
|
90
|
+
`const returnValue = new ${refs.Value}();`,
|
|
91
|
+
`returnValue.init(${renderReturnGType(context, returnRef)});`,
|
|
92
|
+
`${refs.signalEmitv}(${values}, ${lookup}, 0, returnValue);`,
|
|
93
|
+
].join("\n");
|
|
94
|
+
return `case ${quote(signal.name)}: {\n${indent(`${emitStatements}\nreturn;`, 1)}\n}`;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Resolves the `GObject` marshalling references the inline `emit` switch needs.
|
|
98
|
+
* Within the `GObject` namespace the generated `Value`, `signalEmitv`, and
|
|
99
|
+
* `signalLookup` are local; elsewhere they are reached through the cross-
|
|
100
|
+
* namespace `GObject` alias.
|
|
101
|
+
*/
|
|
102
|
+
const gobjectRefs = (context) => {
|
|
103
|
+
if (context.namespace.name === "GObject") {
|
|
104
|
+
return { Value: "Value", signalEmitv: "signalEmitv", signalLookup: "signalLookup" };
|
|
105
|
+
}
|
|
106
|
+
const alias = context.addCrossNamespaceImport("GObject");
|
|
107
|
+
return { Value: `${alias}.Value`, signalEmitv: `${alias}.signalEmitv`, signalLookup: `${alias}.signalLookup` };
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Renders the trampoline FFI descriptor used to connect a handler and the
|
|
111
|
+
* `invoke` closure that marshals the trampoline arguments into the user handler.
|
|
112
|
+
*
|
|
113
|
+
* Out-parameter cells are wrapped in `t.ref(...)` so the native trampoline can
|
|
114
|
+
* write them back; the `invoke` closure mirrors the out-parameter tuple
|
|
115
|
+
* convention used by method out-parameters.
|
|
116
|
+
*/
|
|
117
|
+
const renderTrampolineAndInvoke = (context, collected) => {
|
|
118
|
+
const { signal, namespaceName } = collected;
|
|
119
|
+
const params = signal.parameters.filter((parameter) => !parameter.isVarargs);
|
|
120
|
+
const paramFfi = params.map((parameter) => renderFfiType(context, qualifyTypeRef(parameter.type, namespaceName), parameter.transferOwnership));
|
|
121
|
+
const trampolineParamFfi = params.map((parameter, index) => isOutParameter(parameter) || isCellInout(context, parameter) ? `t.ref(${paramFfi[index]})` : paramFfi[index]);
|
|
122
|
+
const returnRef = qualifyTypeRef(signal.returnValue.type, namespaceName);
|
|
123
|
+
const isVoid = isVoidRef(returnRef);
|
|
124
|
+
const returnFfi = isVoid ? "t.void" : renderFfiType(context, returnRef, signal.returnValue.transferOwnership);
|
|
125
|
+
const trampolineArgs = ['t.object("borrowed")', ...trampolineParamFfi, "t.void"].join(", ");
|
|
126
|
+
const trampoline = `t.trampoline([${trampolineArgs}], ${returnFfi}, { hasDestroy: true, userDataIndex: ${params.length + 1} })`;
|
|
127
|
+
const invoke = renderInvokeClosure(context, collected, params, isVoid ? undefined : returnRef);
|
|
128
|
+
return { trampoline, invoke };
|
|
129
|
+
};
|
|
130
|
+
const collectClassSignals = (context, klass) => {
|
|
131
|
+
const inheritedNames = collectInheritedSignalNames(context, klass);
|
|
132
|
+
const seen = new Set();
|
|
133
|
+
const result = [];
|
|
134
|
+
for (const signal of klass.signals) {
|
|
135
|
+
const name = toCamelCase(signal.name);
|
|
136
|
+
if (inheritedNames.has(name) || seen.has(name))
|
|
137
|
+
continue;
|
|
138
|
+
seen.add(name);
|
|
139
|
+
result.push({ signal, namespaceName: context.namespace.name });
|
|
140
|
+
}
|
|
141
|
+
for (const implementName of klass.implements) {
|
|
142
|
+
const iface = resolveImplementedInterface(context, implementName);
|
|
143
|
+
if (iface === undefined)
|
|
144
|
+
continue;
|
|
145
|
+
for (const signal of iface.klass.signals) {
|
|
146
|
+
const name = toCamelCase(signal.name);
|
|
147
|
+
if (inheritedNames.has(name) || seen.has(name))
|
|
148
|
+
continue;
|
|
149
|
+
seen.add(name);
|
|
150
|
+
result.push({ signal, namespaceName: iface.namespaceName });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
};
|
|
155
|
+
const collectInheritedSignalNames = (context, klass) => {
|
|
156
|
+
const names = new Set();
|
|
157
|
+
forEachAncestor(context, klass, (ancestor) => {
|
|
158
|
+
for (const signal of ancestor.klass.signals)
|
|
159
|
+
names.add(toCamelCase(signal.name));
|
|
160
|
+
for (const implementName of ancestor.klass.implements) {
|
|
161
|
+
const iface = resolveImplementedInterface(context, implementName, ancestor.namespaceName);
|
|
162
|
+
if (iface === undefined)
|
|
163
|
+
continue;
|
|
164
|
+
for (const signal of iface.klass.signals)
|
|
165
|
+
names.add(toCamelCase(signal.name));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
return names;
|
|
169
|
+
};
|
|
170
|
+
const renderInvokeClosure = (context, collected, params, returnRef) => {
|
|
171
|
+
const { namespaceName } = collected;
|
|
172
|
+
const { callArgs, outArgIndices } = planTrampolineArgs(context, params, namespaceName, 1);
|
|
173
|
+
if (outArgIndices.length === 0) {
|
|
174
|
+
if (returnRef !== undefined && isHandlePassing(context, returnRef)) {
|
|
175
|
+
context.addRuntimeImport("tryGetHandle");
|
|
176
|
+
return `(handler, args) => {\n const _result = handler(${callArgs});\n return tryGetHandle(_result);\n}`;
|
|
177
|
+
}
|
|
178
|
+
return `(handler, args) => handler(${callArgs})`;
|
|
179
|
+
}
|
|
180
|
+
return renderOutParamInvoke(context, callArgs, outArgIndices, returnRef);
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Renders the invoke closure for a signal with out-parameters.
|
|
184
|
+
*
|
|
185
|
+
* The handler returns its results as the tuple {@link renderMethodReturnType}
|
|
186
|
+
* describes (`[primary, ...outs]` when both exist, the scalar out alone for a
|
|
187
|
+
* void return with a single out, or an out-only tuple otherwise). The shared
|
|
188
|
+
* {@link renderTupleWriteback} destructures that tuple and writes each out
|
|
189
|
+
* value into its trampoline cell's `value` slot — the native side flushes
|
|
190
|
+
* those cells through the matching C out-pointers, so the tuple convention
|
|
191
|
+
* stays entirely in generated code.
|
|
192
|
+
*
|
|
193
|
+
* @param context - The module context
|
|
194
|
+
* @param callArgs - The rendered in-parameter call arguments
|
|
195
|
+
* @param outArgIndices - Trampoline-arg indices of the out-parameter cells
|
|
196
|
+
* @param returnRef - The signal's return type, or `undefined` for void
|
|
197
|
+
*/
|
|
198
|
+
const renderOutParamInvoke = (context, callArgs, outArgIndices, returnRef) => {
|
|
199
|
+
const body = renderTupleWriteback(context, `handler(${callArgs})`, outArgIndices, returnRef);
|
|
200
|
+
return `(handler, args) => {\n ${body}\n}`;
|
|
201
|
+
};
|
|
202
|
+
const renderReturnGType = (context, ref) => {
|
|
203
|
+
if (ref.kind === "primitive") {
|
|
204
|
+
return `${typeFromNameReference(context)}(${quote(primitiveGTypeName(ref.category))})`;
|
|
205
|
+
}
|
|
206
|
+
if (ref.kind === "named") {
|
|
207
|
+
const owner = ref.namespaceName ?? context.namespace.name;
|
|
208
|
+
const resolved = context.repository.resolveNamed(owner, ref.typeName);
|
|
209
|
+
if (resolved !== undefined && resolved.kind === "enum") {
|
|
210
|
+
context.addNativeImport("call");
|
|
211
|
+
context.addRuntimeImport("t");
|
|
212
|
+
const lib = resolved.namespace.sharedLibrary ?? "";
|
|
213
|
+
const getter = resolved.value.glibGetType ?? "";
|
|
214
|
+
return `call(${quote(lib)}, ${quote(getter)}, [], t.uint64)`;
|
|
215
|
+
}
|
|
216
|
+
if (resolved !== undefined &&
|
|
217
|
+
(resolved.kind === "class" || resolved.kind === "interface" || resolved.kind === "boxed")) {
|
|
218
|
+
const glibTypeName = glibTypeNameOf(resolved.value) ?? ref.typeName;
|
|
219
|
+
return `${typeFromNameReference(context)}(${quote(glibTypeName)})`;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return `${typeFromNameReference(context)}("GObject")`;
|
|
223
|
+
};
|
|
224
|
+
const isVoidRef = (ref) => ref === undefined || (ref.kind === "primitive" && ref.category === "void");
|
|
225
|
+
const glibTypeNameOf = (value) => value.glibTypeName ?? value.cType;
|
|
226
|
+
const typeFromNameReference = (context) => {
|
|
227
|
+
if (context.namespace.name === "GObject")
|
|
228
|
+
return "typeFromName";
|
|
229
|
+
const alias = context.addCrossNamespaceImport("GObject");
|
|
230
|
+
return `${alias}.typeFromName`;
|
|
231
|
+
};
|
|
232
|
+
const primitiveGTypeName = (category) => {
|
|
233
|
+
switch (category) {
|
|
234
|
+
case "boolean":
|
|
235
|
+
return "gboolean";
|
|
236
|
+
case "int8":
|
|
237
|
+
case "int16":
|
|
238
|
+
case "int32":
|
|
239
|
+
return "gint";
|
|
240
|
+
case "uint8":
|
|
241
|
+
case "uint16":
|
|
242
|
+
case "uint32":
|
|
243
|
+
case "unichar":
|
|
244
|
+
return "guint";
|
|
245
|
+
case "int64":
|
|
246
|
+
return "gint64";
|
|
247
|
+
case "uint64":
|
|
248
|
+
case "pointer":
|
|
249
|
+
return "guint64";
|
|
250
|
+
case "float32":
|
|
251
|
+
return "gfloat";
|
|
252
|
+
case "float64":
|
|
253
|
+
return "gdouble";
|
|
254
|
+
case "string":
|
|
255
|
+
return "gchararray";
|
|
256
|
+
case "void":
|
|
257
|
+
return "void";
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
//# sourceMappingURL=signal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signal.js","sourceRoot":"","sources":["../../src/writers/signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGnD,OAAO,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAExD,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;AAoBtD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAAsB,EAAE,KAAe,EAAqB,EAAE;IAC9F,IAAI,KAAK,CAAC,WAAW,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IAC/C,MAAM,OAAO,GAAG,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACpD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACpC,MAAM,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK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,QAAQ,EAAE,CAAC;QACf,KAAK,SAAS;YACV,OAAO,UAAU,CAAC;QACtB,KAAK,MAAM,CAAC;QACZ,KAAK,OAAO,CAAC;QACb,KAAK,OAAO;YACR,OAAO,MAAM,CAAC;QAClB,KAAK,OAAO,CAAC;QACb,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACV,OAAO,OAAO,CAAC;QACnB,KAAK,OAAO;YACR,OAAO,QAAQ,CAAC;QACpB,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACV,OAAO,SAAS,CAAC;QACrB,KAAK,SAAS;YACV,OAAO,QAAQ,CAAC;QACpB,KAAK,SAAS;YACV,OAAO,SAAS,CAAC;QACrB,KAAK,QAAQ;YACT,OAAO,YAAY,CAAC;QACxB,KAAK,MAAM;YACP,OAAO,MAAM,CAAC;IACtB,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ModuleContext } from "../dsl/context.js";
|
|
2
|
+
import type { GirTypeRef, NamedTypeRef } from "../gir/type-ref.js";
|
|
3
|
+
/**
|
|
4
|
+
* TypeScript type annotations the writers emit alongside runtime values.
|
|
5
|
+
*
|
|
6
|
+
* Generated `.ts` files are consumed both as runtime modules (transpiled to
|
|
7
|
+
* `.js`) and as declaration sources (`ts.transpileDeclaration` extracts the
|
|
8
|
+
* `.d.ts` shape). Every exported value, parameter, and return position
|
|
9
|
+
* therefore carries an explicit annotation so isolated declaration emit
|
|
10
|
+
* succeeds without inference. The helpers below produce those annotations
|
|
11
|
+
* from the same `GirTypeRef` shapes the runtime FFI writers consume,
|
|
12
|
+
* importing referenced cross-namespace classes on demand.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Strategy describing the type-ref shapes the FFI and React generators render
|
|
16
|
+
* differently: container style (`Map` vs `Record`), the callback placeholder,
|
|
17
|
+
* whether a `GByteArray` collapses to `number[]`, and how a named reference is
|
|
18
|
+
* qualified and imported. {@link renderBaseTypeFor} renders every other shape
|
|
19
|
+
* identically for both.
|
|
20
|
+
*/
|
|
21
|
+
export type TsTypeTarget = {
|
|
22
|
+
/** `"map"` → `Map<K, V>`; `"record"` → `Record<K, V>`. */
|
|
23
|
+
readonly containerStyle: "map" | "record";
|
|
24
|
+
/** The placeholder emitted for a callback type. */
|
|
25
|
+
readonly callbackType: string;
|
|
26
|
+
/** Whether a `GByteArray` renders as `number[]` instead of `element[]`. */
|
|
27
|
+
readonly byteArrayAsNumber: boolean;
|
|
28
|
+
/** Renders a named reference (class/interface/boxed/enum/callback/alias). */
|
|
29
|
+
readonly renderNamed: (ref: NamedTypeRef) => string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Renders the TypeScript annotation for a `GirTypeRef`, dispatching the shapes
|
|
33
|
+
* both generators share and delegating the divergent ones to {@link target}.
|
|
34
|
+
*
|
|
35
|
+
* @param target - The per-generator rendering strategy
|
|
36
|
+
* @param ref - The type reference, or `undefined` for void
|
|
37
|
+
*/
|
|
38
|
+
export declare const renderBaseTypeFor: (target: TsTypeTarget, ref: GirTypeRef | undefined) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Renders a TypeScript type annotation for a `GirTypeRef` in an FFI module:
|
|
41
|
+
* classes/interfaces/boxeds/enums/callbacks as their qualified identifier,
|
|
42
|
+
* arrays and lists as `T[]`, hashtables as `Map<K, V>`, primitives to their TS
|
|
43
|
+
* counterpart, and `undefined` to `void`.
|
|
44
|
+
*
|
|
45
|
+
* @param context - The module context
|
|
46
|
+
* @param ref - The type reference, or `undefined` for void
|
|
47
|
+
* @param isNullable - Whether the slot accepts `null`
|
|
48
|
+
*/
|
|
49
|
+
export declare const renderTsType: (context: ModuleContext, ref: GirTypeRef | undefined, isNullable?: boolean) => string;
|
|
50
|
+
//# sourceMappingURL=ts-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ts-type.d.ts","sourceRoot":"","sources":["../../src/writers/ts-type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIvD,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEnE;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG;IACvB,0DAA0D;IAC1D,QAAQ,CAAC,cAAc,EAAE,KAAK,GAAG,QAAQ,CAAC;IAC1C,mDAAmD;IACnD,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,2EAA2E;IAC3E,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,6EAA6E;IAC7E,QAAQ,CAAC,WAAW,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,MAAM,CAAC;CACvD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,YAAY,EAAE,KAAK,UAAU,GAAG,SAAS,KAAG,MAsBrF,CAAC;AASF;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,aAAa,EAAE,KAAK,UAAU,GAAG,SAAS,EAAE,oBAAkB,KAAG,MAGtG,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { aliasExportName } from "../dsl/identifier.js";
|
|
2
|
+
import { PRIMITIVE_TS_TYPE } from "../gir/primitives.js";
|
|
3
|
+
/**
|
|
4
|
+
* Renders the TypeScript annotation for a `GirTypeRef`, dispatching the shapes
|
|
5
|
+
* both generators share and delegating the divergent ones to {@link target}.
|
|
6
|
+
*
|
|
7
|
+
* @param target - The per-generator rendering strategy
|
|
8
|
+
* @param ref - The type reference, or `undefined` for void
|
|
9
|
+
*/
|
|
10
|
+
export const renderBaseTypeFor = (target, ref) => {
|
|
11
|
+
if (ref === undefined)
|
|
12
|
+
return "void";
|
|
13
|
+
switch (ref.kind) {
|
|
14
|
+
case "primitive":
|
|
15
|
+
return PRIMITIVE_TS_TYPE[ref.category];
|
|
16
|
+
case "varargs":
|
|
17
|
+
return "unknown[]";
|
|
18
|
+
case "callback":
|
|
19
|
+
return target.callbackType;
|
|
20
|
+
case "named":
|
|
21
|
+
return target.renderNamed(ref);
|
|
22
|
+
case "array":
|
|
23
|
+
return `${renderBaseTypeFor(target, ref.element)}[]`;
|
|
24
|
+
case "list":
|
|
25
|
+
if (ref.flavor === "gbytearray" && target.byteArrayAsNumber)
|
|
26
|
+
return "number[]";
|
|
27
|
+
return `${renderBaseTypeFor(target, ref.element)}[]`;
|
|
28
|
+
case "hashtable": {
|
|
29
|
+
const key = renderBaseTypeFor(target, ref.key);
|
|
30
|
+
const value = renderBaseTypeFor(target, ref.value);
|
|
31
|
+
return target.containerStyle === "record" ? `Record<${key}, ${value}>` : `Map<${key}, ${value}>`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const moduleTarget = (context) => ({
|
|
36
|
+
containerStyle: "map",
|
|
37
|
+
callbackType: "((...args: any[]) => any)",
|
|
38
|
+
byteArrayAsNumber: true,
|
|
39
|
+
renderNamed: (ref) => namedTsType(context, ref),
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Renders a TypeScript type annotation for a `GirTypeRef` in an FFI module:
|
|
43
|
+
* classes/interfaces/boxeds/enums/callbacks as their qualified identifier,
|
|
44
|
+
* arrays and lists as `T[]`, hashtables as `Map<K, V>`, primitives to their TS
|
|
45
|
+
* counterpart, and `undefined` to `void`.
|
|
46
|
+
*
|
|
47
|
+
* @param context - The module context
|
|
48
|
+
* @param ref - The type reference, or `undefined` for void
|
|
49
|
+
* @param isNullable - Whether the slot accepts `null`
|
|
50
|
+
*/
|
|
51
|
+
export const renderTsType = (context, ref, isNullable = false) => {
|
|
52
|
+
const base = renderBaseTypeFor(moduleTarget(context), ref);
|
|
53
|
+
return isNullable ? `${base} | null` : base;
|
|
54
|
+
};
|
|
55
|
+
const namedTsType = (context, ref) => {
|
|
56
|
+
const namespaceName = ref.namespaceName ?? context.namespace.name;
|
|
57
|
+
const resolved = context.repository.resolveNamed(namespaceName, ref.typeName);
|
|
58
|
+
if (resolved === undefined) {
|
|
59
|
+
return context.qualify(namespaceName, ref.typeName);
|
|
60
|
+
}
|
|
61
|
+
return resolvedTsType(context, namespaceName, ref.typeName, resolved);
|
|
62
|
+
};
|
|
63
|
+
const resolvedTsType = (context, namespaceName, typeName, resolved) => {
|
|
64
|
+
switch (resolved.kind) {
|
|
65
|
+
case "class":
|
|
66
|
+
case "interface":
|
|
67
|
+
case "boxed":
|
|
68
|
+
case "enum":
|
|
69
|
+
case "callback":
|
|
70
|
+
return context.qualify(namespaceName, typeName);
|
|
71
|
+
case "alias":
|
|
72
|
+
return context.qualify(namespaceName, aliasExportName(namespaceName, typeName));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=ts-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ts-type.js","sourceRoot":"","sources":["../../src/writers/ts-type.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAkCzD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,MAAoB,EAAE,GAA2B,EAAU,EAAE;IAC3F,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IACrC,QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC;QACf,KAAK,WAAW;YACZ,OAAO,iBAAiB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC3C,KAAK,SAAS;YACV,OAAO,WAAW,CAAC;QACvB,KAAK,UAAU;YACX,OAAO,MAAM,CAAC,YAAY,CAAC;QAC/B,KAAK,OAAO;YACR,OAAO,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACnC,KAAK,OAAO;YACR,OAAO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B,EAAE,GAAiB,EAAU,EAAE;IACtE,MAAM,aAAa,GAAG,GAAG,CAAC,aAAa,IAAI,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC;IAClE,MAAM,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC,aAAa,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9E,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AAC1E,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CACnB,OAAsB,EACtB,aAAqB,EACrB,QAAgB,EAChB,QAAuB,EACjB,EAAE;IACR,QAAQ,QAAQ,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,OAAO,CAAC;QACb,KAAK,WAAW,CAAC;QACjB,KAAK,OAAO,CAAC;QACb,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACX,OAAO,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;QACpD,KAAK,OAAO;YACR,OAAO,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE,eAAe,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;IACxF,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ModuleContext } from "../dsl/context.js";
|
|
2
|
+
import type { GirTypeRef } from "../gir/type-ref.js";
|
|
3
|
+
/**
|
|
4
|
+
* TypeScript type annotations the writers emit alongside runtime values.
|
|
5
|
+
*
|
|
6
|
+
* Generated `.ts` files are consumed both as runtime modules (transpiled to
|
|
7
|
+
* `.js`) and as declaration sources (`ts.transpileDeclaration` extracts the
|
|
8
|
+
* `.d.ts` shape). Every exported value, parameter, and return position
|
|
9
|
+
* therefore carries an explicit annotation so isolated declaration emit
|
|
10
|
+
* succeeds without inference. The helpers below produce those annotations
|
|
11
|
+
* from the same `GirTypeRef` shapes the runtime FFI writers consume,
|
|
12
|
+
* importing referenced cross-namespace classes on demand.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Renders a TypeScript type annotation for a `GirTypeRef`.
|
|
16
|
+
*
|
|
17
|
+
* Routes through resolution (classes/interfaces/boxeds/enums as their
|
|
18
|
+
* PascalCase type, callbacks as `Function`,
|
|
19
|
+
* arrays as `T[]`, lists as `T[]`, hashtables as `Record<K, V>`, primitive
|
|
20
|
+
* categories to their TS counterpart, and `undefined` to `void`).
|
|
21
|
+
*
|
|
22
|
+
* @param ctx - The module context
|
|
23
|
+
* @param ref - The type reference, or `undefined` for void
|
|
24
|
+
* @param isNullable - Whether the slot accepts `null`
|
|
25
|
+
*/
|
|
26
|
+
export declare const writeTsType: (ctx: ModuleContext, ref: GirTypeRef | undefined, isNullable?: boolean) => string;
|
|
27
|
+
//# sourceMappingURL=types-ts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-ts.d.ts","sourceRoot":"","sources":["../../src/writers/types-ts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAGvD,OAAO,KAAK,EAER,UAAU,EAKb,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;;;;GAUG;AAEH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,WAAW,GAAI,KAAK,aAAa,EAAE,KAAK,UAAU,GAAG,SAAS,EAAE,oBAAkB,KAAG,MAGjG,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { toPascalCase } from "@gtkx/utils";
|
|
2
|
+
import { PRIMITIVE_TS_TYPE } from "../gir/primitives.js";
|
|
3
|
+
/**
|
|
4
|
+
* TypeScript type annotations the writers emit alongside runtime values.
|
|
5
|
+
*
|
|
6
|
+
* Generated `.ts` files are consumed both as runtime modules (transpiled to
|
|
7
|
+
* `.js`) and as declaration sources (`ts.transpileDeclaration` extracts the
|
|
8
|
+
* `.d.ts` shape). Every exported value, parameter, and return position
|
|
9
|
+
* therefore carries an explicit annotation so isolated declaration emit
|
|
10
|
+
* succeeds without inference. The helpers below produce those annotations
|
|
11
|
+
* from the same `GirTypeRef` shapes the runtime FFI writers consume,
|
|
12
|
+
* importing referenced cross-namespace classes on demand.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Renders a TypeScript type annotation for a `GirTypeRef`.
|
|
16
|
+
*
|
|
17
|
+
* Routes through resolution (classes/interfaces/boxeds/enums as their
|
|
18
|
+
* PascalCase type, callbacks as `Function`,
|
|
19
|
+
* arrays as `T[]`, lists as `T[]`, hashtables as `Record<K, V>`, primitive
|
|
20
|
+
* categories to their TS counterpart, and `undefined` to `void`).
|
|
21
|
+
*
|
|
22
|
+
* @param ctx - The module context
|
|
23
|
+
* @param ref - The type reference, or `undefined` for void
|
|
24
|
+
* @param isNullable - Whether the slot accepts `null`
|
|
25
|
+
*/
|
|
26
|
+
export const writeTsType = (ctx, ref, isNullable = false) => {
|
|
27
|
+
const base = writeBaseType(ctx, ref);
|
|
28
|
+
return isNullable ? `${base} | null` : base;
|
|
29
|
+
};
|
|
30
|
+
const writeBaseType = (ctx, ref) => {
|
|
31
|
+
if (ref === undefined)
|
|
32
|
+
return "void";
|
|
33
|
+
switch (ref.kind) {
|
|
34
|
+
case "primitive":
|
|
35
|
+
return primitiveTsType(ref);
|
|
36
|
+
case "varargs":
|
|
37
|
+
return "unknown[]";
|
|
38
|
+
case "callback":
|
|
39
|
+
return "((...args: any[]) => any)";
|
|
40
|
+
case "named":
|
|
41
|
+
return namedTsType(ctx, ref);
|
|
42
|
+
case "array":
|
|
43
|
+
return arrayTsType(ctx, ref);
|
|
44
|
+
case "list":
|
|
45
|
+
return listTsType(ctx, ref);
|
|
46
|
+
case "hashtable":
|
|
47
|
+
return hashTableTsType(ctx, ref);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const primitiveTsType = (ref) => PRIMITIVE_TS_TYPE[ref.category];
|
|
51
|
+
const namedTsType = (ctx, ref) => {
|
|
52
|
+
const namespaceName = ref.namespaceName ?? ctx.namespace.name;
|
|
53
|
+
const resolved = ctx.repository.resolveNamed(namespaceName, ref.typeName);
|
|
54
|
+
if (resolved === undefined) {
|
|
55
|
+
return qualifiedTypeReference(ctx, namespaceName, ref.typeName);
|
|
56
|
+
}
|
|
57
|
+
return resolvedTsType(ctx, namespaceName, ref.typeName, resolved);
|
|
58
|
+
};
|
|
59
|
+
const resolvedTsType = (ctx, namespaceName, typeName, resolved) => {
|
|
60
|
+
switch (resolved.kind) {
|
|
61
|
+
case "class":
|
|
62
|
+
case "interface":
|
|
63
|
+
case "boxed":
|
|
64
|
+
case "enum":
|
|
65
|
+
return qualifiedTypeReference(ctx, namespaceName, typeName);
|
|
66
|
+
case "callback":
|
|
67
|
+
return "((...args: any[]) => any)";
|
|
68
|
+
case "alias": {
|
|
69
|
+
const exportName = resolved.cType ?? toPascalCase(typeName);
|
|
70
|
+
if (namespaceName === ctx.namespace.name)
|
|
71
|
+
return exportName;
|
|
72
|
+
return `${ctx.addCrossNamespaceImport(namespaceName)}.${exportName}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const qualifiedTypeReference = (ctx, namespaceName, typeName) => {
|
|
77
|
+
if (namespaceName === ctx.namespace.name)
|
|
78
|
+
return toPascalCase(typeName);
|
|
79
|
+
const alias = ctx.addCrossNamespaceImport(namespaceName);
|
|
80
|
+
return `${alias}.${toPascalCase(typeName)}`;
|
|
81
|
+
};
|
|
82
|
+
const arrayTsType = (ctx, ref) => {
|
|
83
|
+
const element = writeBaseType(ctx, ref.element);
|
|
84
|
+
return `${element}[]`;
|
|
85
|
+
};
|
|
86
|
+
const listTsType = (ctx, ref) => {
|
|
87
|
+
if (ref.flavor === "gbytearray")
|
|
88
|
+
return "number[]";
|
|
89
|
+
const element = writeBaseType(ctx, ref.element);
|
|
90
|
+
return `${element}[]`;
|
|
91
|
+
};
|
|
92
|
+
const hashTableTsType = (ctx, ref) => {
|
|
93
|
+
const key = writeBaseType(ctx, ref.key);
|
|
94
|
+
const value = writeBaseType(ctx, ref.value);
|
|
95
|
+
return `Map<${key}, ${value}>`;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=types-ts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-ts.js","sourceRoot":"","sources":["../../src/writers/types-ts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAWzD;;;;;;;;;;GAUG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAkB,EAAE,GAA2B,EAAE,UAAU,GAAG,KAAK,EAAU,EAAE;IACvG,MAAM,IAAI,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACrC,OAAO,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;AAChD,CAAC,CAAC;AAEF,MAAM,aAAa,GAAG,CAAC,GAAkB,EAAE,GAA2B,EAAU,EAAE;IAC9E,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IACrC,QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC;QACf,KAAK,WAAW;YACZ,OAAO,eAAe,CAAC,GAAG,CAAC,CAAC;QAChC,KAAK,SAAS;YACV,OAAO,WAAW,CAAC;QACvB,KAAK,UAAU;YACX,OAAO,2BAA2B,CAAC;QACvC,KAAK,OAAO;YACR,OAAO,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACjC,KAAK,OAAO;YACR,OAAO,WAA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