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

Integers in Swift were down casted to 32 bits integers. Fixed #688 #695

Merged
merged 2 commits into from Jul 30, 2014

Conversation

Reflejo
Copy link
Contributor

@Reflejo Reflejo commented Jul 28, 2014

When using Swift, Int type is Int32 on 32bits platforms and Int64 on 64 bits platforms. In order to avoid down casting, objcType should be "l" when using 64 bits.

Also added some tests. Note that when assigning this on 32bits platforms:

let longNumber = 17179869184
let intNumber = 2147483647
let negativeLongNumber = -17179869184
let updatedLongNumber = 8589934592

long values will be down casted to 32 bits. Meaning that 17179869184 is going to be 0 as well as 8589934592. But since the value inserted will be 0 the test will pass.

I recommend supporting Int64 as well as Int32 explicitly on Realm.

See #688 for more information.

@@ -86,7 +86,7 @@ import Foundation
case is Bool.Type, is Bool?.Type:
(p, t) = (RLMProperty(name: name, type: .Bool, objectClassName: nil, attributes: attr), "c")
case is Int.Type, is Int?.Type:
(p, t) = (RLMProperty(name: name, type: .Int, objectClassName: nil, attributes: attr), "i")
(p, t) = (RLMProperty(name: name, type: .Int, objectClassName: nil, attributes: attr), "l")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check for __LP64__ here too and use "i" if not defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't do it is because that conditional check doesn't work (just yet?) on swift. Actually a fun fact is: if you explore the internal attributes (using objc_getattributes) of the Int type from swift (either 32bits or 64bits). The result is always "l".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By "doesn't work" I mean LP64 is not defined. I didn't dig on why clang is not including it tho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a FIXME comment so we know fix this later? I think in the long run we will have to move this code to objc to make this work, but for now this is an improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a FIXME, but I think the entire swift file should be replaced eventually. Maintaining the reflection logic in two places is going to be a PIA on the long run. I'm hopping that the next beta would come with a way from objc to get a Swift property type (main problem right now is String which is reflected as "@" from objc). Hopefully it'd be a way of doing this in the near future and this files (rather than this particular line) can be replaced.

And to be honest "l" is the safest choice anyway. If we could do a LP64 conditional, Swift Int type on 32bits would be "l" and on 64bits would be "q"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking for __LP64__, we could do the following:

p = RLMProperty(name: name, type: .Int, objectClassName: nil, attributes: attr)
#if arch(x86_64) || arch(arm64)
t = "l"
#else
t = "i"
#endif

@alazier
Copy link
Contributor

alazier commented Jul 28, 2014

Thoughts @jpsim ?

@jpsim
Copy link
Contributor

jpsim commented Jul 30, 2014

Thanks for your work on this, @Reflejo, it's certainly an improvement. I'd be happy merging this in, if we do the platform check I mentioned (arch(x86_64) || arch(arm64)).

@Reflejo
Copy link
Contributor Author

Reflejo commented Jul 30, 2014

Preprocessor statement added.

@jpsim
Copy link
Contributor

jpsim commented Jul 30, 2014

This works for me, @alazier any other remarks?

alazier added a commit that referenced this pull request Jul 30, 2014
Integers in Swift were down casted to 32 bits integers. Fixed #688
@alazier alazier merged commit d109d57 into realm:master Jul 30, 2014
@alazier
Copy link
Contributor

alazier commented Jul 30, 2014

let it merge

@Reflejo Reflejo deleted the swift-long-property-fix-2 branch July 30, 2014 21:42
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants