Skip to content

Commit 897e8c4

Browse files
author
Alexander Gräfenstein
committed
generate module wrapper
1 parent fdac63d commit 897e8c4

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

lib/converter.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as webidl2 from 'webidl2'
22
import * as ts from 'typescript'
33

44
export function convertIDL(rootTypes: webidl2.IDLRootType[]) {
5-
const file = ts.createSourceFile('ammo.d.ts', '', ts.ScriptTarget.Latest, /* setParentNodes */ false, ts.ScriptKind.TS)
5+
const file = ts.createSourceFile('ammo.d.ts', '', ts.ScriptTarget.Latest, false, ts.ScriptKind.TS)
66

77
const printer = ts.createPrinter({
88
newLine: ts.NewLineKind.LineFeed,
@@ -19,13 +19,37 @@ export function convertIDL(rootTypes: webidl2.IDLRootType[]) {
1919
}
2020
}
2121

22+
// declare function Ammo(): Promise<void>;
23+
const ammoPromise = ts.createFunctionDeclaration(
24+
/* decorators */[],
25+
/* modifiers */[ts.createModifier(ts.SyntaxKind.DeclareKeyword)],
26+
/* asteriskToken */ undefined,
27+
/* name */ 'Ammo',
28+
/* typeParameters */[],
29+
/* parameters */[],
30+
/* type */ ts.createTypeReferenceNode('Promise', [ts.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)]),
31+
/* body */ undefined
32+
)
33+
// function destroy(obj: any): void;
34+
const ammoDestroy = ts.createFunctionDeclaration(
35+
/* decorators */[],
36+
/* modifiers */[],
37+
/* asteriskToken */ undefined,
38+
/* name */ 'destroy',
39+
/* typeParameters */[],
40+
/* parameters */[ts.createParameter([], [], undefined, 'obj', undefined, ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword))],
41+
/* type */ ts.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
42+
/* body */ undefined
43+
)
44+
// declare module Ammo { ... }
2245
const ammo = ts.createModuleDeclaration(
23-
[],
24-
[ts.createModifier(ts.SyntaxKind.DeclareKeyword)],
25-
ts.createIdentifier('Ammo'),
26-
ts.createModuleBlock(nodes)
46+
/* decorators */[],
47+
/* modifiers */[ts.createModifier(ts.SyntaxKind.DeclareKeyword)],
48+
/* name */ ts.createIdentifier('Ammo'),
49+
/* body */ ts.createModuleBlock([ammoDestroy, ...nodes])
2750
)
28-
return printer.printNode(ts.EmitHint.Unspecified, ammo, file)
51+
52+
return [printer.printNode(ts.EmitHint.Unspecified, ammoPromise, file), printer.printNode(ts.EmitHint.Unspecified, ammo, file)].join('\n')
2953
}
3054

3155
function convertInterface(idl: webidl2.InterfaceType) {
@@ -113,7 +137,7 @@ function convertType(idl: webidl2.IDLTypeDescription): ts.TypeNode {
113137
return ts.createArrayTypeNode(subtype)
114138
}
115139
if (idl.union) {
116-
return ts.createUnionTypeNode((idl.idlType).map(convertType))
140+
return ts.createUnionTypeNode(idl.idlType.map(convertType))
117141
}
118142
throw new Error('Unsupported IDL type')
119143
}

0 commit comments

Comments
 (0)