Skip to content

Commit

Permalink
[release/8.0] [browser] Remove duplicated marshaling of return value …
Browse files Browse the repository at this point in the history
…for JSExport (#92886)

* Remove duplicated marshaling of return value for JSExport

* Move unmarshal and return value marshal into try block

---------

Co-authored-by: Marek Fišera <mara@neptuo.com>
  • Loading branch information
github-actions[bot] and maraf authored Oct 3, 2023
1 parent 8632a75 commit 172d79a
Showing 1 changed file with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public JSExportCodeGenerator(

public BlockSyntax GenerateJSExportBody()
{
StatementSyntax invoke = InvokeSyntax();
List<StatementSyntax> invoke = InvokeSyntax();
GeneratedStatements statements = GeneratedStatements.Create(_marshallers, _context);
bool shouldInitializeVariables = !statements.GuaranteedUnmarshal.IsEmpty || !statements.CleanupCallerAllocated.IsEmpty || !statements.CleanupCalleeAllocated.IsEmpty;
VariableDeclarations declarations = VariableDeclarations.GenerateDeclarationsForUnmanagedToManaged(_marshallers, _context, shouldInitializeVariables);
Expand All @@ -79,7 +79,7 @@ public BlockSyntax GenerateJSExportBody()
var tryStatements = new List<StatementSyntax>();
tryStatements.AddRange(statements.Unmarshal);

tryStatements.Add(invoke);
tryStatements.AddRange(invoke);

if (!(statements.GuaranteedUnmarshal.IsEmpty && statements.CleanupCalleeAllocated.IsEmpty))
{
Expand All @@ -93,23 +93,33 @@ public BlockSyntax GenerateJSExportBody()
tryStatements.AddRange(statements.Marshal);

List<StatementSyntax> allStatements = setupStatements;

// Wrap unmarshall, invocation and return value marshalling in try-catch.
// In case of exception, marshal exception instead of return value.
var tryInvokeAndMarshal = TryStatement(SingletonList(CatchClause()
.WithDeclaration(CatchDeclaration(IdentifierName(Constants.ExceptionGlobal)).WithIdentifier(Identifier("ex")))
.WithBlock(Block(SingletonList<StatementSyntax>(
ExpressionStatement(InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(Constants.ArgumentException), IdentifierName(Constants.ToJSMethod)))
.WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("ex")))))))))))
.WithBlock(Block(tryStatements));

List<StatementSyntax> finallyStatements = new List<StatementSyntax>();
if (!(statements.GuaranteedUnmarshal.IsEmpty && statements.CleanupCalleeAllocated.IsEmpty))
{
finallyStatements.Add(IfStatement(IdentifierName(InvokeSucceededIdentifier), Block(statements.GuaranteedUnmarshal.Concat(statements.CleanupCalleeAllocated))));
}

finallyStatements.AddRange(statements.CleanupCallerAllocated);

if (finallyStatements.Count > 0)
{
allStatements.Add(
TryStatement(Block(tryStatements), default, FinallyClause(Block(finallyStatements))));
}
else
{
allStatements.AddRange(tryStatements);
tryInvokeAndMarshal = TryStatement(Block(tryInvokeAndMarshal), default, FinallyClause(Block(finallyStatements)));
}

allStatements.Add(tryInvokeAndMarshal);

return Block(allStatements);
}

Expand Down Expand Up @@ -175,7 +185,7 @@ private void SetupSyntax(List<StatementSyntax> statementsToUpdate)
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)))))))))))));
}

private TryStatementSyntax InvokeSyntax()
private List<StatementSyntax> InvokeSyntax()
{
var statements = new List<StatementSyntax>();
var arguments = new List<ArgumentSyntax>();
Expand Down Expand Up @@ -205,16 +215,8 @@ private TryStatementSyntax InvokeSyntax()
IdentifierName(nativeIdentifier), invocation));

statements.Add(statement);
statements.AddRange(_marshallers.ManagedReturnMarshaller.Generator.Generate(_marshallers.ManagedReturnMarshaller.TypeInfo, _context with { CurrentStage = StubCodeContext.Stage.Marshal }));
}
return TryStatement(SingletonList(CatchClause()
.WithDeclaration(CatchDeclaration(IdentifierName(Constants.ExceptionGlobal)).WithIdentifier(Identifier("ex")))
.WithBlock(Block(SingletonList<StatementSyntax>(
ExpressionStatement(InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(Constants.ArgumentException), IdentifierName(Constants.ToJSMethod)))
.WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("ex")))))))))))
.WithBlock(Block(statements));
return statements;

}

Expand Down

0 comments on commit 172d79a

Please sign in to comment.