@gtkx/runtime 1.0.0-rc.1 → 1.0.0-rc.2
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/dist/arg.d.ts +6 -5
- package/dist/arg.d.ts.map +1 -1
- package/dist/arg.js +5 -4
- package/dist/arg.js.map +1 -1
- package/dist/bind.d.ts +3 -2
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js +5 -3
- package/dist/bind.js.map +1 -1
- package/dist/callback.d.ts +3 -3
- package/dist/callback.d.ts.map +1 -1
- package/dist/callback.js +51 -33
- package/dist/callback.js.map +1 -1
- package/dist/descriptors.d.ts +67 -64
- package/dist/descriptors.d.ts.map +1 -1
- package/dist/descriptors.js +92 -60
- package/dist/descriptors.js.map +1 -1
- package/dist/error.d.ts +5 -6
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +10 -6
- package/dist/error.js.map +1 -1
- package/dist/exit-hook.d.ts +2 -0
- package/dist/exit-hook.d.ts.map +1 -0
- package/dist/exit-hook.js +3 -0
- package/dist/exit-hook.js.map +1 -0
- package/dist/fn.d.ts +2 -2
- package/dist/fn.d.ts.map +1 -1
- package/dist/fn.js +26 -15
- package/dist/fn.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +6 -5
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +6 -5
- package/dist/library.js.map +1 -1
- package/dist/lifecycle.d.ts +10 -9
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +38 -23
- package/dist/lifecycle.js.map +1 -1
- package/dist/listeners.d.ts +8 -8
- package/dist/listeners.d.ts.map +1 -1
- package/dist/listeners.js +28 -15
- package/dist/listeners.js.map +1 -1
- package/dist/mixin.d.ts +5 -11
- package/dist/mixin.d.ts.map +1 -1
- package/dist/mixin.js +24 -14
- package/dist/mixin.js.map +1 -1
- package/dist/native-value.d.ts +3 -2
- package/dist/native-value.d.ts.map +1 -1
- package/dist/native-value.js +75 -42
- package/dist/native-value.js.map +1 -1
- package/dist/object.d.ts +4 -3
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +8 -5
- package/dist/object.js.map +1 -1
- package/dist/promisify.d.ts +9 -1
- package/dist/promisify.d.ts.map +1 -1
- package/dist/promisify.js +18 -15
- package/dist/promisify.js.map +1 -1
- package/dist/properties.d.ts +14 -0
- package/dist/properties.d.ts.map +1 -0
- package/dist/properties.js +79 -0
- package/dist/properties.js.map +1 -0
- package/dist/register-class.d.ts +10 -2
- package/dist/register-class.d.ts.map +1 -1
- package/dist/register-class.js +83 -29
- package/dist/register-class.js.map +1 -1
- package/dist/registry.d.ts +23 -21
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +54 -35
- package/dist/registry.js.map +1 -1
- package/dist/signal.d.ts +7 -6
- package/dist/signal.d.ts.map +1 -1
- package/dist/signal.js +19 -12
- package/dist/signal.js.map +1 -1
- package/dist/t.d.ts +2 -2
- package/dist/t.d.ts.map +1 -1
- package/dist/t.js +2 -1
- package/dist/t.js.map +1 -1
- package/dist/tuple.d.ts +3 -2
- package/dist/tuple.d.ts.map +1 -1
- package/dist/tuple.js +7 -4
- package/dist/tuple.js.map +1 -1
- package/dist/type.d.ts +48 -41
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +107 -87
- package/dist/type.js.map +1 -1
- package/dist/value.d.ts +19 -10
- package/dist/value.d.ts.map +1 -1
- package/dist/value.js +164 -99
- package/dist/value.js.map +1 -1
- package/package.json +4 -6
- package/src/arg.ts +7 -5
- package/src/bind.ts +13 -4
- package/src/callback.ts +97 -35
- package/src/descriptors.ts +227 -85
- package/src/error.ts +18 -9
- package/src/exit-hook.ts +3 -0
- package/src/fn.ts +53 -22
- package/src/index.ts +4 -2
- package/src/library.ts +7 -5
- package/src/lifecycle.ts +54 -26
- package/src/listeners.ts +46 -18
- package/src/mixin.ts +39 -14
- package/src/native-value.ts +103 -46
- package/src/object.ts +19 -7
- package/src/promisify.ts +42 -15
- package/src/properties.ts +115 -0
- package/src/register-class.ts +157 -30
- package/src/registry.ts +144 -54
- package/src/signal.ts +53 -13
- package/src/t.ts +3 -1
- package/src/tuple.ts +16 -4
- package/src/type.ts +183 -93
- package/src/value.ts +231 -108
package/src/value.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { alloc, type Descriptor, type ExternalObject, getType, type Handle, read, write } from "@gtkx/native";
|
|
2
2
|
import { bind, createBindCache } from "./bind.js";
|
|
3
3
|
import {
|
|
4
|
+
type ArrayDescriptor,
|
|
4
5
|
arrayT,
|
|
5
6
|
bigint64T,
|
|
6
7
|
biguint64T,
|
|
@@ -20,6 +21,7 @@ import { toNative } from "./native-value.js";
|
|
|
20
21
|
import { getHandle, getWrapperClass, wrapHandle } from "./registry.js";
|
|
21
22
|
import {
|
|
22
23
|
getStrvType,
|
|
24
|
+
isResolvableDescriptor,
|
|
23
25
|
resolveBoxedType,
|
|
24
26
|
resolveDescriptorType,
|
|
25
27
|
resolveFundamentalType,
|
|
@@ -50,30 +52,13 @@ type ValueType = {
|
|
|
50
52
|
get: (value: ExternalObject<Handle>) => unknown;
|
|
51
53
|
};
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
const newValue = (): ExternalObject<Handle> => alloc(VALUE_SIZE, "GValue");
|
|
55
|
+
type ValueGetter = (value: ExternalObject<Handle>) => unknown;
|
|
55
56
|
|
|
56
57
|
const setBoxedCache = createBindCache();
|
|
57
58
|
const setStaticBoxedCache = createBindCache();
|
|
58
59
|
const dupBoxedCache = createBindCache();
|
|
59
|
-
|
|
60
60
|
const gValueInit = bind(LIB, "g_value_init", [VALUE_T, biguint64T], voidT);
|
|
61
61
|
const gValueCopy = bind(LIB, "g_value_copy", [VALUE_T, VALUE_T], voidT);
|
|
62
|
-
|
|
63
|
-
const bindValueType = (symbol: string, descriptor: Descriptor): ValueType => ({
|
|
64
|
-
set: bind(LIB, `g_value_set_${symbol}`, [VALUE_T, descriptor], voidT),
|
|
65
|
-
get: bind(LIB, `g_value_get_${symbol}`, [VALUE_T], descriptor),
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const setBoxedBind = (name: string) =>
|
|
69
|
-
setBoxedCache(name, LIB, "g_value_set_boxed", [VALUE_T, boxedT(name, { sharedLibrary: LIB })], voidT);
|
|
70
|
-
|
|
71
|
-
const setStaticBoxedBind = (name: string) =>
|
|
72
|
-
setStaticBoxedCache(name, LIB, "g_value_set_static_boxed", [VALUE_T, boxedT(name, { sharedLibrary: LIB })], voidT);
|
|
73
|
-
|
|
74
|
-
const dupBoxedBind = (name: string) =>
|
|
75
|
-
dupBoxedCache(name, LIB, "g_value_dup_boxed", [VALUE_T], boxedT(name, { ownership: "full", sharedLibrary: LIB }));
|
|
76
|
-
|
|
77
62
|
const booleanValueType = bindValueType("boolean", booleanT);
|
|
78
63
|
const typeValueType = bindValueType("gtype", biguint64T);
|
|
79
64
|
const intValueType = bindValueType("int", int32T);
|
|
@@ -95,8 +80,80 @@ const strvValueType: ValueType = {
|
|
|
95
80
|
get: bind(LIB, "g_value_get_boxed", [VALUE_T], arrayT(stringT("borrowed"))),
|
|
96
81
|
};
|
|
97
82
|
|
|
83
|
+
const PLAIN_VALUE_TYPES: Partial<Record<Descriptor["kind"], ValueType>> = {
|
|
84
|
+
boolean: booleanValueType,
|
|
85
|
+
string: stringValueType,
|
|
86
|
+
int8: intValueType,
|
|
87
|
+
int16: intValueType,
|
|
88
|
+
int32: intValueType,
|
|
89
|
+
uint8: uintValueType,
|
|
90
|
+
uint16: uintValueType,
|
|
91
|
+
uint32: uintValueType,
|
|
92
|
+
int64: int64ValueType,
|
|
93
|
+
bigint64: int64ValueType,
|
|
94
|
+
uint64: uint64ValueType,
|
|
95
|
+
biguint64: uint64ValueType,
|
|
96
|
+
float32: floatValueType,
|
|
97
|
+
float64: doubleValueType,
|
|
98
|
+
object: objectValueType,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const PLAIN_VALUE_GETTERS: Map<bigint, ValueGetter> = new Map([
|
|
102
|
+
[TYPE_BOOLEAN, booleanValueType.get],
|
|
103
|
+
[TYPE_GTYPE, typeValueType.get],
|
|
104
|
+
[TYPE_INT, intValueType.get],
|
|
105
|
+
[TYPE_UINT, uintValueType.get],
|
|
106
|
+
[TYPE_INT64, int64ValueType.get],
|
|
107
|
+
[TYPE_UINT64, uint64ValueType.get],
|
|
108
|
+
[TYPE_FLOAT, floatValueType.get],
|
|
109
|
+
[TYPE_DOUBLE, doubleValueType.get],
|
|
110
|
+
[TYPE_ENUM, enumValueType.get],
|
|
111
|
+
[TYPE_FLAGS, flagsValueType.get],
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
const PLAIN_VALUE_SETTERS: Map<bigint, ValueType["set"]> = new Map([
|
|
115
|
+
[TYPE_BOOLEAN, booleanValueType.set],
|
|
116
|
+
[TYPE_GTYPE, typeValueType.set],
|
|
117
|
+
[TYPE_INT, intValueType.set],
|
|
118
|
+
[TYPE_UINT, uintValueType.set],
|
|
119
|
+
[TYPE_INT64, int64ValueType.set],
|
|
120
|
+
[TYPE_UINT64, uint64ValueType.set],
|
|
121
|
+
[TYPE_FLOAT, floatValueType.set],
|
|
122
|
+
[TYPE_DOUBLE, doubleValueType.set],
|
|
123
|
+
[TYPE_ENUM, enumValueType.set],
|
|
124
|
+
[TYPE_FLAGS, flagsValueType.set],
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
const WRAPPED_VALUE_SETTERS: Map<bigint, ValueType["set"]> = new Map([
|
|
128
|
+
[TYPE_STRING, setStringValue],
|
|
129
|
+
[TYPE_OBJECT, handleSetter(objectValueType)],
|
|
130
|
+
[TYPE_PARAM, handleSetter(paramValueType)],
|
|
131
|
+
[TYPE_VARIANT, handleSetter(variantValueType)],
|
|
132
|
+
[TYPE_BOXED, setBoxedFromValue],
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
const getBoxedTypeName = (type: bigint): string => typeName(type) ?? "GBoxed";
|
|
136
|
+
const newValue = (): ExternalObject<Handle> => alloc(VALUE_SIZE, resolveBoxedType(VALUE_T));
|
|
137
|
+
|
|
138
|
+
function bindValueType(symbol: string, descriptor: Descriptor): ValueType {
|
|
139
|
+
return {
|
|
140
|
+
set: bind(LIB, `g_value_set_${symbol}`, [VALUE_T, descriptor], voidT),
|
|
141
|
+
get: bind(LIB, `g_value_get_${symbol}`, [VALUE_T], descriptor),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const setBoxedBind = (name: string) =>
|
|
146
|
+
setBoxedCache(name, LIB, "g_value_set_boxed", [VALUE_T, boxedT(name, { sharedLibrary: LIB })], voidT);
|
|
147
|
+
|
|
148
|
+
const setStaticBoxedBind = (name: string) =>
|
|
149
|
+
setStaticBoxedCache(name, LIB, "g_value_set_static_boxed", [VALUE_T, boxedT(name, { sharedLibrary: LIB })], voidT);
|
|
150
|
+
|
|
151
|
+
const dupBoxedBind = (name: string) =>
|
|
152
|
+
dupBoxedCache(name, LIB, "g_value_dup_boxed", [VALUE_T], boxedT(name, { ownership: "full", sharedLibrary: LIB }));
|
|
153
|
+
|
|
98
154
|
const boxedValueType = (type: bigint): ValueType => {
|
|
99
155
|
const name = getBoxedTypeName(type);
|
|
156
|
+
|
|
100
157
|
return { set: setBoxedBind(name), get: dupBoxedBind(name) };
|
|
101
158
|
};
|
|
102
159
|
|
|
@@ -105,31 +162,37 @@ const enumOrFlagsValueType = (type: bigint): ValueType =>
|
|
|
105
162
|
|
|
106
163
|
const fundamentalValueType = (type: bigint): ValueType => {
|
|
107
164
|
switch (typeFundamental(type)) {
|
|
108
|
-
case TYPE_PARAM:
|
|
165
|
+
case TYPE_PARAM: {
|
|
109
166
|
return paramValueType;
|
|
110
|
-
|
|
167
|
+
}
|
|
168
|
+
case TYPE_VARIANT: {
|
|
111
169
|
return variantValueType;
|
|
112
|
-
|
|
170
|
+
}
|
|
171
|
+
case TYPE_BOXED: {
|
|
113
172
|
return boxedValueType(type);
|
|
114
|
-
|
|
173
|
+
}
|
|
174
|
+
default: {
|
|
115
175
|
throw new Error(`Unsupported fundamental type '${typeName(type) ?? String(type)}' for value`);
|
|
176
|
+
}
|
|
116
177
|
}
|
|
117
178
|
};
|
|
118
179
|
|
|
119
|
-
|
|
180
|
+
function getValueType(value: ExternalObject<Handle>): bigint {
|
|
120
181
|
return read(value, biguint64T, 0) as bigint;
|
|
121
182
|
}
|
|
122
183
|
|
|
123
|
-
|
|
184
|
+
function copyValue(dest: ExternalObject<Handle>, src: ExternalObject<Handle>): void {
|
|
124
185
|
if (getValueType(dest) === TYPE_INVALID) {
|
|
125
186
|
gValueInit(dest, getValueType(src));
|
|
126
187
|
}
|
|
188
|
+
|
|
127
189
|
gValueCopy(src, dest);
|
|
128
190
|
}
|
|
129
191
|
|
|
130
192
|
const newTypedValue = (type: bigint): ExternalObject<Handle> => {
|
|
131
193
|
const value = newValue();
|
|
132
194
|
gValueInit(value, type);
|
|
195
|
+
|
|
133
196
|
return value;
|
|
134
197
|
};
|
|
135
198
|
|
|
@@ -141,6 +204,7 @@ const newBoxedValue = (
|
|
|
141
204
|
const type = resolveDescriptorType(descriptor);
|
|
142
205
|
const value = newTypedValue(type);
|
|
143
206
|
resolveSetBind(getBoxedTypeName(type))(value, getHandle(boxed));
|
|
207
|
+
|
|
144
208
|
return value;
|
|
145
209
|
};
|
|
146
210
|
|
|
@@ -148,160 +212,219 @@ const newBoxedValue = (
|
|
|
148
212
|
* Reads the boxed pointer out of a GValue and returns it wrapped in its registered
|
|
149
213
|
* class, or null when the value does not hold a boxed type.
|
|
150
214
|
*/
|
|
151
|
-
|
|
215
|
+
function getBoxedValue(value: ExternalObject<Handle>): object | null {
|
|
152
216
|
const type = getValueType(value);
|
|
153
|
-
|
|
217
|
+
|
|
218
|
+
if (typeFundamental(type) !== TYPE_BOXED) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
|
|
154
222
|
const cls = getWrapperClass(type);
|
|
155
223
|
const boxed = dupBoxedBind(getBoxedTypeName(type))(value) as ExternalObject<Handle> | null;
|
|
224
|
+
|
|
156
225
|
return wrapHandle(boxed, cls);
|
|
157
226
|
}
|
|
158
227
|
|
|
159
228
|
/** Stores a boxed object, or null, into a GValue that holds a boxed type. */
|
|
160
|
-
|
|
229
|
+
function setBoxedValue(value: ExternalObject<Handle>, boxed: object | null): void {
|
|
161
230
|
const name = getBoxedTypeName(getValueType(value));
|
|
162
231
|
setBoxedBind(name)(value, boxed === null ? null : getHandle(boxed));
|
|
163
232
|
}
|
|
164
233
|
|
|
234
|
+
const arrayValueType = (descriptor: ArrayDescriptor): ValueType => {
|
|
235
|
+
if (descriptor.itemDescriptor.kind === "string" && descriptor.arrayKind === "array") {
|
|
236
|
+
return strvValueType;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
throw new Error(`Unsupported array type ${descriptor.arrayKind} of ${descriptor.itemDescriptor.kind}`);
|
|
240
|
+
};
|
|
241
|
+
|
|
165
242
|
const resolveValueType = (descriptor: Descriptor): ValueType => {
|
|
166
243
|
if (descriptor.kind === "biguint64" && "type" in descriptor) {
|
|
167
244
|
return typeValueType;
|
|
168
245
|
}
|
|
246
|
+
|
|
247
|
+
const plain = PLAIN_VALUE_TYPES[descriptor.kind];
|
|
248
|
+
|
|
249
|
+
if (plain !== undefined) {
|
|
250
|
+
return plain;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (!isResolvableDescriptor(descriptor)) {
|
|
254
|
+
throw new Error(`Unsupported type descriptor '${descriptor.kind}'`);
|
|
255
|
+
}
|
|
256
|
+
|
|
169
257
|
switch (descriptor.kind) {
|
|
170
|
-
case "boolean":
|
|
171
|
-
return booleanValueType;
|
|
172
|
-
case "string":
|
|
173
|
-
return stringValueType;
|
|
174
|
-
case "int8":
|
|
175
|
-
case "int16":
|
|
176
|
-
case "int32":
|
|
177
|
-
return intValueType;
|
|
178
|
-
case "uint8":
|
|
179
|
-
case "uint16":
|
|
180
|
-
case "uint32":
|
|
181
|
-
return uintValueType;
|
|
182
|
-
case "int64":
|
|
183
|
-
case "bigint64":
|
|
184
|
-
return int64ValueType;
|
|
185
|
-
case "uint64":
|
|
186
|
-
case "biguint64":
|
|
187
|
-
return uint64ValueType;
|
|
188
|
-
case "float32":
|
|
189
|
-
return floatValueType;
|
|
190
|
-
case "float64":
|
|
191
|
-
return doubleValueType;
|
|
192
|
-
case "object":
|
|
193
|
-
return objectValueType;
|
|
194
258
|
case "enum":
|
|
195
|
-
case "flags":
|
|
259
|
+
case "flags": {
|
|
196
260
|
return enumOrFlagsValueType(resolveType(descriptor.sharedLibrary, descriptor.getTypeFnName));
|
|
197
|
-
|
|
261
|
+
}
|
|
262
|
+
case "boxed": {
|
|
198
263
|
return boxedValueType(resolveBoxedType(descriptor));
|
|
199
|
-
|
|
264
|
+
}
|
|
265
|
+
case "fundamental": {
|
|
200
266
|
return fundamentalValueType(resolveFundamentalType(descriptor));
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
throw new Error(`Unsupported array type ${descriptor.arrayKind} of ${descriptor.itemDescriptor.kind}`);
|
|
206
|
-
default:
|
|
207
|
-
throw new Error(`Unsupported type descriptor '${descriptor.kind}'`);
|
|
267
|
+
}
|
|
268
|
+
case "array": {
|
|
269
|
+
return arrayValueType(descriptor);
|
|
270
|
+
}
|
|
208
271
|
}
|
|
209
272
|
};
|
|
210
273
|
|
|
211
|
-
const
|
|
274
|
+
const wrappedValueGetter = (fundamental: bigint): ValueGetter | undefined => {
|
|
212
275
|
switch (fundamental) {
|
|
213
|
-
case
|
|
214
|
-
return booleanValueType.get;
|
|
215
|
-
case TYPE_GTYPE:
|
|
216
|
-
return typeValueType.get;
|
|
217
|
-
case TYPE_INT:
|
|
218
|
-
return intValueType.get;
|
|
219
|
-
case TYPE_UINT:
|
|
220
|
-
return uintValueType.get;
|
|
221
|
-
case TYPE_INT64:
|
|
222
|
-
return int64ValueType.get;
|
|
223
|
-
case TYPE_UINT64:
|
|
224
|
-
return uint64ValueType.get;
|
|
225
|
-
case TYPE_FLOAT:
|
|
226
|
-
return floatValueType.get;
|
|
227
|
-
case TYPE_DOUBLE:
|
|
228
|
-
return doubleValueType.get;
|
|
229
|
-
case TYPE_ENUM:
|
|
230
|
-
return enumValueType.get;
|
|
231
|
-
case TYPE_FLAGS:
|
|
232
|
-
return flagsValueType.get;
|
|
233
|
-
case TYPE_STRING:
|
|
276
|
+
case TYPE_STRING: {
|
|
234
277
|
return (value) => stringValueType.get(value) ?? null;
|
|
235
|
-
|
|
278
|
+
}
|
|
279
|
+
case TYPE_OBJECT: {
|
|
236
280
|
return (value) => wrapHandle(objectValueType.get(value) as ExternalObject<Handle> | null);
|
|
237
|
-
|
|
281
|
+
}
|
|
282
|
+
case TYPE_PARAM: {
|
|
238
283
|
return (value) =>
|
|
239
284
|
wrapHandle(paramValueType.get(value) as ExternalObject<Handle> | null, getWrapperClass(TYPE_PARAM));
|
|
240
|
-
|
|
285
|
+
}
|
|
286
|
+
case TYPE_VARIANT: {
|
|
241
287
|
return (value) =>
|
|
242
288
|
wrapHandle(variantValueType.get(value) as ExternalObject<Handle> | null, getWrapperClass(TYPE_VARIANT));
|
|
243
|
-
|
|
289
|
+
}
|
|
290
|
+
case TYPE_BOXED: {
|
|
244
291
|
return getBoxedValue;
|
|
245
|
-
|
|
292
|
+
}
|
|
293
|
+
case TYPE_POINTER: {
|
|
246
294
|
return (value) => {
|
|
247
295
|
if (pointerValueType.get(value)) {
|
|
248
296
|
throw new Error("G_TYPE_POINTER non-null values cannot be marshalled to JS");
|
|
249
297
|
}
|
|
298
|
+
|
|
250
299
|
return null;
|
|
251
300
|
};
|
|
252
|
-
|
|
301
|
+
}
|
|
302
|
+
default: {
|
|
253
303
|
return undefined;
|
|
304
|
+
}
|
|
254
305
|
}
|
|
255
306
|
};
|
|
256
307
|
|
|
257
|
-
|
|
308
|
+
function setStringValue(value: ExternalObject<Handle>, nativeValue: unknown): void {
|
|
309
|
+
stringValueType.set(value, nativeValue ?? null);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function handleSetter(target: ValueType): ValueType["set"] {
|
|
313
|
+
return (value, nativeValue) => {
|
|
314
|
+
target.set(value, nativeValue == null ? null : getHandle(nativeValue));
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function setBoxedFromValue(value: ExternalObject<Handle>, nativeValue: unknown): void {
|
|
319
|
+
setBoxedValue(value, nativeValue ?? null);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const resolveValueGetter = (fundamental: bigint): ValueGetter | undefined =>
|
|
323
|
+
PLAIN_VALUE_GETTERS.get(fundamental) ?? wrappedValueGetter(fundamental);
|
|
324
|
+
|
|
325
|
+
const resolveValueSetter = (fundamental: bigint): ValueType["set"] | undefined =>
|
|
326
|
+
PLAIN_VALUE_SETTERS.get(fundamental) ?? WRAPPED_VALUE_SETTERS.get(fundamental);
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Stores a JavaScript value into an already-initialized GValue, converting it to
|
|
330
|
+
* whatever type the GValue holds.
|
|
331
|
+
*
|
|
332
|
+
* @param value The initialized GValue to write into.
|
|
333
|
+
* @param jsValue The JavaScript value to store.
|
|
334
|
+
*/
|
|
335
|
+
function intoValue(value: ExternalObject<Handle>, jsValue: unknown): void {
|
|
336
|
+
const type = getValueType(value);
|
|
337
|
+
const set = resolveValueSetter(typeFundamental(type));
|
|
338
|
+
|
|
339
|
+
if (set === undefined) {
|
|
340
|
+
throw new Error(`Unsupported type for intoValue: ${typeName(type) ?? String(type)}`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
set(value, jsValue);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const resolveNativeValue = (descriptor: Descriptor, value: unknown): unknown => {
|
|
258
347
|
const isHandleKind = descriptor.kind === "object" || descriptor.kind === "boxed";
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
348
|
+
|
|
349
|
+
if (!isHandleKind) {
|
|
350
|
+
return toNative(descriptor, value);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return value == null ? null : getHandle(value);
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const resolveValueGType = (descriptor: Descriptor, nativeValue: unknown): bigint => {
|
|
357
|
+
if (descriptor.kind !== "object") {
|
|
358
|
+
return resolveDescriptorType(descriptor);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return nativeValue == null ? TYPE_OBJECT : getType(nativeValue as ExternalObject<Handle>);
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
function toValue(descriptor: Descriptor, value: unknown): ExternalObject<Handle> {
|
|
365
|
+
const nativeValue = resolveNativeValue(descriptor, value);
|
|
366
|
+
const type = resolveValueGType(descriptor, nativeValue);
|
|
270
367
|
const gValue = newTypedValue(type);
|
|
271
368
|
resolveValueType(descriptor).set(gValue, nativeValue);
|
|
369
|
+
|
|
272
370
|
return gValue;
|
|
273
371
|
}
|
|
274
372
|
|
|
275
|
-
|
|
373
|
+
function fromValue(value: ExternalObject<Handle>): unknown {
|
|
276
374
|
const type = getValueType(value);
|
|
277
|
-
|
|
375
|
+
|
|
376
|
+
if (type === getStrvType()) {
|
|
377
|
+
return strvValueType.get(value);
|
|
378
|
+
}
|
|
379
|
+
|
|
278
380
|
const get = resolveValueGetter(typeFundamental(type));
|
|
381
|
+
|
|
279
382
|
if (get === undefined) {
|
|
280
383
|
throw new Error(`Unsupported type for fromValue: ${typeName(type) ?? String(type)}`);
|
|
281
384
|
}
|
|
385
|
+
|
|
282
386
|
return get(value);
|
|
283
387
|
}
|
|
284
388
|
|
|
285
|
-
|
|
389
|
+
function newValueForDescriptor(descriptor: Descriptor): ExternalObject<Handle> {
|
|
286
390
|
return newTypedValue(resolveDescriptorType(descriptor));
|
|
287
391
|
}
|
|
288
392
|
|
|
289
|
-
|
|
393
|
+
function outValueForDescriptor(
|
|
290
394
|
descriptor: Descriptor,
|
|
291
395
|
initial?: unknown,
|
|
292
396
|
): { value: ExternalObject<Handle>; read: () => unknown } {
|
|
293
397
|
const storage = alloc(8);
|
|
294
398
|
write(storage, uint64T, 0, 0);
|
|
295
|
-
|
|
399
|
+
|
|
400
|
+
if (initial !== undefined) {
|
|
401
|
+
write(storage, descriptor, 0, initial);
|
|
402
|
+
}
|
|
403
|
+
|
|
296
404
|
const value = newTypedValue(TYPE_POINTER);
|
|
297
405
|
pointerValueType.set(value, storage);
|
|
406
|
+
|
|
298
407
|
return { value, read: () => read(storage, descriptor, 0) };
|
|
299
408
|
}
|
|
300
409
|
|
|
301
|
-
|
|
410
|
+
function outValueForBoxedDescriptor(descriptor: Descriptor, boxed: object): ExternalObject<Handle> {
|
|
302
411
|
return newBoxedValue(descriptor, boxed, setBoxedBind);
|
|
303
412
|
}
|
|
304
413
|
|
|
305
|
-
|
|
414
|
+
function inoutValueForBoxedDescriptor(descriptor: Descriptor, boxed: object): ExternalObject<Handle> {
|
|
306
415
|
return newBoxedValue(descriptor, boxed, setStaticBoxedBind);
|
|
307
416
|
}
|
|
417
|
+
|
|
418
|
+
export {
|
|
419
|
+
getValueType,
|
|
420
|
+
intoValue,
|
|
421
|
+
copyValue,
|
|
422
|
+
getBoxedValue,
|
|
423
|
+
setBoxedValue,
|
|
424
|
+
toValue,
|
|
425
|
+
fromValue,
|
|
426
|
+
newValueForDescriptor,
|
|
427
|
+
outValueForDescriptor,
|
|
428
|
+
outValueForBoxedDescriptor,
|
|
429
|
+
inoutValueForBoxedDescriptor,
|
|
430
|
+
};
|