Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
fix(void-fn): Handle void fn params (#21)
Browse files Browse the repository at this point in the history
fixes #16
  • Loading branch information
bengreenier authored Jan 5, 2022
1 parent 0fd7382 commit 3f20c4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
17 changes: 9 additions & 8 deletions packages/clangffi/src/lib/tsgen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,15 @@ export class TsGen implements ISourceGenerator {

const typeClass = decl.typeClass;

if (
typeClass.isFunctionType &&
typeClass.returnType &&
typeClass.paramTypes
) {
const params = typeClass.paramTypes
.map((p) => resolveType(p, this.refResolver))
.join(",");
if (typeClass.isFunctionType && typeClass.returnType) {
let params = "";

// if we actually have params, overwrite the empty string
if (typeClass.paramTypes && typeClass.paramTypes.length > 0) {
params = typeClass.paramTypes
.map((p) => resolveType(p, this.refResolver))
.join(",");
}

this.fnBuilder.appendLine(
`"${name}": [${resolveType(
Expand Down
24 changes: 15 additions & 9 deletions packages/clangffi/src/lib/tsgen/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ export class TSResolver implements ITypeNameResolver {
);

// ts needs names before types so generate that signature
const sig = params
.split(",")
.map((p, i) => `arg${i}: ${p}`.trim())
.join(", ");
// if `params.length` is zero we have no params so just use an empty string
const sig =
params.length == 0
? ""
: params
.split(",")
.map((p, i) => `arg${i}: ${p}`.trim())
.join(", ");

return `(${sig}) => ${ret}`;
}
Expand Down Expand Up @@ -245,13 +249,15 @@ export function resolveType(type: Type, resolver: ITypeNameResolver): string {
);
}
// function
else if (type.isFunctionType && type.paramTypes && type.returnType) {
else if (type.isFunctionType && type.returnType) {
resolveLog(`${type.name} is fn`);

// resolve all the params first
const params = type.paramTypes
.map((p) => resolveType(p, resolver))
.join(", ");
let params = "";

// if we actually have params, overwrite the empty string
if (type.paramTypes && type.paramTypes.length > 0) {
params = type.paramTypes.map((p) => resolveType(p, resolver)).join(", ");
}

resolveLog(`${type.name} resolved params`);

Expand Down

0 comments on commit 3f20c4a

Please sign in to comment.