diff --git a/packages/win32-def/package.json b/packages/win32-def/package.json index e70a72dd..7ded8579 100644 --- a/packages/win32-def/package.json +++ b/packages/win32-def/package.json @@ -56,9 +56,11 @@ }, "license": "MIT", "dependencies": { - "@waiting/shared-core": "^23.25.3", "koffi": "^2.9.2" }, + "devDependencies": { + "@waiting/shared-core": "^23.25.5" + }, "engines": { "node": ">=18.11.0" }, diff --git a/packages/win32-def/src/lib/ffi.ts b/packages/win32-def/src/lib/ffi.ts new file mode 100644 index 00000000..748b11a4 --- /dev/null +++ b/packages/win32-def/src/lib/ffi.ts @@ -0,0 +1,36 @@ + +export type FnCallParam = string | string[] | readonly string[] +export type FnCallParams = FnCallParam[] | readonly FnCallParam[] | never[] +export type FnParamsExpand = string[][] + +export function expandFFIParamArray(input: FnCallParams): FnParamsExpand { + const res: FnParamsExpand = [] + const tmp: string[] = [] + permute(input, 0, tmp, res) + return res +} + +function permute(input: FnCallParams, index: number, current: string[], result: FnParamsExpand): void { + if (index === input.length) { + result.push(current) + return + } + + const item = input[index] + + if (Array.isArray(item)) { + const len = item.length + + for (let i = 0; i < len; i += 1) { + const tmp = item[i] ?? [] + permute(input, index + 1, current.concat(tmp), result) + } + } + else if (typeof item === 'string') { + permute(input, index + 1, current.concat([item]), result) + } + else { + throw new TypeError('invalid input') + } +} + diff --git a/packages/win32-def/src/lib/loader/loader.helper.ts b/packages/win32-def/src/lib/loader/loader.helper.ts index 444f785b..98bc0142 100644 --- a/packages/win32-def/src/lib/loader/loader.helper.ts +++ b/packages/win32-def/src/lib/loader/loader.helper.ts @@ -1,6 +1,5 @@ import assert from 'node:assert' -import { expandFFIParamArray } from '@waiting/shared-core' import koffi from 'koffi' import { loadOptionsDefault } from '##/lib/config.js' @@ -18,6 +17,8 @@ import type { UpdateMultipleChoiceMapperOptions, } from '##/lib/types.js' +import { expandFFIParamArray } from '../ffi.js' + import { LoaderCache } from './loader.cache.js' import type { BindOptions,