node-ctypes 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +932 -0
- package/README.md +446 -99
- 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 -62
- package/lib/core/callback.js +7 -3
- package/lib/core/conversions.js +226 -34
- package/lib/core/funcptr.js +115 -57
- package/lib/core/types.js +7 -2
- package/lib/index.d.ts +119 -54
- package/lib/index.js +10 -8
- package/lib/memory/buffer.js +39 -8
- package/lib/memory/operations.js +4 -6
- package/lib/memory/pointer.js +105 -38
- package/lib/platform/win_types.js +8 -3
- package/lib/structures/Structure.js +8 -39
- package/lib/structures/helpers/array.js +31 -12
- package/lib/structures/helpers/bitfield.js +17 -8
- package/lib/structures/helpers/common.js +218 -29
- package/lib/structures/helpers/layout.js +17 -3
- package/lib/structures/helpers/struct.js +25 -26
- package/lib/structures/helpers/union.js +32 -22
- package/lib/types/SimpleCData.js +11 -32
- package/lib/types/primitives.js +34 -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, needsArgumentOwners, resultCheck, resultConverter, scalarResult, 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 = "";
|
|
@@ -94,7 +82,7 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
94
82
|
// argtypes / restype, come in Python.
|
|
95
83
|
const resolve = () => {
|
|
96
84
|
if (!cachedFunc) {
|
|
97
|
-
cachedFunc = cdll._createFunction(name, restype === undefined ?
|
|
85
|
+
cachedFunc = cdll._createFunction(name, restype === undefined ? cdll._defaultRestype() : restype, argtypes);
|
|
98
86
|
Object.defineProperty(cachedFunc, kErrcheckFunc, { value: func });
|
|
99
87
|
if (errcheck !== null) {
|
|
100
88
|
cachedFunc.errcheck = errcheck;
|
|
@@ -108,6 +96,8 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
108
96
|
const func = (...args) => (cachedFunc ?? resolve())(...args);
|
|
109
97
|
|
|
110
98
|
Object.defineProperties(func, {
|
|
99
|
+
address: { get: () => resolve().address },
|
|
100
|
+
_as_parameter_: { get: () => resolve().address },
|
|
111
101
|
argtypes: {
|
|
112
102
|
get: () => argtypes,
|
|
113
103
|
set: (value) => {
|
|
@@ -118,7 +108,7 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
118
108
|
configurable: true,
|
|
119
109
|
},
|
|
120
110
|
restype: {
|
|
121
|
-
get: () => (restype === undefined ?
|
|
111
|
+
get: () => (restype === undefined ? cdll._defaultRestype() : restype),
|
|
122
112
|
set: (value) => {
|
|
123
113
|
restype = value;
|
|
124
114
|
cachedFunc = null; // Invalidate cache
|
|
@@ -128,6 +118,7 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
128
118
|
errcheck: {
|
|
129
119
|
get: () => errcheck,
|
|
130
120
|
set: (value) => {
|
|
121
|
+
if (value !== null && value !== undefined && typeof value !== "function") throw new TypeError("errcheck must be a function or null");
|
|
131
122
|
errcheck = value ?? null;
|
|
132
123
|
if (cachedFunc) {
|
|
133
124
|
cachedFunc.errcheck = errcheck;
|
|
@@ -196,7 +187,11 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
196
187
|
* @param {Object} [options] - Opzioni aggiuntive (es. { abi: 'stdcall' })
|
|
197
188
|
* @returns {Function} Funzione callable
|
|
198
189
|
*/
|
|
199
|
-
|
|
190
|
+
_defaultRestype() {
|
|
191
|
+
return FunctionWrapper.defaultRestype;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
func(name, returnType = this._defaultRestype(), argTypes = [], options = {}) {
|
|
200
195
|
const cacheKey = `${name}:${typeKey(returnType)}:${argTypes.map(typeKey).join(",")}${optionsKey(options)}`;
|
|
201
196
|
let fn = this._cache.get(cacheKey);
|
|
202
197
|
if (fn === undefined) {
|
|
@@ -211,8 +206,8 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
211
206
|
* restype, es. HRESULT in OleDLL). undefined se non c'è.
|
|
212
207
|
* @protected
|
|
213
208
|
*/
|
|
214
|
-
_resultCheck(
|
|
215
|
-
return
|
|
209
|
+
_resultCheck(returnType, name) {
|
|
210
|
+
return resultCheck(returnType, name, this.constructor.name);
|
|
216
211
|
}
|
|
217
212
|
|
|
218
213
|
/**
|
|
@@ -226,6 +221,7 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
226
221
|
...(this._use_last_error ? { use_last_error: true } : {}),
|
|
227
222
|
...(this._use_errno ? { use_errno: true } : {}),
|
|
228
223
|
...(structResult(returnType) ? { struct_buffer: true } : {}),
|
|
224
|
+
...(scalarResult(returnType) ? { scalar_buffer: true } : {}),
|
|
229
225
|
};
|
|
230
226
|
const ffiFunc = this._lib.func(name, _toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native), mergedOptions);
|
|
231
227
|
|
|
@@ -241,6 +237,25 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
241
237
|
const C = argumentConverters(argTypes, native);
|
|
242
238
|
const [C0, C1, C2, C3, C4, C5] = C;
|
|
243
239
|
const convertAll = (args) => convertArguments(args, C);
|
|
240
|
+
const hasAdapters = C.some((convert) => convert.requiresOwners);
|
|
241
|
+
const baseConvert = resultConverter(returnType);
|
|
242
|
+
const check = this._resultCheck(returnType, name);
|
|
243
|
+
const convertResult = check === undefined ? baseConvert : baseConvert === undefined ? check : (result) => check(baseConvert(result));
|
|
244
|
+
const captureHere = convertResult === undefined;
|
|
245
|
+
let jsErrcheck = null;
|
|
246
|
+
let checkedSync;
|
|
247
|
+
const checkedCall = (method, args, checked) => {
|
|
248
|
+
const originalArgs = Array.isArray(args) ? args : Array.prototype.slice.call(args);
|
|
249
|
+
const func = errcheckFunc();
|
|
250
|
+
const finish = (result) => checked(convertResult === undefined ? result : convertResult(result), func, originalArgs);
|
|
251
|
+
const call = method === "call" ? (checkedSync ??= ffiFunc._makeCheckedCall()) : undefined;
|
|
252
|
+
return callWithResultHandler(ffiFunc, method, originalArgs, C, finish, hasAdapters || argumentsNeedOwners(originalArgs), call);
|
|
253
|
+
};
|
|
254
|
+
const ownedCall = (method, args) => callWithArgumentOwners(ffiFunc, method, args, C);
|
|
255
|
+
const invoke = (method, args) => {
|
|
256
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, args, jsErrcheck);
|
|
257
|
+
return captureHere && (hasAdapters || argumentsNeedOwners(args)) ? ownedCall(method, args) : ffiFunc[method](...convertAll(args));
|
|
258
|
+
};
|
|
244
259
|
|
|
245
260
|
// Funzioni specializzate per numero di argomenti (niente array per la
|
|
246
261
|
// chiamata comune); `method` è "call" o "callAsync".
|
|
@@ -248,35 +263,54 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
248
263
|
switch (argCount) {
|
|
249
264
|
case 0:
|
|
250
265
|
return function () {
|
|
251
|
-
|
|
266
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
267
|
+
return arguments.length === 0 ? ffiFunc[method]() : invoke(method, arguments);
|
|
252
268
|
};
|
|
253
269
|
case 1:
|
|
254
270
|
return function (a0) {
|
|
255
|
-
|
|
271
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
272
|
+
if (arguments.length !== 1) return invoke(method, arguments);
|
|
273
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0))) return ownedCall(method, arguments);
|
|
274
|
+
return ffiFunc[method](C0(a0));
|
|
256
275
|
};
|
|
257
276
|
case 2:
|
|
258
277
|
return function (a0, a1) {
|
|
259
|
-
|
|
278
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
279
|
+
if (arguments.length !== 2) return invoke(method, arguments);
|
|
280
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1))) return ownedCall(method, arguments);
|
|
281
|
+
return ffiFunc[method](C0(a0), C1(a1));
|
|
260
282
|
};
|
|
261
283
|
case 3:
|
|
262
284
|
return function (a0, a1, a2) {
|
|
263
|
-
|
|
285
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
286
|
+
if (arguments.length !== 3) return invoke(method, arguments);
|
|
287
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2))) return ownedCall(method, arguments);
|
|
288
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2));
|
|
264
289
|
};
|
|
265
290
|
case 4:
|
|
266
291
|
return function (a0, a1, a2, a3) {
|
|
267
|
-
|
|
292
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
293
|
+
if (arguments.length !== 4) return invoke(method, arguments);
|
|
294
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3))) return ownedCall(method, arguments);
|
|
295
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3));
|
|
268
296
|
};
|
|
269
297
|
case 5:
|
|
270
298
|
return function (a0, a1, a2, a3, a4) {
|
|
271
|
-
|
|
299
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
300
|
+
if (arguments.length !== 5) return invoke(method, arguments);
|
|
301
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3) || needsArgumentOwners(a4))) return ownedCall(method, arguments);
|
|
302
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4));
|
|
272
303
|
};
|
|
273
304
|
case 6:
|
|
274
305
|
return function (a0, a1, a2, a3, a4, a5) {
|
|
275
|
-
|
|
306
|
+
if (captureHere && jsErrcheck !== null) return checkedCall(method, arguments, jsErrcheck);
|
|
307
|
+
if (arguments.length !== 6) return invoke(method, arguments);
|
|
308
|
+
if (captureHere && (hasAdapters || needsArgumentOwners(a0) || needsArgumentOwners(a1) || needsArgumentOwners(a2) || needsArgumentOwners(a3) || needsArgumentOwners(a4) || needsArgumentOwners(a5))) return ownedCall(method, arguments);
|
|
309
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4), C5(a5));
|
|
276
310
|
};
|
|
277
311
|
default:
|
|
278
312
|
return function (...args) {
|
|
279
|
-
return
|
|
313
|
+
return invoke(method, args);
|
|
280
314
|
};
|
|
281
315
|
}
|
|
282
316
|
};
|
|
@@ -284,24 +318,20 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
284
318
|
let callMethod = specialize("call");
|
|
285
319
|
let asyncCallMethod = specialize("callAsync");
|
|
286
320
|
|
|
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;
|
|
321
|
+
// Result handlers run inside the native call scope, after conversion and
|
|
322
|
+
// with the hook/arguments captured before any argument adapter executes.
|
|
294
323
|
if (convertResult !== undefined) {
|
|
295
324
|
const rawCall = callMethod;
|
|
296
325
|
const rawAsync = asyncCallMethod;
|
|
297
|
-
const finish = (result
|
|
298
|
-
const converted = convertResult(result);
|
|
299
|
-
return jsErrcheck === null ? converted : jsErrcheck(converted, errcheckFunc(), args);
|
|
300
|
-
};
|
|
326
|
+
const finish = (result) => convertResult(result);
|
|
301
327
|
callMethod = function (...args) {
|
|
328
|
+
if (jsErrcheck !== null) return checkedCall("call", args, jsErrcheck);
|
|
329
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "call", args, C, finish);
|
|
302
330
|
return finish(rawCall.apply(this, args), args);
|
|
303
331
|
};
|
|
304
332
|
asyncCallMethod = function (...args) {
|
|
333
|
+
if (jsErrcheck !== null) return checkedCall("callAsync", args, jsErrcheck);
|
|
334
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, C, finish);
|
|
305
335
|
return rawAsync.apply(this, args).then((result) => finish(result, args));
|
|
306
336
|
};
|
|
307
337
|
}
|
|
@@ -330,21 +360,18 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
330
360
|
Object.defineProperties(callMethod, {
|
|
331
361
|
funcName: { value: name },
|
|
332
362
|
address: { value: ffiFunc.address },
|
|
363
|
+
_as_parameter_: { value: ffiFunc.address },
|
|
333
364
|
_ffi: { value: ffiFunc },
|
|
334
365
|
callAsync: { value: asyncCallMethod, writable: false, enumerable: false, configurable: false },
|
|
335
366
|
// Esponi errcheck come setter/getter
|
|
336
367
|
errcheck: {
|
|
337
368
|
get() {
|
|
338
|
-
return
|
|
369
|
+
return jsErrcheck;
|
|
339
370
|
},
|
|
340
371
|
set(callback) {
|
|
341
372
|
callback = callback ?? null;
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
jsErrcheck = callback;
|
|
345
|
-
} else {
|
|
346
|
-
ffiFunc.setErrcheck(callback === null ? null : (result, _native, args) => callback(result, errcheckFunc(), args));
|
|
347
|
-
}
|
|
373
|
+
if (callback !== null && typeof callback !== "function") throw new TypeError("errcheck must be a function or null");
|
|
374
|
+
jsErrcheck = callback;
|
|
348
375
|
},
|
|
349
376
|
enumerable: false,
|
|
350
377
|
configurable: true,
|
|
@@ -371,7 +398,10 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
371
398
|
* @returns {Object} Oggetto callback con proprietà pointer e metodi
|
|
372
399
|
*/
|
|
373
400
|
callback(fn, returnType, argTypes = []) {
|
|
374
|
-
|
|
401
|
+
const args = [_toNativeReturnType(returnType, native), _toNativeTypes(argTypes, native), adaptCallbackFunction(fn, argTypes, returnType, native)];
|
|
402
|
+
const scalarArgs = argTypes.map(scalarResult);
|
|
403
|
+
if (scalarArgs.some(Boolean)) args.push(scalarArgs);
|
|
404
|
+
return this._lib.callback(...args);
|
|
375
405
|
}
|
|
376
406
|
|
|
377
407
|
/**
|
|
@@ -434,21 +464,8 @@ export function createLibraryClasses(Library, LRUCache, _toNativeType, _toNative
|
|
|
434
464
|
* controllo precede errcheck e non viene sostituito da esso.
|
|
435
465
|
*/
|
|
436
466
|
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
|
-
};
|
|
467
|
+
_defaultRestype() {
|
|
468
|
+
return OleDLL.defaultRestype;
|
|
452
469
|
}
|
|
453
470
|
}
|
|
454
471
|
|
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 = {
|