Skip to content

Commit

Permalink
[Javascript] Fix syntax error for signed enum (#5503)
Browse files Browse the repository at this point in the history
* wrap quotes to enum name map to prevent syntax errorn when enum value is negative

* Add a test that covers signed enum case
  • Loading branch information
nullbus authored and aardappel committed Sep 5, 2019
1 parent d0e3870 commit 4b870ac
Show file tree
Hide file tree
Showing 29 changed files with 668 additions and 71 deletions.
92 changes: 74 additions & 18 deletions dart/test/monster_test_my_game.example_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'include_test2_my_game.example_generated.dart';
import './monster_test_my_game_generated.dart' as my_game;
import './monster_test_my_game.example2_generated.dart' as my_game_example2;

/// Composite components of Monster color.
class Color {
final int value;
const Color._(this.value);
Expand All @@ -26,7 +27,12 @@ class Color {
static bool containsValue(int value) => values.containsKey(value);

static const Color Red = const Color._(1);

/// \brief color Green
/// Green is bit_flag with value (1u << 1)
static const Color Green = const Color._(2);

/// \brief color Blue (1u << 3)
static const Color Blue = const Color._(8);
static get values => {1: Red,2: Green,8: Blue,};

Expand All @@ -46,7 +52,48 @@ class _ColorReader extends fb.Reader<Color> {

@override
Color read(fb.BufferContext bc, int offset) =>
new Color.fromValue(const fb.Int8Reader().read(bc, offset));
new Color.fromValue(const fb.Uint8Reader().read(bc, offset));
}

class Race {
final int value;
const Race._(this.value);

factory Race.fromValue(int value) {
if (value == null) value = 0;
if (!values.containsKey(value)) {
throw new StateError('Invalid value $value for bit flag enum Race');
}
return values[value];
}

static const int minValue = -1;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);

static const Race None = const Race._(-1);
static const Race Human = const Race._(0);
static const Race Dwarf = const Race._(1);
static const Race Elf = const Race._(2);
static get values => {-1: None,0: Human,1: Dwarf,2: Elf,};

static const fb.Reader<Race> reader = const _RaceReader();

@override
String toString() {
return 'Race{value: $value}';
}
}

class _RaceReader extends fb.Reader<Race> {
const _RaceReader();

@override
int get size => 1;

@override
Race read(fb.BufferContext bc, int offset) =>
new Race.fromValue(const fb.Int8Reader().read(bc, offset));
}

class AnyTypeId {
Expand Down Expand Up @@ -108,9 +155,9 @@ class AnyUniqueAliasesTypeId {

static const AnyUniqueAliasesTypeId NONE = const AnyUniqueAliasesTypeId._(0);
static const AnyUniqueAliasesTypeId M = const AnyUniqueAliasesTypeId._(1);
static const AnyUniqueAliasesTypeId T = const AnyUniqueAliasesTypeId._(2);
static const AnyUniqueAliasesTypeId TS = const AnyUniqueAliasesTypeId._(2);
static const AnyUniqueAliasesTypeId M2 = const AnyUniqueAliasesTypeId._(3);
static get values => {0: NONE,1: M,2: T,3: M2,};
static get values => {0: NONE,1: M,2: TS,3: M2,};

static const fb.Reader<AnyUniqueAliasesTypeId> reader = const _AnyUniqueAliasesTypeIdReader();

Expand Down Expand Up @@ -259,7 +306,7 @@ class TestSimpleTableWithEnum {
final fb.BufferContext _bc;
final int _bcOffset;

Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 4, 2));
Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 4, 2));

@override
String toString() {
Expand Down Expand Up @@ -287,7 +334,7 @@ class TestSimpleTableWithEnumBuilder {
}

int addColor(Color color) {
fbBuilder.addInt8(0, color?.value);
fbBuilder.addUint8(0, color?.value);
return fbBuilder.offset;
}

Expand All @@ -311,7 +358,7 @@ class TestSimpleTableWithEnumObjectBuilder extends fb.ObjectBuilder {
assert(fbBuilder != null);

fbBuilder.startTable();
fbBuilder.addInt8(0, _color?.value);
fbBuilder.addUint8(0, _color?.value);
return fbBuilder.endTable();
}

Expand All @@ -335,7 +382,7 @@ class Vec3 {
double get y => const fb.Float32Reader().read(_bc, _bcOffset + 4);
double get z => const fb.Float32Reader().read(_bc, _bcOffset + 8);
double get test1 => const fb.Float64Reader().read(_bc, _bcOffset + 16);
Color get test2 => new Color.fromValue(const fb.Int8Reader().read(_bc, _bcOffset + 24));
Color get test2 => new Color.fromValue(const fb.Uint8Reader().read(_bc, _bcOffset + 24));
Test get test3 => Test.reader.read(_bc, _bcOffset + 26);

@override
Expand Down Expand Up @@ -366,7 +413,7 @@ class Vec3Builder {
fbBuilder.pad(2);
test3();
fbBuilder.pad(1);
fbBuilder.putInt8(test2?.value);
fbBuilder.putUint8(test2?.value);
fbBuilder.putFloat64(test1);
fbBuilder.pad(4);
fbBuilder.putFloat32(z);
Expand Down Expand Up @@ -409,7 +456,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder {
fbBuilder.pad(2);
_test3.finish(fbBuilder);
fbBuilder.pad(1);
fbBuilder.putInt8(_test2?.value);
fbBuilder.putUint8(_test2?.value);
fbBuilder.putFloat64(_test1);
fbBuilder.pad(4);
fbBuilder.putFloat32(_z);
Expand Down Expand Up @@ -690,7 +737,7 @@ class Monster {
int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100);
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null);
List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null);
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 8));
Color get color => new Color.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 16, 8));
AnyTypeId get testType => new AnyTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 18, 0));
dynamic get test {
switch (testType?.value) {
Expand All @@ -702,8 +749,8 @@ class Monster {
}
List<Test> get test4 => const fb.ListReader<Test>(Test.reader).vTableGet(_bc, _bcOffset, 22, null);
List<String> get testarrayofstring => const fb.ListReader<String>(const fb.StringReader()).vTableGet(_bc, _bcOffset, 24, null);
/// an example documentation comment: this will end up in the generated code
/// multiline too
/// an example documentation comment: this will end up in the generated code
/// multiline too
List<Monster> get testarrayoftables => const fb.ListReader<Monster>(Monster.reader).vTableGet(_bc, _bcOffset, 26, null);
Monster get enemy => Monster.reader.vTableGet(_bc, _bcOffset, 28, null);
List<int> get testnestedflatbuffer => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 30, null);
Expand Down Expand Up @@ -740,7 +787,7 @@ class Monster {
dynamic get anyUnique {
switch (anyUniqueType?.value) {
case 1: return M.reader.vTableGet(_bc, _bcOffset, 92, null);
case 2: return T.reader.vTableGet(_bc, _bcOffset, 92, null);
case 2: return TS.reader.vTableGet(_bc, _bcOffset, 92, null);
case 3: return M2.reader.vTableGet(_bc, _bcOffset, 92, null);
default: return null;
}
Expand All @@ -755,10 +802,11 @@ class Monster {
}
}
List<Color> get vectorOfEnums => const fb.ListReader<Color>(Color.reader).vTableGet(_bc, _bcOffset, 98, null);
Race get signedEnum => new Race.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 100, -1));

@override
String toString() {
return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences, anyUniqueType: $anyUniqueType, anyUnique: $anyUnique, anyAmbiguousType: $anyAmbiguousType, anyAmbiguous: $anyAmbiguous, vectorOfEnums: $vectorOfEnums}';
return 'Monster{pos: $pos, mana: $mana, hp: $hp, name: $name, inventory: $inventory, color: $color, testType: $testType, test: $test, test4: $test4, testarrayofstring: $testarrayofstring, testarrayoftables: $testarrayoftables, enemy: $enemy, testnestedflatbuffer: $testnestedflatbuffer, testempty: $testempty, testbool: $testbool, testhashs32Fnv1: $testhashs32Fnv1, testhashu32Fnv1: $testhashu32Fnv1, testhashs64Fnv1: $testhashs64Fnv1, testhashu64Fnv1: $testhashu64Fnv1, testhashs32Fnv1a: $testhashs32Fnv1a, testhashu32Fnv1a: $testhashu32Fnv1a, testhashs64Fnv1a: $testhashs64Fnv1a, testhashu64Fnv1a: $testhashu64Fnv1a, testarrayofbools: $testarrayofbools, testf: $testf, testf2: $testf2, testf3: $testf3, testarrayofstring2: $testarrayofstring2, testarrayofsortedstruct: $testarrayofsortedstruct, flex: $flex, test5: $test5, vectorOfLongs: $vectorOfLongs, vectorOfDoubles: $vectorOfDoubles, parentNamespaceTest: $parentNamespaceTest, vectorOfReferrables: $vectorOfReferrables, singleWeakReference: $singleWeakReference, vectorOfWeakReferences: $vectorOfWeakReferences, vectorOfStrongReferrables: $vectorOfStrongReferrables, coOwningReference: $coOwningReference, vectorOfCoOwningReferences: $vectorOfCoOwningReferences, nonOwningReference: $nonOwningReference, vectorOfNonOwningReferences: $vectorOfNonOwningReferences, anyUniqueType: $anyUniqueType, anyUnique: $anyUnique, anyAmbiguousType: $anyAmbiguousType, anyAmbiguous: $anyAmbiguous, vectorOfEnums: $vectorOfEnums, signedEnum: $signedEnum}';
}
}

