MCPcopy Create free account
hub / github.com/ASDAlexander77/TypeScript2Cxx / processFunctionExpression

Method processFunctionExpression

src/emitter.ts:717–811  ·  view source on GitHub ↗
(node: ts.FunctionExpression | ts.ArrowFunction | ts.FunctionDeclaration)

Source from the content-addressed store, hash-verified

715 }
716
717 private processFunctionExpression(node: ts.FunctionExpression | ts.ArrowFunction | ts.FunctionDeclaration): void {
718 if (!node.body
719 || ((<any>node).body.statements
720 && (<any>node).body.statements.length === 0
721 && ((<any>node).body.statements).isMissingList)) {
722 // function without body;
723 return;
724 }
725
726 const noReturn = !this.hasReturn(node);
727 const noParams = node.parameters.length === 0 && !this.hasArguments(node);
728 const noCapture = !this.requireCapture(node);
729
730 const isFunctionDeclaration = node.kind === ts.SyntaxKind.FunctionDeclaration;
731 const isFunctionExpression = node.kind === ts.SyntaxKind.FunctionExpression;
732 const isFunction = isFunctionDeclaration || isFunctionExpression;
733 const isArrowFunction = node.kind === ts.SyntaxKind.ArrowFunction;
734 const writeAsLambdaCFunction = isArrowFunction || isFunction;
735 const isAnyCastRequired = node.parent.kind === ts.SyntaxKind.CallExpression || node.parent.kind === ts.SyntaxKind.ParenthesizedExpression;
736
737 if (writeAsLambdaCFunction) {
738 if (isFunctionDeclaration) {
739 // named function
740 this.processExpression(node.name);
741 this.writer.writeString(' = ');
742 } else if (isFunctionExpression && isAnyCastRequired) {
743 this.writer.writeString('any(');
744 }
745
746 // lambda
747 const noReturnPart = noReturn ? '' : '_R';
748 const noParamsPart = noParams ? '' : '_PARAMS';
749 const noCapturePart = noCapture ? '' : 'C';
750 if (isArrowFunction) {
751 const byReference = (<any>node).__lambda_by_reference ? '&' : '=';
752 this.writer.writeString(`LMB${noReturnPart}${noParamsPart}(${byReference})`);
753 } else {
754 this.writer.writeString(`FN${noCapturePart}${noReturnPart}${noParamsPart}()`);
755 }
756 }
757
758 if (writeAsLambdaCFunction && !noReturn) {
759 this.writer.writeStringNewLine(' -> any');
760 } else {
761 this.writer.writeStringNewLine();
762 }
763
764 this.writer.BeginBlock();
765
766 // read params
767 if (!noParams) {
768 this.writer.writeStringNewLine('HEADER');
769 }
770
771 node.parameters.forEach(element => {
772 if (element.name.kind !== ts.SyntaxKind.Identifier) {
773 throw new Error('Not implemented');
774 }

Callers 4

processExpressionMethod · 0.95
processTryStatementMethod · 0.95
processArrowFunctionMethod · 0.95

Calls 12

hasReturnMethod · 0.95
hasArgumentsMethod · 0.95
requireCaptureMethod · 0.95
processExpressionMethod · 0.95
declareMethod · 0.95
processStatementMethod · 0.95
writeStringMethod · 0.80
writeStringNewLineMethod · 0.80
BeginBlockMethod · 0.80
EndOfStatementMethod · 0.80
EndBlockMethod · 0.80
cancelNewLineMethod · 0.80

Tested by

no test coverage detected