Commit d8408560 authored by Taj Morton's avatar Taj Morton Committed by Wouter van Oortmerssen

In Javascript, generate bidirectional mappings for enums, between Name: Value…

In Javascript, generate bidirectional mappings for enums, between Name: Value and between Value: Name. (#4960)
parent 925c1d77
...@@ -355,9 +355,22 @@ class JsGenerator : public BaseGenerator { ...@@ -355,9 +355,22 @@ class JsGenerator : public BaseGenerator {
if (it != enum_def.vals.vec.begin()) { code += '\n'; } if (it != enum_def.vals.vec.begin()) { code += '\n'; }
GenDocComment(ev.doc_comment, code_ptr, "", " "); GenDocComment(ev.doc_comment, code_ptr, "", " ");
} }
// Generate mapping between EnumName: EnumValue(int)
code += " " + ev.name; code += " " + ev.name;
code += lang_.language == IDLOptions::kTs ? "= " : ": "; code += lang_.language == IDLOptions::kTs ? "= " : ": ";
code += NumToString(ev.value); code += NumToString(ev.value);
if (lang_.language == IDLOptions::kJs) {
// In pure Javascript, generate mapping between EnumValue(int):
// 'EnumName' so enums can be looked up by their ID.
code += ", ";
code += NumToString(ev.value);
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
code += "'" + ev.name + "'";
}
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n"; code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
if (ev.union_type.struct_def) { if (ev.union_type.struct_def) {
......
...@@ -28,19 +28,19 @@ MyGame.OtherNameSpace = MyGame.OtherNameSpace || {}; ...@@ -28,19 +28,19 @@ MyGame.OtherNameSpace = MyGame.OtherNameSpace || {};
* @enum * @enum
*/ */
MyGame.Example.Color = { MyGame.Example.Color = {
Red: 1, Red: 1, 1: 'Red',
Green: 2, Green: 2, 2: 'Green',
Blue: 8 Blue: 8, 8: 'Blue'
}; };
/** /**
* @enum * @enum
*/ */
MyGame.Example.Any = { MyGame.Example.Any = {
NONE: 0, NONE: 0, 0: 'NONE',
Monster: 1, Monster: 1, 1: 'Monster',
TestSimpleTableWithEnum: 2, TestSimpleTableWithEnum: 2, 2: 'TestSimpleTableWithEnum',
MyGame_Example2_Monster: 3 MyGame_Example2_Monster: 3, 3: 'MyGame_Example2_Monster'
}; };
/** /**
......
...@@ -16,9 +16,9 @@ NamespaceA.NamespaceB = NamespaceA.NamespaceB || {}; ...@@ -16,9 +16,9 @@ NamespaceA.NamespaceB = NamespaceA.NamespaceB || {};
* @enum * @enum
*/ */
NamespaceA.NamespaceB.EnumInNestedNS = { NamespaceA.NamespaceB.EnumInNestedNS = {
A: 0, A: 0, 0: 'A',
B: 1, B: 1, 1: 'B',
C: 2 C: 2, 2: 'C'
}; };
/** /**
......
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
* @enum * @enum
*/ */
var Character = { var Character = {
NONE: 0, NONE: 0, 0: 'NONE',
MuLan: 1, MuLan: 1, 1: 'MuLan',
Rapunzel: 2, Rapunzel: 2, 2: 'Rapunzel',
Belle: 3, Belle: 3, 3: 'Belle',
BookFan: 4, BookFan: 4, 4: 'BookFan',
Other: 5, Other: 5, 5: 'Other',
Unused: 6 Unused: 6, 6: 'Unused'
}; };
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment