@@ -44,12 +44,13 @@ var LibraryEmbind = {
4444 } ,
4545 $FunctionDefinition__deps : [ '$createJsInvoker' , '$createJsInvokerSignature' , '$emittedFunctions' ] ,
4646 $FunctionDefinition : class {
47- constructor ( name , returnType , argumentTypes , functionIndex , thisType = null , isAsync = false ) {
47+ constructor ( name , returnType , argumentTypes , functionIndex , thisType = null , isConstructor = false , isAsync = false ) {
4848 this . name = name ;
4949 this . returnType = returnType ;
5050 this . argumentTypes = argumentTypes ;
5151 this . functionIndex = functionIndex ;
5252 this . thisType = thisType ;
53+ this . isConstructor = isConstructor ;
5354 this . isAsync = isAsync ;
5455 }
5556
@@ -75,7 +76,14 @@ var LibraryEmbind = {
7576 }
7677
7778 out . push ( argOut . join ( ', ' ) ) ;
78- out . push ( `): ${ nameMap ( this . returnType , true ) } ` ) ;
79+ let returnType = this . returnType ;
80+ // Constructors can return a pointer, but it will be a non-null pointer.
81+ // Change the return type to the class type so the TS output doesn't
82+ // have `| null`.
83+ if ( this . isConstructor && this . returnType instanceof PointerDefinition ) {
84+ returnType = this . returnType . classType ;
85+ }
86+ out . push ( `): ${ nameMap ( returnType , true ) } ` ) ;
7987 }
8088
8189 printFunction ( nameMap , out ) {
@@ -183,7 +191,7 @@ var LibraryEmbind = {
183191 printModuleEntry ( nameMap , out ) {
184192 out . push ( ` ${ this . name } : {` ) ;
185193 const entries = [ ] ;
186- for ( const construct of this . constructors ) {
194+ for ( const construct of this . constructors ) {
187195 const entry = [ ] ;
188196 entry . push ( 'new' ) ;
189197 construct . printSignature ( nameMap , entry ) ;
@@ -368,7 +376,7 @@ var LibraryEmbind = {
368376 return tsName ;
369377 }
370378 if ( type instanceof PointerDefinition ) {
371- return this . typeToJsName ( type . classType ) ;
379+ return ` ${ this . typeToJsName ( type . classType ) } | null` ;
372380 }
373381 if ( type instanceof OptionalType ) {
374382 return `${ this . typeToJsName ( type . type ) } | undefined` ;
@@ -432,7 +440,7 @@ var LibraryEmbind = {
432440 registerType ( id , new IntegerType ( id ) ) ;
433441 } ,
434442 $createFunctionDefinition__deps : [ '$FunctionDefinition' , '$heap32VectorToArray' , '$readLatin1String' , '$Argument' , '$whenDependentTypesAreResolved' , '$getFunctionName' , '$getFunctionArgsName' , '$PointerDefinition' , '$ClassDefinition' ] ,
435- $createFunctionDefinition : ( name , argCount , rawArgTypesAddr , functionIndex , hasThis , isAsync , cb ) => {
443+ $createFunctionDefinition : ( name , argCount , rawArgTypesAddr , functionIndex , hasThis , isConstructor , isAsync , cb ) => {
436444 const argTypes = heap32VectorToArray ( argCount , rawArgTypesAddr ) ;
437445 name = typeof name === 'string' ? name : readLatin1String ( name ) ;
438446
@@ -462,7 +470,7 @@ var LibraryEmbind = {
462470 args . push ( new Argument ( `_${ i - argStart } ` , argTypes [ i ] ) ) ;
463471 }
464472 }
465- const funcDef = new FunctionDefinition ( name , returnType , args , functionIndex , thisType , isAsync ) ;
473+ const funcDef = new FunctionDefinition ( name , returnType , args , functionIndex , thisType , isConstructor , isAsync ) ;
466474 cb ( funcDef ) ;
467475 return [ ] ;
468476 } ) ;
@@ -514,7 +522,7 @@ var LibraryEmbind = {
514522 } ,
515523 _embind_register_function__deps : [ '$moduleDefinitions' , '$createFunctionDefinition' ] ,
516524 _embind_register_function : ( name , argCount , rawArgTypesAddr , signature , rawInvoker , fn , isAsync ) => {
517- createFunctionDefinition ( name , argCount , rawArgTypesAddr , fn , false , isAsync , ( funcDef ) => {
525+ createFunctionDefinition ( name , argCount , rawArgTypesAddr , fn , false , false , isAsync , ( funcDef ) => {
518526 moduleDefinitions . push ( funcDef ) ;
519527 } ) ;
520528 } ,
@@ -559,7 +567,7 @@ var LibraryEmbind = {
559567 ) {
560568 whenDependentTypesAreResolved ( [ ] , [ rawClassType ] , function ( classType ) {
561569 classType = classType [ 0 ] ;
562- createFunctionDefinition ( `constructor ${ classType . name } ` , argCount , rawArgTypesAddr , rawConstructor , false , false , ( funcDef ) => {
570+ createFunctionDefinition ( `constructor ${ classType . name } ` , argCount , rawArgTypesAddr , rawConstructor , false , true , false , ( funcDef ) => {
563571 classType . constructors . push ( funcDef ) ;
564572 } ) ;
565573 return [ ] ;
@@ -575,7 +583,7 @@ var LibraryEmbind = {
575583 context ,
576584 isPureVirtual ,
577585 isAsync ) {
578- createFunctionDefinition ( methodName , argCount , rawArgTypesAddr , context , true , isAsync , ( funcDef ) => {
586+ createFunctionDefinition ( methodName , argCount , rawArgTypesAddr , context , true , false , isAsync , ( funcDef ) => {
579587 const classDef = funcDef . thisType ;
580588 classDef . methods . push ( funcDef ) ;
581589 } ) ;
@@ -616,7 +624,7 @@ var LibraryEmbind = {
616624 isAsync ) {
617625 whenDependentTypesAreResolved ( [ ] , [ rawClassType ] , function ( classType ) {
618626 classType = classType [ 0 ] ;
619- createFunctionDefinition ( methodName , argCount , rawArgTypesAddr , fn , false , isAsync , ( funcDef ) => {
627+ createFunctionDefinition ( methodName , argCount , rawArgTypesAddr , fn , false , false , isAsync , ( funcDef ) => {
620628 classType . staticMethods . push ( funcDef ) ;
621629 } ) ;
622630 return [ ] ;
0 commit comments