@gtkx/runtime 1.2.2 → 1.4.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 (101) hide show
  1. package/README.md +6 -4
  2. package/dist/arg.d.ts +6 -1
  3. package/dist/arg.d.ts.map +1 -1
  4. package/dist/arg.js +2 -1
  5. package/dist/arg.js.map +1 -1
  6. package/dist/closure.d.ts.map +1 -1
  7. package/dist/closure.js +2 -11
  8. package/dist/closure.js.map +1 -1
  9. package/dist/descriptors.d.ts +21 -3
  10. package/dist/descriptors.d.ts.map +1 -1
  11. package/dist/descriptors.js +50 -21
  12. package/dist/descriptors.js.map +1 -1
  13. package/dist/field.d.ts +42 -0
  14. package/dist/field.d.ts.map +1 -0
  15. package/dist/field.js +41 -0
  16. package/dist/field.js.map +1 -0
  17. package/dist/fn.d.ts +2 -0
  18. package/dist/fn.d.ts.map +1 -1
  19. package/dist/fn.js +50 -10
  20. package/dist/fn.js.map +1 -1
  21. package/dist/index.d.ts +14 -6
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +13 -5
  24. package/dist/index.js.map +1 -1
  25. package/dist/mixin.d.ts +2 -1
  26. package/dist/mixin.d.ts.map +1 -1
  27. package/dist/mixin.js +1 -1
  28. package/dist/mixin.js.map +1 -1
  29. package/dist/native-value.d.ts +9 -3
  30. package/dist/native-value.d.ts.map +1 -1
  31. package/dist/native-value.js +86 -9
  32. package/dist/native-value.js.map +1 -1
  33. package/dist/object.d.ts +14 -6
  34. package/dist/object.d.ts.map +1 -1
  35. package/dist/object.js +21 -10
  36. package/dist/object.js.map +1 -1
  37. package/dist/param-spec.d.ts +32 -1
  38. package/dist/param-spec.d.ts.map +1 -1
  39. package/dist/param-spec.js +67 -5
  40. package/dist/param-spec.js.map +1 -1
  41. package/dist/promisify.d.ts +13 -1
  42. package/dist/promisify.d.ts.map +1 -1
  43. package/dist/promisify.js +14 -1
  44. package/dist/promisify.js.map +1 -1
  45. package/dist/properties.d.ts +48 -5
  46. package/dist/properties.d.ts.map +1 -1
  47. package/dist/properties.js +191 -20
  48. package/dist/properties.js.map +1 -1
  49. package/dist/regex.d.ts +25 -0
  50. package/dist/regex.d.ts.map +1 -0
  51. package/dist/regex.js +59 -0
  52. package/dist/regex.js.map +1 -0
  53. package/dist/register-class.d.ts +165 -15
  54. package/dist/register-class.d.ts.map +1 -1
  55. package/dist/register-class.js +175 -19
  56. package/dist/register-class.js.map +1 -1
  57. package/dist/registry.d.ts +48 -4
  58. package/dist/registry.d.ts.map +1 -1
  59. package/dist/registry.js +147 -3
  60. package/dist/registry.js.map +1 -1
  61. package/dist/signal.d.ts +11 -1
  62. package/dist/signal.d.ts.map +1 -1
  63. package/dist/signal.js +66 -6
  64. package/dist/signal.js.map +1 -1
  65. package/dist/t.d.ts +11 -0
  66. package/dist/t.d.ts.map +1 -1
  67. package/dist/t.js +3 -0
  68. package/dist/t.js.map +1 -1
  69. package/dist/type.d.ts +2 -1
  70. package/dist/type.d.ts.map +1 -1
  71. package/dist/type.js +5 -1
  72. package/dist/type.js.map +1 -1
  73. package/dist/value.d.ts +48 -4
  74. package/dist/value.d.ts.map +1 -1
  75. package/dist/value.js +172 -16
  76. package/dist/value.js.map +1 -1
  77. package/dist/variant.d.ts +75 -26
  78. package/dist/variant.d.ts.map +1 -1
  79. package/dist/variant.js +44 -29
  80. package/dist/variant.js.map +1 -1
  81. package/package.json +5 -5
  82. package/src/arg.ts +6 -1
  83. package/src/closure.ts +2 -14
  84. package/src/descriptors.ts +74 -21
  85. package/src/field.ts +76 -0
  86. package/src/fn.ts +73 -17
  87. package/src/index.ts +46 -5
  88. package/src/mixin.ts +1 -1
  89. package/src/native-value.ts +146 -14
  90. package/src/object.ts +23 -11
  91. package/src/param-spec.ts +96 -8
  92. package/src/promisify.ts +27 -1
  93. package/src/properties.ts +280 -32
  94. package/src/regex.ts +92 -0
  95. package/src/register-class.ts +485 -42
  96. package/src/registry.ts +219 -4
  97. package/src/signal.ts +117 -4
  98. package/src/t.ts +13 -0
  99. package/src/type.ts +7 -0
  100. package/src/value.ts +268 -14
  101. package/src/variant.ts +184 -53
@@ -1,4 +1,8 @@
1
- import { getHandle, getWrapperClass, wrapCallScopedObject, wrapHandle, wrapObject } from "./registry.js";
1
+ import { bindFunctionPointer, call, getType, } from "@gtkx/native";
2
+ import { boxedT, isGtypeDescriptor, refT, } from "./descriptors.js";
3
+ import { checkError } from "./error.js";
4
+ import { LIB } from "./library.js";
5
+ import { coerceGType, getHandle, getWrapperClass, resolveWrapperClass, wrapCallScopedObject, wrapFundamentalHandle, wrapHandle, wrapObject, } from "./registry.js";
2
6
  import { resolveDescriptorType } from "./type.js";
