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

Java: fix unsigned default value code generation and add 'L' suffix for long default value #4051

Merged
6 commits merged into from
Oct 21, 2016

Conversation

blep
Copy link
Contributor

@blep blep commented Oct 10, 2016

As mentionned in #4048 (keysearch_test), Java code generation is currently broken w.r.t. default value:

  1. if the value type is unsigned and value is in the "unsigned range" (for example 255 for a ubyte)
  2. if the value type is a long, the required 'L' suffix is missing

This PR fix those issue by "converting" unsigned value to their signed equivalent in Java (255 becomes -1), and adding 'L' suffix to long default value.

The full impact can be seen in one of our branch (where I integrated most pending PR):
https://github.com/rgilles/flatbuffers/blob/java_fieldbykey/tests/keysearch_test/Testing/KeySearch/UIntEntry.java#L19
Default value is -1 instead of 4294967295 as is currently generated:
https://github.com/rgilles/flatbuffers/blob/keysearch_test_data/tests/keysearch_test/Testing/KeySearch/UIntEntry.java#L19

@ghost
Copy link

ghost commented Oct 10, 2016

I don't think I understand. We use "user facing" signed types one size bigger whenever you read an unsigned value. So ubyte is presented as a short. If a ubyte contains 255, I would want that to be 255 as a short, not -1, right?

@blep
Copy link
Contributor Author

blep commented Oct 11, 2016

Hmm, indeed I suspect you are correct. The "signed" conversion should only be needed for ulong. Now that generated code compile, I can write some tests. I'll get back to you once I've done that.

blep added 3 commits October 11, 2016 13:18
…d specific handling as "user facing" type is int. uint need 'L' suffix as "user facing" type is long.
…buffers into java_unsigned_default_value

# Conflicts:
#	tests/MyGame/Example/Monster.java
…er facing" type in builder.add<type>() calls.
public static void addTesthashu32Fnv1(FlatBufferBuilder builder, long testhashu32Fnv1) { builder.addInt(17, (int)testhashu32Fnv1, 0); }
public static void addTesthashs64Fnv1(FlatBufferBuilder builder, long testhashs64Fnv1) { builder.addLong(18, testhashs64Fnv1, 0); }
public static void addTesthashu64Fnv1(FlatBufferBuilder builder, long testhashu64Fnv1) { builder.addLong(19, testhashu64Fnv1, 0); }
public static void addTesthashu32Fnv1(FlatBufferBuilder builder, long testhashu32Fnv1) { builder.addInt(17, (int)testhashu32Fnv1, (int)0L); }
Copy link

Choose a reason for hiding this comment

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

here it is adding both the L and an (int) cast, that may be a bit superfluous?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you asked, the code that generate the default value do it using user facing type. The problem is that some part of the builder API don't use user facing type, hence the cast.

@@ -26,7 +26,7 @@ public partial struct TestSimpleTableWithEnum : IFlatbufferObject
}

public static void StartTestSimpleTableWithEnum(FlatBufferBuilder builder) { builder.StartObject(1); }
public static void AddColor(FlatBufferBuilder builder, Color color) { builder.AddSbyte(0, (sbyte)color, 2); }
public static void AddColor(FlatBufferBuilder builder, Color color) { builder.AddSbyte(0, (sbyte)color, (sbyte)2); }
Copy link

Choose a reason for hiding this comment

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

This may not be needed in C#

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, should be fixed now.

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

@ghost
Copy link

ghost commented Oct 17, 2016

This PR gained a lot commits and changed files, something is not quite right?

@blep blep force-pushed the java_unsigned_default_value branch from d89aded to 1945e11 Compare October 18, 2016 12:01
@googlebot
Copy link

CLAs look good, thanks!

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

@blep blep force-pushed the java_unsigned_default_value branch from d89aded to 1945e11 Compare October 18, 2016 12:33
@googlebot
Copy link

CLAs look good, thanks!

@blep
Copy link
Contributor Author

blep commented Oct 18, 2016

Fixed too many commit issues (I had merged romain's master by error instead of upstream).

code += ", " + GenDefaultValue(field.value, false);
code += ", ";
if (lang_.language == IDLOptions::kJava)
code += SourceCastBasic( field.value.type );
Copy link

Choose a reason for hiding this comment

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

remove spaces in ()

@ghost
Copy link

ghost commented Oct 19, 2016

Mostly looks good.. ready to go in?

@blep
Copy link
Contributor Author

blep commented Oct 20, 2016

Yes, this has been well tested on my side with keysearch_test #4048 generated code.

@ghost ghost merged commit 5b5fcbf into google:master Oct 21, 2016
@ghost
Copy link

ghost commented Oct 21, 2016

Thanks!

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants