node-ctypes 2.3.0 → 2.5.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/CHANGELOG.md +1003 -0
- package/README.md +483 -103
- package/build/Release/ctypes-darwin-arm64.node +0 -0
- package/build/Release/ctypes-darwin-x64.node +0 -0
- package/build/Release/ctypes-linux-arm64-musl.node +0 -0
- package/build/Release/ctypes-linux-arm64.node +0 -0
- package/build/Release/ctypes-linux-x64-musl.node +0 -0
- package/build/Release/ctypes-linux-x64.node +0 -0
- package/build/Release/ctypes-win32-arm64.node +0 -0
- package/build/Release/ctypes-win32-x64.node +0 -0
- package/lib/core/Library.js +79 -106
- package/lib/core/callback.js +7 -3
- package/lib/core/conversions.js +374 -48
- package/lib/core/funcptr.js +186 -64
- package/lib/core/types.js +7 -2
- package/lib/index.d.ts +139 -61
- package/lib/index.js +10 -8
- package/lib/memory/buffer.js +62 -13
- package/lib/memory/operations.js +4 -6
- package/lib/memory/pointer.js +147 -74
- package/lib/platform/win_types.js +8 -3
- package/lib/structures/Structure.js +60 -45
- package/lib/structures/helpers/array.js +31 -12
- package/lib/structures/helpers/bitfield.js +17 -8
- package/lib/structures/helpers/common.js +223 -29
- package/lib/structures/helpers/layout.js +17 -3
- package/lib/structures/helpers/struct.js +66 -51
- package/lib/structures/helpers/union.js +36 -26
- package/lib/types/SimpleCData.js +11 -32
- package/lib/types/primitives.js +35 -16
- package/lib/utils/cache.js +34 -2
- package/package.json +3 -1
package/lib/core/conversions.js
CHANGED
|
@@ -10,6 +10,115 @@ import { mirrorAddress, mirrorsActive, refresh } from "../memory/mirror.js";
|
|
|
10
10
|
import { ArgumentError } from "../platform/errors.js";
|
|
11
11
|
import { encodeWide } from "../memory/buffer.js";
|
|
12
12
|
import { buildWrapper } from "../structures/helpers/wrap.js";
|
|
13
|
+
import { activeArgumentOwners as argumentOwnerRegistry, byrefOwners, isScalarSubclass, resolveParameter, scalarView, structArgumentFactories } from "../structures/helpers/common.js";
|
|
14
|
+
|
|
15
|
+
const activeArgumentOwners = argumentOwnerRegistry;
|
|
16
|
+
|
|
17
|
+
function retainArgument(owners, value) {
|
|
18
|
+
if (owners !== undefined && value !== null && (typeof value === "object" || typeof value === "function")) owners.push(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function needsArgumentOwners(value) {
|
|
22
|
+
if (value === null || (typeof value !== "object" && typeof value !== "function")) return false;
|
|
23
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
|
|
24
|
+
return "_buffer" in value || "_as_parameter_" in value || (mirrorsActive && mirrorAddress(value) !== undefined);
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function argumentsNeedOwners(args) {
|
|
30
|
+
for (let i = 0; i < args.length; i++) if (needsArgumentOwners(args[i])) return true;
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The general fallback receives the hook snapshot taken before any owner probe.
|
|
35
|
+
export function specializeCall(ffiFunc, method, converters, state, invoke, captureHere = true) {
|
|
36
|
+
const [C0, C1, C2, C3, C4, C5] = converters;
|
|
37
|
+
const hasAdapters = converters.some((convert) => convert.requiresOwners);
|
|
38
|
+
switch (converters.length) {
|
|
39
|
+
case 0:
|
|
40
|
+
return function () {
|
|
41
|
+
const checked = captureHere ? state.errcheck : null;
|
|
42
|
+
if (checked !== null || arguments.length !== 0) return invoke(arguments, checked);
|
|
43
|
+
return ffiFunc[method]();
|
|
44
|
+
};
|
|
45
|
+
case 1:
|
|
46
|
+
return function (a0) {
|
|
47
|
+
const checked = captureHere ? state.errcheck : null;
|
|
48
|
+
if (checked !== null || arguments.length !== 1) return invoke(arguments, checked);
|
|
49
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
50
|
+
return ffiFunc[method](C0(a0));
|
|
51
|
+
};
|
|
52
|
+
case 2:
|
|
53
|
+
return function (a0, a1) {
|
|
54
|
+
const checked = captureHere ? state.errcheck : null;
|
|
55
|
+
if (checked !== null || arguments.length !== 2) return invoke(arguments, checked);
|
|
56
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
57
|
+
return ffiFunc[method](C0(a0), C1(a1));
|
|
58
|
+
};
|
|
59
|
+
case 3:
|
|
60
|
+
return function (a0, a1, a2) {
|
|
61
|
+
const checked = captureHere ? state.errcheck : null;
|
|
62
|
+
if (checked !== null || arguments.length !== 3) return invoke(arguments, checked);
|
|
63
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
64
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2));
|
|
65
|
+
};
|
|
66
|
+
case 4:
|
|
67
|
+
return function (a0, a1, a2, a3) {
|
|
68
|
+
const checked = captureHere ? state.errcheck : null;
|
|
69
|
+
if (checked !== null || arguments.length !== 4) return invoke(arguments, checked);
|
|
70
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
71
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3));
|
|
72
|
+
};
|
|
73
|
+
case 5:
|
|
74
|
+
return function (a0, a1, a2, a3, a4) {
|
|
75
|
+
const checked = captureHere ? state.errcheck : null;
|
|
76
|
+
if (checked !== null || arguments.length !== 5) return invoke(arguments, checked);
|
|
77
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3) || needsArgumentOwners(a4))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
78
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4));
|
|
79
|
+
};
|
|
80
|
+
case 6:
|
|
81
|
+
return function (a0, a1, a2, a3, a4, a5) {
|
|
82
|
+
const checked = captureHere ? state.errcheck : null;
|
|
83
|
+
if (checked !== null || arguments.length !== 6) return invoke(arguments, checked);
|
|
84
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3) || needsArgumentOwners(a4) || needsArgumentOwners(a5)))
|
|
85
|
+
return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
86
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4), C5(a5));
|
|
87
|
+
};
|
|
88
|
+
default:
|
|
89
|
+
return function (...args) {
|
|
90
|
+
const checked = captureHere ? state.errcheck : null;
|
|
91
|
+
return invoke(args, checked);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The registry is a GC root, including while another converter reenters JS.
|
|
97
|
+
export function callWithArgumentOwners(ffiFunc, method, args, converters, finish, resultHandler, checkedCall) {
|
|
98
|
+
const owners = [];
|
|
99
|
+
activeArgumentOwners.add(owners);
|
|
100
|
+
let pending = false;
|
|
101
|
+
try {
|
|
102
|
+
const converted = convertArguments(args, converters, owners);
|
|
103
|
+
const result =
|
|
104
|
+
resultHandler === undefined ? ffiFunc[method](...converted) : checkedCall === undefined ? ffiFunc[method === "callAsync" ? "_callAsyncWithErrcheck" : "_callWithErrcheck"](resultHandler, ...converted) : checkedCall(resultHandler, ...converted);
|
|
105
|
+
if (method === "callAsync") {
|
|
106
|
+
const completed = finish === undefined ? result : result.then((value) => finish(value, args));
|
|
107
|
+
const promise = completed.finally(() => activeArgumentOwners.delete(owners));
|
|
108
|
+
pending = true;
|
|
109
|
+
return promise;
|
|
110
|
+
}
|
|
111
|
+
return finish === undefined ? result : finish(result, args);
|
|
112
|
+
} finally {
|
|
113
|
+
if (!pending) activeArgumentOwners.delete(owners);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function callWithResultHandler(ffiFunc, method, args, converters, handler, retainOwners, checkedCall) {
|
|
118
|
+
if (retainOwners) return callWithArgumentOwners(ffiFunc, method, args, converters, undefined, handler, checkedCall);
|
|
119
|
+
if (checkedCall !== undefined) return checkedCall(handler, ...convertArguments(args, converters));
|
|
120
|
+
return ffiFunc[method === "callAsync" ? "_callAsyncWithErrcheck" : "_callWithErrcheck"](handler, ...convertArguments(args, converters));
|
|
121
|
+
}
|
|
13
122
|
|
|
14
123
|
/**
|
|
15
124
|
* Base conversion of an object argument:
|
|
@@ -20,54 +129,69 @@ import { buildWrapper } from "../structures/helpers/wrap.js";
|
|
|
20
129
|
* original address
|
|
21
130
|
* - anything else unchanged
|
|
22
131
|
*/
|
|
23
|
-
export function coerceObject(a) {
|
|
132
|
+
export function coerceObject(a, owners) {
|
|
133
|
+
return objectMemory(resolveParameter(a, owners), owners);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function objectMemory(a, owners) {
|
|
24
137
|
if (a === null || typeof a !== "object") return a;
|
|
138
|
+
retainArgument(owners, a);
|
|
25
139
|
const b = a._buffer;
|
|
26
140
|
if (b !== undefined) {
|
|
141
|
+
retainArgument(owners, b);
|
|
27
142
|
// Puntatore creato da indirizzo (fromAddress): senza Buffer va passato
|
|
28
143
|
// l'indirizzo, altrimenti al C arriverebbe NULL.
|
|
29
144
|
if (b === null && a._pointerType !== undefined) return a.address;
|
|
30
145
|
if (mirrorsActive) return mirrorAddress(b) ?? b;
|
|
31
146
|
return b;
|
|
32
147
|
}
|
|
33
|
-
|
|
148
|
+
const parameter = a._as_parameter_;
|
|
149
|
+
if (parameter !== undefined) {
|
|
150
|
+
retainArgument(owners, parameter);
|
|
151
|
+
return parameter;
|
|
152
|
+
}
|
|
34
153
|
if (mirrorsActive) return mirrorAddress(a) ?? a;
|
|
35
154
|
return a;
|
|
36
155
|
}
|
|
37
156
|
|
|
38
157
|
// Struct/union passato per valore: il nativo copia i byte del Buffer, che per
|
|
39
158
|
// una copia di memoria nativa vanno prima riletti.
|
|
40
|
-
function coerceStructValue(a) {
|
|
159
|
+
function coerceStructValue(a, owners) {
|
|
41
160
|
if (a !== null && typeof a === "object") {
|
|
161
|
+
retainArgument(owners, a);
|
|
42
162
|
const b = Buffer.isBuffer(a._buffer) ? a._buffer : a;
|
|
43
163
|
if (Buffer.isBuffer(b)) {
|
|
164
|
+
retainArgument(owners, b);
|
|
44
165
|
refresh(b);
|
|
45
166
|
return b;
|
|
46
167
|
}
|
|
47
168
|
}
|
|
48
|
-
return coerceObject(a);
|
|
169
|
+
return coerceObject(a, owners);
|
|
49
170
|
}
|
|
50
171
|
|
|
51
172
|
const isStructType = (t) => t !== null && (typeof t === "function" || typeof t === "object") && (typeof t._buildStruct === "function" || typeof t._buildUnion === "function" || t._isStructType === true);
|
|
52
173
|
|
|
53
174
|
/**
|
|
54
175
|
* Converter for a struct/union passed by value. A plain object (fields by
|
|
55
|
-
* name, a node-ctypes extension)
|
|
56
|
-
*
|
|
57
|
-
*
|
|
176
|
+
* name, a node-ctypes extension) uses the type's field writers. Simple
|
|
177
|
+
* functional structs omit the transient Proxy; other types still construct
|
|
178
|
+
* an instance so custom constructors and complex layouts keep their behavior.
|
|
179
|
+
* An array is rejected, as a Python tuple.
|
|
58
180
|
*/
|
|
59
181
|
function structArgument(T, index) {
|
|
60
|
-
const instance = typeof T === "function" ? (values) => new T(values) : (values) => T.create(values);
|
|
182
|
+
const instance = typeof T === "function" ? (values) => new T(values) : (structArgumentFactories.get(T) ?? ((values) => T.create(values)));
|
|
61
183
|
const coerce = mirrorsActive ? coerceStructValue : coerceObject;
|
|
62
|
-
return (a) => {
|
|
184
|
+
return (a, owners) => {
|
|
185
|
+
a = resolveArgument(a, owners, index);
|
|
186
|
+
retainArgument(owners, a);
|
|
63
187
|
if (Array.isArray(a)) {
|
|
64
188
|
// Python rifiuta anche una tupla.
|
|
65
189
|
throw new ArgumentError(`argument ${index + 1}: TypeError: expected ${T.name || "structure"} instance instead of Array`);
|
|
66
190
|
}
|
|
67
191
|
if (a !== null && typeof a === "object" && a._buffer === undefined && a._as_parameter_ === undefined && !ArrayBuffer.isView(a) && !(a instanceof ArrayBuffer)) {
|
|
68
|
-
return coerce(instance(a));
|
|
192
|
+
return coerce(instance(a), owners);
|
|
69
193
|
}
|
|
70
|
-
return coerce(a);
|
|
194
|
+
return coerce(a, owners);
|
|
71
195
|
};
|
|
72
196
|
}
|
|
73
197
|
|
|
@@ -76,10 +200,11 @@ function structArgument(T, index) {
|
|
|
76
200
|
* instance is passed by value (`abs(c_int(-5))`), other objects as
|
|
77
201
|
* {@link coerceObject}.
|
|
78
202
|
*/
|
|
79
|
-
export function coerceValue(a) {
|
|
80
|
-
if (a === null || typeof a !== "object") return a;
|
|
203
|
+
export function coerceValue(a, owners) {
|
|
204
|
+
if (a === null || (typeof a !== "object" && typeof a !== "function")) return a;
|
|
205
|
+
retainArgument(owners, a);
|
|
81
206
|
if (a._isSimpleCDataInstance === true) return a.value;
|
|
82
|
-
return coerceObject(a);
|
|
207
|
+
return coerceObject(a, owners);
|
|
83
208
|
}
|
|
84
209
|
|
|
85
210
|
/**
|
|
@@ -87,9 +212,11 @@ export function coerceValue(a) {
|
|
|
87
212
|
* argtypes): a SimpleCData instance is passed as is, and the native call uses
|
|
88
213
|
* its type, as Python does (`printf("%f", c_double(2.0))`).
|
|
89
214
|
*/
|
|
90
|
-
export function coerceVariadic(a) {
|
|
215
|
+
export function coerceVariadic(a, owners) {
|
|
216
|
+
a = resolveParameter(a, owners);
|
|
217
|
+
retainArgument(owners, a);
|
|
91
218
|
if (a !== null && typeof a === "object" && a._isSimpleCDataInstance === true) return a;
|
|
92
|
-
return coerceObject(a);
|
|
219
|
+
return coerceObject(a, owners);
|
|
93
220
|
}
|
|
94
221
|
|
|
95
222
|
/**
|
|
@@ -103,42 +230,187 @@ export function coerceVariadic(a) {
|
|
|
103
230
|
* @returns {Function[]} One converter per declared argument
|
|
104
231
|
*/
|
|
105
232
|
export function argumentConverters(argTypes, native) {
|
|
106
|
-
const pointerKinds = new Set([native.CType.POINTER, native.CType.STRING, native.CType.WSTRING]);
|
|
107
233
|
const baseConverter = (t, index) => {
|
|
108
234
|
if (isStructType(t)) return structArgument(t, index);
|
|
109
235
|
if (t !== null && (typeof t === "function" || typeof t === "object") && t._isArrayType === true) return arrayArgument(t, index);
|
|
236
|
+
if (t !== null && typeof t === "object" && t._pointerTo !== undefined) return pointerArgument(t, index, native);
|
|
237
|
+
const kind = typeof t === "function" ? t._type : t;
|
|
238
|
+
if (kind === native.CType.STRING || kind === native.CType.WSTRING) return stringArgument(t, kind, index, native);
|
|
239
|
+
if (kind === native.CType.POINTER) return voidPointerArgument(index, native);
|
|
110
240
|
if (typeof t !== "function" || t._isSimpleCData !== true) return coerceObject;
|
|
111
|
-
|
|
112
|
-
return (a) => {
|
|
113
|
-
if (a === null || typeof a !== "object") return a;
|
|
114
|
-
if (a._isSimpleCDataInstance === true && a instanceof t) return a.value;
|
|
115
|
-
return coerceObject(a);
|
|
116
|
-
};
|
|
241
|
+
return scalarArgument(t, index);
|
|
117
242
|
};
|
|
118
243
|
return argTypes.map((t, index) => {
|
|
119
244
|
const base = baseConverter(t, index);
|
|
120
|
-
if (t === null || (typeof t !== "function" && typeof t !== "object")
|
|
121
|
-
|
|
245
|
+
if (t === null || (typeof t !== "function" && typeof t !== "object")) return base;
|
|
246
|
+
const fromParam = t.from_param;
|
|
247
|
+
if (fromParam === undefined) return base;
|
|
248
|
+
return fromParamArgument(t, base.fromParam ?? base, index, fromParam);
|
|
122
249
|
});
|
|
123
250
|
}
|
|
124
251
|
|
|
252
|
+
function pointerValue(value, native) {
|
|
253
|
+
const buffer = value._buffer;
|
|
254
|
+
if (mirrorsActive) refresh(buffer);
|
|
255
|
+
return native.POINTER_SIZE === 8 ? buffer.readBigUInt64LE(0) : BigInt(buffer.readUInt32LE(0));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function voidPointerArgument(index, native) {
|
|
259
|
+
return (value, owners) => {
|
|
260
|
+
value = resolveArgument(value, owners, index);
|
|
261
|
+
if (typeof value === "number" && !Number.isInteger(value)) {
|
|
262
|
+
throw new ArgumentError(`argument ${index + 1}: TypeError: 'float' object cannot be interpreted as ctypes.c_void_p`);
|
|
263
|
+
}
|
|
264
|
+
if (value?._isSimpleCDataInstance === true && value.constructor._isPointerValued === true) {
|
|
265
|
+
retainArgument(owners, value);
|
|
266
|
+
return pointerValue(value, native);
|
|
267
|
+
}
|
|
268
|
+
return objectMemory(value, owners);
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function stringArgument(T, kind, index, native) {
|
|
273
|
+
const wide = kind === native.CType.WSTRING;
|
|
274
|
+
const charKind = wide ? "wchar" : "char";
|
|
275
|
+
const typeName = wide ? "c_wchar_p" : "c_char_p";
|
|
276
|
+
const fail = (value) => {
|
|
277
|
+
const name =
|
|
278
|
+
typeof value === "bigint" || (typeof value === "number" && Number.isInteger(value)) ? "int" : typeof value === "number" ? "float" : typeof value === "boolean" ? "bool" : value?.constructor?.name === "Object" ? "object" : typeof value;
|
|
279
|
+
throw new ArgumentError(`argument ${index + 1}: TypeError: '${name}' object cannot be interpreted as ctypes.${typeName}`);
|
|
280
|
+
};
|
|
281
|
+
const convert = (value, owners) => {
|
|
282
|
+
value = resolveArgument(value, owners, index);
|
|
283
|
+
if (value === null || value === undefined || typeof value === "string") return value;
|
|
284
|
+
retainArgument(owners, value);
|
|
285
|
+
if (value?._isSimpleCDataInstance === true) {
|
|
286
|
+
if (typeof T === "function" ? !(value instanceof T) : value.constructor._type !== kind) fail(value);
|
|
287
|
+
return pointerValue(value, native);
|
|
288
|
+
}
|
|
289
|
+
const element = value?._pointerType?._pointerTo ?? value?._arrayType?.elementType;
|
|
290
|
+
if (element !== undefined) {
|
|
291
|
+
if (element._charKind !== charKind) fail(value);
|
|
292
|
+
return objectMemory(value, owners);
|
|
293
|
+
}
|
|
294
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return objectMemory(value, owners);
|
|
295
|
+
fail(value);
|
|
296
|
+
};
|
|
297
|
+
// A custom from_param result is already adapted and may be a c_void_p.
|
|
298
|
+
Object.defineProperty(convert, "fromParam", {
|
|
299
|
+
value: (value, owners) => {
|
|
300
|
+
value = resolveArgument(value, owners, index);
|
|
301
|
+
if (value?._isSimpleCDataInstance === true) {
|
|
302
|
+
retainArgument(owners, value);
|
|
303
|
+
return value.constructor._isPointerValued === true ? pointerValue(value, native) : value.value;
|
|
304
|
+
}
|
|
305
|
+
return objectMemory(value, owners);
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
return convert;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function scalarArgument(T, index) {
|
|
312
|
+
const boolean = T._isBoolean === true;
|
|
313
|
+
return (value, owners) => {
|
|
314
|
+
if (value !== null && (typeof value === "object" || typeof value === "function")) value = resolveArgument(value, owners, index);
|
|
315
|
+
if (value === null || typeof value !== "object") return value;
|
|
316
|
+
retainArgument(owners, value);
|
|
317
|
+
if (value._isSimpleCDataInstance === true) {
|
|
318
|
+
if (!boolean && !(value instanceof T)) {
|
|
319
|
+
throw new ArgumentError(`argument ${index + 1}: TypeError: incompatible types, ${value.constructor.name} instance instead of ${T.name} instance`);
|
|
320
|
+
}
|
|
321
|
+
return value.value;
|
|
322
|
+
}
|
|
323
|
+
return objectMemory(value, owners);
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function resolveArgument(value, owners, index) {
|
|
328
|
+
try {
|
|
329
|
+
return resolveParameter(value, owners);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
throw new ArgumentError(`argument ${index + 1}: ${error?.name ?? "Error"}: ${error?.message ?? String(error)}`, { cause: error });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const compatibleType = (actual, expected) => actual === expected || (typeof actual === "function" && typeof expected === "function" && actual.prototype instanceof expected);
|
|
336
|
+
|
|
337
|
+
function pointerArgument(T, index, native) {
|
|
338
|
+
const base = T._pointerTo;
|
|
339
|
+
const fail = () => {
|
|
340
|
+
throw new ArgumentError(`argument ${index + 1}: TypeError: incompatible pointer, expected POINTER(${base.name ?? "type"})`);
|
|
341
|
+
};
|
|
342
|
+
const convert = (value, owners) => {
|
|
343
|
+
const argument = value;
|
|
344
|
+
value = resolveArgument(value, owners, index);
|
|
345
|
+
const direct = value === argument;
|
|
346
|
+
// CPython's implicit byref, array decay and NULL conversions apply only to the original argument.
|
|
347
|
+
if (!direct && value === null) fail();
|
|
348
|
+
if (value === null || value === undefined || typeof value === "bigint") return value;
|
|
349
|
+
if (typeof value === "number") {
|
|
350
|
+
if (!Number.isInteger(value)) fail();
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
if (typeof value === "string" && base._charKind === "char") return value;
|
|
354
|
+
if (typeof value === "string" && base._charKind === "wchar") {
|
|
355
|
+
const buffer = Buffer.concat([encodeWide(value, native.WCHAR_SIZE), Buffer.alloc(native.WCHAR_SIZE)]);
|
|
356
|
+
retainArgument(owners, buffer);
|
|
357
|
+
return buffer;
|
|
358
|
+
}
|
|
359
|
+
if (typeof base === "function" && value instanceof base) {
|
|
360
|
+
if (!direct) fail();
|
|
361
|
+
return objectMemory(value, owners);
|
|
362
|
+
}
|
|
363
|
+
const reference = value !== null && (typeof value === "object" || typeof value === "function") ? byrefOwners.get(value) : undefined;
|
|
364
|
+
if (reference !== undefined) {
|
|
365
|
+
if (reference.type !== undefined && !compatibleType(reference.type, base)) fail();
|
|
366
|
+
return objectMemory(value, owners);
|
|
367
|
+
}
|
|
368
|
+
if (value?._pointerType !== undefined) {
|
|
369
|
+
if (!compatibleType(value._pointerType._pointerTo, base)) fail();
|
|
370
|
+
return objectMemory(value, owners);
|
|
371
|
+
}
|
|
372
|
+
if (value?._arrayType !== undefined) {
|
|
373
|
+
if (!direct) fail();
|
|
374
|
+
if ((!compatibleType(value._arrayType, base) && !compatibleType(value._arrayType.elementType, base)) || value._arrayType._swapped_) fail();
|
|
375
|
+
return coerceObject(value, owners);
|
|
376
|
+
}
|
|
377
|
+
if (value?._isSimpleCDataInstance === true || value?._structDef !== undefined) {
|
|
378
|
+
if (typeof base === "function" && value instanceof base) return objectMemory(value, owners);
|
|
379
|
+
const kind = value.constructor?._type;
|
|
380
|
+
if ((base._charKind === "char" && kind === native.CType.STRING) || (base._charKind === "wchar" && kind === native.CType.WSTRING)) {
|
|
381
|
+
if (mirrorsActive) refresh(value._buffer);
|
|
382
|
+
return native.POINTER_SIZE === 8 ? value._buffer.readBigUInt64LE(0) : BigInt(value._buffer.readUInt32LE(0));
|
|
383
|
+
}
|
|
384
|
+
fail();
|
|
385
|
+
}
|
|
386
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || Buffer.isBuffer(value?._buffer)) return objectMemory(value, owners);
|
|
387
|
+
fail();
|
|
388
|
+
};
|
|
389
|
+
if (base._charKind === "wchar") Object.defineProperty(convert, "requiresOwners", { value: true });
|
|
390
|
+
return convert;
|
|
391
|
+
}
|
|
392
|
+
|
|
125
393
|
/**
|
|
126
394
|
* Converter for an argtype with a `from_param` method (Python protocol): it
|
|
127
395
|
* receives the argument as passed, its result (or the result's
|
|
128
396
|
* `_as_parameter_`) is converted as the declared type. Its errors become
|
|
129
397
|
* ArgumentError "argument N: <name>: <message>", as in Python.
|
|
130
398
|
*/
|
|
131
|
-
function fromParamArgument(T, base, index) {
|
|
132
|
-
|
|
399
|
+
function fromParamArgument(T, base, index, fromParam) {
|
|
400
|
+
const converter = (a, owners) => {
|
|
401
|
+
retainArgument(owners, a);
|
|
133
402
|
let converted;
|
|
134
403
|
try {
|
|
135
|
-
converted =
|
|
404
|
+
converted = Reflect.apply(fromParam, T, [a]);
|
|
136
405
|
} catch (e) {
|
|
137
406
|
if (e instanceof ArgumentError) throw e;
|
|
138
407
|
throw new ArgumentError(`argument ${index + 1}: ${e?.name ?? "Error"}: ${e?.message ?? String(e)}`, { cause: e });
|
|
139
408
|
}
|
|
140
|
-
|
|
409
|
+
retainArgument(owners, converted);
|
|
410
|
+
return base(converted, owners);
|
|
141
411
|
};
|
|
412
|
+
Object.defineProperty(converter, "requiresOwners", { value: true });
|
|
413
|
+
return converter;
|
|
142
414
|
}
|
|
143
415
|
|
|
144
416
|
const arrayTypeName = (T) => `${T.elementType?.name || "array"} * ${T.length}`;
|
|
@@ -161,15 +433,19 @@ function describeValue(a) {
|
|
|
161
433
|
* @returns {Function} Converter
|
|
162
434
|
*/
|
|
163
435
|
function arrayArgument(T, index) {
|
|
164
|
-
return (a) => {
|
|
436
|
+
return (a, owners) => {
|
|
437
|
+
a = resolveArgument(a, owners, index);
|
|
438
|
+
retainArgument(owners, a);
|
|
165
439
|
if (a !== null && typeof a === "object") {
|
|
166
440
|
const arrayType = a._arrayType;
|
|
167
441
|
if (arrayType !== undefined) {
|
|
168
|
-
if (arrayType === T || (arrayType.elementType === T.elementType && arrayType.length === T.length)) return coerceObject(a);
|
|
442
|
+
if (arrayType === T || (arrayType.elementType === T.elementType && arrayType.length === T.length)) return coerceObject(a, owners);
|
|
169
443
|
} else if (ArrayBuffer.isView(a) || a instanceof ArrayBuffer) {
|
|
170
|
-
return coerceObject(a);
|
|
444
|
+
return coerceObject(a, owners);
|
|
171
445
|
} else if (a._as_parameter_ !== undefined) {
|
|
172
|
-
|
|
446
|
+
const parameter = a._as_parameter_;
|
|
447
|
+
retainArgument(owners, parameter);
|
|
448
|
+
return parameter;
|
|
173
449
|
}
|
|
174
450
|
}
|
|
175
451
|
throw new ArgumentError(`argument ${index + 1}: TypeError: expected ${arrayTypeName(T)} instance instead of ${describeValue(a)}`);
|
|
@@ -180,16 +456,22 @@ function arrayArgument(T, index) {
|
|
|
180
456
|
* Convert all call arguments: declared ones with their converter, the rest
|
|
181
457
|
* (variadic or without argtypes) with the Python default conversion.
|
|
182
458
|
*/
|
|
183
|
-
export function convertArguments(args, converters) {
|
|
459
|
+
export function convertArguments(args, converters, owners) {
|
|
184
460
|
const n = args.length;
|
|
185
461
|
const declared = converters.length;
|
|
186
462
|
const out = new Array(n);
|
|
187
463
|
for (let i = 0; i < n; i++) {
|
|
188
|
-
out[i] = i < declared ? converters[i](args[i]) : coerceVariadic(args[i]);
|
|
464
|
+
out[i] = i < declared ? converters[i](args[i], owners) : coerceVariadic(args[i], owners);
|
|
189
465
|
}
|
|
190
466
|
return out;
|
|
191
467
|
}
|
|
192
468
|
|
|
469
|
+
function copyResultBytes(result) {
|
|
470
|
+
const bytes = Buffer.alloc(result.length);
|
|
471
|
+
bytes.set(result);
|
|
472
|
+
return bytes;
|
|
473
|
+
}
|
|
474
|
+
|
|
193
475
|
/**
|
|
194
476
|
* Python restype conversion done in JS: `POINTER(T)` returns a pointer object
|
|
195
477
|
* (`null` for NULL), a CFUNCTYPE type returns a callable. Undefined when the
|
|
@@ -199,29 +481,50 @@ export function convertArguments(args, converters) {
|
|
|
199
481
|
* @returns {Function|undefined} Converter from the native result
|
|
200
482
|
*/
|
|
201
483
|
export function resultConverter(restype) {
|
|
484
|
+
if (isScalarSubclass(restype)) return (bytes) => scalarView(restype, bytes);
|
|
485
|
+
if (restype?._isVariantBool === true) return (result) => result !== 0;
|
|
202
486
|
if (restype !== null && typeof restype === "object" && restype._pointerTo !== undefined) {
|
|
203
487
|
return (r) => (r === null || r === 0n ? null : restype.fromAddress(r));
|
|
204
488
|
}
|
|
205
489
|
if (typeof restype === "function" && restype._isFuncPtr === true) {
|
|
206
490
|
return (r) => (r === null || r === 0n ? null : restype(r));
|
|
207
491
|
}
|
|
208
|
-
//
|
|
209
|
-
//
|
|
210
|
-
// a ogni chiamata, copiato qui nella memoria dell'istanza. Python:
|
|
211
|
-
// un'istanza della classe.
|
|
492
|
+
// Snapshot before constructors/readers can reenter: toObject may keep
|
|
493
|
+
// array views or union getters over the native function's reusable buffer.
|
|
212
494
|
if (typeof restype === "function" && typeof restype._buildStruct === "function") {
|
|
213
|
-
return (r) =>
|
|
214
|
-
const bytes = Buffer.alloc(r.length);
|
|
215
|
-
bytes.set(r);
|
|
216
|
-
return new restype(bytes);
|
|
217
|
-
};
|
|
495
|
+
return (r) => new restype(copyResultBytes(r));
|
|
218
496
|
}
|
|
219
497
|
if (restype !== null && typeof restype === "object" && restype._isStructType === true) {
|
|
220
|
-
return (r) => restype.toObject(r);
|
|
498
|
+
return (r) => restype.toObject(copyResultBytes(r));
|
|
221
499
|
}
|
|
222
500
|
return undefined;
|
|
223
501
|
}
|
|
224
502
|
|
|
503
|
+
export function scalarResult(restype) {
|
|
504
|
+
return isScalarSubclass(restype);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export function resultCheck(restype, name, loader = "FFI") {
|
|
508
|
+
const check = restype?._check_retval_;
|
|
509
|
+
const hresult = restype?._isHResult === true;
|
|
510
|
+
if (!hresult && check === undefined) return undefined;
|
|
511
|
+
return (result) => {
|
|
512
|
+
if (check !== undefined && typeof check !== "function") throw new TypeError("_check_retval_ must be callable");
|
|
513
|
+
if (hresult) {
|
|
514
|
+
const value = result?._isSimpleCDataInstance === true ? result.value : result;
|
|
515
|
+
const number = typeof value === "bigint" ? Number(value) : value;
|
|
516
|
+
if (typeof number === "number" && number < 0) {
|
|
517
|
+
const hex = (number >>> 0).toString(16).padStart(8, "0").toUpperCase();
|
|
518
|
+
const error = new Error(`${loader} '${name}' failed with HRESULT 0x${hex}`);
|
|
519
|
+
error.hresult = number;
|
|
520
|
+
error.winerror = number >>> 0;
|
|
521
|
+
throw error;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return check === undefined ? result : check.call(restype, result);
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
225
528
|
/**
|
|
226
529
|
* True when `restype` is a struct or union returned by value: the native
|
|
227
530
|
* function must then return its bytes (FFIFunction option `struct_buffer`),
|
|
@@ -245,13 +548,38 @@ export function structResult(restype) {
|
|
|
245
548
|
* @returns {Function} Function to register with the native callback
|
|
246
549
|
*/
|
|
247
550
|
export function adaptCallbackFunction(fn, argTypes, returnType, native) {
|
|
551
|
+
const scalarTypes = argTypes.map((type) => (isScalarSubclass(type) ? type : null));
|
|
552
|
+
const hasScalars = scalarTypes.some((type) => type !== null);
|
|
248
553
|
const pointerTypes = argTypes.map((t) => (t !== null && typeof t === "object" && t._pointerTo !== undefined ? t : null));
|
|
249
554
|
const hasPointers = pointerTypes.some((t) => t !== null);
|
|
250
555
|
// Struct/union per valore: il nativo passa una copia in un Buffer.
|
|
251
556
|
const structViews = argTypes.map(structView);
|
|
252
557
|
const hasStructs = structViews.some((view) => view !== null);
|
|
558
|
+
const variantArgs = argTypes.map((type) => type?._isVariantBool === true);
|
|
559
|
+
const hasVariants = variantArgs.some(Boolean);
|
|
560
|
+
const variantResult = returnType?._isVariantBool === true;
|
|
253
561
|
const toResult = stringResult(returnType, native);
|
|
562
|
+
const convertResult = (result) => {
|
|
563
|
+
if (variantResult) return returnType.from_param(result);
|
|
564
|
+
if (toResult !== undefined && typeof result === "string") return toResult(result);
|
|
565
|
+
return result !== null && (typeof result === "object" || typeof result === "function") ? coerceValue(result) : result;
|
|
566
|
+
};
|
|
567
|
+
if (!hasScalars && !hasVariants && !hasPointers && !hasStructs) {
|
|
568
|
+
return function (...args) {
|
|
569
|
+
return convertResult(fn.apply(this, args));
|
|
570
|
+
};
|
|
571
|
+
}
|
|
254
572
|
return function (...args) {
|
|
573
|
+
if (hasScalars) {
|
|
574
|
+
for (let i = 0; i < scalarTypes.length; i++) {
|
|
575
|
+
if (scalarTypes[i] !== null) args[i] = scalarView(scalarTypes[i], args[i]);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
if (hasVariants) {
|
|
579
|
+
for (let i = 0; i < variantArgs.length; i++) {
|
|
580
|
+
if (variantArgs[i] && scalarTypes[i] === null) args[i] = args[i] !== 0;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
255
583
|
if (hasPointers) {
|
|
256
584
|
for (let i = 0; i < pointerTypes.length; i++) {
|
|
257
585
|
const T = pointerTypes[i];
|
|
@@ -265,9 +593,7 @@ export function adaptCallbackFunction(fn, argTypes, returnType, native) {
|
|
|
265
593
|
if (structViews[i] !== null) args[i] = structViews[i](args[i]);
|
|
266
594
|
}
|
|
267
595
|
}
|
|
268
|
-
|
|
269
|
-
if (toResult !== undefined && typeof result === "string") return toResult(result);
|
|
270
|
-
return result !== null && typeof result === "object" ? coerceValue(result) : result;
|
|
596
|
+
return convertResult(fn.apply(this, args));
|
|
271
597
|
};
|
|
272
598
|
}
|
|
273
599
|
|