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
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/core/Library.js
CHANGED
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
*/
|
|
41
41
|
|
|
42
42
|
import { _toNativeReturnType } from "./types.js";
|
|
43
|
-
import { adaptCallbackFunction, argumentConverters, convertArguments, resultConverter, structResult } from "./conversions.js";
|
|
43
|
+
import { adaptCallbackFunction, argumentConverters, argumentsNeedOwners, callWithArgumentOwners, callWithResultHandler, convertArguments, resultCheck, resultConverter, scalarResult, specializeCall, structResult } from "./conversions.js";
|
|
44
|
+
import { createTypeKey } from "../utils/cache.js";
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
47
|
* Creates the Library-related classes with required dependencies injected.
|
|
@@ -54,24 +55,11 @@ import { adaptCallbackFunction, argumentConverters, convertArguments, resultConv
|
|
|
54
55
|
* @private
|
|
55
56
|
*/
|
|
56
57
|
export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNativeTypes, native) {
|
|
57
|
-
|
|
58
|
-
// un id stabile; `${tipo}` darebbe "[object Object]" per tutti i POINTER.
|
|
59
|
-
const typeIds = new WeakMap();
|
|
58
|
+
const typeKey = createTypeKey();
|
|
60
59
|
// Funzione passata a errcheck quando la chiamata passa da un attributo
|
|
61
60
|
// della libreria (FunctionWrapper).
|
|
62
61
|
const kErrcheckFunc = Symbol("errcheckFunc");
|
|
63
62
|
|
|
64
|
-
let nextTypeId = 0;
|
|
65
|
-
const typeKey = (t) => {
|
|
66
|
-
if (t === null || (typeof t !== "object" && typeof t !== "function")) return String(t);
|
|
67
|
-
let id = typeIds.get(t);
|
|
68
|
-
if (id === undefined) {
|
|
69
|
-
id = `#${++nextTypeId}`;
|
|
70
|
-
typeIds.set(t, id);
|
|
71
|
-
}
|
|
72
|
-
return id;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
63
|
// Chiave di cache delle opzioni di func() (abi, ...), indipendente dall'ordine.
|
|
76
64
|
const optionsKey = (options) => {
|
|
77
65
|
let key = "";
|
|
@@ -84,7 +72,10 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
84
72
|
class FunctionWrapper {
|
|
85
73
|
constructor(cdll, name) {
|
|
86
74
|
const argtypes = [];
|
|
75
|
+
let declaredArgtypes = [];
|
|
76
|
+
let converters = [];
|
|
87
77
|
let restype = undefined;
|
|
78
|
+
let resultSignature;
|
|
88
79
|
let errcheck = null;
|
|
89
80
|
let cachedFunc = null;
|
|
90
81
|
|
|
@@ -94,7 +85,9 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
94
85
|
// argtypes / restype, come in Python.
|
|
95
86
|
const resolve = () => {
|
|
96
87
|
if (!cachedFunc) {
|
|
97
|
-
|
|
88
|
+
const returnType = restype === undefined ? cdll._defaultRestype() : restype;
|
|
89
|
+
const signature = resultSignature ?? { represent: resultConverter(returnType), check: cdll._resultCheck(returnType, name) };
|
|
90
|
+
cachedFunc = cdll._createFunction(name, returnType, declaredArgtypes, {}, { ...signature, converters });
|
|
98
91
|
Object.defineProperty(cachedFunc, kErrcheckFunc, { value: func });
|
|
99
92
|
if (errcheck !== null) {
|
|
100
93
|
cachedFunc.errcheck = errcheck;
|
|
@@ -108,18 +101,26 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
108
101
|
const func = (...args) => (cachedFunc ?? resolve())(...args);
|
|
109
102
|
|
|
110
103
|
Object.defineProperties(func, {
|
|
104
|
+
address: { get: () => resolve().address },
|
|
105
|
+
_as_parameter_: { get: () => resolve().address },
|
|
111
106
|
argtypes: {
|
|
112
107
|
get: () => argtypes,
|
|
113
108
|
set: (value) => {
|
|
114
109
|
// Python: argtypes = None toglie i tipi degli argomenti.
|
|
115
|
-
|
|
110
|
+
const types = [...(value ?? [])];
|
|
111
|
+
const next = argumentConverters(types, native);
|
|
112
|
+
argtypes.splice(0, argtypes.length, ...types);
|
|
113
|
+
declaredArgtypes = types;
|
|
114
|
+
converters = next;
|
|
116
115
|
cachedFunc = null; // Invalidate cache
|
|
117
116
|
},
|
|
118
117
|
configurable: true,
|
|
119
118
|
},
|
|
120
119
|
restype: {
|
|
121
|
-
get: () => (restype === undefined ?
|
|
120
|
+
get: () => (restype === undefined ? cdll._defaultRestype() : restype),
|
|
122
121
|
set: (value) => {
|
|
122
|
+
const returnType = value === undefined ? cdll._defaultRestype() : value;
|
|
123
|
+
resultSignature = { represent: resultConverter(returnType), check: cdll._resultCheck(returnType, name) };
|
|
123
124
|
restype = value;
|
|
124
125
|
cachedFunc = null; // Invalidate cache
|
|
125
126
|
},
|
|
@@ -128,6 +129,7 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
128
129
|
errcheck: {
|
|
129
130
|
get: () => errcheck,
|
|
130
131
|
set: (value) => {
|
|
132
|
+
if (value !== null && value !== undefined && typeof value !== "function") throw new TypeError("errcheck must be a function or null");
|
|
131
133
|
errcheck = value ?? null;
|
|
132
134
|
if (cachedFunc) {
|
|
133
135
|
cachedFunc.errcheck = errcheck;
|
|
@@ -196,7 +198,11 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
196
198
|
* @param {Object} [options] - Opzioni aggiuntive (es. { abi: 'stdcall' })
|
|
197
199
|
* @returns {Function} Funzione callable
|
|
198
200
|
*/
|
|
199
|
-
|
|
201
|
+
_defaultRestype() {
|
|
202
|
+
return FunctionWrapper.defaultRestype;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
func(name, returnType = this._defaultRestype(), argTypes = [], options = {}) {
|
|
200
206
|
const cacheKey = `${name}:${typeKey(returnType)}:${argTypes.map(typeKey).join(",")}${optionsKey(options)}`;
|
|
201
207
|
let fn = this._cache.get(cacheKey);
|
|
202
208
|
if (fn === undefined) {
|
|
@@ -211,97 +217,71 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
211
217
|
* restype, es. HRESULT in OleDLL). undefined se non c'è.
|
|
212
218
|
* @protected
|
|
213
219
|
*/
|
|
214
|
-
_resultCheck(
|
|
215
|
-
return
|
|
220
|
+
_resultCheck(returnType, name) {
|
|
221
|
+
return resultCheck(returnType, name, this.constructor.name);
|
|
216
222
|
}
|
|
217
223
|
|
|
218
224
|
/**
|
|
219
225
|
* Crea una nuova funzione, senza cache (vedi func()).
|
|
220
226
|
* @protected
|
|
221
227
|
*/
|
|
222
|
-
_createFunction(name, returnType, argTypes = [], options = {}) {
|
|
228
|
+
_createFunction(name, returnType, argTypes = [], options = {}, signature) {
|
|
223
229
|
// Propagate library-level use_last_error / use_errno to the FFIFunction
|
|
224
230
|
const mergedOptions = {
|
|
225
231
|
...options,
|
|
226
232
|
...(this._use_last_error ? { use_last_error: true } : {}),
|
|
227
233
|
...(this._use_errno ? { use_errno: true } : {}),
|
|
228
234
|
...(structResult(returnType) ? { struct_buffer: true } : {}),
|
|
235
|
+
...(scalarResult(returnType) ? { scalar_buffer: true } : {}),
|
|
229
236
|
};
|
|
230
|
-
const ffiFunc = this._lib.func(name, _toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native), mergedOptions);
|
|
237
|
+
const ffiFunc = this._lib.func(name, signature?.nativeRestype ?? _toNativeReturnType(returnType, native), signature?.nativeArgtypes ?? _toNativeTypes(argTypes, native), mergedOptions);
|
|
231
238
|
|
|
232
239
|
// =========================================================================
|
|
233
240
|
// OPTIMIZATION: Create specialized wrapper based on argument types
|
|
234
241
|
// For primitive-only args, bypass the processing loop entirely
|
|
235
242
|
// =========================================================================
|
|
236
243
|
|
|
237
|
-
const argCount = argTypes.length;
|
|
238
|
-
|
|
239
244
|
// Conversione degli argomenti come il from_param di base di Python
|
|
240
245
|
// (vedi conversions.js): un convertitore per posizione dichiarata.
|
|
241
|
-
const C = argumentConverters(argTypes, native);
|
|
242
|
-
const [C0, C1, C2, C3, C4, C5] = C;
|
|
246
|
+
const C = signature?.converters ?? argumentConverters(argTypes, native);
|
|
243
247
|
const convertAll = (args) => convertArguments(args, C);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return function (a0, a1, a2) {
|
|
263
|
-
return arguments.length === 3 ? ffiFunc[method](C0(a0), C1(a1), C2(a2)) : ffiFunc[method](...convertAll(arguments));
|
|
264
|
-
};
|
|
265
|
-
case 4:
|
|
266
|
-
return function (a0, a1, a2, a3) {
|
|
267
|
-
return arguments.length === 4 ? ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3)) : ffiFunc[method](...convertAll(arguments));
|
|
268
|
-
};
|
|
269
|
-
case 5:
|
|
270
|
-
return function (a0, a1, a2, a3, a4) {
|
|
271
|
-
return arguments.length === 5 ? ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4)) : ffiFunc[method](...convertAll(arguments));
|
|
272
|
-
};
|
|
273
|
-
case 6:
|
|
274
|
-
return function (a0, a1, a2, a3, a4, a5) {
|
|
275
|
-
return arguments.length === 6 ? ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4), C5(a5)) : ffiFunc[method](...convertAll(arguments));
|
|
276
|
-
};
|
|
277
|
-
default:
|
|
278
|
-
return function (...args) {
|
|
279
|
-
return ffiFunc[method](...convertAll(args));
|
|
280
|
-
};
|
|
281
|
-
}
|
|
248
|
+
const hasAdapters = C.some((convert) => convert.requiresOwners);
|
|
249
|
+
const baseConvert = signature === undefined ? resultConverter(returnType) : signature.represent;
|
|
250
|
+
const check = signature === undefined ? this._resultCheck(returnType, name) : signature.check;
|
|
251
|
+
const convertResult = check === undefined ? baseConvert : baseConvert === undefined ? check : (result) => check(baseConvert(result));
|
|
252
|
+
const captureHere = convertResult === undefined;
|
|
253
|
+
const callState = { errcheck: null };
|
|
254
|
+
let checkedSync;
|
|
255
|
+
const checkedCall = (method, args, checked) => {
|
|
256
|
+
const originalArgs = Array.isArray(args) ? args : Array.prototype.slice.call(args);
|
|
257
|
+
const func = errcheckFunc();
|
|
258
|
+
const finish = (result) => checked(convertResult === undefined ? result : convertResult(result), func, originalArgs);
|
|
259
|
+
const call = method === "call" ? (checkedSync ??= ffiFunc._makeCheckedCall()) : undefined;
|
|
260
|
+
return callWithResultHandler(ffiFunc, method, originalArgs, C, finish, hasAdapters || argumentsNeedOwners(originalArgs), call);
|
|
261
|
+
};
|
|
262
|
+
const ownedCall = (method, args) => callWithArgumentOwners(ffiFunc, method, args, C);
|
|
263
|
+
const invoke = (method, args, checked) => {
|
|
264
|
+
if (captureHere && checked !== null) return checkedCall(method, args, checked);
|
|
265
|
+
return captureHere && (hasAdapters || argumentsNeedOwners(args)) ? ownedCall(method, args) : ffiFunc[method](...convertAll(args));
|
|
282
266
|
};
|
|
283
267
|
|
|
284
|
-
let callMethod =
|
|
285
|
-
let asyncCallMethod =
|
|
268
|
+
let callMethod = specializeCall(ffiFunc, "call", C, callState, (args, checked) => invoke("call", args, checked), captureHere);
|
|
269
|
+
let asyncCallMethod = specializeCall(ffiFunc, "callAsync", C, callState, (args, checked) => invoke("callAsync", args, checked), captureHere);
|
|
286
270
|
|
|
287
|
-
//
|
|
288
|
-
//
|
|
289
|
-
// in questo caso errcheck è applicato qui invece che nel nativo.
|
|
290
|
-
const baseConvert = resultConverter(returnType);
|
|
291
|
-
const check = this._resultCheck(returnType, name);
|
|
292
|
-
const convertResult = check === undefined ? baseConvert : baseConvert === undefined ? check : (result) => check(baseConvert(result));
|
|
293
|
-
let jsErrcheck = null;
|
|
271
|
+
// Result handlers run inside the native call scope, after conversion and
|
|
272
|
+
// with the hook/arguments captured before any argument adapter executes.
|
|
294
273
|
if (convertResult !== undefined) {
|
|
295
274
|
const rawCall = callMethod;
|
|
296
275
|
const rawAsync = asyncCallMethod;
|
|
297
|
-
const finish = (result
|
|
298
|
-
const converted = convertResult(result);
|
|
299
|
-
return jsErrcheck === null ? converted : jsErrcheck(converted, errcheckFunc(), args);
|
|
300
|
-
};
|
|
276
|
+
const finish = (result) => convertResult(result);
|
|
301
277
|
callMethod = function (...args) {
|
|
278
|
+
if (callState.errcheck !== null) return checkedCall("call", args, callState.errcheck);
|
|
279
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "call", args, C, finish);
|
|
302
280
|
return finish(rawCall.apply(this, args), args);
|
|
303
281
|
};
|
|
304
282
|
asyncCallMethod = function (...args) {
|
|
283
|
+
if (callState.errcheck !== null) return checkedCall("callAsync", args, callState.errcheck);
|
|
284
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, C, finish);
|
|
305
285
|
return rawAsync.apply(this, args).then((result) => finish(result, args));
|
|
306
286
|
};
|
|
307
287
|
}
|
|
@@ -330,21 +310,18 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
330
310
|
Object.defineProperties(callMethod, {
|
|
331
311
|
funcName: { value: name },
|
|
332
312
|
address: { value: ffiFunc.address },
|
|
313
|
+
_as_parameter_: { value: ffiFunc.address },
|
|
333
314
|
_ffi: { value: ffiFunc },
|
|
334
315
|
callAsync: { value: asyncCallMethod, writable: false, enumerable: false, configurable: false },
|
|
335
316
|
// Esponi errcheck come setter/getter
|
|
336
317
|
errcheck: {
|
|
337
318
|
get() {
|
|
338
|
-
return
|
|
319
|
+
return callState.errcheck;
|
|
339
320
|
},
|
|
340
321
|
set(callback) {
|
|
341
322
|
callback = callback ?? null;
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
jsErrcheck = callback;
|
|
345
|
-
} else {
|
|
346
|
-
ffiFunc.setErrcheck(callback === null ? null : (result, _native, args) => callback(result, errcheckFunc(), args));
|
|
347
|
-
}
|
|
323
|
+
if (callback !== null && typeof callback !== "function") throw new TypeError("errcheck must be a function or null");
|
|
324
|
+
callState.errcheck = callback;
|
|
348
325
|
},
|
|
349
326
|
enumerable: false,
|
|
350
327
|
configurable: true,
|
|
@@ -371,7 +348,10 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
371
348
|
* @returns {Object} Oggetto callback con proprietà pointer e metodi
|
|
372
349
|
*/
|
|
373
350
|
callback(fn, returnType, argTypes = []) {
|
|
374
|
-
|
|
351
|
+
const args = [_toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native), adaptCallbackFunction(fn, argTypes, returnType, native)];
|
|
352
|
+
const scalarArgs = argTypes.map(scalarResult);
|
|
353
|
+
if (scalarArgs.some(Boolean)) args.push(scalarArgs);
|
|
354
|
+
return this._lib.callback(...args);
|
|
375
355
|
}
|
|
376
356
|
|
|
377
357
|
/**
|
|
@@ -418,11 +398,17 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
418
398
|
* WinDLL - come CDLL ma con stdcall di default (per Windows)
|
|
419
399
|
*/
|
|
420
400
|
class WinDLL extends CDLL {
|
|
421
|
-
_createFunction(name, returnType, argTypes = [], options = {}) {
|
|
422
|
-
return super._createFunction(
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
401
|
+
_createFunction(name, returnType, argTypes = [], options = {}, signature) {
|
|
402
|
+
return super._createFunction(
|
|
403
|
+
name,
|
|
404
|
+
returnType,
|
|
405
|
+
argTypes,
|
|
406
|
+
{
|
|
407
|
+
abi: "stdcall",
|
|
408
|
+
...options,
|
|
409
|
+
},
|
|
410
|
+
signature,
|
|
411
|
+
);
|
|
426
412
|
}
|
|
427
413
|
}
|
|
428
414
|
|
|
@@ -434,21 +420,8 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
434
420
|
* controllo precede errcheck e non viene sostituito da esso.
|
|
435
421
|
*/
|
|
436
422
|
class OleDLL extends WinDLL {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
return undefined;
|
|
440
|
-
}
|
|
441
|
-
return (result) => {
|
|
442
|
-
const n = typeof result === "bigint" ? Number(result) : result;
|
|
443
|
-
if (typeof n === "number" && n < 0) {
|
|
444
|
-
const hex = (n >>> 0).toString(16).padStart(8, "0").toUpperCase();
|
|
445
|
-
const err = new Error(`OleDLL '${name}' failed with HRESULT 0x${hex}`);
|
|
446
|
-
err.hresult = n;
|
|
447
|
-
err.winerror = n >>> 0;
|
|
448
|
-
throw err;
|
|
449
|
-
}
|
|
450
|
-
return result;
|
|
451
|
-
};
|
|
423
|
+
_defaultRestype() {
|
|
424
|
+
return OleDLL.defaultRestype;
|
|
452
425
|
}
|
|
453
426
|
}
|
|
454
427
|
|
package/lib/core/callback.js
CHANGED
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
*/
|
|
55
55
|
|
|
56
56
|
import { _toNativeReturnType, _toNativeTypes } from "./types.js";
|
|
57
|
-
import { adaptCallbackFunction } from "./conversions.js";
|
|
57
|
+
import { adaptCallbackFunction, scalarResult } from "./conversions.js";
|
|
58
58
|
import { trackCallback } from "./callback-tracker.js";
|
|
59
59
|
|
|
60
60
|
/**
|
|
@@ -147,7 +147,9 @@ import { trackCallback } from "./callback-tracker.js";
|
|
|
147
147
|
*/
|
|
148
148
|
export function callback(fn, returnType, argTypes = [], native, abi = undefined) {
|
|
149
149
|
const args = [adaptCallbackFunction(fn, argTypes, returnType, native), _toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native)];
|
|
150
|
-
|
|
150
|
+
const scalarArgs = argTypes.map(scalarResult);
|
|
151
|
+
if (scalarArgs.some(Boolean)) args.push(abi ?? "default", scalarArgs);
|
|
152
|
+
else if (abi) args.push(abi);
|
|
151
153
|
const cb = new native.Callback(...args);
|
|
152
154
|
|
|
153
155
|
const wrapper = {
|
|
@@ -276,7 +278,9 @@ export function callback(fn, returnType, argTypes = [], native, abi = undefined)
|
|
|
276
278
|
*/
|
|
277
279
|
export function threadSafeCallback(fn, returnType, argTypes = [], native, abi = undefined) {
|
|
278
280
|
const args = [adaptCallbackFunction(fn, argTypes, returnType, native), _toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native)];
|
|
279
|
-
|
|
281
|
+
const scalarArgs = argTypes.map(scalarResult);
|
|
282
|
+
if (scalarArgs.some(Boolean)) args.push(abi ?? "default", scalarArgs);
|
|
283
|
+
else if (abi) args.push(abi);
|
|
280
284
|
const cb = new native.ThreadSafeCallback(...args);
|
|
281
285
|
|
|
282
286
|
const wrapper = {
|