@gtkx/gir 0.4.0 → 0.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gtkx/gir",
3
- "version": "0.4.0",
3
+ "version": "0.4.3",
4
4
  "description": "GObject Introspection file parser for GTKX",
5
5
  "keywords": [
6
6
  "gtk",
package/src/parser.ts CHANGED
@@ -248,10 +248,12 @@ export class GirParser {
248
248
  const closure = param["@_closure"] as string | undefined;
249
249
  const destroy = param["@_destroy"] as string | undefined;
250
250
  const transferOwnership = param["@_transfer-ownership"] as string | undefined;
251
+ const callerAllocates = param["@_caller-allocates"] as string | undefined;
251
252
  return {
252
253
  name: String(param["@_name"] ?? ""),
253
254
  type: this.parseType((param.type ?? param.array) as Record<string, unknown> | undefined),
254
255
  direction: (String(param["@_direction"] ?? "in") as "in" | "out" | "inout") || "in",
256
+ callerAllocates: callerAllocates === "1",
255
257
  nullable: param["@_nullable"] === "1",
256
258
  optional: param["@_allow-none"] === "1",
257
259
  scope: scope as "async" | "call" | "notified" | undefined,
package/src/types.ts CHANGED
@@ -208,6 +208,8 @@ export interface GirParameter {
208
208
  type: GirType;
209
209
  /** The parameter direction (in, out, or inout). */
210
210
  direction?: "in" | "out" | "inout";
211
+ /** Whether the caller allocates memory for out parameters. */
212
+ callerAllocates?: boolean;
211
213
  /** Whether this parameter can be null. */
212
214
  nullable?: boolean;
213
215
  /** Whether this parameter is optional. */
@@ -766,6 +768,14 @@ export class TypeMapper {
766
768
  this.onExternalTypeUsed = callback ?? undefined;
767
769
  }
768
770
 
771
+ /**
772
+ * Gets the current external type usage callback.
773
+ * @returns The callback or null if not set
774
+ */
775
+ getExternalTypeUsageCallback(): ((usage: ExternalTypeUsage) => void) | null {
776
+ return this.onExternalTypeUsed ?? null;
777
+ }
778
+
769
779
  /**
770
780
  * Sets a callback to track same-namespace class/interface usage during type mapping.
771
781
  * @param callback - Called when a same-namespace class is used, or null to clear
@@ -774,6 +784,14 @@ export class TypeMapper {
774
784
  this.onSameNamespaceClassUsed = callback ?? undefined;
775
785
  }
776
786
 
787
+ /**
788
+ * Gets the current same-namespace class usage callback.
789
+ * @returns The callback or null if not set
790
+ */
791
+ getSameNamespaceClassUsageCallback(): ((className: string, originalName: string) => void) | null {
792
+ return this.onSameNamespaceClassUsed ?? null;
793
+ }
794
+
777
795
  /**
778
796
  * Sets the type registry for cross-namespace type resolution.
779
797
  * @param registry - The TypeRegistry instance
@@ -992,38 +1010,37 @@ export class TypeMapper {
992
1010
  */
993
1011
  mapParameter(param: GirParameter): MappedType {
994
1012
  if (param.direction === "out" || param.direction === "inout") {
995
- // Temporarily disable callbacks while mapping the inner type,
996
- // since Ref<unknown> doesn't need the type import
997
- const savedRecordCallback = this.onRecordUsed;
998
- const savedExternalCallback = this.onExternalTypeUsed;
999
- const savedClassCallback = this.onSameNamespaceClassUsed;
1000
- this.onRecordUsed = undefined;
1001
- this.onExternalTypeUsed = undefined;
1002
- this.onSameNamespaceClassUsed = undefined;
1003
-
1004
1013
  const innerType = this.mapType(param.type);
1005
-
1006
- // Restore callbacks
1007
- this.onRecordUsed = savedRecordCallback;
1008
- this.onExternalTypeUsed = savedExternalCallback;
1009
- this.onSameNamespaceClassUsed = savedClassCallback;
1010
-
1011
- // For boxed/gobject types, Ref wraps the raw ptr (unknown), not the wrapper class
1012
1014
  const isBoxedOrGObject = innerType.ffi.type === "boxed" || innerType.ffi.type === "gobject";
1013
- const tsType = isBoxedOrGObject ? "unknown" : innerType.ts;
1014
1015
 
1015
- // Only trigger callbacks for non-boxed/gobject types that will actually appear in TS
1016
+ if (param.callerAllocates && isBoxedOrGObject) {
1017
+ return {
1018
+ ...innerType,
1019
+ ffi: {
1020
+ ...innerType.ffi,
1021
+ borrowed: true,
1022
+ },
1023
+ };
1024
+ }
1025
+
1016
1026
  if (!isBoxedOrGObject) {
1017
- // Re-map with callbacks enabled to register the type usage
1018
- this.mapType(param.type);
1027
+ return {
1028
+ ts: `Ref<${innerType.ts}>`,
1029
+ ffi: {
1030
+ type: "ref",
1031
+ innerType: innerType.ffi,
1032
+ },
1033
+ };
1019
1034
  }
1020
1035
 
1021
1036
  return {
1022
- ts: `Ref<${tsType}>`,
1037
+ ts: `Ref<${innerType.ts}>`,
1023
1038
  ffi: {
1024
1039
  type: "ref",
1025
1040
  innerType: innerType.ffi,
1026
1041
  },
1042
+ externalType: innerType.externalType,
1043
+ kind: innerType.kind,
1027
1044
  };
1028
1045
  }
1029
1046