win32-def
Version:
win32 definitions for node-ffi
63 lines • 2.13 kB
JavaScript
import assert from 'node:assert';
// #region processDefList()
export function processDefList(dllFuncs, fns) {
const map = genDefList(dllFuncs);
const map2 = new Map();
if (fns && Array.isArray(fns) && fns.length) {
fns.forEach((fnName) => {
if (fnName.endsWith('_Async')) {
const syncFnName = fnName.replace(/_Async$/u, '');
const data = map.get(syncFnName);
if (data) {
map2.set(syncFnName, data);
}
}
else { // sync
const data = map.get(fnName);
if (data) {
map2.set(fnName, data);
}
}
});
return map2;
}
return map;
}
// #region genDefList()
/**
* Generate function definitions via converting macro windows data type (like PVOID) to the expected value.
*/
export function genDefList(dllFuncs) {
const ret = typeof dllFuncs === 'function'
? genListFromClass(dllFuncs) // class def
: genListFromObject(dllFuncs); // const def
return ret;
}
function genListFromObject(input) {
const ret = new Map();
for (const fnName of Object.keys(input)) {
// @ts-ignore
const ps = input[fnName];
if (Array.isArray(ps) && ps.length === 2) {
assert(ps[0] && ps[1], `dellFuncs has no property method name "${fnName}"`);
assert(typeof ps[0] === 'string', `dellFuncs has no property method name "${fnName}"`);
ret.set(fnName, ps);
}
}
return ret;
}
function genListFromClass(input) {
assert(typeof input === 'function', 'input must be a class');
const ret = new Map();
for (const key in input) {
// @ts-ignore
const ps = input[key];
if (Array.isArray(ps) && ps.length === 2) {
assert(ps[0] && ps[1], `dellFuncs has no property method name "${key}"`);
assert(typeof ps[0] === 'string', `dellFuncs has no property method name "${key}"`);
ret.set(key, ps);
}
}
return ret;
}
//# sourceMappingURL=def.helper.js.map