-
Notifications
You must be signed in to change notification settings - Fork 46
[JExtract] Bridge closures with UnsafeRawBufferPointer parameter #282
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,8 @@ enum ConversionStep: Equatable { | |
/// Perform multiple conversions using the same input. | ||
case aggregate([ConversionStep], name: String?) | ||
|
||
indirect case closureLowering(parameters: [ConversionStep], result: ConversionStep) | ||
|
||
indirect case member(ConversionStep, member: String) | ||
|
||
/// Count the number of times that the placeholder occurs within this | ||
|
@@ -73,13 +75,20 @@ enum ConversionStep: Equatable { | |
inner.placeholderCount | ||
case .initialize(_, arguments: let arguments): | ||
arguments.reduce(0) { $0 + $1.argument.placeholderCount } | ||
case .placeholder, .tupleExplode: | ||
case .placeholder, .tupleExplode, .closureLowering: | ||
1 | ||
case .tuplify(let elements), .aggregate(let elements, _): | ||
elements.reduce(0) { $0 + $1.placeholderCount } | ||
} | ||
} | ||
|
||
var isPlaceholder: Bool { | ||
if case .placeholder = self { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/// Convert the conversion step into an expression with the given | ||
/// value as the placeholder value in the expression. | ||
func asExprSyntax(placeholder: String, bodyItems: inout [CodeBlockItemSyntax]) -> ExprSyntax? { | ||
|
@@ -165,6 +174,48 @@ enum ConversionStep: Equatable { | |
} | ||
} | ||
return nil | ||
|
||
case .closureLowering(let parameterSteps, let resultStep): | ||
var body: [CodeBlockItemSyntax] = [] | ||
|
||
// Lower parameters. | ||
var params: [String] = [] | ||
var args: [ExprSyntax] = [] | ||
for (i, parameterStep) in parameterSteps.enumerated() { | ||
let paramName = "_\(i)" | ||
params.append(paramName) | ||
if case .tuplify(let elemSteps) = parameterStep { | ||
for elemStep in elemSteps { | ||
if let elemExpr = elemStep.asExprSyntax(placeholder: paramName, bodyItems: &body) { | ||
args.append(elemExpr) | ||
} | ||
} | ||
} else if let paramExpr = parameterStep.asExprSyntax(placeholder: paramName, bodyItems: &body) { | ||
args.append(paramExpr) | ||
} | ||
} | ||
|
||
// Call the lowered closure with lowered parameters. | ||
let loweredResult = "\(placeholder)(\(args.map(\.description).joined(separator: ", ")))" | ||
|
||
// Raise the lowered result. | ||
let result = resultStep.asExprSyntax(placeholder: loweredResult.description, bodyItems: &body) | ||
body.append("return \(result)") | ||
|
||
// Construct the closure expression. | ||
var closure = ExprSyntax( | ||
""" | ||
{ (\(raw: params.joined(separator: ", "))) in | ||
} | ||
""" | ||
).cast(ClosureExprSyntax.self) | ||
|
||
closure.statements = CodeBlockItemListSyntax { | ||
body.map { | ||
$0.with(\.leadingTrivia, [.newlines(1), .spaces(4)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I didn't know about .newlines, that's nice |
||
} | ||
} | ||
return ExprSyntax(closure) | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that we now have another lower/translate function for closure parameters (in addition to regular parameters, and results), and probably we'd add more for closure result conversions. But this works for now, and we can consolidate them after we implement more types, and find common logics among them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but still managable... agreed on converging later if we find ways to do so 👍