Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] [browser] Remove duplicated marshaling of return value for JSExport #92886

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading