-
Notifications
You must be signed in to change notification settings - Fork 60
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
[swift2objc] Support wrapper classes for primitives #1984
base: main
Are you sure you want to change the base?
Conversation
atm I have declared the |
That's fine. Nothing wrong with immutable global variables. As long as you're not adding more elements to this map at runtime, it ok for it to be global. Add me as a reviewer when this PR is ready for review. |
PR HealthBreaking changes ✔️
Changelog Entry ✔️
Changes to files need to be accounted for in their respective changelogs. API leaks ✔️The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
License Headers ✔️
All source files should start with a license header. Unrelated files missing license headers
|
I think now I'll have to change it, but can you review the overall approach first.
Yea it's ready, but I think I don't have permissions to add reviewers for PRs. |
pkgs/swift2objc/lib/src/ast/declarations/built_in/built_in_declaration.dart
Outdated
Show resolved
Hide resolved
Package publishing
Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation. |
import '../../transformer/_core/utils.dart'; | ||
import '../transform.dart'; | ||
|
||
final primitiveWrappers = <ReferredType, ReferredType>{ |
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.
You're not actually using this as a map. It's only used by getPrimitiveWrapper
, and that iterates through it. So it can be a list instead. In fact, it can be an unmodifiable list.
final primitiveWrappers = List.unmodifiable(<(ReferredType, ReferredType)>[
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.
When declaring an unmodifiable list as above, the type inference fail, causing the list to be assigned a dynamic type. This lead to analyzer issues.
Also when I declare the type explicitly it causes analyzer issues.
So i create it as mutable list for now.
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.
What if you try List<(ReferredType, ReferredType)>.unmodifiable(<(ReferredType, ReferredType)>[
? Or List<(ReferredType, ReferredType)>.unmodifiable([
?
pkgs/swift2objc/lib/src/transformer/_core/primitive_wrappers.dart
Outdated
Show resolved
Hide resolved
pkgs/swift2objc/lib/src/transformer/transformers/transform_function.dart
Outdated
Show resolved
Hide resolved
pkgs/swift2objc/lib/src/transformer/transformers/transform_function.dart
Outdated
Show resolved
Hide resolved
pkgs/swift2objc/lib/src/transformer/_core/primitive_wrappers.dart
Outdated
Show resolved
Hide resolved
} | ||
|
||
ReferredType? getPrimitiveWrapper(DeclaredType other) { | ||
final primitiveWrappers = <(ReferredType, ReferredType)>[ |
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.
Make primitiveWrappers
a private global. If you declare it in here it'll be reconstructed every time.
return (wrapper, true); | ||
} | ||
|
||
ReferredType? getPrimitiveWrapper(DeclaredType other) { |
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.
Make this private: _getPrimitiveWrapper
. I suggest a refactor in maybeWrapValue
that will remove the need for calling getPrimitiveWrapper
in transform_function.dart.
// Support Optional primitives as return Type | ||
// TODO(https://github.com/dart-lang/native/issues/1743) | ||
|
||
(ReferredType, bool) getWrapperIfNeeded( |
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.
Google style is to call functions that may or may not do a thing maybeFoo
instead of fooIfNeeded
. So call this function maybeGetPrimitiveWrapper
.
@@ -90,10 +91,18 @@ MethodDeclaration _transformFunction( | |||
transformationMap, | |||
); | |||
|
|||
final shouldWrapPrimitives = originalFunction.throws && |
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.
You can make this variable just equal to originalFunction.throws
. The other two conditions will be implicitly checked inside maybeWrapValue
, if you do the refactor I suggest in there.
(String value, ReferredType type) maybeWrapValue(ReferredType type, | ||
String value, UniqueNamer globalNamer, TransformationMap transformationMap, | ||
{bool shouldWrapPrimitives = false}) { | ||
if (shouldWrapPrimitives) { |
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.
Refactor that will simplify other stuff:
final (wrappedPrimitiveType, returnsWrappedPrimitive) =
getWrapperIfNeeded(type, shouldWrapPrimitives, transformationMap);
if (returnsWrappedPrimitive) {
return ('${(wrappedPrimitiveType as DeclaredType).name}($value)', wrappedPrimitiveType);
}
@@ -174,6 +185,7 @@ List<String> _generateStatements( | |||
resultName, |
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.
You shouldn't need to call maybeWrapValue
again. Reuse the result of the earlier call.
if (originalVariable.throws || originalVariable.async) { | ||
final prefix = [ | ||
if (originalVariable.throws) 'try', | ||
if (originalVariable.async) 'await' | ||
].join(' '); | ||
|
||
final (type, _) = getWrapperIfNeeded( |
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.
Hmmm. This is a bit suspicious. Should you be calling maybeWrapValue
here, and using the wrapping code to construct the return statement on line 98?
Closes [swift2objc] Support
throws
on methods returning primitive types #1761Add Support for wrapping primitive types to return wrappers in throwing methods.
Contribution guidelines:
dart format
.Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.