Expand Down Expand Up @@ -802,7 +850,7 @@ class MonsterBuilder {
return fbBuilder.offset;
}
int addColor(Color color) {
fbBuilder.addInt8(6, color?.value);
fbBuilder.addUint8(6, color?.value);
return fbBuilder.offset;
}
int addTestType(AnyTypeId testType) {
Expand Down Expand Up @@ -969,6 +1017,10 @@ class MonsterBuilder {
fbBuilder.addOffset(47, offset);
return fbBuilder.offset;
}
int addSignedEnum(Race signedEnum) {
fbBuilder.addInt8(48, signedEnum?.value);
return fbBuilder.offset;
}

int finish() {
return fbBuilder.endTable();
Expand Down Expand Up @@ -1023,6 +1075,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
final AnyAmbiguousAliasesTypeId _anyAmbiguousType;
final dynamic _anyAmbiguous;
final List<Color> _vectorOfEnums;
final Race _signedEnum;

MonsterObjectBuilder({
Vec3ObjectBuilder pos,
Expand Down Expand Up @@ -1072,6 +1125,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
AnyAmbiguousAliasesTypeId anyAmbiguousType,
dynamic anyAmbiguous,
List<Color> vectorOfEnums,
Race signedEnum,
})
: _pos = pos,
_mana = mana,
Expand Down Expand Up @@ -1119,7 +1173,8 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
_anyUnique = anyUnique,
_anyAmbiguousType = anyAmbiguousType,
_anyAmbiguous = anyAmbiguous,
_vectorOfEnums = vectorOfEnums;
_vectorOfEnums = vectorOfEnums,
_signedEnum = signedEnum;

/// Finish building, and store into the [fbBuilder].
@override
Expand Down Expand Up @@ -1185,7 +1240,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
final int anyUniqueOffset = _anyUnique?.getOrCreateOffset(fbBuilder);
final int anyAmbiguousOffset = _anyAmbiguous?.getOrCreateOffset(fbBuilder);
final int vectorOfEnumsOffset = _vectorOfEnums?.isNotEmpty == true
? fbBuilder.writeListInt8(_vectorOfEnums.map((f) => f.value))
? fbBuilder.writeListUint8(_vectorOfEnums.map((f) => f.value))
: null;

fbBuilder.startTable();
Expand All @@ -1200,7 +1255,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
if (inventoryOffset != null) {
fbBuilder.addOffset(5, inventoryOffset);
}
fbBuilder.addInt8(6, _color?.value);
fbBuilder.addUint8(6, _color?.value);
fbBuilder.addUint8(7, _testType?.value);
if (testOffset != null) {
fbBuilder.addOffset(8, testOffset);
Expand Down Expand Up @@ -1288,6 +1343,7 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
if (vectorOfEnumsOffset != null) {
fbBuilder.addOffset(47, vectorOfEnumsOffset);
}
fbBuilder.addInt8(48, _signedEnum?.value);
return fbBuilder.endTable();
}

Expand Down
2 changes: 1 addition & 1 deletion src/idl_gen_js_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class JsTsGenerator : public BaseGenerator {

// Generate mapping between EnumName: EnumValue(int)
if (reverse) {
code += " " + enum_def.ToString(ev);
code += " '" + enum_def.ToString(ev) + "'";
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
code += "'" + ev.name + "'";
} else {
Expand Down
3 changes: 3 additions & 0 deletions tests/FlatBuffers.Test/FlatBuffers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
<Compile Include="..\MyGame\Example\Color.cs">
<Link>MyGame\Example\Color.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Race.cs">
<Link>MyGame\Example\Race.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Monster.cs">
<Link>MyGame\Example\Monster.cs</Link>
</Compile>
Expand Down
5 changes: 4 additions & 1 deletion tests/MyGame/Example/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ public struct Monster : IFlatbufferObject
#endif
public MyGame.Example.Color[] GetVectorOfEnumsArray() { int o = __p.__offset(98); if (o == 0) return null; int p = __p.__vector(o); int l = __p.__vector_len(o); MyGame.Example.Color[] a = new MyGame.Example.Color[l]; for (int i = 0; i < l; i++) { a[i] = (MyGame.Example.Color)__p.bb.Get(p + i * 1); } return a; }
public bool MutateVectorOfEnums(int j, MyGame.Example.Color vector_of_enums) { int o = __p.__offset(98); if (o != 0) { __p.bb.Put(__p.__vector(o) + j * 1, (byte)vector_of_enums); return true; } else { return false; } }
public MyGame.Example.Race SignedEnum { get { int o = __p.__offset(100); return o != 0 ? (MyGame.Example.Race)__p.bb.GetSbyte(o + __p.bb_pos) : MyGame.Example.Race.None; } }
public bool MutateSignedEnum(MyGame.Example.Race signed_enum) { int o = __p.__offset(100); if (o != 0) { __p.bb.PutSbyte(o + __p.bb_pos, (sbyte)signed_enum); return true; } else { return false; } }

public static void StartMonster(FlatBufferBuilder builder) { builder.StartTable(48); }
public static void StartMonster(FlatBufferBuilder builder) { builder.StartTable(49); }
public static void AddPos(FlatBufferBuilder builder, Offset<MyGame.Example.Vec3> posOffset) { builder.AddStruct(0, posOffset.Value, 0); }
public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); }
public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); }
Expand Down Expand Up @@ -284,6 +286,7 @@ public struct Monster : IFlatbufferObject
public static void AddVectorOfEnums(FlatBufferBuilder builder, VectorOffset vectorOfEnumsOffset) { builder.AddOffset(47, vectorOfEnumsOffset.Value, 0); }
public static VectorOffset CreateVectorOfEnumsVector(FlatBufferBuilder builder, MyGame.Example.Color[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte((byte)data[i]); return builder.EndVector(); }
public static void StartVectorOfEnumsVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
public static void AddSignedEnum(FlatBufferBuilder builder, MyGame.Example.Race signedEnum) { builder.AddSbyte(48, (sbyte)signedEnum, -1); }
public static Offset<MyGame.Example.Monster> EndMonster(FlatBufferBuilder builder) {
int o = builder.EndTable();
builder.Required(o, 10); // name
Expand Down
17 changes: 16 additions & 1 deletion tests/MyGame/Example/Monster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion tests/MyGame/Example/Monster.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ public final class Monster extends Table {
public ByteBuffer vectorOfEnumsAsByteBuffer() { return __vector_as_bytebuffer(98, 1); }
public ByteBuffer vectorOfEnumsInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 98, 1); }
public boolean mutateVectorOfEnums(int j, int vector_of_enums) { int o = __offset(98); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)vector_of_enums); return true; } else { return false; } }
public byte signedEnum() { int o = __offset(100); return o != 0 ? bb.get(o + bb_pos) : -1; }
public boolean mutateSignedEnum(byte signed_enum) { int o = __offset(100); if (o != 0) { bb.put(o + bb_pos, signed_enum); return true; } else { return false; } }

public static void startMonster(FlatBufferBuilder builder) { builder.startTable(48); }
public static void startMonster(FlatBufferBuilder builder) { builder.startTable(49); }
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); }
public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); }
Expand Down Expand Up @@ -241,6 +243,7 @@ public final class Monster extends Table {
public static void addVectorOfEnums(FlatBufferBuilder builder, int vectorOfEnumsOffset) { builder.addOffset(47, vectorOfEnumsOffset, 0); }
public static int createVectorOfEnumsVector(FlatBufferBuilder builder, byte[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addByte(data[i]); return builder.endVector(); }
public static void startVectorOfEnumsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
public static void addSignedEnum(FlatBufferBuilder builder, byte signedEnum) { builder.addByte(48, signedEnum, -1); }
public static int endMonster(FlatBufferBuilder builder) {
int o = builder.endTable();
builder.required(o, 10); // name
Expand Down
Loading

0 comments on commit 4b870ac

Please sign in to comment.