mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Made JS enum declarations compatible with google closure
Original change by: https://github.com/alexames Change-Id: Ib65bd02156d1c3637ed278a8334a2307caacaa44
This commit is contained in:
@@ -168,7 +168,8 @@ class JsTsGenerator : public BaseGenerator {
|
||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||
++it) {
|
||||
auto &enum_def = **it;
|
||||
GenEnum(enum_def, enum_code_ptr, exports_code_ptr, reexports);
|
||||
GenEnum(enum_def, enum_code_ptr, exports_code_ptr, reexports, false);
|
||||
GenEnum(enum_def, enum_code_ptr, exports_code_ptr, reexports, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,12 +323,16 @@ class JsTsGenerator : public BaseGenerator {
|
||||
|
||||
// Generate an enum declaration and an enum string lookup table.
|
||||
void GenEnum(EnumDef &enum_def, std::string *code_ptr,
|
||||
std::string *exports_ptr, reexport_map &reexports) {
|
||||
std::string *exports_ptr, reexport_map &reexports,
|
||||
bool reverse) {
|
||||
if (enum_def.generated) return;
|
||||
if (reverse && lang_.language == IDLOptions::kTs) return; // FIXME.
|
||||
std::string &code = *code_ptr;
|
||||
std::string &exports = *exports_ptr;
|
||||
GenDocComment(enum_def.doc_comment, code_ptr, "@enum");
|
||||
GenDocComment(enum_def.doc_comment, code_ptr,
|
||||
reverse ? "@enum {string}" : "@enum {number}");
|
||||
std::string ns = GetNameSpace(enum_def);
|
||||
std::string enum_def_name = enum_def.name + (reverse ? "Name" : "");
|
||||
if (lang_.language == IDLOptions::kTs) {
|
||||
if (!ns.empty()) { code += "export namespace " + ns + "{\n"; }
|
||||
code += "export enum " + enum_def.name + "{\n";
|
||||
@@ -335,15 +340,15 @@ class JsTsGenerator : public BaseGenerator {
|
||||
if (enum_def.defined_namespace->components.empty()) {
|
||||
code += "var ";
|
||||
if (parser_.opts.use_goog_js_export_format) {
|
||||
exports += "goog.exportSymbol('" + enum_def.name + "', " +
|
||||
exports += "goog.exportSymbol('" + enum_def_name + "', " +
|
||||
enum_def.name + ");\n";
|
||||
} else if (parser_.opts.use_ES6_js_export_format) {
|
||||
exports += "export {" + enum_def.name + "};\n";
|
||||
exports += "export {" + enum_def_name + "};\n";
|
||||
} else {
|
||||
exports += "this." + enum_def.name + " = " + enum_def.name + ";\n";
|
||||
exports += "this." + enum_def_name + " = " + enum_def_name + ";\n";
|
||||
}
|
||||
}
|
||||
code += WrapInNameSpace(enum_def) + " = {\n";
|
||||
code += WrapInNameSpace(enum_def) + (reverse ? "Name" : "") + " = {\n";
|
||||
}
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
@@ -354,18 +359,14 @@ class JsTsGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
// Generate mapping between EnumName: EnumValue(int)
|
||||
code += " " + ev.name;
|
||||
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
|
||||
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);
|
||||
if (reverse) {
|
||||
code += " " + NumToString(ev.value);
|
||||
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
|
||||
code += "'" + ev.name + "'";
|
||||
} else {
|
||||
code += " " + ev.name;
|
||||
code += lang_.language == IDLOptions::kTs ? "= " : ": ";
|
||||
code += NumToString(ev.value);
|
||||
}
|
||||
|
||||
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
|
||||
|
||||
@@ -25,42 +25,81 @@ MyGame.Example2 = MyGame.Example2 || {};
|
||||
MyGame.OtherNameSpace = MyGame.OtherNameSpace || {};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
MyGame.Example.Color = {
|
||||
Red: 1, 1: 'Red',
|
||||
Green: 2, 2: 'Green',
|
||||
Blue: 8, 8: 'Blue'
|
||||
Red: 1,
|
||||
Green: 2,
|
||||
Blue: 8
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {string}
|
||||
*/
|
||||
MyGame.Example.ColorName = {
|
||||
1: 'Red',
|
||||
2: 'Green',
|
||||
8: 'Blue'
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
MyGame.Example.Any = {
|
||||
NONE: 0, 0: 'NONE',
|
||||
Monster: 1, 1: 'Monster',
|
||||
TestSimpleTableWithEnum: 2, 2: 'TestSimpleTableWithEnum',
|
||||
MyGame_Example2_Monster: 3, 3: 'MyGame_Example2_Monster'
|
||||
NONE: 0,
|
||||
Monster: 1,
|
||||
TestSimpleTableWithEnum: 2,
|
||||
MyGame_Example2_Monster: 3
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {string}
|
||||
*/
|
||||
MyGame.Example.AnyName = {
|
||||
0: 'NONE',
|
||||
1: 'Monster',
|
||||
2: 'TestSimpleTableWithEnum',
|
||||
3: 'MyGame_Example2_Monster'
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
MyGame.Example.AnyUniqueAliases = {
|
||||
NONE: 0, 0: 'NONE',
|
||||
M: 1, 1: 'M',
|
||||
T: 2, 2: 'T',
|
||||
M2: 3, 3: 'M2'
|
||||
NONE: 0,
|
||||
M: 1,
|
||||
T: 2,
|
||||
M2: 3
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {string}
|
||||
*/
|
||||
MyGame.Example.AnyUniqueAliasesName = {
|
||||
0: 'NONE',
|
||||
1: 'M',
|
||||
2: 'T',
|
||||
3: 'M2'
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number}
|
||||
*/
|
||||
MyGame.Example.AnyAmbiguousAliases = {
|
||||
NONE: 0, 0: 'NONE',
|
||||
M1: 1, 1: 'M1',
|
||||
M2: 2, 2: 'M2',
|
||||
M3: 3, 3: 'M3'
|
||||
NONE: 0,
|
||||
M1: 1,
|
||||
M2: 2,
|
||||
M3: 3
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
*/
|
||||
MyGame.Example.AnyAmbiguousAliasesName = {
|
||||
0: 'NONE',
|
||||
1: 'M1',
|
||||
2: 'M2',
|
||||
3: 'M3'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export namespace MyGame.Example{
|
||||
export enum Color{
|
||||
@@ -11,7 +11,7 @@ export enum Color{
|
||||
}};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export namespace MyGame.Example{
|
||||
export enum Any{
|
||||
@@ -22,7 +22,7 @@ export enum Any{
|
||||
}};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export namespace MyGame.Example{
|
||||
export enum AnyUniqueAliases{
|
||||
@@ -33,7 +33,7 @@ export enum AnyUniqueAliases{
|
||||
}};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export namespace MyGame.Example{
|
||||
export enum AnyAmbiguousAliases{
|
||||
|
||||
@@ -13,12 +13,21 @@ var NamespaceA = NamespaceA || {};
|
||||
NamespaceA.NamespaceB = NamespaceA.NamespaceB || {};
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
NamespaceA.NamespaceB.EnumInNestedNS = {
|
||||
A: 0, 0: 'A',
|
||||
B: 1, 1: 'B',
|
||||
C: 2, 2: 'C'
|
||||
A: 0,
|
||||
B: 1,
|
||||
C: 2
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
*/
|
||||
NamespaceA.NamespaceB.EnumInNestedNSName = {
|
||||
0: 'A',
|
||||
1: 'B',
|
||||
2: 'C'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export namespace NamespaceA.NamespaceB{
|
||||
export enum EnumInNestedNS{
|
||||
|
||||
@@ -1,16 +1,29 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
var Character = {
|
||||
NONE: 0, 0: 'NONE',
|
||||
MuLan: 1, 1: 'MuLan',
|
||||
Rapunzel: 2, 2: 'Rapunzel',
|
||||
Belle: 3, 3: 'Belle',
|
||||
BookFan: 4, 4: 'BookFan',
|
||||
Other: 5, 5: 'Other',
|
||||
Unused: 6, 6: 'Unused'
|
||||
NONE: 0,
|
||||
MuLan: 1,
|
||||
Rapunzel: 2,
|
||||
Belle: 3,
|
||||
BookFan: 4,
|
||||
Other: 5,
|
||||
Unused: 6
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
*/
|
||||
var CharacterName = {
|
||||
0: 'NONE',
|
||||
1: 'MuLan',
|
||||
2: 'Rapunzel',
|
||||
3: 'Belle',
|
||||
4: 'BookFan',
|
||||
5: 'Other',
|
||||
6: 'Unused'
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -459,6 +472,7 @@ Movie.createMovie = function(builder, mainCharacterType, mainCharacterOffset, ch
|
||||
|
||||
// Exports for Node.js and RequireJS
|
||||
this.Character = Character;
|
||||
this.CharacterName = CharacterName;
|
||||
this.Attacker = Attacker;
|
||||
this.Rapunzel = Rapunzel;
|
||||
this.BookReader = BookReader;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/**
|
||||
* @enum
|
||||
* @enum {number}
|
||||
*/
|
||||
export enum Character{
|
||||
NONE= 0,
|
||||
|
||||
Reference in New Issue
Block a user