From 5b5fcbfc005c90b89d32ff50bf7e59ebdba97521 Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Fri, 21 Oct 2016 23:27:15 +0200 Subject: [PATCH 1/2] Java: fix unsigned default value code generation and add 'L' suffix for long default value (#4051) * Java: emit "signed" equivalent of unsigned default value in generated code and add "L" suffix to long default value. * Updated generated code * Only convert ulong to "signed" equivalent. ubyte and ushort don't need specific handling as "user facing" type is int. uint need 'L' suffix as "user facing" type is long. * Added missing cast to primitive type of default value which is in "user facing" type in builder.add() calls. * Do not cast default value to actual type in C#. --- src/idl_gen_general.cpp | 17 ++++++++++++++++- tests/MyGame/Example/Monster.java | 24 ++++++++++++------------ tests/MyGame/Example/Stat.java | 6 +++--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 94fee3e1c..2d8d83cf5 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -490,9 +490,21 @@ std::string GenDefaultValue(const Value &value, bool enableLangOverrides) { return GenEnumDefaultValue(value); } } + + auto longSuffix = lang_.language == IDLOptions::kJava ? "L" : ""; switch (value.type.base_type) { case BASE_TYPE_FLOAT: return value.constant + "f"; case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true"; + case BASE_TYPE_ULONG: + { + if (lang_.language != IDLOptions::kJava) + return value.constant; + // Converts the ulong into its bits signed equivalent + uint64_t defaultValue = StringToUInt(value.constant.c_str()); + return NumToString(static_cast(defaultValue)) + longSuffix; + } + case BASE_TYPE_UINT: + case BASE_TYPE_LONG: return value.constant + longSuffix; default: return value.constant; } } @@ -1240,7 +1252,10 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) { lang_.language == IDLOptions::kCSharp) { code += ".Value"; } - code += ", " + GenDefaultValue(field.value, false); + code += ", "; + if (lang_.language == IDLOptions::kJava) + code += SourceCastBasic( field.value.type ); + code += GenDefaultValue(field.value, false); code += "); }\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { auto vector_type = field.value.type.VectorType(); diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index b68e625fa..10a9aa4d4 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -61,19 +61,19 @@ public final class Monster extends Table { public boolean mutateTestbool(boolean testbool) { int o = __offset(34); if (o != 0) { bb.put(o + bb_pos, (byte)(testbool ? 1 : 0)); return true; } else { return false; } } public int testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.getInt(o + bb_pos) : 0; } public boolean mutateTesthashs32Fnv1(int testhashs32_fnv1) { int o = __offset(36); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1); return true; } else { return false; } } - public long testhashu32Fnv1() { int o = __offset(38); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; } + public long testhashu32Fnv1() { int o = __offset(38); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; } public boolean mutateTesthashu32Fnv1(long testhashu32_fnv1) { int o = __offset(38); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1); return true; } else { return false; } } - public long testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public long testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.getLong(o + bb_pos) : 0L; } public boolean mutateTesthashs64Fnv1(long testhashs64_fnv1) { int o = __offset(40); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1); return true; } else { return false; } } - public long testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public long testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.getLong(o + bb_pos) : 0L; } public boolean mutateTesthashu64Fnv1(long testhashu64_fnv1) { int o = __offset(42); if (o != 0) { bb.putLong(o + bb_pos, testhashu64_fnv1); return true; } else { return false; } } public int testhashs32Fnv1a() { int o = __offset(44); return o != 0 ? bb.getInt(o + bb_pos) : 0; } public boolean mutateTesthashs32Fnv1a(int testhashs32_fnv1a) { int o = __offset(44); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1a); return true; } else { return false; } } - public long testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; } + public long testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; } public boolean mutateTesthashu32Fnv1a(long testhashu32_fnv1a) { int o = __offset(46); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1a); return true; } else { return false; } } - public long testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public long testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.getLong(o + bb_pos) : 0L; } public boolean mutateTesthashs64Fnv1a(long testhashs64_fnv1a) { int o = __offset(48); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1a); return true; } else { return false; } } - public long testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public long testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.getLong(o + bb_pos) : 0L; } public boolean mutateTesthashu64Fnv1a(long testhashu64_fnv1a) { int o = __offset(50); if (o != 0) { bb.putLong(o + bb_pos, testhashu64_fnv1a); return true; } else { return false; } } public boolean testarrayofbools(int j) { int o = __offset(52); return o != 0 ? 0!=bb.get(__vector(o) + j * 1) : false; } public int testarrayofboolsLength() { int o = __offset(52); return o != 0 ? __vector_len(o) : 0; } @@ -114,13 +114,13 @@ public final class Monster extends Table { public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); } public static void addTestbool(FlatBufferBuilder builder, boolean testbool) { builder.addBoolean(15, testbool, false); } public static void addTesthashs32Fnv1(FlatBufferBuilder builder, int testhashs32Fnv1) { builder.addInt(16, testhashs32Fnv1, 0); } - 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); } + public static void addTesthashs64Fnv1(FlatBufferBuilder builder, long testhashs64Fnv1) { builder.addLong(18, testhashs64Fnv1, 0L); } + public static void addTesthashu64Fnv1(FlatBufferBuilder builder, long testhashu64Fnv1) { builder.addLong(19, testhashu64Fnv1, 0L); } public static void addTesthashs32Fnv1a(FlatBufferBuilder builder, int testhashs32Fnv1a) { builder.addInt(20, testhashs32Fnv1a, 0); } - public static void addTesthashu32Fnv1a(FlatBufferBuilder builder, long testhashu32Fnv1a) { builder.addInt(21, (int)testhashu32Fnv1a, 0); } - public static void addTesthashs64Fnv1a(FlatBufferBuilder builder, long testhashs64Fnv1a) { builder.addLong(22, testhashs64Fnv1a, 0); } - public static void addTesthashu64Fnv1a(FlatBufferBuilder builder, long testhashu64Fnv1a) { builder.addLong(23, testhashu64Fnv1a, 0); } + public static void addTesthashu32Fnv1a(FlatBufferBuilder builder, long testhashu32Fnv1a) { builder.addInt(21, (int)testhashu32Fnv1a, (int)0L); } + public static void addTesthashs64Fnv1a(FlatBufferBuilder builder, long testhashs64Fnv1a) { builder.addLong(22, testhashs64Fnv1a, 0L); } + public static void addTesthashu64Fnv1a(FlatBufferBuilder builder, long testhashu64Fnv1a) { builder.addLong(23, testhashu64Fnv1a, 0L); } public static void addTestarrayofbools(FlatBufferBuilder builder, int testarrayofboolsOffset) { builder.addOffset(24, testarrayofboolsOffset, 0); } public static int createTestarrayofboolsVector(FlatBufferBuilder builder, boolean[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addBoolean(data[i]); return builder.endVector(); } public static void startTestarrayofboolsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } diff --git a/tests/MyGame/Example/Stat.java b/tests/MyGame/Example/Stat.java index 9a751421e..351e5b3d0 100644 --- a/tests/MyGame/Example/Stat.java +++ b/tests/MyGame/Example/Stat.java @@ -16,7 +16,7 @@ public final class Stat extends Table { public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } public ByteBuffer idAsByteBuffer() { return __vector_as_bytebuffer(4, 1); } - public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0L; } public boolean mutateVal(long val) { int o = __offset(6); if (o != 0) { bb.putLong(o + bb_pos, val); return true; } else { return false; } } public int count() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; } public boolean mutateCount(int count) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, (short)count); return true; } else { return false; } } @@ -34,8 +34,8 @@ public final class Stat extends Table { public static void startStat(FlatBufferBuilder builder) { builder.startObject(3); } public static void addId(FlatBufferBuilder builder, int idOffset) { builder.addOffset(0, idOffset, 0); } - public static void addVal(FlatBufferBuilder builder, long val) { builder.addLong(1, val, 0); } - public static void addCount(FlatBufferBuilder builder, int count) { builder.addShort(2, (short)count, 0); } + public static void addVal(FlatBufferBuilder builder, long val) { builder.addLong(1, val, 0L); } + public static void addCount(FlatBufferBuilder builder, int count) { builder.addShort(2, (short)count, (short)0); } public static int endStat(FlatBufferBuilder builder) { int o = builder.endObject(); return o; From d8944e45a277f47e655601de2e7799ca691c7ab4 Mon Sep 17 00:00:00 2001 From: cajun-rat Date: Fri, 21 Oct 2016 23:52:42 +0200 Subject: [PATCH 2/2] Fix Closure Compiler warnings (#4067) * Make parameter order in jsdocs match function When the order doesn't match the Google Closure Javascript compiler generates warnings * Prefix optional parameters with opt_ The Closure Compiler emits warnings when this isn't the case --- js/flatbuffers.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/js/flatbuffers.js b/js/flatbuffers.js index 69bb783e9..c1296d557 100644 --- a/js/flatbuffers.js +++ b/js/flatbuffers.js @@ -75,8 +75,8 @@ flatbuffers.isLittleEndian = new Uint16Array(new Uint8Array([1, 0]).buffer)[0] = /** * @constructor - * @param {number} high * @param {number} low + * @param {number} high */ flatbuffers.Long = function(low, high) { /** @@ -93,8 +93,8 @@ flatbuffers.Long = function(low, high) { }; /** - * @param {number} high * @param {number} low + * @param {number} high * @returns {flatbuffers.Long} */ flatbuffers.Long.create = function(low, high) { @@ -129,11 +129,13 @@ flatbuffers.Long.ZERO = new flatbuffers.Long(0, 0); * Create a FlatBufferBuilder. * * @constructor - * @param {number=} initial_size + * @param {number=} opt_initial_size */ -flatbuffers.Builder = function(initial_size) { - if (!initial_size) { - initial_size = 1024; +flatbuffers.Builder = function(opt_initial_size) { + if (!opt_initial_size) { + var initial_size = 1024; + } else { + var initial_size = opt_initial_size; } /** @@ -642,10 +644,11 @@ outer_loop: * Finalize a buffer, poiting to the given `root_table`. * * @param {flatbuffers.Offset} root_table - * @param {string=} file_identifier + * @param {string=} opt_file_identifier */ -flatbuffers.Builder.prototype.finish = function(root_table, file_identifier) { - if (file_identifier) { +flatbuffers.Builder.prototype.finish = function(root_table, opt_file_identifier) { + if (opt_file_identifier) { + var file_identifier = opt_file_identifier; this.prep(this.minalign, flatbuffers.SIZEOF_INT + flatbuffers.FILE_IDENTIFIER_LENGTH); if (file_identifier.length != flatbuffers.FILE_IDENTIFIER_LENGTH) { @@ -1018,10 +1021,10 @@ flatbuffers.ByteBuffer.prototype.__union = function(t, offset) { * FlatBuffer later on. * * @param {number} offset - * @param {flatbuffers.Encoding=} optionalEncoding Defaults to UTF16_STRING + * @param {flatbuffers.Encoding=} opt_encoding Defaults to UTF16_STRING * @returns {string|Uint8Array} */ -flatbuffers.ByteBuffer.prototype.__string = function(offset, optionalEncoding) { +flatbuffers.ByteBuffer.prototype.__string = function(offset, opt_encoding) { offset += this.readInt32(offset); var length = this.readInt32(offset); @@ -1030,7 +1033,7 @@ flatbuffers.ByteBuffer.prototype.__string = function(offset, optionalEncoding) { offset += flatbuffers.SIZEOF_INT; - if (optionalEncoding === flatbuffers.Encoding.UTF8_BYTES) { + if (opt_encoding === flatbuffers.Encoding.UTF8_BYTES) { return this.bytes_.subarray(offset, offset + length); }