@@ -59,7 +59,7 @@ public JSExportCodeGenerator(
5959
6060 public BlockSyntax GenerateJSExportBody ( )
6161 {
62- StatementSyntax invoke = InvokeSyntax ( ) ;
62+ List < StatementSyntax > invoke = InvokeSyntax ( ) ;
6363 GeneratedStatements statements = GeneratedStatements . Create ( _marshallers , _context ) ;
6464 bool shouldInitializeVariables = ! statements . GuaranteedUnmarshal . IsEmpty || ! statements . CleanupCallerAllocated . IsEmpty || ! statements . CleanupCalleeAllocated . IsEmpty ;
6565 VariableDeclarations declarations = VariableDeclarations . GenerateDeclarationsForUnmanagedToManaged ( _marshallers , _context , shouldInitializeVariables ) ;
@@ -79,7 +79,7 @@ public BlockSyntax GenerateJSExportBody()
7979 var tryStatements = new List < StatementSyntax > ( ) ;
8080 tryStatements . AddRange ( statements . Unmarshal ) ;
8181
82- tryStatements . Add ( invoke ) ;
82+ tryStatements . AddRange ( invoke ) ;
8383
8484 if ( ! ( statements . GuaranteedUnmarshal . IsEmpty && statements . CleanupCalleeAllocated . IsEmpty ) )
8585 {
@@ -93,23 +93,33 @@ public BlockSyntax GenerateJSExportBody()
9393 tryStatements . AddRange ( statements . Marshal ) ;
9494
9595 List < StatementSyntax > allStatements = setupStatements ;
96+
97+ // Wrap unmarshall, invocation and return value marshalling in try-catch.
98+ // In case of exception, marshal exception instead of return value.
99+ var tryInvokeAndMarshal = TryStatement ( SingletonList ( CatchClause ( )
100+ . WithDeclaration ( CatchDeclaration ( IdentifierName ( Constants . ExceptionGlobal ) ) . WithIdentifier ( Identifier ( "ex" ) ) )
101+ . WithBlock ( Block ( SingletonList < StatementSyntax > (
102+ ExpressionStatement ( InvocationExpression (
103+ MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
104+ IdentifierName ( Constants . ArgumentException ) , IdentifierName ( Constants . ToJSMethod ) ) )
105+ . WithArgumentList ( ArgumentList ( SingletonSeparatedList ( Argument ( IdentifierName ( "ex" ) ) ) ) ) ) ) ) ) ) )
106+ . WithBlock ( Block ( tryStatements ) ) ;
107+
96108 List < StatementSyntax > finallyStatements = new List < StatementSyntax > ( ) ;
97109 if ( ! ( statements . GuaranteedUnmarshal . IsEmpty && statements . CleanupCalleeAllocated . IsEmpty ) )
98110 {
99111 finallyStatements . Add ( IfStatement ( IdentifierName ( InvokeSucceededIdentifier ) , Block ( statements . GuaranteedUnmarshal . Concat ( statements . CleanupCalleeAllocated ) ) ) ) ;
100112 }
101113
102114 finallyStatements . AddRange ( statements . CleanupCallerAllocated ) ;
115+
103116 if ( finallyStatements . Count > 0 )
104117 {
105- allStatements . Add (
106- TryStatement ( Block ( tryStatements ) , default , FinallyClause ( Block ( finallyStatements ) ) ) ) ;
107- }
108- else
109- {
110- allStatements . AddRange ( tryStatements ) ;
118+ tryInvokeAndMarshal = TryStatement ( Block ( tryInvokeAndMarshal ) , default , FinallyClause ( Block ( finallyStatements ) ) ) ;
111119 }
112120
121+ allStatements . Add ( tryInvokeAndMarshal ) ;
122+
113123 return Block ( allStatements ) ;
114124 }
115125
@@ -175,7 +185,7 @@ private void SetupSyntax(List<StatementSyntax> statementsToUpdate)
175185 Argument ( LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( 1 ) ) ) ) ) ) ) ) ) ) ) ) ) ;
176186 }
177187
178- private TryStatementSyntax InvokeSyntax ( )
188+ private List < StatementSyntax > InvokeSyntax ( )
179189 {
180190 var statements = new List < StatementSyntax > ( ) ;
181191 var arguments = new List < ArgumentSyntax > ( ) ;
@@ -205,16 +215,8 @@ private TryStatementSyntax InvokeSyntax()
205215 IdentifierName ( nativeIdentifier ) , invocation ) ) ;
206216
207217 statements . Add ( statement ) ;
208- statements . AddRange ( _marshallers . ManagedReturnMarshaller . Generator . Generate ( _marshallers . ManagedReturnMarshaller . TypeInfo , _context with { CurrentStage = StubCodeContext . Stage . Marshal } ) ) ;
209218 }
210- return TryStatement ( SingletonList ( CatchClause ( )
211- . WithDeclaration ( CatchDeclaration ( IdentifierName ( Constants . ExceptionGlobal ) ) . WithIdentifier ( Identifier ( "ex" ) ) )
212- . WithBlock ( Block ( SingletonList < StatementSyntax > (
213- ExpressionStatement ( InvocationExpression (
214- MemberAccessExpression ( SyntaxKind . SimpleMemberAccessExpression ,
215- IdentifierName ( Constants . ArgumentException ) , IdentifierName ( Constants . ToJSMethod ) ) )
216- . WithArgumentList ( ArgumentList ( SingletonSeparatedList ( Argument ( IdentifierName ( "ex" ) ) ) ) ) ) ) ) ) ) )
217- . WithBlock ( Block ( statements ) ) ;
219+ return statements ;
218220
219221 }
220222
0 commit comments