Skip to content

Commit

Permalink
Add recursive Struct support
Browse files Browse the repository at this point in the history
  • Loading branch information
sliftist authored and waitingsong committed Jun 14, 2024
1 parent 46157bd commit 088c3c7
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/win32-def/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ export function StructType<T extends StructDefType>(
input: T,
options?: StructCharOptions,
): StructTypeConstructor<T> {

const initType = genInitTyp(input, options)
// @ts-ignore
return Struct(initType)
return genInitTyp<T>(input, options)
}
export function StructFactory<T>(input: StructDefType, options?: StructCharOptions): T {
const initType = genInitTyp(input, options)
// @ts-ignore
return new Struct(initType)() as unknown as T
return new genInitTyp<T>(input, options)()
}

function genInitTyp(input: StructDefType, options?: StructCharOptions): StructDefType {
function genInitTyp(input: StructDefType, options?: StructCharOptions): StructTypeConstructor {
const opts: Required<StructCharOptions> = {
...defaultStructCharOptions,
...options,
Expand All @@ -61,14 +58,20 @@ function genInitTyp(input: StructDefType, options?: StructCharOptions): StructDe
&& opts.CharDefs.includes(value)) {

initType[key] = wcharBuffer(opts.maxCharLength)
}
else {
// @TODO recursive convertion
} else if (
typeof value === 'object'
&& value
// This is the same check "ref-napi" does to determine if the type
&& !('size' in value && 'indirection' in value)
) {
initType[key] = genInitTyp(value as StructDefType, options) as StructTypeConstructor
} else {
initType[key] = value
}
})

return initType
// @ts-ignore
return Struct(initType)
}

export interface StructCharOptions {
Expand Down

0 comments on commit 088c3c7

Please sign in to comment.