node-ctypes 2.14.0 → 2.15.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 +27 -1
- 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 +69 -51
- package/lib/core/callback-conversions.js +123 -0
- package/lib/core/callback.js +68 -4
- package/lib/core/conversions.js +74 -412
- package/lib/core/funcptr.js +31 -287
- package/lib/core/invocation.js +170 -0
- package/lib/core/native-aggregate.js +193 -0
- package/lib/core/parameters.js +45 -0
- package/lib/core/paramflags.js +183 -0
- package/lib/core/results.js +86 -0
- package/lib/core/types.js +14 -277
- package/lib/index.js +2 -1
- package/lib/memory/buffer.js +1 -1
- package/lib/memory/mirror.js +2 -2
- package/lib/memory/operations.js +2 -1
- package/lib/memory/ownership.js +218 -0
- package/lib/memory/pointer.js +6 -1
- package/lib/platform/win_types.js +1 -1
- package/lib/structures/Structure.js +50 -37
- package/lib/structures/helpers/array.js +27 -13
- package/lib/structures/helpers/bitfield.js +1 -1
- package/lib/structures/helpers/common.js +3 -368
- package/lib/structures/helpers/fields.js +111 -0
- package/lib/structures/helpers/layout.js +44 -57
- package/lib/structures/helpers/struct.js +19 -108
- package/lib/structures/helpers/union.js +13 -114
- package/lib/structures/helpers/wrap.js +1 -1
- package/lib/structures/protocol.js +8 -0
- package/lib/types/SimpleCData.js +2 -1
- package/lib/types/primitives.js +2 -1
- package/lib/types/scalar-protocol.js +120 -0
- package/package.json +1 -1
package/lib/core/funcptr.js
CHANGED
|
@@ -37,217 +37,17 @@
|
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
39
|
import { _toNativeReturnType, _toNativeTypes } from "./types.js";
|
|
40
|
-
import { argumentConverters,
|
|
41
|
-
import {
|
|
42
|
-
import {
|
|
40
|
+
import { argumentConverters, convertArguments } from "./conversions.js";
|
|
41
|
+
import { argumentsNeedOwners, callWithArgumentOwners, createCheckedInvoker, specializeCall } from "./invocation.js";
|
|
42
|
+
import { composeResult, resultCheck, resultConverter, scalarResult, structResult } from "./results.js";
|
|
43
|
+
import { callback as createCallback, guardCallbackCalls } from "./callback.js";
|
|
44
|
+
import { normalizeParamflags, applyParamflags } from "./paramflags.js";
|
|
45
|
+
import { keepFunctionOwner } from "../memory/ownership.js";
|
|
43
46
|
import { cacheWeakValue, createTypeKey } from "../utils/cache.js";
|
|
44
47
|
import { callOn } from "./executor.js";
|
|
45
48
|
|
|
46
49
|
const prototypeOwners = new WeakMap();
|
|
47
50
|
|
|
48
|
-
/**
|
|
49
|
-
* Internal helper: builds a function wrapper that interprets paramflags
|
|
50
|
-
* (Python ctypes-style in/out/default semantics) on top of an already-bound
|
|
51
|
-
* native function. Used by both CFUNCTYPE and WINFUNCTYPE factories.
|
|
52
|
-
*
|
|
53
|
-
* `paramflags` is an array the same length as argtypes, where each entry is:
|
|
54
|
-
* { dir: "in" | "out" | "inout", name: string, default?: any }
|
|
55
|
-
*
|
|
56
|
-
* Calling conventions:
|
|
57
|
-
* - Positional: fn(arg1, arg2, ...) — only "in" params supplied in order
|
|
58
|
-
* - Named: fn({ name1: v1, name2: v2 }) — object with in-param names
|
|
59
|
-
*
|
|
60
|
-
* Return value (Python ctypes parity):
|
|
61
|
-
* - Nessun out param → il valore del restype (tal quale).
|
|
62
|
-
* - 1 out param, nessun altro → il valore della out (non un array).
|
|
63
|
-
* - N out params → array di N elementi [out1, out2, ...].
|
|
64
|
-
*
|
|
65
|
-
* Quando ci sono out params, `result` (restype) NON appare nel return.
|
|
66
|
-
* Se serve ispezionarlo usa `.errcheck = fn` che riceve (result, func, args),
|
|
67
|
-
* con le istanze "out" in args; il valore restituito da errcheck diventa il
|
|
68
|
-
* risultato, salvo `args` stesso (allora il risultato sono le out).
|
|
69
|
-
*
|
|
70
|
-
* @private
|
|
71
|
-
*/
|
|
72
|
-
function applyParamflags(rawFn, restype, argtypes, paramflags) {
|
|
73
|
-
if (paramflags.length !== argtypes.length) {
|
|
74
|
-
throw new TypeError(`paramflags length (${paramflags.length}) must match argtypes length (${argtypes.length})`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Classify slots and cache pointed-to types for out-params
|
|
78
|
-
const slots = paramflags.map((pf, idx) => {
|
|
79
|
-
if (!["in", "out", "inout"].includes(pf.dir)) throw new TypeError(`paramflags[${idx}] has an invalid direction`);
|
|
80
|
-
const isOut = pf.dir !== "in";
|
|
81
|
-
const argtype = argtypes[idx];
|
|
82
|
-
let outType = null;
|
|
83
|
-
if (isOut) {
|
|
84
|
-
// Expect POINTER(T) argtype; derive base type for read-back
|
|
85
|
-
outType = argtype && argtype._pointerTo ? argtype._pointerTo : null;
|
|
86
|
-
if (!outType) {
|
|
87
|
-
throw new TypeError(`paramflags[${idx}] is "out" but argtype ${idx} is not a POINTER(T)`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return { idx, name: pf.name, dir: pf.dir, default: pf.default, argtype, outType, simple: outType?._isSimpleCData === true && !isScalarSubclass(outType) };
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
const inputs = slots.filter((s) => s.dir !== "out");
|
|
94
|
-
const outputs = slots.filter((s) => s.dir !== "in");
|
|
95
|
-
|
|
96
|
-
let errcheck = null;
|
|
97
|
-
|
|
98
|
-
// `call`: the callOn target when `method` is "callOn".
|
|
99
|
-
const invokeWithFlags = (method, callArgs, call) => {
|
|
100
|
-
const checked = errcheck;
|
|
101
|
-
const namedForm =
|
|
102
|
-
callArgs.length === 1 &&
|
|
103
|
-
callArgs[0] !== null &&
|
|
104
|
-
typeof callArgs[0] === "object" &&
|
|
105
|
-
!Buffer.isBuffer(callArgs[0]) &&
|
|
106
|
-
!ArrayBuffer.isView(callArgs[0]) &&
|
|
107
|
-
!(callArgs[0] instanceof ArrayBuffer) &&
|
|
108
|
-
!Array.isArray(callArgs[0]) &&
|
|
109
|
-
// Exclude struct/union proxies (they have _buffer)
|
|
110
|
-
callArgs[0]._buffer === undefined;
|
|
111
|
-
|
|
112
|
-
const rawArgs = new Array(slots.length);
|
|
113
|
-
|
|
114
|
-
// Populate in-slots
|
|
115
|
-
let posIdx = 0;
|
|
116
|
-
for (const input of inputs) {
|
|
117
|
-
let value;
|
|
118
|
-
if (namedForm) {
|
|
119
|
-
value = callArgs[0][input.name];
|
|
120
|
-
if (value === undefined) value = input.default;
|
|
121
|
-
} else {
|
|
122
|
-
value = posIdx < callArgs.length ? callArgs[posIdx++] : input.default;
|
|
123
|
-
}
|
|
124
|
-
if (value === undefined) {
|
|
125
|
-
throw new TypeError(`paramflags: missing required arg "${input.name}"`);
|
|
126
|
-
}
|
|
127
|
-
rawArgs[input.idx] = value;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Python: un parametro "out" è un'istanza del tipo puntato, passata per
|
|
131
|
-
// riferimento (il suo Buffer).
|
|
132
|
-
const outs = outputs.map((output) => {
|
|
133
|
-
const instance = output.dir === "inout" ? rawArgs[output.idx] : newOutInstance(output.outType, output.name);
|
|
134
|
-
rawArgs[output.idx] = instance;
|
|
135
|
-
return { instance, type: output.outType, dir: output.dir, simple: output.simple };
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const finish = (result) => {
|
|
139
|
-
// Returning the original args continues normal output unpacking.
|
|
140
|
-
if (checked !== null) {
|
|
141
|
-
const value = checked(result, paramflagsWrapper, rawArgs);
|
|
142
|
-
if (value !== rawArgs) return value;
|
|
143
|
-
}
|
|
144
|
-
if (outs.length === 0) return result;
|
|
145
|
-
const values = outs.map(({ instance, type, dir, simple }) => {
|
|
146
|
-
if (dir === "inout") return instance;
|
|
147
|
-
const convert = instance.__ctypes_from_outparam__;
|
|
148
|
-
if (convert === scalarOutparam) return simple ? instance.value : instance;
|
|
149
|
-
if (convert !== undefined) {
|
|
150
|
-
if (typeof convert !== "function") throw new TypeError("__ctypes_from_outparam__ must be callable");
|
|
151
|
-
return convert.call(instance);
|
|
152
|
-
}
|
|
153
|
-
if (typeof type === "function") return instance;
|
|
154
|
-
return type.toObject(instance._buffer);
|
|
155
|
-
});
|
|
156
|
-
return values.length === 1 ? values[0] : values;
|
|
157
|
-
};
|
|
158
|
-
// The private binding snapshots this handler and keeps adapter temporaries
|
|
159
|
-
// alive until output processing finishes, including on an async rejection.
|
|
160
|
-
const previous = rawFn.errcheck;
|
|
161
|
-
rawFn.errcheck = finish;
|
|
162
|
-
try {
|
|
163
|
-
if (method === "callOn") return rawFn._callOn(call, rawArgs);
|
|
164
|
-
return method === "callAsync" ? rawFn.callAsync(...rawArgs) : rawFn(...rawArgs);
|
|
165
|
-
} finally {
|
|
166
|
-
rawFn.errcheck = previous;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const paramflagsWrapper = (...args) => invokeWithFlags("call", args);
|
|
171
|
-
Object.defineProperties(paramflagsWrapper, {
|
|
172
|
-
funcName: { value: rawFn.funcName },
|
|
173
|
-
address: { value: rawFn.address },
|
|
174
|
-
_as_parameter_: { value: rawFn.address },
|
|
175
|
-
_ffi: { value: rawFn._ffi },
|
|
176
|
-
callAsync: { value: (...args) => invokeWithFlags("callAsync", args) },
|
|
177
|
-
callOn: { value: (target, ...args) => callOn(paramflagsWrapper, target, args) },
|
|
178
|
-
_callOn: { value: (call, args) => invokeWithFlags("callOn", args, call) },
|
|
179
|
-
errcheck: {
|
|
180
|
-
get: () => errcheck,
|
|
181
|
-
set: (callback) => {
|
|
182
|
-
if (callback !== null && callback !== undefined && typeof callback !== "function") {
|
|
183
|
-
throw new TypeError("errcheck must be a function or null");
|
|
184
|
-
}
|
|
185
|
-
errcheck = callback ?? null;
|
|
186
|
-
},
|
|
187
|
-
enumerable: false,
|
|
188
|
-
configurable: true,
|
|
189
|
-
},
|
|
190
|
-
});
|
|
191
|
-
return paramflagsWrapper;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Istanza per un parametro "out": SimpleCData, Structure/Union class o
|
|
195
|
-
// definizione struct()/union().
|
|
196
|
-
function newOutInstance(type, name) {
|
|
197
|
-
if (typeof type === "function" && (type._isSimpleCData || type._isArrayType || typeof type._buildStruct === "function")) {
|
|
198
|
-
return new type();
|
|
199
|
-
}
|
|
200
|
-
if (type && typeof type === "object" && typeof type.create === "function") {
|
|
201
|
-
return type.create();
|
|
202
|
-
}
|
|
203
|
-
throw new TypeError(`paramflags "out" param "${name}": cannot determine the target type`);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Normalize a paramflags entry: Python tuple `[flag, name, default]`
|
|
208
|
-
* (flag 1 = in, 2 = out, 3 = inout, 4 = in defaulting to 0) or `{ dir, name, default }`.
|
|
209
|
-
* @private
|
|
210
|
-
*/
|
|
211
|
-
function toParamFlag(pf) {
|
|
212
|
-
if (!Array.isArray(pf)) return pf;
|
|
213
|
-
const [flag = 1, name, ...rest] = pf;
|
|
214
|
-
const dir = (flag & 3) === 3 ? "inout" : flag & 2 ? "out" : "in";
|
|
215
|
-
const result = { dir, name };
|
|
216
|
-
if (rest.length > 0) result.default = rest[0];
|
|
217
|
-
else if (flag & 4) result.default = 0;
|
|
218
|
-
return result;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Attach a `.bind(library, name, paramflags)` method to a CFUNCTYPE/WINFUNCTYPE
|
|
223
|
-
* factory, enabling Python-style typed binding with in/out params.
|
|
224
|
-
* @private
|
|
225
|
-
*/
|
|
226
|
-
function attachBind(factory, restype, argtypes, abi, signature) {
|
|
227
|
-
factory.bind = function bindWithFlags(library, symbolName, paramflags) {
|
|
228
|
-
// Validate paramflags up-front so shape errors surface before the symbol
|
|
229
|
-
// lookup (which may fail for unrelated reasons like missing export).
|
|
230
|
-
if (!Array.isArray(paramflags)) {
|
|
231
|
-
throw new TypeError("paramflags must be an array");
|
|
232
|
-
}
|
|
233
|
-
paramflags = paramflags.map(toParamFlag);
|
|
234
|
-
if (paramflags.length !== argtypes.length) {
|
|
235
|
-
throw new TypeError(`paramflags length (${paramflags.length}) must match argtypes length (${argtypes.length})`);
|
|
236
|
-
}
|
|
237
|
-
for (let i = 0; i < paramflags.length; i++) {
|
|
238
|
-
const pf = paramflags[i];
|
|
239
|
-
if (pf.dir !== "in" && !(argtypes[i] && argtypes[i]._pointerTo)) {
|
|
240
|
-
throw new TypeError(`paramflags[${i}] is "out" but argtype ${i} is not a POINTER(T)`);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
const rawFn = library._createFunction(symbolName, restype, argtypes, abi ? { abi } : {}, signature);
|
|
244
|
-
const bound = applyParamflags(rawFn, restype, argtypes, paramflags);
|
|
245
|
-
prototypeOwners.set(bound, factory);
|
|
246
|
-
return bound;
|
|
247
|
-
};
|
|
248
|
-
return factory;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
51
|
// Tipo funzione CFUNCTYPE / WINFUNCTYPE: chiamato con un indirizzo restituisce
|
|
252
52
|
// una funzione chiamabile, con una funzione JS crea un callback C.
|
|
253
53
|
function createFuncPtrType(native, typeName, abi) {
|
|
@@ -273,15 +73,13 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
273
73
|
const nativeArgtypes = _toNativeTypes(argtypes, native);
|
|
274
74
|
const converters = argumentConverters(argtypes, native);
|
|
275
75
|
const hasAdapters = converters.some((convert) => convert.requiresOwners);
|
|
276
|
-
const represent = resultConverter(restype);
|
|
76
|
+
const represent = resultConverter(restype, native);
|
|
277
77
|
const check = resultCheck(restype, typeName);
|
|
278
|
-
const convertResult =
|
|
78
|
+
const convertResult = composeResult(represent, check);
|
|
279
79
|
const signature = { nativeRestype, nativeArgtypes, converters, represent, check };
|
|
280
80
|
|
|
281
81
|
function factory(addressOrFn, paramflags) {
|
|
282
82
|
let callback;
|
|
283
|
-
let released = false;
|
|
284
|
-
let activeCalls = 0;
|
|
285
83
|
// Python ctypes parity: CFUNCTYPE(...)(jsFunc) wraps a JS function as a C callback.
|
|
286
84
|
if (typeof addressOrFn === "function") {
|
|
287
85
|
callback = createCallback(addressOrFn, restype, argtypes, native, abi);
|
|
@@ -325,21 +123,16 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
325
123
|
// Come le funzioni di CDLL: restype POINTER/CFUNCTYPE convertito, ed
|
|
326
124
|
// errcheck applicato sul valore convertito.
|
|
327
125
|
const callState = { errcheck: null };
|
|
328
|
-
|
|
126
|
+
const checkedCall = createCheckedInvoker(ffiFunc, converters, hasAdapters, convertResult, () => exposed);
|
|
329
127
|
const finish = (result) => (convertResult === undefined ? result : convertResult(result));
|
|
330
|
-
const
|
|
331
|
-
const originalArgs = Array.isArray(args) ? args : Array.prototype.slice.call(args);
|
|
332
|
-
const resultHandler = (result) => checked(finish(result), exposed, originalArgs);
|
|
333
|
-
const call = method === "call" ? (checkedSync ??= ffiFunc._makeCheckedCall()) : undefined;
|
|
334
|
-
return callWithResultHandler(ffiFunc, method, originalArgs, converters, resultHandler, hasAdapters || argumentsNeedOwners(originalArgs), call, target);
|
|
335
|
-
};
|
|
128
|
+
const finishAsync = (result) => (convertResult === undefined ? result : convertResult(result, true));
|
|
336
129
|
|
|
337
130
|
const invoke = (args, checked) => {
|
|
338
131
|
if (checked !== null) return checkedCall("call", args, checked);
|
|
339
132
|
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "call", args, converters, finish);
|
|
340
133
|
return finish(ffiFunc.call(...convertArguments(args, converters)), args);
|
|
341
134
|
};
|
|
342
|
-
|
|
135
|
+
const callMethod =
|
|
343
136
|
convertResult === undefined && !hasAdapters && converters.length <= 6
|
|
344
137
|
? specializeCall(ffiFunc, "call", converters, callState, invoke)
|
|
345
138
|
: function (...args) {
|
|
@@ -348,86 +141,31 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
348
141
|
|
|
349
142
|
const asyncCall = function (...args) {
|
|
350
143
|
if (callState.errcheck !== null) return checkedCall("callAsync", args, callState.errcheck);
|
|
351
|
-
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, converters,
|
|
352
|
-
return ffiFunc.callAsync(...convertArguments(args, converters)).then((result) =>
|
|
144
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, converters, finishAsync);
|
|
145
|
+
return ffiFunc.callAsync(...convertArguments(args, converters)).then((result) => finishAsync(result, args));
|
|
353
146
|
};
|
|
354
147
|
// callOn(): the same call on the Executor of `target` (this invocation's own).
|
|
355
148
|
const onExecutor = function (target, args) {
|
|
356
149
|
if (callState.errcheck !== null) return checkedCall("callAsync", args, callState.errcheck, target);
|
|
357
|
-
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, converters,
|
|
358
|
-
return ffiFunc.callOn(target, ...convertArguments(args, converters)).then((result) =>
|
|
150
|
+
if (hasAdapters || argumentsNeedOwners(args)) return callWithArgumentOwners(ffiFunc, "callAsync", args, converters, finishAsync, target);
|
|
151
|
+
return ffiFunc.callOn(target, ...convertArguments(args, converters)).then((result) => finishAsync(result, args));
|
|
359
152
|
};
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
let
|
|
363
|
-
if (callback !== undefined) {
|
|
364
|
-
const assertLive = () => {
|
|
365
|
-
if (released) throw new Error("Callback has been released");
|
|
366
|
-
};
|
|
367
|
-
const release = () => {
|
|
368
|
-
if (activeCalls !== 0) throw new Error("Cannot release callback while calls to it are in progress");
|
|
369
|
-
if (!released) {
|
|
370
|
-
callback.release();
|
|
371
|
-
released = true;
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
const call = callMethod;
|
|
375
|
-
callMethod = function (...args) {
|
|
376
|
-
assertLive();
|
|
377
|
-
activeCalls++;
|
|
378
|
-
try {
|
|
379
|
-
return call(...args);
|
|
380
|
-
} finally {
|
|
381
|
-
activeCalls--;
|
|
382
|
-
}
|
|
383
|
-
};
|
|
384
|
-
callAsync = function (...args) {
|
|
385
|
-
assertLive();
|
|
386
|
-
activeCalls++;
|
|
387
|
-
try {
|
|
388
|
-
return asyncCall(...args).finally(() => activeCalls--);
|
|
389
|
-
} catch (error) {
|
|
390
|
-
activeCalls--;
|
|
391
|
-
throw error;
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
callOnExecutor = function (target, args) {
|
|
395
|
-
assertLive();
|
|
396
|
-
activeCalls++;
|
|
397
|
-
try {
|
|
398
|
-
return onExecutor(target, args).finally(() => activeCalls--);
|
|
399
|
-
} catch (error) {
|
|
400
|
-
activeCalls--;
|
|
401
|
-
throw error;
|
|
402
|
-
}
|
|
403
|
-
};
|
|
404
|
-
callbackProperties = Object.getOwnPropertyDescriptors(callback);
|
|
405
|
-
callbackProperties.release.value = release;
|
|
406
|
-
callbackProperties[Symbol.dispose].value = release;
|
|
407
|
-
callbackProperties._as_parameter_ = {
|
|
408
|
-
get() {
|
|
409
|
-
assertLive();
|
|
410
|
-
return callback.pointer;
|
|
411
|
-
},
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
// As CDLL.func: the native entry function when the arguments that the
|
|
415
|
-
// JavaScript does not convert are enough (see Library._createFunction);
|
|
416
|
-
// not for a JS callback, whose calls are counted here.
|
|
417
|
-
let exposed = callMethod;
|
|
153
|
+
const guarded = callback && guardCallbackCalls(callback, callMethod, asyncCall, onExecutor);
|
|
154
|
+
const call = guarded?.call ?? callMethod;
|
|
155
|
+
let exposed = call;
|
|
418
156
|
if (callback === undefined && convertResult === undefined && !hasAdapters && converters.every((convert) => convert.passesPrimitives === true || convert.passesStrings === true)) {
|
|
419
157
|
exposed = ffiFunc._makeEntry(callMethod) ?? callMethod;
|
|
420
158
|
}
|
|
421
159
|
Object.defineProperties(exposed, {
|
|
422
|
-
...
|
|
160
|
+
...guarded?.properties,
|
|
423
161
|
name: { value: "callMethod", configurable: true },
|
|
424
162
|
length: { value: 0, configurable: true },
|
|
425
163
|
funcName: { value: `0x${address.toString(16)}` },
|
|
426
164
|
address: { value: ffiFunc.address },
|
|
427
|
-
_as_parameter_:
|
|
165
|
+
_as_parameter_: guarded?.properties._as_parameter_ ?? { value: ffiFunc.address },
|
|
428
166
|
_ffi: { value: ffiFunc },
|
|
429
167
|
callAsync: {
|
|
430
|
-
value: callAsync,
|
|
168
|
+
value: guarded?.callAsync ?? asyncCall,
|
|
431
169
|
writable: false,
|
|
432
170
|
enumerable: false,
|
|
433
171
|
configurable: false,
|
|
@@ -438,7 +176,7 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
438
176
|
enumerable: false,
|
|
439
177
|
configurable: false,
|
|
440
178
|
},
|
|
441
|
-
_callOn: { value: callOnExecutor },
|
|
179
|
+
_callOn: { value: guarded?.callOnExecutor ?? onExecutor },
|
|
442
180
|
errcheck: {
|
|
443
181
|
get() {
|
|
444
182
|
return callState.errcheck;
|
|
@@ -447,14 +185,14 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
447
185
|
callback = callback ?? null;
|
|
448
186
|
if (callback !== null && typeof callback !== "function") throw new TypeError("errcheck must be a function or null");
|
|
449
187
|
callState.errcheck = callback;
|
|
450
|
-
if (exposed !==
|
|
188
|
+
if (exposed !== call) ffiFunc._setEntryDefer(callback !== null);
|
|
451
189
|
},
|
|
452
190
|
enumerable: false,
|
|
453
191
|
configurable: true,
|
|
454
192
|
},
|
|
455
193
|
});
|
|
456
194
|
|
|
457
|
-
if (callback !== undefined) keepFunctionOwner(
|
|
195
|
+
if (callback !== undefined) keepFunctionOwner(exposed, callback);
|
|
458
196
|
prototypeOwners.set(exposed, factory);
|
|
459
197
|
return exposed;
|
|
460
198
|
}
|
|
@@ -463,7 +201,13 @@ function createFuncPtrType(native, typeName, abi) {
|
|
|
463
201
|
factory.restype = restype;
|
|
464
202
|
factory.argtypes = argtypes.slice();
|
|
465
203
|
factory._isFuncPtr = true;
|
|
466
|
-
|
|
204
|
+
factory.bind = function bindWithFlags(library, symbolName, paramflags) {
|
|
205
|
+
const flags = normalizeParamflags(paramflags, argtypes);
|
|
206
|
+
const rawFn = library._createFunction(symbolName, restype, argtypes, abi ? { abi } : {}, signature);
|
|
207
|
+
const bound = applyParamflags(rawFn, argtypes, flags, signature);
|
|
208
|
+
prototypeOwners.set(bound, factory);
|
|
209
|
+
return bound;
|
|
210
|
+
};
|
|
467
211
|
|
|
468
212
|
signatures.set(factory, { restype, argtypes });
|
|
469
213
|
cacheWeakValue(cache, key, factory);
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/** Native call dispatch and the lifetime of per-invocation arguments. */
|
|
2
|
+
import { activeArgumentOwners, activeOwnerStack } from "./parameters.js";
|
|
3
|
+
import { convertArguments, needsArgumentOwners } from "./conversions.js";
|
|
4
|
+
|
|
5
|
+
const reflectApply = Reflect.apply;
|
|
6
|
+
|
|
7
|
+
export function argumentsNeedOwners(args) {
|
|
8
|
+
for (let i = 0; i < args.length; i++) if (needsArgumentOwners(args[i])) return true;
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// The general fallback receives the hook snapshot taken before any owner probe.
|
|
13
|
+
export function specializeCall(ffiFunc, method, converters, state, invoke, captureHere = true) {
|
|
14
|
+
const [C0, C1, C2, C3, C4, C5] = converters;
|
|
15
|
+
const [N0, N1, N2, N3, N4, N5] = converters.map((convert) => convert.needsOwners ?? needsArgumentOwners);
|
|
16
|
+
const hasAdapters = converters.some((convert) => convert.requiresOwners);
|
|
17
|
+
const primitivesPass = converters.every((convert) => convert.passesPrimitives === true);
|
|
18
|
+
const sync = method !== "callAsync";
|
|
19
|
+
switch (converters.length) {
|
|
20
|
+
case 0:
|
|
21
|
+
return function () {
|
|
22
|
+
const checked = captureHere ? state.errcheck : null;
|
|
23
|
+
if (checked !== null || arguments.length !== 0) return invoke(arguments, checked);
|
|
24
|
+
return ffiFunc[method]();
|
|
25
|
+
};
|
|
26
|
+
case 1:
|
|
27
|
+
return function (a0) {
|
|
28
|
+
const checked = captureHere ? state.errcheck : null;
|
|
29
|
+
if (checked !== null || arguments.length !== 1) return invoke(arguments, checked);
|
|
30
|
+
if (captureHere && (hasAdapters || N0(a0))) return sync ? callOwned1(ffiFunc, method, C0, a0) : callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
31
|
+
return ffiFunc[method](C0(a0));
|
|
32
|
+
};
|
|
33
|
+
case 2:
|
|
34
|
+
return function (a0, a1) {
|
|
35
|
+
const checked = captureHere ? state.errcheck : null;
|
|
36
|
+
if (checked !== null || arguments.length !== 2) return invoke(arguments, checked);
|
|
37
|
+
if (captureHere && (hasAdapters || N0(a0) || N1(a1))) return sync ? callOwned2(ffiFunc, method, C0, C1, a0, a1) : callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
38
|
+
return ffiFunc[method](C0(a0), C1(a1));
|
|
39
|
+
};
|
|
40
|
+
case 3:
|
|
41
|
+
return function (a0, a1, a2) {
|
|
42
|
+
const checked = captureHere ? state.errcheck : null;
|
|
43
|
+
if (checked !== null || arguments.length !== 3) return invoke(arguments, checked);
|
|
44
|
+
if (captureHere && (hasAdapters || N0(a0) || N1(a1) || N2(a2))) return sync ? callOwned3(ffiFunc, method, C0, C1, C2, a0, a1, a2) : callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
45
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2));
|
|
46
|
+
};
|
|
47
|
+
case 4:
|
|
48
|
+
return function (a0, a1, a2, a3) {
|
|
49
|
+
const checked = captureHere ? state.errcheck : null;
|
|
50
|
+
if (checked !== null || arguments.length !== 4) return invoke(arguments, checked);
|
|
51
|
+
if (captureHere && (hasAdapters || N0(a0) || N1(a1) || N2(a2) || N3(a3))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
52
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3));
|
|
53
|
+
};
|
|
54
|
+
case 5:
|
|
55
|
+
return function (a0, a1, a2, a3, a4) {
|
|
56
|
+
const checked = captureHere ? state.errcheck : null;
|
|
57
|
+
if (checked !== null || arguments.length !== 5) return invoke(arguments, checked);
|
|
58
|
+
if (captureHere && (hasAdapters || N0(a0) || N1(a1) || N2(a2) || N3(a3) || N4(a4))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
59
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4));
|
|
60
|
+
};
|
|
61
|
+
case 6:
|
|
62
|
+
return function (a0, a1, a2, a3, a4, a5) {
|
|
63
|
+
const checked = captureHere ? state.errcheck : null;
|
|
64
|
+
if (checked !== null || arguments.length !== 6) return invoke(arguments, checked);
|
|
65
|
+
if (captureHere && (hasAdapters || N0(a0) || N1(a1) || N2(a2) || N3(a3) || N4(a4) || N5(a5))) return callWithArgumentOwners(ffiFunc, method, arguments, converters);
|
|
66
|
+
return ffiFunc[method](C0(a0), C1(a1), C2(a2), C3(a3), C4(a4), C5(a5));
|
|
67
|
+
};
|
|
68
|
+
default:
|
|
69
|
+
return function () {
|
|
70
|
+
const checked = captureHere ? state.errcheck : null;
|
|
71
|
+
// Scalar parameters receiving primitives need no conversion and no
|
|
72
|
+
// owners: the arguments object goes to the native call as it is.
|
|
73
|
+
if (primitivesPass && checked === null && arguments.length === converters.length) {
|
|
74
|
+
let primitive = true;
|
|
75
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
76
|
+
const value = arguments[i];
|
|
77
|
+
if (value !== null && (typeof value === "object" || typeof value === "function")) {
|
|
78
|
+
primitive = false;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (primitive) return reflectApply(ffiFunc[method], ffiFunc, arguments);
|
|
83
|
+
}
|
|
84
|
+
return invoke(arguments, checked);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Synchronous owned calls nest strictly: the owners array is a stack entry
|
|
90
|
+
// and the converters run without an intermediate argument array.
|
|
91
|
+
function callOwned1(ffiFunc, method, C0, a0) {
|
|
92
|
+
const owners = [];
|
|
93
|
+
activeOwnerStack.push(owners);
|
|
94
|
+
try {
|
|
95
|
+
return ffiFunc[method](C0(a0, owners));
|
|
96
|
+
} finally {
|
|
97
|
+
activeOwnerStack.pop();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function callOwned2(ffiFunc, method, C0, C1, a0, a1) {
|
|
102
|
+
const owners = [];
|
|
103
|
+
activeOwnerStack.push(owners);
|
|
104
|
+
try {
|
|
105
|
+
return ffiFunc[method](C0(a0, owners), C1(a1, owners));
|
|
106
|
+
} finally {
|
|
107
|
+
activeOwnerStack.pop();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function callOwned3(ffiFunc, method, C0, C1, C2, a0, a1, a2) {
|
|
112
|
+
const owners = [];
|
|
113
|
+
activeOwnerStack.push(owners);
|
|
114
|
+
try {
|
|
115
|
+
return ffiFunc[method](C0(a0, owners), C1(a1, owners), C2(a2, owners));
|
|
116
|
+
} finally {
|
|
117
|
+
activeOwnerStack.pop();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The native asynchronous entry: on the Executor of `call` (callOn: the
|
|
122
|
+
// target of this invocation only) or, without it, on the libuv pool.
|
|
123
|
+
function invokeAsync(ffiFunc, call, resultHandler, converted) {
|
|
124
|
+
if (call === undefined) return resultHandler === undefined ? ffiFunc.callAsync(...converted) : ffiFunc._callAsyncWithErrcheck(resultHandler, ...converted);
|
|
125
|
+
return resultHandler === undefined ? ffiFunc.callOn(call, ...converted) : ffiFunc._callOnWithErrcheck(call, resultHandler, ...converted);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The registry is a GC root, including while another converter reenters JS.
|
|
129
|
+
// `call`: the callOn target of an asynchronous call.
|
|
130
|
+
export function callWithArgumentOwners(ffiFunc, method, args, converters, finish, call, resultHandler, checkedCall) {
|
|
131
|
+
const owners = [];
|
|
132
|
+
// Synchronous calls nest strictly, so a stack avoids hashing a fresh array.
|
|
133
|
+
activeOwnerStack.push(owners);
|
|
134
|
+
try {
|
|
135
|
+
const converted = convertArguments(args, converters, owners);
|
|
136
|
+
const result =
|
|
137
|
+
method === "callAsync"
|
|
138
|
+
? invokeAsync(ffiFunc, call, resultHandler, converted)
|
|
139
|
+
: resultHandler === undefined
|
|
140
|
+
? ffiFunc[method](...converted)
|
|
141
|
+
: checkedCall === undefined
|
|
142
|
+
? ffiFunc._callWithErrcheck(resultHandler, ...converted)
|
|
143
|
+
: checkedCall(resultHandler, ...converted);
|
|
144
|
+
if (method === "callAsync") {
|
|
145
|
+
const completed = finish === undefined ? result : result.then((value) => finish(value, args));
|
|
146
|
+
activeArgumentOwners.add(owners);
|
|
147
|
+
return completed.finally(() => activeArgumentOwners.delete(owners));
|
|
148
|
+
}
|
|
149
|
+
return finish === undefined ? result : finish(result, args);
|
|
150
|
+
} finally {
|
|
151
|
+
activeOwnerStack.pop();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Only the native entry belongs to the binding. Hook, public callable and
|
|
156
|
+
// original arguments belong to this invocation, even if a converter reenters.
|
|
157
|
+
export function createCheckedInvoker(ffiFunc, converters, hasAdapters, convertResult, publicCallable) {
|
|
158
|
+
let checkedSync;
|
|
159
|
+
return (method, args, checked, target) => {
|
|
160
|
+
const original = Array.isArray(args) ? args : Array.prototype.slice.call(args);
|
|
161
|
+
const fn = publicCallable();
|
|
162
|
+
const async = method === "callAsync";
|
|
163
|
+
const finish = (result) => checked(convertResult === undefined ? result : convertResult(result, async), fn, original);
|
|
164
|
+
const entry = async ? undefined : (checkedSync ??= ffiFunc._makeCheckedCall());
|
|
165
|
+
if (hasAdapters || argumentsNeedOwners(original)) return callWithArgumentOwners(ffiFunc, method, original, converters, undefined, target, finish, entry);
|
|
166
|
+
if (entry !== undefined) return entry(finish, ...convertArguments(original, converters));
|
|
167
|
+
if (async) return invokeAsync(ffiFunc, target, finish, convertArguments(original, converters));
|
|
168
|
+
return ffiFunc._callWithErrcheck(finish, ...convertArguments(original, converters));
|
|
169
|
+
};
|
|
170
|
+
}
|