3
7
  const MARSHALLED_KINDS = new Set([
4
8
  "object",
@@ -8,6 +12,7 @@ const MARSHALLED_KINDS = new Set([
8
12
  "array",
9
13
  "hashtable",
10
14
  ]);
15
+ const NULL_POINTER = 0n;
11
16
  function isMarshalledDescriptor(descriptor) {
12
17
  return MARSHALLED_KINDS.has(descriptor.kind);
13
18
  }
@@ -18,14 +23,74 @@ function collectionFromNative(descriptor, value) {
18
23
  return value === null ? null : mapCollection(descriptor, value, fromNative);
19
24
  }
20
25
  function boxedFromNative(descriptor, value) {
21
- return value == null
22
- ? null
23
- : wrapHandle(value, getWrapperClass(resolveDescriptorType(descriptor)));
26
+ if (value == null) {
27
+ return null;
28
+ }
29
+ const handle = value;
30
+ const type = resolveDescriptorType(descriptor);
31
+ return wrapHandle(handle, getWrapperClass(type));
32
+ }
33
+ function fundamentalWrapperClass(descriptor, handle) {
34
+ if (descriptor.wrapperClass !== undefined) {
35
+ return descriptor.wrapperClass;
36
+ }
37
+ const declaredType = resolveDescriptorType(descriptor);
38
+ return resolveWrapperClass(getType(handle, declaredType)) ?? getWrapperClass(declaredType);
24
39
  }
25
40
  function fundamentalFromNative(descriptor, value) {
26
- return value == null
27
- ? null
28
- : wrapHandle(value, descriptor.wrapperClass ?? getWrapperClass(resolveDescriptorType(descriptor)));
41
+ if (value == null) {
42
+ return null;
43
+ }
44
+ const handle = value;
45
+ return wrapFundamentalHandle(handle, fundamentalWrapperClass(descriptor, handle));
46
+ }
47
+ const errorRefDescriptor = () => refT(boxedT("GError", { ownership: "full", sharedLibrary: LIB, getTypeFnName: "g_error_get_type" }));
48
+ function decodedCallbackValues(descriptor, target, inputs) {
49
+ const values = [];
50
+ let cursor = 0;
51
+ for (const [index, argDescriptor] of descriptor.argDescriptors.entries()) {
52
+ if (index === descriptor.userDataIndex) {
53
+ values.push(target.userData ?? NULL_POINTER);
54
+ }
55
+ else {
56
+ values.push(toNative(argDescriptor, inputs[cursor]));
57
+ cursor += 1;
58
+ }
59
+ }
60
+ return values;
61
+ }
62
+ function decodedCallbackCallable(descriptor, target) {
63
+ const canThrow = descriptor.canThrow === true;
64
+ const isOneShot = descriptor.scope === "async";
65
+ const argDescriptors = canThrow ? [...descriptor.argDescriptors, errorRefDescriptor()] : descriptor.argDescriptors;
66
+ let bound;
67
+ let isSpent = false;
68
+ return (...inputs) => {
69
+ if (isSpent) {
70
+ throw new Error("An async-scoped callback was already invoked; its native caller has released it");
71
+ }
72
+ bound ??= bindFunctionPointer(target.fnPtr, argDescriptors, descriptor.returnDescriptor, "decoded callback");
73
+ const values = decodedCallbackValues(descriptor, target, inputs);
74
+ isSpent = isOneShot;
75
+ if (!canThrow) {
76
+ return fromNative(descriptor.returnDescriptor, call(bound, values));
77
+ }
78
+ const errorRef = { value: null };
79
+ values.push(errorRef);
80
+ const result = call(bound, values);
81
+ checkError(errorRef);
82
+ return fromNative(descriptor.returnDescriptor, result);
83
+ };
84
+ }
85
+ function callbackFromNative(descriptor, value) {
86
+ if (value == null) {
87
+ return value;
88
+ }
89
+ const target = value;
90
+ if (typeof target.fnPtr !== "bigint") {
91
+ return value;
92
+ }
93
+ return decodedCallbackCallable(descriptor, { fnPtr: target.fnPtr, userData: target.userData });
29
94
  }
30
95
  function hashTableFromNative(descriptor, value) {
31
96
  if (value === null) {
@@ -40,12 +105,20 @@ function hashTableFromNative(descriptor, value) {
40
105
  /**
41
106
  * Converts a raw value returned from native code into its JavaScript form,
42
107
  * wrapping object, struct, boxed, and fundamental handles and recursively
43
- * converting arrays and hash tables according to the descriptor.
108
+ * converting arrays and hash tables according to the descriptor. A callback
109
+ * decoded from a native function pointer and its bound user data becomes a
110
+ * callable function that invokes the native callback; it stays valid only as
111
+ * long as the native caller keeps the pointer pair alive, which for an
112
+ * async-scoped callback means until the first invocation — calling one a
113
+ * second time throws instead of reaching the released native closure.
44
114
  *
45
115
  * @param descriptor Describes the native type of the value.
46
116
  * @param value The raw native value to convert.
47
117
  */
48
118
  function fromNative(descriptor, value) {
119
+ if (descriptor.kind === "callback") {
120
+ return callbackFromNative(descriptor, value);
121
+ }
49
122
  if (!isMarshalledDescriptor(descriptor)) {
50
123
  return value;
51
124
  }
@@ -84,13 +157,17 @@ function hashTableToNative(descriptor, value) {
84
157
  }
85
158
  /**
86
159
  * Converts a JavaScript value into the raw form native code expects, unwrapping
87
- * object, struct, boxed, and fundamental wrappers back to their handles and
160
+ * object, struct, boxed, and fundamental wrappers back to their handles,
161
+ * resolving a class passed for a GType to the GType it was registered under, and
88
162
  * recursively converting arrays and maps according to the descriptor.
89
163
  *
90
164
  * @param descriptor Describes the native type to convert to.
91
165
  * @param value The JavaScript value to convert.
92
166
  */
93
167
  function toNative(descriptor, value) {
168
+ if (isGtypeDescriptor(descriptor)) {
169
+ return coerceGType(value);
170
+ }
94
171
  if (!isMarshalledDescriptor(descriptor)) {
95
172
  return value;
96
173
  }
@@ -1 +1 @@
1
- {"version":3,"file":"native-value.js","sourceRoot":"","sources":["../src/native-value.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,oBAAoB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACzG,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAKlD,MAAM,gBAAgB,GAA4B,IAAI,GAAG,CAAiB;IACtE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/native\";\nimport type { ArrayDescriptor, FundamentalDescriptor, HashTableDescriptor, StructDescriptor } from \"./descriptors.js\";\nimport { getHandle, getWrapperClass, wrapCallScopedObject, wrapHandle, wrapObject } from \"./registry.js\";\nimport { resolveDescriptorType } from \"./type.js\";\n\ntype MarshalledKind = \"object\" | \"struct\" | \"boxed\" | \"fundamental\" | \"array\" | \"hashtable\";\ntype MarshalledDescriptor = Extract<Descriptor, { kind: MarshalledKind }>;\n\nconst MARSHALLED_KINDS: Set<Descriptor[\"kind\"]> = new Set<MarshalledKind>([\n \"object\",\n \"struct\",\n \"boxed\",\n \"fundamental\",\n \"array\",\n \"hashtable\",\n]);\n\nfunction isMarshalledDescriptor(descriptor: Descriptor): descriptor is MarshalledDescriptor {\n return MARSHALLED_KINDS.has(descriptor.kind);\n}\n\nfunction mapCollection(\n descriptor: ArrayDescriptor,\n value: unknown,\n convert: (itemDescriptor: Descriptor, item: unknown) => unknown,\n): unknown[] {\n return (value as unknown[]).map((item) => convert(descriptor.itemDescriptor, item));\n}\n\nfunction collectionFromNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value === null ? null : mapCollection(descriptor, value, fromNative);\n}\n\nfunction boxedFromNative(descriptor: Descriptor, value: unknown): unknown {\n return value == null\n ? null\n : wrapHandle(value as ExternalObject<Handle>, getWrapperClass(resolveDescriptorType(descriptor)));\n}\n\nfunction fundamentalFromNative(descriptor: FundamentalDescriptor, value: unknown): unknown {\n return value == null\n ? null\n : wrapHandle(\n value as ExternalObject<Handle>,\n descriptor.wrapperClass ?? getWrapperClass(resolveDescriptorType(descriptor)),\n );\n}\n\nfunction hashTableFromNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value === null) {\n return null;\n }\n\n const entries = value as [unknown, unknown][];\n\n return new Map(\n entries.map(([key, val]): [unknown, unknown] => [\n fromNative(descriptor.keyDescriptor, key),\n fromNative(descriptor.valueDescriptor, val),\n ]),\n );\n}\n\n/**\n * Converts a raw value returned from native code into its JavaScript form,\n * wrapping object, struct, boxed, and fundamental handles and recursively\n * converting arrays and hash tables according to the descriptor.\n *\n * @param descriptor Describes the native type of the value.\n * @param value The raw native value to convert.\n */\nfunction fromNative(descriptor: Descriptor, value: unknown): unknown {\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\": {\n return descriptor.isCallScoped === true ? wrapCallScopedObject(value) : wrapObject(value);\n }\n case \"struct\": {\n return wrapHandle(value as ExternalObject<Handle> | null, (descriptor as StructDescriptor).wrapperClass);\n }\n case \"boxed\": {\n return boxedFromNative(descriptor, value);\n }\n case \"fundamental\": {\n return fundamentalFromNative(descriptor, value);\n }\n case \"array\": {\n return collectionFromNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableFromNative(descriptor, value);\n }\n }\n}\n\nfunction collectionToNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value == null ? null : mapCollection(descriptor, value, toNative);\n}\n\nfunction hashTableToNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value == null) {\n return null;\n }\n\n return [...(value as Map<unknown, unknown>)].map(([key, val]): [unknown, unknown] => [\n toNative(descriptor.keyDescriptor, key),\n toNative(descriptor.valueDescriptor, val),\n ]);\n}\n\n/**\n * Converts a JavaScript value into the raw form native code expects, unwrapping\n * object, struct, boxed, and fundamental wrappers back to their handles and\n * recursively converting arrays and maps according to the descriptor.\n *\n * @param descriptor Describes the native type to convert to.\n * @param value The JavaScript value to convert.\n */\nfunction toNative(descriptor: Descriptor, value: unknown): unknown {\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\":\n case \"struct\":\n case \"boxed\":\n case \"fundamental\": {\n const instance = value as object | null | undefined;\n\n return instance == null ? null : getHandle(instance);\n }\n case \"array\": {\n return collectionToNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableToNative(descriptor, value);\n }\n }\n}\n\nexport { fromNative, toNative };\n"]}
1
+ {"version":3,"file":"native-value.js","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AAC;IAED,OAAO,CAAC,GAAI,KAA+B,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAsB,EAAE,CAAC;QACjF,QAAQ,CAAC,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC;QACvC,QAAQ,CAAC,UAAU,CAAC,eAAe,EAAE,GAAG,CAAC;KAC5C,CAAC,CAAC;AACP,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,QAAQ,CAAC,UAAsB,EAAE,KAAc;IACpD,IAAI,iBAAiB,CAAC,UAAU,CAAC,EAAE,CAAC;QAChC,OAAO,WAAW,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IAED,IAAI,CAAC,sBAAsB,CAAC,UAAU,CAAC,EAAE,CAAC;QACtC,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACtB,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,OAAO,CAAC;QACb,KAAK,aAAa,CAAC,CAAC,CAAC;YACjB,MAAM,QAAQ,GAAG,KAAkC,CAAC;YAEpD,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QACzD,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACX,OAAO,kBAAkB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,WAAW,CAAC,CAAC,CAAC;YACf,OAAO,iBAAiB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAChD,CAAC;IACL,CAAC;AACL,CAAC;AAED,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC","sourcesContent":["import type { AnyClass } from \"@gtkx/utils\";\nimport {\n bindFunctionPointer,\n call,\n type CallDescriptor,\n type Descriptor,\n type ExternalObject,\n getType,\n type Handle,\n type Ref,\n} from \"@gtkx/native\";\nimport {\n type ArrayDescriptor,\n boxedT,\n type CallbackDescriptor,\n type FundamentalDescriptor,\n type HashTableDescriptor,\n isGtypeDescriptor,\n refT,\n type StructDescriptor,\n} from \"./descriptors.js\";\nimport { checkError } from \"./error.js\";\nimport { LIB } from \"./library.js\";\nimport {\n coerceGType,\n getHandle,\n getWrapperClass,\n resolveWrapperClass,\n wrapCallScopedObject,\n wrapFundamentalHandle,\n wrapHandle,\n wrapObject,\n} from \"./registry.js\";\nimport { resolveDescriptorType } from \"./type.js\";\n\ntype MarshalledKind = \"object\" | \"struct\" | \"boxed\" | \"fundamental\" | \"array\" | \"hashtable\";\ntype MarshalledDescriptor = Extract<Descriptor, { kind: MarshalledKind }>;\ntype DecodedCallbackTarget = { fnPtr: bigint; userData?: bigint | undefined };\n\nconst MARSHALLED_KINDS: Set<Descriptor[\"kind\"]> = new Set<MarshalledKind>([\n \"object\",\n \"struct\",\n \"boxed\",\n \"fundamental\",\n \"array\",\n \"hashtable\",\n]);\n\nconst NULL_POINTER = 0n;\n\nfunction isMarshalledDescriptor(descriptor: Descriptor): descriptor is MarshalledDescriptor {\n return MARSHALLED_KINDS.has(descriptor.kind);\n}\n\nfunction mapCollection(\n descriptor: ArrayDescriptor,\n value: unknown,\n convert: (itemDescriptor: Descriptor, item: unknown) => unknown,\n): unknown[] {\n return (value as unknown[]).map((item) => convert(descriptor.itemDescriptor, item));\n}\n\nfunction collectionFromNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value === null ? null : mapCollection(descriptor, value, fromNative);\n}\n\nfunction boxedFromNative(descriptor: Descriptor, value: unknown): unknown {\n if (value == null) {\n return null;\n }\n\n const handle = value as ExternalObject<Handle>;\n const type = resolveDescriptorType(descriptor);\n\n return wrapHandle(handle, getWrapperClass(type));\n}\n\nfunction fundamentalWrapperClass(descriptor: FundamentalDescriptor, handle: ExternalObject<Handle>): AnyClass {\n if (descriptor.wrapperClass !== undefined) {\n return descriptor.wrapperClass;\n }\n\n const declaredType = resolveDescriptorType(descriptor);\n\n return resolveWrapperClass(getType(handle, declaredType)) ?? getWrapperClass(declaredType);\n}\n\nfunction fundamentalFromNative(descriptor: FundamentalDescriptor, value: unknown): unknown {\n if (value == null) {\n return null;\n }\n\n const handle = value as ExternalObject<Handle>;\n\n return wrapFundamentalHandle(handle, fundamentalWrapperClass(descriptor, handle));\n}\n\nconst errorRefDescriptor = (): Descriptor =>\n refT(boxedT(\"GError\", { ownership: \"full\", sharedLibrary: LIB, getTypeFnName: \"g_error_get_type\" }));\n\nfunction decodedCallbackValues(\n descriptor: CallbackDescriptor,\n target: DecodedCallbackTarget,\n inputs: unknown[],\n): unknown[] {\n const values: unknown[] = [];\n let cursor = 0;\n\n for (const [index, argDescriptor] of descriptor.argDescriptors.entries()) {\n if (index === descriptor.userDataIndex) {\n values.push(target.userData ?? NULL_POINTER);\n } else {\n values.push(toNative(argDescriptor, inputs[cursor]));\n cursor += 1;\n }\n }\n\n return values;\n}\n\nfunction decodedCallbackCallable(\n descriptor: CallbackDescriptor,\n target: DecodedCallbackTarget,\n): (...inputs: unknown[]) => unknown {\n const canThrow = descriptor.canThrow === true;\n const isOneShot = descriptor.scope === \"async\";\n const argDescriptors = canThrow ? [...descriptor.argDescriptors, errorRefDescriptor()] : descriptor.argDescriptors;\n let bound: ExternalObject<CallDescriptor> | undefined;\n let isSpent = false;\n\n return (...inputs) => {\n if (isSpent) {\n throw new Error(\"An async-scoped callback was already invoked; its native caller has released it\");\n }\n\n bound ??= bindFunctionPointer(target.fnPtr, argDescriptors, descriptor.returnDescriptor, \"decoded callback\");\n const values = decodedCallbackValues(descriptor, target, inputs);\n isSpent = isOneShot;\n\n if (!canThrow) {\n return fromNative(descriptor.returnDescriptor, call(bound, values));\n }\n\n const errorRef: Ref = { value: null };\n values.push(errorRef);\n const result = call(bound, values);\n checkError(errorRef);\n\n return fromNative(descriptor.returnDescriptor, result);\n };\n}\n\nfunction callbackFromNative(descriptor: CallbackDescriptor, value: unknown): unknown {\n if (value == null) {\n return value;\n }\n\n const target = value as Partial<DecodedCallbackTarget>;\n\n if (typeof target.fnPtr !== \"bigint\") {\n return value;\n }\n\n return decodedCallbackCallable(descriptor, { fnPtr: target.fnPtr, userData: target.userData });\n}\n\nfunction hashTableFromNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value === null) {\n return null;\n }\n\n const entries = value as [unknown, unknown][];\n\n return new Map(\n entries.map(([key, val]): [unknown, unknown] => [\n fromNative(descriptor.keyDescriptor, key),\n fromNative(descriptor.valueDescriptor, val),\n ]),\n );\n}\n\n/**\n * Converts a raw value returned from native code into its JavaScript form,\n * wrapping object, struct, boxed, and fundamental handles and recursively\n * converting arrays and hash tables according to the descriptor. A callback\n * decoded from a native function pointer and its bound user data becomes a\n * callable function that invokes the native callback; it stays valid only as\n * long as the native caller keeps the pointer pair alive, which for an\n * async-scoped callback means until the first invocation — calling one a\n * second time throws instead of reaching the released native closure.\n *\n * @param descriptor Describes the native type of the value.\n * @param value The raw native value to convert.\n */\nfunction fromNative(descriptor: Descriptor, value: unknown): unknown {\n if (descriptor.kind === \"callback\") {\n return callbackFromNative(descriptor, value);\n }\n\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\": {\n return descriptor.isCallScoped === true ? wrapCallScopedObject(value) : wrapObject(value);\n }\n case \"struct\": {\n return wrapHandle(value as ExternalObject<Handle> | null, (descriptor as StructDescriptor).wrapperClass);\n }\n case \"boxed\": {\n return boxedFromNative(descriptor, value);\n }\n case \"fundamental\": {\n return fundamentalFromNative(descriptor, value);\n }\n case \"array\": {\n return collectionFromNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableFromNative(descriptor, value);\n }\n }\n}\n\nfunction collectionToNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value == null ? null : mapCollection(descriptor, value, toNative);\n}\n\nfunction hashTableToNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value == null) {\n return null;\n }\n\n return [...(value as Map<unknown, unknown>)].map(([key, val]): [unknown, unknown] => [\n toNative(descriptor.keyDescriptor, key),\n toNative(descriptor.valueDescriptor, val),\n ]);\n}\n\n/**\n * Converts a JavaScript value into the raw form native code expects, unwrapping\n * object, struct, boxed, and fundamental wrappers back to their handles,\n * resolving a class passed for a GType to the GType it was registered under, and\n * recursively converting arrays and maps according to the descriptor.\n *\n * @param descriptor Describes the native type to convert to.\n * @param value The JavaScript value to convert.\n */\nfunction toNative(descriptor: Descriptor, value: unknown): unknown {\n if (isGtypeDescriptor(descriptor)) {\n return coerceGType(value);\n }\n\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\":\n case \"struct\":\n case \"boxed\":\n case \"fundamental\": {\n const instance = value as object | null | undefined;\n\n return instance == null ? null : getHandle(instance);\n }\n case \"array\": {\n return collectionToNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableToNative(descriptor, value);\n }\n }\n}\n\nexport { fromNative, toNative };\n"]}
package/dist/object.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Descriptor, type ExternalObject, type Handle } from "@gtkx/native";
1
+ import { type Descriptor } from "@gtkx/native";
2
2
  import { type AnyClass } from "@gtkx/utils";
3
3
  /**
4
4
  * One construct property a wrapper class accepts: the canonical `GObject` name it is set under,
@@ -28,17 +28,24 @@ declare function registerConstructProperties(cls: AnyClass, bindings: ConstructB
28
28
  * for a read-only property and for a value of a type the property cannot hold, and
29
29
  * a `RangeError` for a value the ParamSpec rejects. A value marshalled through a
30
30
  * declared descriptor is converted rather than checked, so a `null` handed to a
31
- * numeric one of those lands 0 rather than being refused.
31
+ * numeric one of those lands 0 rather than being refused, and a number is fitted to
32
+ * the property first, as `coerceObjectProperty` does: truncated toward zero for a
33
+ * whole-number property and clamped to the range its `GObject.ParamSpec` allows.
32
34
  * Properties whose value is `undefined` are skipped. A type registered with
33
35
  * `registerClass` binds the wrapper before its `constructed` slot runs, so an
34
36
  * override of that slot already sees a usable instance.
37
+ * When construction returns an object that already has a wrapper — it reached
38
+ * JavaScript and was wrapped before `g_object_new` returned, for example a
39
+ * `Gtk.Window` observed through the toplevels list — that existing wrapper is
40
+ * returned instead of binding `wrapper`, so both references stay one object.
35
41
  *
36
42
  * @param gtype The GType of the object to construct.
37
43
  * @param props Property names mapped to the values to set them to.
38
44
  * @param wrapper The wrapper instance to bind to the new object.
39
- * @returns The handle of the newly created object.
45
+ * @returns The wrapper bound to the constructed object: `wrapper` itself, or
46
+ * the wrapper the object already had.
40
47
  */
41
- declare function newObjectWithProperties(gtype: bigint, props: object, wrapper: object): ExternalObject<Handle>;
48
+ declare function newObjectWithProperties<T extends object>(gtype: bigint, props: object, wrapper: T): T;
42
49
  /**
43
50
  * Reads a GObject property and converts it to its JavaScript value using the
44
51
  * descriptor.
@@ -52,8 +59,9 @@ declare function getObjectProperty(obj: object, propertyName: string, descriptor
52
59
  * Writes a JavaScript value to a GObject property, converting it to native form
53
60
  * using the descriptor. The descriptor converts what it is given rather than checking it against
54
61
  * the property's `GObject.ParamSpec`, so `null` and `undefined` written to a numeric or enum
55
- * property land 0, where the same write to a property installed through `registerClass` is
56
- * refused with a `TypeError`.
62
+ * property land 0, and a fractional number written to a whole-number property is truncated toward
63
+ * zero, where the same writes to a property installed through `registerClass` are refused with a
64
+ * `TypeError`.
57
65
  *
58
66
  * @param obj The object to write to.
59
67
  * @param propertyName The property name.
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAAa,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAE,KAAK,QAAQ,EAAkB,MAAM,aAAa,CAAC;AAQ5D;;;GAGG;AACH,KAAK,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;AAC/D,wGAAwG;AACxG,KAAK,iBAAiB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oBD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,iBAAS,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAsB9F;AAED;;;;;;;GAOG;AACH,iBAAS,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAK7F;AAED;;;;;;;;;;;;GAYG;AACH,iBAAS,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAE5G;AAED,OAAO,EACH,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,iBAAiB,EACjB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACzB,CAAC"}
package/dist/object.js CHANGED
@@ -3,9 +3,9 @@ import { getParentClass } from "@gtkx/utils";
3
3
  import { bind } from "./bind.js";
4
4
  import { objectT, stringT, voidT } from "./descriptors.js";
5
5
  import { LIB, VALUE_T } from "./library.js";
6
- import { constructPropertyFor } from "./properties.js";
6
+ import { coercePropertyValue, constructPropertyFor } from "./properties.js";
7
7
  import { getHandle, registerWrapper } from "./registry.js";
8
- import { fromValue, newValueForDescriptor, toValue } from "./value.js";
8
+ import { fromValueForDescriptor, newValueForDescriptor, toValue } from "./value.js";
9
9
  const declaredBindings = new WeakMap();
10
10
  const resolvedBindings = new WeakMap();
11
11
  const declarations = { generation: 0 };
@@ -66,7 +66,7 @@ function constructPropertyForEntry(source, name, value) {
66
66
  if (binding === undefined) {
67
67
  return constructPropertyFor(source.gtype, name, value, source.wrapper);
68
68
  }
69
- return { name: binding[0], value: toValue(binding[1], value) };
69
+ return { name: binding[0], value: toValue(binding[1], coercePropertyValue(source.gtype, binding[0], value)) };
70
70
  }
71
71
  /**
72
72
  * Constructs a new GObject of the given type, setting the supplied construct
@@ -78,15 +78,22 @@ function constructPropertyForEntry(source, name, value) {
78
78
  * for a read-only property and for a value of a type the property cannot hold, and
79
79
  * a `RangeError` for a value the ParamSpec rejects. A value marshalled through a
80
80
  * declared descriptor is converted rather than checked, so a `null` handed to a
81
- * numeric one of those lands 0 rather than being refused.
81
+ * numeric one of those lands 0 rather than being refused, and a number is fitted to
82
+ * the property first, as `coerceObjectProperty` does: truncated toward zero for a
83
+ * whole-number property and clamped to the range its `GObject.ParamSpec` allows.
82
84
  * Properties whose value is `undefined` are skipped. A type registered with
83
85
  * `registerClass` binds the wrapper before its `constructed` slot runs, so an
84
86
  * override of that slot already sees a usable instance.
87
+ * When construction returns an object that already has a wrapper — it reached
88
+ * JavaScript and was wrapped before `g_object_new` returned, for example a
89
+ * `Gtk.Window` observed through the toplevels list — that existing wrapper is
90
+ * returned instead of binding `wrapper`, so both references stay one object.
85
91
  *
86
92
  * @param gtype The GType of the object to construct.
87
93
  * @param props Property names mapped to the values to set them to.
88
94
  * @param wrapper The wrapper instance to bind to the new object.
89
- * @returns The handle of the newly created object.
95
+ * @returns The wrapper bound to the constructed object: `wrapper` itself, or
96
+ * the wrapper the object already had.
90
97
  */
91
98
  function newObjectWithProperties(gtype, props, wrapper) {
92
99
  const names = [];
@@ -100,8 +107,11 @@ function newObjectWithProperties(gtype, props, wrapper) {
100
107
  values.push(property.value);
101
108
  }
102
109
  }
103
- newObject(gtype, names, values, wrapper, registerWrapper);
104
- return getHandle(wrapper);
110
+ const existing = newObject(gtype, names, values, wrapper, registerWrapper);
111
+ if (existing !== null) {
112
+ return existing;
113
+ }
114
+ return wrapper;
105
115
  }
106
116
  /**
107
117
  * Reads a GObject property and converts it to its JavaScript value using the
@@ -114,14 +124,15 @@ function newObjectWithProperties(gtype, props, wrapper) {
114
124
  function getObjectProperty(obj, propertyName, descriptor) {
115
125
  const value = newValueForDescriptor(descriptor);
116
126
  gObjectGetProperty(getHandle(obj), propertyName, value);
117
- return fromValue(value);
127
+ return fromValueForDescriptor(descriptor, value);
118
128
  }
119
129
  /**
120
130
  * Writes a JavaScript value to a GObject property, converting it to native form
121
131
  * using the descriptor. The descriptor converts what it is given rather than checking it against
122
132
  * the property's `GObject.ParamSpec`, so `null` and `undefined` written to a numeric or enum
123
- * property land 0, where the same write to a property installed through `registerClass` is
124
- * refused with a `TypeError`.
133
+ * property land 0, and a fractional number written to a whole-number property is truncated toward
134
+ * zero, where the same writes to a property installed through `registerClass` are refused with a
135
+ * `TypeError`.
125
136
  *
126
137
  * @param obj The object to write to.
127
138
  * @param propertyName The property name.
@@ -1 +1 @@
1
- {"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAiB,cAAc,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAA0B,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAWvE,MAAM,gBAAgB,GAAyC,IAAI,OAAO,EAAE,CAAC;AAC7E,MAAM,gBAAgB,GAAwC,IAAI,OAAO,EAAE,CAAC;AAC5E,MAAM,YAAY,GAAG,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;AACvC,MAAM,WAAW,GAAsB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAsB,CAAC;AAEhF,MAAM,kBAAkB,GAAG,IAAI,CAC3B,GAAG,EACH,uBAAuB,EACvB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,CAAC","sourcesContent":["import { type Descriptor, type ExternalObject, type Handle, newObject } from \"@gtkx/native\";\nimport { type AnyClass, getParentClass } from \"@gtkx/utils\";\nimport { bind } from \"./bind.js\";\nimport { objectT, stringT, voidT } from \"./descriptors.js\";\nimport { LIB, VALUE_T } from \"./library.js\";\nimport { type ConstructProperty, constructPropertyFor } from \"./properties.js\";\nimport { getHandle, registerWrapper } from \"./registry.js\";\nimport { fromValue, newValueForDescriptor, toValue } from \"./value.js\";\n\n/**\n * One construct property a wrapper class accepts: the canonical `GObject` name it is set under,\n * and the descriptor its value is marshalled through.\n */\ntype ConstructBinding = [name: string, descriptor: Descriptor];\n/** The construct properties a wrapper class accepts, keyed by the camelCased name callers give them. */\ntype ConstructBindings = Record<string, ConstructBinding>;\ntype ResolvedBindings = { generation: number; bindings: ConstructBindings };\n\nconst declaredBindings: WeakMap<AnyClass, ConstructBindings> = new WeakMap();\nconst resolvedBindings: WeakMap<AnyClass, ResolvedBindings> = new WeakMap();\nconst declarations = { generation: 0 };\nconst NO_BINDINGS: ConstructBindings = Object.create(null) as ConstructBindings;\n\nconst gObjectGetProperty = bind(\n LIB,\n \"g_object_get_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nconst gObjectSetProperty = bind(\n LIB,\n \"g_object_set_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nfunction collectDeclaredBindings(cls: AnyClass): ConstructBindings[] {\n const declared: ConstructBindings[] = [];\n let current: AnyClass | null = cls;\n\n while (current !== null) {\n const own = declaredBindings.get(current);\n\n if (own !== undefined) {\n declared.push(own);\n }\n\n current = getParentClass(current);\n }\n\n return declared;\n}\n\nfunction mergeDeclaredBindings(cls: AnyClass): ConstructBindings {\n const declared = collectDeclaredBindings(cls);\n const merged: ConstructBindings = Object.create(null) as ConstructBindings;\n\n for (let index = declared.length - 1; index >= 0; index--) {\n Object.assign(merged, declared[index]);\n }\n\n return merged;\n}\n\nfunction constructBindingsFor(cls: AnyClass | undefined): ConstructBindings {\n if (cls === undefined) {\n return NO_BINDINGS;\n }\n\n const cached = resolvedBindings.get(cls);\n\n if (cached?.generation === declarations.generation) {\n return cached.bindings;\n }\n\n const bindings = mergeDeclaredBindings(cls);\n resolvedBindings.set(cls, { generation: declarations.generation, bindings });\n\n return bindings;\n}\n\n/**\n * Declares the construct properties a wrapper class accepts, so `newObjectWithProperties`\n * marshals each one through its descriptor rather than resolving it from the\n * `GObject.ParamSpec` the type installs. A class inherits the declarations of its ancestors.\n * Registering takes effect immediately, including for classes already constructed from and for\n * subclasses that already inherited an earlier declaration.\n *\n * @param cls The wrapper class the properties are declared on.\n * @param bindings CamelCased property names mapped to their canonical name and descriptor.\n */\nfunction registerConstructProperties(cls: AnyClass, bindings: ConstructBindings): void {\n declaredBindings.set(cls, bindings);\n declarations.generation += 1;\n}\n\nfunction constructPropertyForEntry(\n source: { gtype: bigint; bindings: ConstructBindings; wrapper: object },\n name: string,\n value: unknown,\n): ConstructProperty | undefined {\n if (value === undefined) {\n return undefined;\n }\n\n const binding = source.bindings[name];\n\n if (binding === undefined) {\n return constructPropertyFor(source.gtype, name, value, source.wrapper);\n }\n\n return { name: binding[0], value: toValue(binding[1], value) };\n}\n\n/**\n * Constructs a new GObject of the given type, setting the supplied construct\n * properties, and binds `wrapper` to it. A property the wrapper's class declares\n * through `registerConstructProperties` is marshalled through its descriptor; any\n * other one is marshalled through the `GObject.ParamSpec` the type installs under\n * that name, dashed or camelCased, and is skipped when the type installs none.\n * A value that ParamSpec would refuse throws before GObject sees it: a `TypeError`\n * for a read-only property and for a value of a type the property cannot hold, and\n * a `RangeError` for a value the ParamSpec rejects. A value marshalled through a\n * declared descriptor is converted rather than checked, so a `null` handed to a\n * numeric one of those lands 0 rather than being refused.\n * Properties whose value is `undefined` are skipped. A type registered with\n * `registerClass` binds the wrapper before its `constructed` slot runs, so an\n * override of that slot already sees a usable instance.\n *\n * @param gtype The GType of the object to construct.\n * @param props Property names mapped to the values to set them to.\n * @param wrapper The wrapper instance to bind to the new object.\n * @returns The handle of the newly created object.\n */\nfunction newObjectWithProperties(gtype: bigint, props: object, wrapper: object): ExternalObject<Handle> {\n const names: string[] = [];\n const values: ExternalObject<Handle>[] = [];\n const bindings = constructBindingsFor(wrapper.constructor as AnyClass | undefined);\n const source = { gtype, bindings, wrapper };\n\n for (const name of Object.keys(props)) {\n const property = constructPropertyForEntry(source, name, Reflect.get(props, name));\n\n if (property !== undefined) {\n names.push(property.name);\n values.push(property.value);\n }\n }\n\n newObject(gtype, names, values, wrapper, registerWrapper);\n\n return getHandle(wrapper);\n}\n\n/**\n * Reads a GObject property and converts it to its JavaScript value using the\n * descriptor.\n *\n * @param obj The object to read from.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n */\nfunction getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown {\n const value = newValueForDescriptor(descriptor);\n gObjectGetProperty(getHandle(obj), propertyName, value);\n\n return fromValue(value);\n}\n\n/**\n * Writes a JavaScript value to a GObject property, converting it to native form\n * using the descriptor. The descriptor converts what it is given rather than checking it against\n * the property's `GObject.ParamSpec`, so `null` and `undefined` written to a numeric or enum\n * property land 0, where the same write to a property installed through `registerClass` is\n * refused with a `TypeError`.\n *\n * @param obj The object to write to.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n * @param jsValue The value to set.\n */\nfunction setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void {\n gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));\n}\n\nexport {\n newObjectWithProperties,\n getObjectProperty,\n registerConstructProperties,\n setObjectProperty,\n type ConstructBinding,\n type ConstructBindings,\n};\n"]}
1
+ {"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAiB,cAAc,EAAE,MAAM,aAAa,CAAC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,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;IACL,CAAC;IAED,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC;IAE3E,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO,QAAa,CAAC;IACzB,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB;IAChF,MAAM,KAAK,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IAChD,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;IAExD,OAAO,sBAAsB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;AACrD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB,EAAE,OAAgB;IAClG,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,OAAO,EACH,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,iBAAiB,GAGpB,CAAC","sourcesContent":["import { type Descriptor, type ExternalObject, type Handle, newObject } from \"@gtkx/native\";\nimport { type AnyClass, getParentClass } from \"@gtkx/utils\";\nimport { bind } from \"./bind.js\";\nimport { objectT, stringT, voidT } from \"./descriptors.js\";\nimport { LIB, VALUE_T } from \"./library.js\";\nimport { coercePropertyValue, type ConstructProperty, constructPropertyFor } from \"./properties.js\";\nimport { getHandle, registerWrapper } from \"./registry.js\";\nimport { fromValueForDescriptor, newValueForDescriptor, toValue } from \"./value.js\";\n\n/**\n * One construct property a wrapper class accepts: the canonical `GObject` name it is set under,\n * and the descriptor its value is marshalled through.\n */\ntype ConstructBinding = [name: string, descriptor: Descriptor];\n/** The construct properties a wrapper class accepts, keyed by the camelCased name callers give them. */\ntype ConstructBindings = Record<string, ConstructBinding>;\ntype ResolvedBindings = { generation: number; bindings: ConstructBindings };\n\nconst declaredBindings: WeakMap<AnyClass, ConstructBindings> = new WeakMap();\nconst resolvedBindings: WeakMap<AnyClass, ResolvedBindings> = new WeakMap();\nconst declarations = { generation: 0 };\nconst NO_BINDINGS: ConstructBindings = Object.create(null) as ConstructBindings;\n\nconst gObjectGetProperty = bind(\n LIB,\n \"g_object_get_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nconst gObjectSetProperty = bind(\n LIB,\n \"g_object_set_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nfunction collectDeclaredBindings(cls: AnyClass): ConstructBindings[] {\n const declared: ConstructBindings[] = [];\n let current: AnyClass | null = cls;\n\n while (current !== null) {\n const own = declaredBindings.get(current);\n\n if (own !== undefined) {\n declared.push(own);\n }\n\n current = getParentClass(current);\n }\n\n return declared;\n}\n\nfunction mergeDeclaredBindings(cls: AnyClass): ConstructBindings {\n const declared = collectDeclaredBindings(cls);\n const merged: ConstructBindings = Object.create(null) as ConstructBindings;\n\n for (let index = declared.length - 1; index >= 0; index--) {\n Object.assign(merged, declared[index]);\n }\n\n return merged;\n}\n\nfunction constructBindingsFor(cls: AnyClass | undefined): ConstructBindings {\n if (cls === undefined) {\n return NO_BINDINGS;\n }\n\n const cached = resolvedBindings.get(cls);\n\n if (cached?.generation === declarations.generation) {\n return cached.bindings;\n }\n\n const bindings = mergeDeclaredBindings(cls);\n resolvedBindings.set(cls, { generation: declarations.generation, bindings });\n\n return bindings;\n}\n\n/**\n * Declares the construct properties a wrapper class accepts, so `newObjectWithProperties`\n * marshals each one through its descriptor rather than resolving it from the\n * `GObject.ParamSpec` the type installs. A class inherits the declarations of its ancestors.\n * Registering takes effect immediately, including for classes already constructed from and for\n * subclasses that already inherited an earlier declaration.\n *\n * @param cls The wrapper class the properties are declared on.\n * @param bindings CamelCased property names mapped to their canonical name and descriptor.\n */\nfunction registerConstructProperties(cls: AnyClass, bindings: ConstructBindings): void {\n declaredBindings.set(cls, bindings);\n declarations.generation += 1;\n}\n\nfunction constructPropertyForEntry(\n source: { gtype: bigint; bindings: ConstructBindings; wrapper: object },\n name: string,\n value: unknown,\n): ConstructProperty | undefined {\n if (value === undefined) {\n return undefined;\n }\n\n const binding = source.bindings[name];\n\n if (binding === undefined) {\n return constructPropertyFor(source.gtype, name, value, source.wrapper);\n }\n\n return { name: binding[0], value: toValue(binding[1], coercePropertyValue(source.gtype, binding[0], value)) };\n}\n\n/**\n * Constructs a new GObject of the given type, setting the supplied construct\n * properties, and binds `wrapper` to it. A property the wrapper's class declares\n * through `registerConstructProperties` is marshalled through its descriptor; any\n * other one is marshalled through the `GObject.ParamSpec` the type installs under\n * that name, dashed or camelCased, and is skipped when the type installs none.\n * A value that ParamSpec would refuse throws before GObject sees it: a `TypeError`\n * for a read-only property and for a value of a type the property cannot hold, and\n * a `RangeError` for a value the ParamSpec rejects. A value marshalled through a\n * declared descriptor is converted rather than checked, so a `null` handed to a\n * numeric one of those lands 0 rather than being refused, and a number is fitted to\n * the property first, as `coerceObjectProperty` does: truncated toward zero for a\n * whole-number property and clamped to the range its `GObject.ParamSpec` allows.\n * Properties whose value is `undefined` are skipped. A type registered with\n * `registerClass` binds the wrapper before its `constructed` slot runs, so an\n * override of that slot already sees a usable instance.\n * When construction returns an object that already has a wrapper — it reached\n * JavaScript and was wrapped before `g_object_new` returned, for example a\n * `Gtk.Window` observed through the toplevels list — that existing wrapper is\n * returned instead of binding `wrapper`, so both references stay one object.\n *\n * @param gtype The GType of the object to construct.\n * @param props Property names mapped to the values to set them to.\n * @param wrapper The wrapper instance to bind to the new object.\n * @returns The wrapper bound to the constructed object: `wrapper` itself, or\n * the wrapper the object already had.\n */\nfunction newObjectWithProperties<T extends object>(gtype: bigint, props: object, wrapper: T): T {\n const names: string[] = [];\n const values: ExternalObject<Handle>[] = [];\n const bindings = constructBindingsFor(wrapper.constructor as AnyClass | undefined);\n const source = { gtype, bindings, wrapper };\n\n for (const name of Object.keys(props)) {\n const property = constructPropertyForEntry(source, name, Reflect.get(props, name));\n\n if (property !== undefined) {\n names.push(property.name);\n values.push(property.value);\n }\n }\n\n const existing = newObject(gtype, names, values, wrapper, registerWrapper);\n\n if (existing !== null) {\n return existing as T;\n }\n\n return wrapper;\n}\n\n/**\n * Reads a GObject property and converts it to its JavaScript value using the\n * descriptor.\n *\n * @param obj The object to read from.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n */\nfunction getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown {\n const value = newValueForDescriptor(descriptor);\n gObjectGetProperty(getHandle(obj), propertyName, value);\n\n return fromValueForDescriptor(descriptor, value);\n}\n\n/**\n * Writes a JavaScript value to a GObject property, converting it to native form\n * using the descriptor. The descriptor converts what it is given rather than checking it against\n * the property's `GObject.ParamSpec`, so `null` and `undefined` written to a numeric or enum\n * property land 0, and a fractional number written to a whole-number property is truncated toward\n * zero, where the same writes to a property installed through `registerClass` are refused with a\n * `TypeError`.\n *\n * @param obj The object to write to.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n * @param jsValue The value to set.\n */\nfunction setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void {\n gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));\n}\n\nexport {\n newObjectWithProperties,\n getObjectProperty,\n registerConstructProperties,\n setObjectProperty,\n type ConstructBinding,\n type ConstructBindings,\n};\n"]}
@@ -1,11 +1,42 @@
1
1
  import { type ExternalObject, type Handle } from "@gtkx/native";
2
2
  type ValueGuard = (value: unknown) => boolean;
3
+ declare const INT32_MINIMUM = -2147483648;
4
+ declare const INT32_MAXIMUM = 2147483647;
5
+ declare const INT64_MINIMUM: bigint;
6
+ declare const INT64_MAXIMUM: bigint;
7
+ declare const UINT64_MAXIMUM: bigint;
8
+ declare const WHOLE_NUMBER_RANGES: Map<bigint, [minimum: number, maximum: number]>;
3
9
  declare const getParamFlags: (pspec: ExternalObject<Handle>) => number;
4
10
  declare const getParamValueType: (pspec: ExternalObject<Handle>) => bigint;
11
+ /**
12
+ * Reads the `GParamFlags` bitfield of a `GObject.ParamSpec`.
13
+ *
14
+ * @param spec The param spec wrapper to read.
15
+ * @returns The spec's flags.
16
+ */
17
+ declare const getParamSpecFlags: (spec: object) => number;
18
+ /**
19
+ * Reads the GType of the values a `GObject.ParamSpec` describes.
20
+ *
21
+ * @param spec The param spec wrapper to read.
22
+ * @returns The GType of the spec's values.
23
+ */
24
+ declare const getParamSpecValueType: (spec: object) => bigint;
25
+ /**
26
+ * Reads the GType a `GObject.ParamSpec` is installed on.
27
+ *
28
+ * @param spec The param spec wrapper to read.
29
+ * @returns The owning GType, or `TYPE_INVALID` while the spec is not installed on any type.
30
+ */
31
+ declare const getParamSpecOwnerType: (spec: object) => bigint;
5
32
  declare const isParamWritable: (flags: number) => boolean;
33
+ declare const isParamReadable: (flags: number) => boolean;
6
34
  declare const isParamConstructOnly: (flags: number) => boolean;
7
35
  declare const isParamLaxlyValidated: (flags: number) => boolean;
36
+ declare const isParamExplicitlyNotified: (flags: number) => boolean;
8
37
  declare const wasParamValueModified: (pspec: ExternalObject<Handle>, value: ExternalObject<Handle>) => boolean;
38
+ declare function isStringArray(value: unknown[]): boolean;
39
+ declare function resolveGtype(value: unknown): bigint;
9
40
  declare function valueGuardFor(valueType: bigint): ValueGuard;
10
- export { getParamFlags, getParamValueType, isParamConstructOnly, isParamLaxlyValidated, isParamWritable, type ValueGuard, valueGuardFor, wasParamValueModified, };
41
+ export { WHOLE_NUMBER_RANGES, getParamFlags, getParamSpecFlags, getParamSpecOwnerType, getParamSpecValueType, getParamValueType, INT32_MAXIMUM, INT32_MINIMUM, INT64_MAXIMUM, INT64_MINIMUM, isParamConstructOnly, isParamReadable, isStringArray, isParamExplicitlyNotified, isParamLaxlyValidated, isParamWritable, resolveGtype, UINT64_MAXIMUM, type ValueGuard, valueGuardFor, wasParamValueModified, };
11
42
  //# sourceMappingURL=param-spec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"param-spec.d.ts","sourceRoot":"","sources":["../src/param-spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAAQ,MAAM,cAAc,CAAC;AAkCtE,KAAK,UAAU,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;AA6D9C,QAAA,MAAM,aAAa,GAAI,OAAO,cAAc,CAAC,MAAM,CAAC,KAAG,MAA2D,CAAC;AAEnH,QAAA,MAAM,iBAAiB,GAAI,OAAO,cAAc,CAAC,MAAM,CAAC,KAAG,MACE,CAAC;AAE9D,QAAA,MAAM,eAAe,GAAI,OAAO,MAAM,KAAG,OAAyC,CAAC;AACnF,QAAA,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,OAA+C,CAAC;AAC9F,QAAA,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,OAA+C,CAAC;AAE/F,QAAA,MAAM,qBAAqB,GAAI,OAAO,cAAc,CAAC,MAAM,CAAC,EAAE,OAAO,cAAc,CAAC,MAAM,CAAC,KAAG,OAC/C,CAAC;AAoEhD,iBAAS,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAepD;AAoCD,OAAO,EACH,aAAa,EACb,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,eAAe,EACf,KAAK,UAAU,EACf,aAAa,EACb,qBAAqB,GACxB,CAAC"}
1
+ {"version":3,"file":"param-spec.d.ts","sourceRoot":"","sources":["../src/param-spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAAQ,MAAM,cAAc,CAAC;AAkCtE,KAAK,UAAU,GAA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AAa,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAQhD;AAsCD,iBAAS,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQ5C;AAcD,iBAAS,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAepD;AA4CD,OAAO,EACH,mBAAmB,EACnB,aAAa,EACb,iBAAiB,EACjB,qBAAqB,EACrB,qBAAqB,EACrB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,aAAa,EACb,aAAa,EACb,oBAAoB,EACpB,eAAe,EACf,aAAa,EACb,yBAAyB,EACzB,qBAAqB,EACrB,eAAe,EACf,YAAY,EACZ,cAAc,EACd,KAAK,UAAU,EACf,aAAa,EACb,qBAAqB,GACxB,CAAC"}
@@ -2,15 +2,17 @@ import { read } from "@gtkx/native";
2
2
  import { bind } from "./bind.js";
3
3
  import { biguint64T, booleanT, stringT, uint32T, voidT } from "./descriptors.js";
4
4
  import { LIB, PARAM_T, VALUE_T } from "./library.js";
5
- import { getInstanceType } from "./registry.js";
5
+ import { getHandle, getInstanceType } from "./registry.js";
6
6
  import { getStrvType, isTypedClass, TYPE_BOOLEAN, TYPE_BOXED, TYPE_CHAR, TYPE_DOUBLE, TYPE_ENUM, TYPE_FLAGS, TYPE_FLOAT, TYPE_GTYPE, TYPE_INT, TYPE_INT64, TYPE_INTERFACE, TYPE_INVALID, TYPE_LONG, TYPE_OBJECT, TYPE_PARAM, TYPE_POINTER, TYPE_STRING, TYPE_UCHAR, TYPE_UINT, TYPE_UINT64, TYPE_ULONG, TYPE_VARIANT, typeFundamental, typeIsA, } from "./type.js";
7
7
  const PARAM_READABLE = 1;
8
8
  const PARAM_WRITABLE = 2;
9
9
  const PARAM_CONSTRUCT_ONLY = 8;
10
10
  const PARAM_LAX_VALIDATION = 16;
11
+ const PARAM_EXPLICIT_NOTIFY = 1 << 30;
11
12
  const READ_FLAGS = PARAM_READABLE | PARAM_WRITABLE | PARAM_CONSTRUCT_ONLY | PARAM_LAX_VALIDATION;
12
13
  const FLAGS_BYTE_OFFSET = 16;
13
14
  const VALUE_TYPE_BYTE_OFFSET = 24;
15
+ const OWNER_TYPE_BYTE_OFFSET = 32;
14
16
  const LAYOUT_PROBE_NAME = "gtkx-param-layout";
15
17
  const INT8_MINIMUM = -128;
16
18
  const INT8_MAXIMUM = 127;
@@ -29,6 +31,19 @@ const WRAPPED_FUNDAMENTALS = new Set([
29
31
  TYPE_VARIANT,
30
32
  ]);
31
33
  const isWideUnsignedValue = wideIntegerGuardFor(0n, UINT64_MAXIMUM);
34
+ const MAX_SAFE = Number.MAX_SAFE_INTEGER;
35
+ const WHOLE_NUMBER_RANGES = new Map([
36
+ [TYPE_CHAR, [INT8_MINIMUM, INT8_MAXIMUM]],
37
+ [TYPE_UCHAR, [0, UINT8_MAXIMUM]],
38
+ [TYPE_INT, [INT32_MINIMUM, INT32_MAXIMUM]],
39
+ [TYPE_UINT, [0, UINT32_MAXIMUM]],
40
+ [TYPE_ENUM, [INT32_MINIMUM, INT32_MAXIMUM]],
41
+ [TYPE_FLAGS, [0, UINT32_MAXIMUM]],
42
+ [TYPE_LONG, [-MAX_SAFE, MAX_SAFE]],
43
+ [TYPE_ULONG, [0, MAX_SAFE]],
44
+ [TYPE_INT64, [-MAX_SAFE, MAX_SAFE]],
45
+ [TYPE_UINT64, [0, MAX_SAFE]],
46
+ ]);
32
47
  const SCALAR_GUARDS = new Map([
33
48
  [TYPE_BOOLEAN, isBooleanValue],
34
49
  [TYPE_STRING, isStringValue],
@@ -53,9 +68,42 @@ const paramSpecRefSink = bind(LIB, "g_param_spec_ref_sink", [PARAM_T], PARAM_T);
53
68
  const newParamSpecBoolean = bind(LIB, "g_param_spec_boolean", [stringT("borrowed"), stringT("borrowed"), stringT("borrowed"), booleanT, uint32T], PARAM_T);
54
69
  const getParamFlags = (pspec) => read(pspec, uint32T, FLAGS_BYTE_OFFSET);
55
70
  const getParamValueType = (pspec) => read(pspec, biguint64T, VALUE_TYPE_BYTE_OFFSET);
71
+ const getParamOwnerType = (pspec) => read(pspec, biguint64T, OWNER_TYPE_BYTE_OFFSET);
72
+ /**
73
+ * Reads the `GParamFlags` bitfield of a `GObject.ParamSpec`.
74
+ *
75
+ * @param spec The param spec wrapper to read.
76
+ * @returns The spec's flags.
77
+ */
78
+ const getParamSpecFlags = (spec) => {
79
+ assertParamLayout();
80
+ return getParamFlags(getHandle(spec));
81
+ };
82
+ /**
83
+ * Reads the GType of the values a `GObject.ParamSpec` describes.
84
+ *
85
+ * @param spec The param spec wrapper to read.
86
+ * @returns The GType of the spec's values.
87
+ */
88
+ const getParamSpecValueType = (spec) => {
89
+ assertParamLayout();
90
+ return getParamValueType(getHandle(spec));
91
+ };
92
+ /**
93
+ * Reads the GType a `GObject.ParamSpec` is installed on.
94
+ *
95
+ * @param spec The param spec wrapper to read.
96
+ * @returns The owning GType, or `TYPE_INVALID` while the spec is not installed on any type.
97
+ */
98
+ const getParamSpecOwnerType = (spec) => {
99
+ assertParamLayout();
100
+ return getParamOwnerType(getHandle(spec));
101
+ };
56
102
  const isParamWritable = (flags) => (flags & PARAM_WRITABLE) !== 0;
103
+ const isParamReadable = (flags) => (flags & PARAM_READABLE) !== 0;
57
104
  const isParamConstructOnly = (flags) => (flags & PARAM_CONSTRUCT_ONLY) !== 0;
58
105
  const isParamLaxlyValidated = (flags) => (flags & PARAM_LAX_VALIDATION) !== 0;
106
+ const isParamExplicitlyNotified = (flags) => (flags & PARAM_EXPLICIT_NOTIFY) !== 0;
59
107
  const wasParamValueModified = (pspec, value) => paramValueValidate(pspec, value);
60
108
  function isBooleanValue(value) {
61
109
  return typeof value === "boolean";
@@ -63,8 +111,16 @@ function isBooleanValue(value) {
63
111
  function isStringValue(value) {
64
112
  return value == null || typeof value === "string";
65
113
  }
114
+ function isStringArray(value) {
115
+ for (const item of value) {
116
+ if (typeof item !== "string") {
117
+ return false;
118
+ }
119
+ }
120
+ return true;
121
+ }
66
122
  function isStrvValue(value) {
67
- return value == null || (Array.isArray(value) && value.every((item) => typeof item === "string"));
123
+ return value == null || (Array.isArray(value) && isStringArray(value));
68
124
  }
69
125
  function isNumberValue(value) {
70
126
  return typeof value === "number";
@@ -122,7 +178,11 @@ function readProbeLayout(flags) {
122
178
  const probe = newParamSpecBoolean(LAYOUT_PROBE_NAME, null, null, false, flags);
123
179
  paramSpecRefSink(probe);
124
180
  try {
125
- return { flags: getParamFlags(probe), valueType: getParamValueType(probe) };
181
+ return {
182
+ flags: getParamFlags(probe),
183
+ valueType: getParamValueType(probe),
184
+ ownerType: getParamOwnerType(probe),
185
+ };
126
186
  }
127
187
  finally {
128
188
  paramSpecUnref(probe);
@@ -130,7 +190,9 @@ function readProbeLayout(flags) {
130
190
  }
131
191
  function isLayoutIntact(flags) {
132
192
  const layout = readProbeLayout(flags);
133
- return (layout.flags & READ_FLAGS) === flags && layout.valueType === TYPE_BOOLEAN;
193
+ return ((layout.flags & READ_FLAGS) === flags &&
194
+ layout.valueType === TYPE_BOOLEAN &&
195
+ layout.ownerType === TYPE_INVALID);
134
196
  }
135
197
  function assertParamLayout() {
136
198
  if (layout.wasChecked) {
@@ -143,5 +205,5 @@ function assertParamLayout() {
143
205
  }
144
206
  throw new Error("GParamSpec does not match the memory layout this build reads a property's flags and value type at");
145
207
  }
146
- export { getParamFlags, getParamValueType, isParamConstructOnly, isParamLaxlyValidated, isParamWritable, valueGuardFor, wasParamValueModified, };
208
+ export { WHOLE_NUMBER_RANGES, getParamFlags, getParamSpecFlags, getParamSpecOwnerType, getParamSpecValueType, getParamValueType, INT32_MAXIMUM, INT32_MINIMUM, INT64_MAXIMUM, INT64_MINIMUM, isParamConstructOnly, isParamReadable, isStringArray, isParamExplicitlyNotified, isParamLaxlyValidated, isParamWritable, resolveGtype, UINT64_MAXIMUM, valueGuardFor, wasParamValueModified, };
147
209
  //# sourceMappingURL=param-spec.js.map