@gtkx/runtime 1.2.2 → 1.3.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/README.md +2 -1
- package/dist/arg.d.ts +6 -1
- package/dist/arg.d.ts.map +1 -1
- package/dist/arg.js +2 -1
- package/dist/arg.js.map +1 -1
- package/dist/closure.d.ts.map +1 -1
- package/dist/closure.js +2 -11
- package/dist/closure.js.map +1 -1
- package/dist/descriptors.d.ts +21 -3
- package/dist/descriptors.d.ts.map +1 -1
- package/dist/descriptors.js +50 -21
- package/dist/descriptors.js.map +1 -1
- package/dist/fn.d.ts +2 -0
- package/dist/fn.d.ts.map +1 -1
- package/dist/fn.js +50 -10
- package/dist/fn.js.map +1 -1
- package/dist/index.d.ts +11 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -4
- package/dist/index.js.map +1 -1
- package/dist/mixin.d.ts +2 -1
- package/dist/mixin.d.ts.map +1 -1
- package/dist/mixin.js +1 -1
- package/dist/mixin.js.map +1 -1
- package/dist/native-value.d.ts +9 -3
- package/dist/native-value.d.ts.map +1 -1
- package/dist/native-value.js +86 -9
- package/dist/native-value.js.map +1 -1
- package/dist/object.d.ts +14 -6
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +21 -10
- package/dist/object.js.map +1 -1
- package/dist/param-spec.d.ts +31 -1
- package/dist/param-spec.d.ts.map +1 -1
- package/dist/param-spec.js +66 -5
- package/dist/param-spec.js.map +1 -1
- package/dist/promisify.d.ts +13 -1
- package/dist/promisify.d.ts.map +1 -1
- package/dist/promisify.js +14 -1
- package/dist/promisify.js.map +1 -1
- package/dist/properties.d.ts +28 -1
- package/dist/properties.d.ts.map +1 -1
- package/dist/properties.js +139 -8
- package/dist/properties.js.map +1 -1
- package/dist/regex.d.ts +25 -0
- package/dist/regex.d.ts.map +1 -0
- package/dist/regex.js +59 -0
- package/dist/regex.js.map +1 -0
- package/dist/register-class.d.ts +154 -11
- package/dist/register-class.d.ts.map +1 -1
- package/dist/register-class.js +157 -19
- package/dist/register-class.js.map +1 -1
- package/dist/registry.d.ts +48 -4
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +147 -3
- package/dist/registry.js.map +1 -1
- package/dist/signal.d.ts +8 -1
- package/dist/signal.d.ts.map +1 -1
- package/dist/signal.js +41 -3
- package/dist/signal.js.map +1 -1
- package/dist/type.d.ts +2 -1
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +5 -1
- package/dist/type.js.map +1 -1
- package/dist/value.d.ts +48 -4
- package/dist/value.d.ts.map +1 -1
- package/dist/value.js +172 -16
- package/dist/value.js.map +1 -1
- package/dist/variant.d.ts +75 -26
- package/dist/variant.d.ts.map +1 -1
- package/dist/variant.js +44 -29
- package/dist/variant.js.map +1 -1
- package/package.json +4 -4
- package/src/arg.ts +6 -1
- package/src/closure.ts +2 -14
- package/src/descriptors.ts +74 -21
- package/src/fn.ts +73 -17
- package/src/index.ts +31 -4
- package/src/mixin.ts +1 -1
- package/src/native-value.ts +146 -14
- package/src/object.ts +23 -11
- package/src/param-spec.ts +94 -8
- package/src/promisify.ts +27 -1
- package/src/properties.ts +184 -5
- package/src/regex.ts +92 -0
- package/src/register-class.ts +446 -38
- package/src/registry.ts +219 -4
- package/src/signal.ts +77 -0
- package/src/type.ts +7 -0
- package/src/value.ts +268 -14
- package/src/variant.ts +184 -53
package/src/variant.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as GLib from "@gtkx/gi/glib";
|
|
2
2
|
|
|
3
|
-
/** JavaScript type every GVariant basic type code unpacks to
|
|
4
|
-
type BasicValueMap = {
|
|
3
|
+
/** JavaScript type every GVariant basic type code unpacks to, with a nested variant held as `Nested`. */
|
|
4
|
+
type BasicValueMap<Nested> = {
|
|
5
5
|
/** Boolean, held as a single byte reading 0 or 1. */
|
|
6
6
|
b: boolean;
|
|
7
7
|
/** Unsigned byte. */
|
|
@@ -29,32 +29,49 @@ type BasicValueMap = {
|
|
|
29
29
|
/** GVariant type signature. */
|
|
30
30
|
g: string;
|
|
31
31
|
/** Boxed variant holding a value of any type. */
|
|
32
|
-
v:
|
|
32
|
+
v: Nested;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
/** Single-character code of one of the GVariant basic types in {@link BasicValueMap}. */
|
|
36
|
-
type BasicCode = keyof BasicValueMap
|
|
37
|
-
/**
|
|
38
|
-
type
|
|
36
|
+
type BasicCode = keyof BasicValueMap<unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* JavaScript type a byte array (`ay`) unpacks to: the same one the generated bindings use for byte sequences, so a
|
|
39
|
+
* `Uint8Array` under the `v2ByteArrays` future flag and a `number[]` otherwise.
|
|
40
|
+
*/
|
|
41
|
+
type ByteArray = ReturnType<GLib.Bytes["unrefToArray"]>;
|
|
42
|
+
/** Values a byte array (`ay`) packs from. */
|
|
43
|
+
type ByteArrayInput = Uint8Array | number[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parses the element type of an array, yielding the array type it produces and the rest of the string. An array of
|
|
47
|
+
* bytes yields the byte array type instead of `number[]`.
|
|
48
|
+
*/
|
|
49
|
+
type ParseArray<S extends string, Bytes, Nested> = S extends `y${infer Rest}`
|
|
50
|
+
? [Bytes, Rest]
|
|
51
|
+
: Parse<S, Bytes, Nested> extends [infer V, infer R extends string]
|
|
52
|
+
? [V[], R]
|
|
53
|
+
: never;
|
|
54
|
+
|
|
39
55
|
/** Parses the element type of a maybe, yielding the nullable type it produces and the rest of the string. */
|
|
40
|
-
type ParseMaybe<S extends string
|
|
56
|
+
type ParseMaybe<S extends string, Bytes, Nested> =
|
|
57
|
+
Parse<S, Bytes, Nested> extends [infer V, infer R extends string] ? [V | null, R] : never;
|
|
41
58
|
|
|
42
59
|
/** Collects tuple member types into `Acc` up to the closing parenthesis, yielding them and the rest of the string. */
|
|
43
|
-
type ParseTuple<S extends string, Acc extends unknown[]> = S extends `)${infer Rest}`
|
|
60
|
+
type ParseTuple<S extends string, Bytes, Nested, Acc extends unknown[]> = S extends `)${infer Rest}`
|
|
44
61
|
? [Acc, Rest]
|
|
45
|
-
: [Parse<S>] extends [never]
|
|
62
|
+
: [Parse<S, Bytes, Nested>] extends [never]
|
|
46
63
|
? never
|
|
47
|
-
: Parse<S> extends [infer V, infer R extends string]
|
|
48
|
-
? ParseTuple<R, [...Acc, V]>
|
|
64
|
+
: Parse<S, Bytes, Nested> extends [infer V, infer R extends string]
|
|
65
|
+
? ParseTuple<R, Bytes, Nested, [...Acc, V]>
|
|
49
66
|
: never;
|
|
50
67
|
|
|
51
68
|
/** Types a dictionary entry key can hold. */
|
|
52
69
|
type DictKey = string | number | bigint | boolean;
|
|
53
70
|
|
|
54
71
|
/** Parses the key and value types of a dictionary entry, yielding them and the rest of the string after its brace. */
|
|
55
|
-
type ParsePair<S extends string> =
|
|
56
|
-
Parse<S> extends [infer K, infer R1 extends string]
|
|
57
|
-
? Parse<R1> extends [infer V, infer R2 extends string]
|
|
72
|
+
type ParsePair<S extends string, Bytes, Nested> =
|
|
73
|
+
Parse<S, Bytes, Nested> extends [infer K, infer R1 extends string]
|
|
74
|
+
? Parse<R1, Bytes, Nested> extends [infer V, infer R2 extends string]
|
|
58
75
|
? R2 extends `}${infer R3}`
|
|
59
76
|
? [K] extends [DictKey]
|
|
60
77
|
? [K, V, R3]
|
|
@@ -67,43 +84,76 @@ type ParsePair<S extends string> =
|
|
|
67
84
|
* Parses a dictionary entry into the collection an array of those entries unpacks to, a record for string keys and a
|
|
68
85
|
* `Map` otherwise, plus the rest of the string.
|
|
69
86
|
*/
|
|
70
|
-
type ParseDict<S extends string> =
|
|
71
|
-
ParsePair<S> extends [infer K, infer V, infer R extends string]
|
|
87
|
+
type ParseDict<S extends string, Bytes, Nested> =
|
|
88
|
+
ParsePair<S, Bytes, Nested> extends [infer K, infer V, infer R extends string]
|
|
72
89
|
? [[K] extends [string] ? Record<string, V> : Map<K, V>, R]
|
|
73
90
|
: never;
|
|
74
91
|
|
|
75
92
|
/** Parses a standalone dictionary entry into a key and value pair, yielding it and the rest of the string. */
|
|
76
|
-
type ParseEntry<S extends string> =
|
|
77
|
-
ParsePair<S> extends [infer K, infer V, infer R extends string] ? [[K, V], R] : never;
|
|
93
|
+
type ParseEntry<S extends string, Bytes, Nested> =
|
|
94
|
+
ParsePair<S, Bytes, Nested> extends [infer K, infer V, infer R extends string] ? [[K, V], R] : never;
|
|
78
95
|
|
|
79
96
|
/** Parses what follows an `a` as a dictionary when it opens an entry, and as a plain array otherwise. */
|
|
80
|
-
type ParseArrayOrDict<S extends string> = S extends `{${infer Rest}`
|
|
97
|
+
type ParseArrayOrDict<S extends string, Bytes, Nested> = S extends `{${infer Rest}`
|
|
98
|
+
? ParseDict<Rest, Bytes, Nested>
|
|
99
|
+
: ParseArray<S, Bytes, Nested>;
|
|
81
100
|
|
|
82
101
|
/**
|
|
83
102
|
* Parses the type at the head of `S`, yielding the JavaScript type it unpacks to and the rest of the string, or
|
|
84
103
|
* `never` when the string is malformed.
|
|
85
104
|
*/
|
|
86
|
-
type Parse<S extends string> = S extends `a${infer Rest}`
|
|
87
|
-
? ParseArrayOrDict<Rest>
|
|
105
|
+
type Parse<S extends string, Bytes, Nested> = S extends `a${infer Rest}`
|
|
106
|
+
? ParseArrayOrDict<Rest, Bytes, Nested>
|
|
88
107
|
: S extends `m${infer Rest}`
|
|
89
|
-
? ParseMaybe<Rest>
|
|
108
|
+
? ParseMaybe<Rest, Bytes, Nested>
|
|
90
109
|
: S extends `(${infer Rest}`
|
|
91
|
-
? ParseTuple<Rest, []>
|
|
110
|
+
? ParseTuple<Rest, Bytes, Nested, []>
|
|
92
111
|
: S extends `{${infer Rest}`
|
|
93
|
-
? ParseEntry<Rest>
|
|
112
|
+
? ParseEntry<Rest, Bytes, Nested>
|
|
94
113
|
: S extends `${infer C}${infer Rest}`
|
|
95
114
|
? C extends BasicCode
|
|
96
|
-
? [BasicValueMap[C], Rest]
|
|
115
|
+
? [BasicValueMap<Nested>[C], Rest]
|
|
97
116
|
: never
|
|
98
117
|
: never;
|
|
99
118
|
|
|
100
|
-
/** JavaScript type a
|
|
101
|
-
type
|
|
119
|
+
/** JavaScript type a type string `S` describes, or `unknown` when `S` is not one complete type. */
|
|
120
|
+
type ParsedValue<S extends string, Bytes, Nested> = [Parse<S, Bytes, Nested>] extends [never]
|
|
102
121
|
? unknown
|
|
103
|
-
: Parse<S> extends [infer V, ""]
|
|
122
|
+
: Parse<S, Bytes, Nested> extends [infer V, ""]
|
|
104
123
|
? V
|
|
105
124
|
: unknown;
|
|
106
125
|
|
|
126
|
+
/**
|
|
127
|
+
* JavaScript type a variant of type string `S` unpacks to, or `unknown` when `S` is not one complete type. A byte
|
|
128
|
+
* array (`ay`) unpacks to {@link ByteArray} and a nested variant (`v`) to the `GLib.Variant` itself.
|
|
129
|
+
*/
|
|
130
|
+
type VariantValue<S extends string> = ParsedValue<S, ByteArray, GLib.Variant>;
|
|
131
|
+
/**
|
|
132
|
+
* JavaScript type {@link toVariant} packs into a variant of type string `S`, or `unknown` when `S` is not one
|
|
133
|
+
* complete type. The same as {@link VariantValue}, except that a byte array (`ay`) also packs from a `number[]`.
|
|
134
|
+
*/
|
|
135
|
+
type VariantInput<S extends string> = ParsedValue<S, ByteArrayInput, GLib.Variant>;
|
|
136
|
+
/**
|
|
137
|
+
* JavaScript type a variant of type string `S` unpacks to when every nested variant is unwrapped as well, so a `v`
|
|
138
|
+
* reads as `unknown`.
|
|
139
|
+
*/
|
|
140
|
+
type RecursiveVariantValue<S extends string> = ParsedValue<S, ByteArray, unknown>;
|
|
141
|
+
|
|
142
|
+
/** Options {@link fromVariant} reads. */
|
|
143
|
+
type FromVariantOptions = {
|
|
144
|
+
/**
|
|
145
|
+
* Whether to unwrap every nested variant (`v`) into the value it holds, all the way down, instead of handing back
|
|
146
|
+
* the `GLib.Variant` itself. Discards the type information those variants carry.
|
|
147
|
+
*/
|
|
148
|
+
recursive?: boolean;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** {@link FromVariantOptions} with recursive unwrapping selected, typing the result accordingly. */
|
|
152
|
+
type RecursiveFromVariantOptions = {
|
|
153
|
+
/** Unwraps every nested variant (`v`) into the value it holds, all the way down. */
|
|
154
|
+
recursive: true;
|
|
155
|
+
};
|
|
156
|
+
|
|
107
157
|
type VariantTypeNode =
|
|
108
158
|
| { kind: "basic"; code: BasicCode } |
|
|
109
159
|
{ kind: "array"; elementTypeString: string; element: VariantTypeNode } |
|
|
@@ -123,6 +173,7 @@ const CONTAINER_PARSERS: Record<string, (source: string, start: number) => [Vari
|
|
|
123
173
|
};
|
|
124
174
|
|
|
125
175
|
const parsedTypes: Map<string, VariantTypeNode> = new Map();
|
|
176
|
+
const BYTE_ARRAY_TYPE_STRING = "ay";
|
|
126
177
|
|
|
127
178
|
const unpackBasic: Record<BasicCode, (variant: GLib.Variant) => unknown> = {
|
|
128
179
|
b: (variant) => variant.getBoolean(),
|
|
@@ -257,6 +308,9 @@ const parseVariantType = (typeString: string): VariantTypeNode => {
|
|
|
257
308
|
return node;
|
|
258
309
|
};
|
|
259
310
|
|
|
311
|
+
const isByteArray = (node: VariantTypeNode): boolean =>
|
|
312
|
+
node.kind === "array" && node.element.kind === "basic" && node.element.code === "y";
|
|
313
|
+
|
|
260
314
|
const unpackChildren = (variant: GLib.Variant, unpackChild: (child: GLib.Variant) => unknown): unknown[] => {
|
|
261
315
|
const children: unknown[] = [];
|
|
262
316
|
const count = variant.nChildren();
|
|
@@ -268,16 +322,25 @@ const unpackChildren = (variant: GLib.Variant, unpackChild: (child: GLib.Variant
|
|
|
268
322
|
return children;
|
|
269
323
|
};
|
|
270
324
|
|
|
271
|
-
const unpackPair = (
|
|
272
|
-
|
|
273
|
-
|
|
325
|
+
const unpackPair = (
|
|
326
|
+
key: VariantTypeNode,
|
|
327
|
+
value: VariantTypeNode,
|
|
328
|
+
entry: GLib.Variant,
|
|
329
|
+
isRecursive: boolean,
|
|
330
|
+
): [unknown, unknown] => [
|
|
331
|
+
unpackNode(key, entry.getChildValue(0), isRecursive),
|
|
332
|
+
unpackNode(value, entry.getChildValue(1), isRecursive),
|
|
274
333
|
];
|
|
275
334
|
|
|
276
335
|
const unpackDict = (
|
|
277
336
|
node: { key: VariantTypeNode; value: VariantTypeNode },
|
|
278
337
|
variant: GLib.Variant,
|
|
338
|
+
isRecursive: boolean,
|
|
279
339
|
): Record<string, unknown> | Map<unknown, unknown> => {
|
|
280
|
-
const entries = unpackChildren(variant, (entry) => unpackPair(node.key, node.value, entry)) as [
|
|
340
|
+
const entries = unpackChildren(variant, (entry) => unpackPair(node.key, node.value, entry, isRecursive)) as [
|
|
341
|
+
unknown,
|
|
342
|
+
unknown,
|
|
343
|
+
][];
|
|
281
344
|
|
|
282
345
|
if (!isStringKeyed(node.key)) {
|
|
283
346
|
return new Map(entries);
|
|
@@ -288,31 +351,39 @@ const unpackDict = (
|
|
|
288
351
|
return Object.fromEntries(stringEntries);
|
|
289
352
|
};
|
|
290
353
|
|
|
291
|
-
const unpackMaybe = (element: VariantTypeNode, variant: GLib.Variant): unknown => {
|
|
354
|
+
const unpackMaybe = (element: VariantTypeNode, variant: GLib.Variant, isRecursive: boolean): unknown => {
|
|
292
355
|
const child = variant.getMaybe();
|
|
293
356
|
|
|
294
|
-
return child === null ? null : unpackNode(element, child);
|
|
357
|
+
return child === null ? null : unpackNode(element, child, isRecursive);
|
|
295
358
|
};
|
|
296
359
|
|
|
297
|
-
const
|
|
360
|
+
const unpackNested = (variant: GLib.Variant, isRecursive: boolean): unknown => {
|
|
361
|
+
const held = variant.getVariant();
|
|
362
|
+
|
|
363
|
+
return isRecursive ? unpackNode(parseVariantType(held.getTypeString()), held, true) : held;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const unpackNode = (node: VariantTypeNode, variant: GLib.Variant, isRecursive: boolean): unknown => {
|
|
298
367
|
switch (node.kind) {
|
|
299
368
|
case "basic": {
|
|
300
|
-
return unpackBasic[node.code](variant);
|
|
369
|
+
return node.code === "v" ? unpackNested(variant, isRecursive) : unpackBasic[node.code](variant);
|
|
301
370
|
}
|
|
302
371
|
case "array": {
|
|
303
|
-
return
|
|
372
|
+
return isByteArray(node)
|
|
373
|
+
? variant.getDataAsBytes().unrefToArray()
|
|
374
|
+
: unpackChildren(variant, (child) => unpackNode(node.element, child, isRecursive));
|
|
304
375
|
}
|
|
305
376
|
case "dict": {
|
|
306
|
-
return unpackDict(node, variant);
|
|
377
|
+
return unpackDict(node, variant, isRecursive);
|
|
307
378
|
}
|
|
308
379
|
case "entry": {
|
|
309
|
-
return unpackPair(node.key, node.value, variant);
|
|
380
|
+
return unpackPair(node.key, node.value, variant, isRecursive);
|
|
310
381
|
}
|
|
311
382
|
case "tuple": {
|
|
312
|
-
return node.items.map((item, index) => unpackNode(item, variant.getChildValue(index)));
|
|
383
|
+
return node.items.map((item, index) => unpackNode(item, variant.getChildValue(index), isRecursive));
|
|
313
384
|
}
|
|
314
385
|
case "maybe": {
|
|
315
|
-
return unpackMaybe(node.element, variant);
|
|
386
|
+
return unpackMaybe(node.element, variant, isRecursive);
|
|
316
387
|
}
|
|
317
388
|
}
|
|
318
389
|
};
|
|
@@ -369,12 +440,28 @@ const packMaybe = (node: { elementTypeString: string; element: VariantTypeNode }
|
|
|
369
440
|
value === null ? null : packNode(node.element, value),
|
|
370
441
|
);
|
|
371
442
|
|
|
443
|
+
const packByteArray = (value: unknown): GLib.Variant => {
|
|
444
|
+
if (!(value instanceof Uint8Array) && !Array.isArray(value)) {
|
|
445
|
+
throw new TypeError("Expected a Uint8Array or an array of byte values for a byte array (ay)");
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return GLib.Variant.newFromBytes(
|
|
449
|
+
GLib.VariantType.new(BYTE_ARRAY_TYPE_STRING),
|
|
450
|
+
GLib.Bytes.new(value as ByteArrayInput),
|
|
451
|
+
true,
|
|
452
|
+
);
|
|
453
|
+
};
|
|
454
|
+
|
|
372
455
|
const packNode = (node: VariantTypeNode, value: unknown): GLib.Variant => {
|
|
373
456
|
switch (node.kind) {
|
|
374
457
|
case "basic": {
|
|
375
458
|
return packBasic[node.code](value);
|
|
376
459
|
}
|
|
377
460
|
case "array": {
|
|
461
|
+
if (isByteArray(node)) {
|
|
462
|
+
return packByteArray(value);
|
|
463
|
+
}
|
|
464
|
+
|
|
378
465
|
return GLib.Variant.newArray(
|
|
379
466
|
GLib.VariantType.new(node.elementTypeString),
|
|
380
467
|
(value as unknown[]).map((item) => packNode(node.element, item)),
|
|
@@ -401,27 +488,71 @@ const packNode = (node: VariantTypeNode, value: unknown): GLib.Variant => {
|
|
|
401
488
|
* Packs a JavaScript value into the `GLib.Variant` a GVariant type string describes, building the
|
|
402
489
|
* nested arrays, dictionaries, tuples and maybes the type calls for. A type string given as a
|
|
403
490
|
* literal also types `value`, so `"as"` takes an array of strings, `"a{sv}"` a record of variants,
|
|
404
|
-
* `"(si)"` a string and a number pair,
|
|
491
|
+
* `"(si)"` a string and a number pair, a 64-bit type a `bigint`, and `"ay"` a `Uint8Array` or an
|
|
492
|
+
* array of byte values.
|
|
405
493
|
* @param typeString GVariant type of the variant to build, such as `"a{sv}"`.
|
|
406
494
|
* @param value Value to pack, shaped the way `typeString` describes.
|
|
407
495
|
* @returns The packed variant.
|
|
408
|
-
* @throws {Error} When the type string is not one complete GVariant type,
|
|
409
|
-
* an object path or a type signature is not a valid one
|
|
496
|
+
* @throws {Error} When the type string is not one complete GVariant type, when a value packed as
|
|
497
|
+
* an object path or a type signature is not a valid one, or when a byte array is packed from
|
|
498
|
+
* anything but a `Uint8Array` or an array of byte values.
|
|
410
499
|
*/
|
|
411
|
-
const toVariant = <S extends string>(typeString: S, value:
|
|
500
|
+
const toVariant = <S extends string>(typeString: S, value: VariantInput<S>): GLib.Variant =>
|
|
412
501
|
packNode(parseVariantType(typeString), value);
|
|
413
502
|
|
|
414
503
|
/**
|
|
415
|
-
* Unpacks a `GLib.Variant` into the JavaScript value
|
|
416
|
-
*
|
|
417
|
-
* else to a `Map`, an array to an array, a
|
|
418
|
-
* `
|
|
504
|
+
* Unpacks a `GLib.Variant` into the JavaScript value its GVariant type describes, the inverse of
|
|
505
|
+
* {@link toVariant}. A dictionary keyed by strings unpacks to a record and one keyed by anything
|
|
506
|
+
* else to a `Map`, an array to an array, a byte array (`ay`) to the byte array type the generated
|
|
507
|
+
* bindings use (`Uint8Array` under the `v2ByteArrays` future flag, `number[]` otherwise), a tuple
|
|
508
|
+
* to an array of its members, a maybe to its value or `null`, and a nested variant to the
|
|
509
|
+
* `GLib.Variant` itself unless `recursive` is set, in which case it is unwrapped all the way down.
|
|
510
|
+
*
|
|
511
|
+
* The type string is optional: without one the variant's own type is read, and the result is
|
|
512
|
+
* `unknown`. With one given as a literal, the result is typed from it.
|
|
419
513
|
* @param typeString GVariant type the variant holds, such as `"a{sv}"`.
|
|
420
514
|
* @param variant Variant to read, which has to hold that type.
|
|
421
|
-
* @
|
|
515
|
+
* @param options Whether to unwrap nested variants recursively.
|
|
516
|
+
* @returns The unpacked value.
|
|
422
517
|
* @throws {Error} When the type string is not one complete GVariant type.
|
|
423
518
|
*/
|
|
424
|
-
|
|
425
|
-
unpackNode(parseVariantType(typeString), variant) as VariantValue<S>;
|
|
519
|
+
function fromVariant<S extends string>(typeString: S, variant: GLib.Variant): VariantValue<S>;
|
|
426
520
|
|
|
427
|
-
|
|
521
|
+
function fromVariant<S extends string>(
|
|
522
|
+
typeString: S,
|
|
523
|
+
variant: GLib.Variant,
|
|
524
|
+
options: RecursiveFromVariantOptions,
|
|
525
|
+
): RecursiveVariantValue<S>;
|
|
526
|
+
|
|
527
|
+
function fromVariant<S extends string>(
|
|
528
|
+
typeString: S,
|
|
529
|
+
variant: GLib.Variant,
|
|
530
|
+
options: FromVariantOptions,
|
|
531
|
+
): VariantValue<S> | RecursiveVariantValue<S>;
|
|
532
|
+
|
|
533
|
+
function fromVariant(variant: GLib.Variant, options?: FromVariantOptions): unknown;
|
|
534
|
+
|
|
535
|
+
function fromVariant(
|
|
536
|
+
first: string | GLib.Variant,
|
|
537
|
+
second?: GLib.Variant | FromVariantOptions,
|
|
538
|
+
third?: FromVariantOptions,
|
|
539
|
+
): unknown {
|
|
540
|
+
if (typeof first === "string") {
|
|
541
|
+
return unpackNode(parseVariantType(first), second as GLib.Variant, third?.recursive === true);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const options = second as FromVariantOptions | undefined;
|
|
545
|
+
|
|
546
|
+
return unpackNode(parseVariantType(first.getTypeString()), first, options?.recursive === true);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export {
|
|
550
|
+
type ByteArray,
|
|
551
|
+
type FromVariantOptions,
|
|
552
|
+
fromVariant,
|
|
553
|
+
type RecursiveFromVariantOptions,
|
|
554
|
+
type RecursiveVariantValue,
|
|
555
|
+
toVariant,
|
|
556
|
+
type VariantInput,
|
|
557
|
+
type VariantValue,
|
|
558
|
+
};
|