@@ -8,53 +8,34 @@ export class TypeScriptDeclaration {
88 this . module = this . root . tsDeclareModule ( modulename ) ;
99 this . module . name = modulename ;
1010 }
11-
12- this . includeList = { } ; //{key=>{value:true}}
13- this . includes = this . module . child ( ) ;
14- this . includes . tags . module = this . module . tags . module ;
15- this . structs = this . module . child ( ) ;
16- this . structs . tags . module = this . module . tags . module ;
17- this . functions = this . module . child ( ) ;
18- this . functions . tags . module = this . module . tags . module ;
19- this . constants = this . module . child ( ) ;
20- this . constants . tags . module = this . module . tags . module ;
2111 this . defined = { } ;
12+ this . functions = [ ] ;
13+ this . structs = [ ] ;
14+ this . callbacks = [ ] ;
15+ this . aliases = [ ] ;
16+ this . constants = [ ] ;
2217 }
23- addFunction ( name , api , lookups ) {
24- const options = api . binding || { } ;
25- const para = ( api . params || [ ] ) . filter ( x => ! x . binding . ignore ) . map ( x => ( { name : ( x . type == '...' || x . binding . jsType == '...' ?'...' :'' ) + x . name , type : x . binding . jsType ?? this . toJsType ( x . type , lookups ) } ) ) ;
26- const returnType = options . jsReturns ?? this . toJsType ( api . returnType , lookups ) ;
27- this . functions . tsDeclareFunction ( name , para , returnType , api . description ) ;
18+ addConstant ( name , type , description ) {
2819 this . defined [ name ] = true ;
20+ this . constants . push ( [ name , type , description ] ) ;
21+ }
22+ addFunction ( api ) {
23+ this . defined [ api . name ] = true ;
24+ this . functions . push ( api ) ;
2925 }
3026 addStruct ( api ) {
31- const options = api . binding || { } ;
32- var fields = api . fields . filter ( x => ! ! ( options . properties || { } ) [ x . name ] ) . map ( x => ( { name : x . name , description : x . description , type : this . toJsType ( x . type ) } ) ) ;
33- this . structs . tsDeclareInterface ( api . name , fields ) ;
34- this . structs . tsDeclareType ( api . name , ! ! ( options . createConstructor ) , fields ) ;
3527 this . defined [ api . name ] = true ;
28+ if ( api . fields == undefined ) debugger ;
29+ this . structs . push ( api ) ;
3630 }
37- addCallback ( name , parameters , lookups ) {
31+ addCallback ( name , parameters ) {
3832 if ( this . defined [ name ] ) return ;
39- let ret = parameters . returnType ;
40- if ( ret == undefined ) {
41- ret == 'void' ;
42- } else {
43- ret = this . toJsType ( ret , lookups ) ;
44- }
45- let inlinefn ;
46- if ( parameters . threaded ) {
47- //when inside of a worker, we pass the indentyfier that will be caught by onmessage
48- inlinefn = 'string | number' ;
49- } else {
50- inlinefn = `(${ parameters . map ( a => `${ a . name } :${ this . toJsType ( a . type . replaceAll ( '&' , '*' ) , lookups ) } ` ) . join ( ',' ) } )=>${ ret } ` ;
51- }
52- this . structs . tsAliasType ( name , inlinefn ) ;
5333 this . defined [ name ] = true ;
34+ this . callbacks . push ( { name, parameters} ) ;
5435 }
55- addAlias ( name , aliasof , lookups ) {
56- aliasof = this . toJsType ( aliasof , lookups ) ;
57- this . structs . tsAliasType ( name , aliasof ) ;
36+ addAlias ( name , aliasof ) {
37+ this . defined [ name ] = true ;
38+ this . aliases . push ( { name, aliasof} ) ;
5839 }
5940 toJsType ( type , lookups ) {
6041 if ( type . startsWith ( 'const ' ) ) {
@@ -64,10 +45,18 @@ export class TypeScriptDeclaration {
6445 let variable = lookups . variables [ type ] ;
6546 if ( variable ) {
6647 //custom type
67- let sourceModule = lookups . includegen . tokenList . find ( a => a . type == 'include' && a . text . vars . find ( b => b . name == type ) != undefined ) ;
68- if ( sourceModule . text . name != this . module . name ) {
69- if ( this . includeList [ sourceModule ] == undefined ) this . includeList [ sourceModule ] = [ ] ;
70- this . includeList [ sourceModule ] [ type ] = true ;
48+ let sourceModule = lookups . includegen . tokenList . find ( a => a . type == 'include' && a . text . vars . find ( b => b . name == type ) != undefined ) . text . name ;
49+ let moduletypingName ;
50+ for ( let key in lookups . modules ) {
51+ const module = lookups . modules [ key ] ;
52+ if ( module . gen . c_source == sourceModule ) {
53+ moduletypingName = 'rayjs:' + module . gen . name ;
54+ break ;
55+ }
56+ }
57+ if ( moduletypingName != this . module . name ) {
58+ if ( this . includeList [ moduletypingName ] == undefined ) this . includeList [ moduletypingName ] = { } ;
59+ this . includeList [ moduletypingName ] [ type ] = true ;
7160 }
7261 }
7362 }
@@ -145,11 +134,62 @@ export class TypeScriptDeclaration {
145134 return type . replace ( " *" , "" ) . replace ( "const " , "" ) ;
146135 }
147136 }
148- writeTo ( filename ) {
137+ writeTo ( filename , lookups ) {
149138 //reset includes
139+ this . includeList = { } ; //{key=>{value:true}}
140+ this . includesGen = this . module . child ( ) ;
141+ this . includesGen . tags . module = this . module . tags . module ;
142+ this . structsGen = this . module . child ( ) ;
143+ this . structsGen . tags . module = this . module . tags . module ;
144+ this . functionsGen = this . module . child ( ) ;
145+ this . functionsGen . tags . module = this . module . tags . module ;
146+ this . constantsGen = this . module . child ( ) ;
147+ this . constantsGen . tags . module = this . module . tags . module ;
148+
149+ //Structs
150+ for ( let struct of this . structs ) {
151+ const options = struct . binding || { } ;
152+ var fields = struct . fields . filter ( x => x . binding . get ) . map ( x => ( { name : x . name , description : x . description , type : this . toJsType ( x . type ) } ) ) ;
153+ this . structsGen . tsDeclareInterface ( struct . name , fields ) ;
154+ this . structsGen . tsDeclareType ( struct . name , ! ! ( options . createConstructor ) , fields ) ;
155+ }
156+ //Callbacks
157+ for ( let callback of this . callbacks ) {
158+ let ret = callback . parameters . returnType ;
159+ if ( ret == undefined ) {
160+ ret == 'void' ;
161+ } else {
162+ ret = this . toJsType ( ret , lookups ) ;
163+ }
164+ let inlinefn ;
165+ if ( callback . parameters . threaded ) {
166+ //when inside of a worker, we pass the indentyfier that will be caught by onmessage
167+ inlinefn = 'string | number' ;
168+ } else {
169+ inlinefn = `(${ callback . parameters . map ( a => `${ a . name } :${ this . toJsType ( a . type . replaceAll ( '&' , '*' ) , lookups ) } ` ) . join ( ',' ) } )=>${ ret } ` ;
170+ }
171+ this . structsGen . tsAliasType ( callback . name , inlinefn ) ;
172+ }
173+ //Aliases
174+ for ( let alias of this . aliases ) {
175+ const aliasof = this . toJsType ( alias . aliasof , lookups ) ;
176+ this . structsGen . tsAliasType ( alias . name , aliasof ) ;
177+ }
178+ //Constants
179+ for ( let constant of this . constants ) {
180+ this . constantsGen . tsDeclareConstant ( ...constant ) ;
181+ }
182+ //Functions
183+ for ( let fn of this . functions ) {
184+ const options = fn . binding || { } ;
185+ const arg = fn . args . filter ( x => ! x . binding . ignore ) . map ( x => ( { name : ( x . type == '...' ?'...' :'' ) + x . name , type : x . binding . jsType ?? this . toJsType ( x . type , lookups ) } ) ) ;
186+ const returnType = options . jsReturns ?? this . toJsType ( fn . returnType , lookups ) ;
187+ this . functionsGen . tsDeclareFunction ( fn . name , arg , returnType , fn . description ) ;
188+ }
189+
150190 for ( let key in this . includeList ) {
151191 let toload = this . includeList [ key ] ;
152- this . includes . tsImportTypes ( key , Object . keys ( toload ) ) ;
192+ this . includesGen . tsImportTypes ( key , Object . keys ( toload ) ) ;
153193 }
154194 //generate code
155195 writeFileSync ( filename , this . root . toString ( ) ) ;
@@ -200,9 +240,9 @@ class TypescriptGenerator {
200240 let declare = this . tags . module ?'' :'declare ' ;
201241 this . line ( `${ declare } var ${ name } : {\n` ) ;
202242 this . indent ( ) ;
203- this . line ( " prototype: " + name ) ;
243+ this . line ( ` prototype: ${ name } \n` ) ;
204244 if ( hasConstructor )
205- this . line ( `new(${ parameters . map ( x => x . name + "?: " + x . type ) . join ( ', ' ) } ): ${ name } ` ) ;
245+ this . line ( `new(${ parameters . map ( x => x . name + "?: " + x . type ) . join ( ', ' ) } ): ${ name } \n ` ) ;
206246 this . unindent ( ) ;
207247 this . line ( "}\n" ) ;
208248 }
@@ -223,7 +263,7 @@ class TypescriptGenerator {
223263 this . line ( "}\n" ) ;
224264 }
225265 tsImportTypes ( module , types ) {
226- this . line ( `import type {${ types . join ( ',' ) } } from '${ module } '` ) ;
266+ this . line ( `import type {${ types . join ( ',' ) } } from '${ module } ';\n ` ) ;
227267 }
228268 tsDeclareModule ( name ) {
229269 this . line ( `declare module "${ name } " {\n` ) ;
@@ -235,7 +275,7 @@ class TypescriptGenerator {
235275 return child ;
236276 }
237277 tsAliasType ( name , aliasof ) {
238- this . line ( `type ${ name } = ${ aliasof } ` ) ;
278+ this . line ( `type ${ name } = ${ aliasof } ;\n ` ) ;
239279 }
240280 tsDocComment ( comment ) {
241281 this . line ( `/** ${ comment } */\n` ) ;
0 commit comments