8000 Maybe 70% complete ES6 implementation generation · reddaly/protobuf-javascript@e7e3a45 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7e3a45

Browse files
committed
Maybe 70% complete ES6 implementation generation
1 parent 611d03d commit e7e3a45

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

generator/js_generator.cc

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,12 @@ bool HasRepeatedFields(const GeneratorOptions& options,
11461146
return false;
11471147
}
11481148

1149-
static const char* kRepeatedFieldArrayName = ".repeatedFields_";
1149+
static const char* kRepeatedFieldArrayName = "repeatedFields_";
11501150

11511151
std::string RepeatedFieldsArrayName(const GeneratorOptions& options,
11521152
const Descriptor* desc) {
11531153
return HasRepeatedFields(options, desc)
1154-
? (GetMessagePath(options, desc) + kRepeatedFieldArrayName)
11 8000 54+
? (GetMessagePath(options, desc) + "." + kRepeatedFieldArrayName)
11551155
: "null";
11561156
}
11571157

@@ -2027,6 +2027,10 @@ void Generator::GenerateClassEs6(const GeneratorOptions& options,
20272027
return;
20282028
}
20292029

2030+
2031+
std::string prefix = (desc->containing_type() == nullptr) ?
2032+
"export " : ("static " + desc->name() + " = ");
2033+
20302034
printer->Print("\n");
20312035
printer->Print(
20322036
"/**\n"
@@ -2044,12 +2048,8 @@ void Generator::GenerateClassEs6(const GeneratorOptions& options,
20442048
" * @extends {jspb.Message}\n"
20452049
" * @constructor\n"
20462050
" */\n"
2047-
2048-
" // DO NOT SUBMIT: \n"
2049-
" // classprefix = $classprefix$\n"
2050-
" // classname = $classname$ \n"
2051-
2052-
"export class $classname$ extends jspb.Message {\n",
2051+
"$prefix$class $classname$ extends jspb.Message {\n",
2052+
"prefix", prefix,
20532053
"classname", desc->name());
20542054

20552055
printer->Indent();
@@ -2159,22 +2159,22 @@ void Generator::GenerateClassConstructorAndDeclareExtensionFieldInfo(
21592159
void Generator::GenerateClassFieldInfo(const GeneratorOptions& options,
21602160
io::Printer* printer,
21612161
const Descriptor* desc) const {
2162+
const std::string className = GetMessagePath(options, desc);
21622163
if (HasRepeatedFields(options, desc)) {
21632164
printer->Print(
21642165
"/**\n"
21652166
" * List of repeated fields within this message type.\n"
21662167
" * @private {!Array<number>}\n"
21672168
" * @const\n"
21682169
" */\n"
2169-
"$classname$$rptfieldarray$ = $rptfields$;\n"
2170+
"$lhs$ = $rptfields$;\n"
21702171
"\n",
2171-
"classname", GetMessagePath(options, desc), "rptfieldarray",
2172-
kRepeatedFieldArrayName, "rptfields",
2173-
RepeatedFieldNumberList(options, desc));
2172+
"lhs", StaticMemberAssignmentLhs(
2173+
options, className.c_str(), kRepeatedFieldArrayName),
2174+
"rptfields", RepeatedFieldNumberList(options, desc));
21742175
}
21752176

21762177
if (HasOneofFields(desc)) {
2177-
const std::string className = GetMessagePath(options, desc);
21782178
const std::string assignment = (
21792179
options.import_style == GeneratorOptions::kImportEs6
21802180
) ? (
@@ -3070,17 +3070,24 @@ const char * methodEndBrace = WantEs6(options) ? "}" : "};";
30703070
void Generator::GenerateRepeatedPrimitiveHelperMethods(
30713071
const GeneratorOptions& options, io::Printer* printer,
30723072
const FieldDescriptor* field, bool untyped) const {
3073+
30733074
const std::string classSymbol = GetMessagePath(options, field->containing_type());
3075+
const std::string adderName = JSGetterName(options, field, BYTES_DEFAULT,
3076+
/* drop_list = */ true);
3077+
const std::string adderMethodStart = MethodStart(
3078+
options, classSymbol.c_str(), adderName.c_str());
3079+
30743080
// clang-format off
30753081
printer->Print(
30763082
"/**\n"
30773083
" * @param {$optionaltype$} value\n"
30783084
" * @param {number=} opt_index\n"
30793085
" * @return {!$class$} returns this\n"
30803086
" */\n"
3081-
"$class$.prototype.$addername$ = function(value, opt_index) {\n"
3087+
"$methodstart$(value, opt_index) {\n"
30823088
" return jspb.Message.addToRepeatedField(this, "
30833089
"$index$",
3090+
"methodstart", adderMethodStart,
30843091
"class", classSymbol, "addername",
30853092
"add" + JSGetterName(options, field, BYTES_DEFAULT,
30863093
/* drop_list = */ true),
@@ -3110,16 +3117,23 @@ void Generator::GenerateRepeatedPrimitiveHelperMethods(
31103117
void Generator::GenerateRepeatedMessageHelperMethods(
31113118
const GeneratorOptions& options, io::Printer* printer,
31123119
const FieldDescriptor* field) const {
3120+
3121+
const std::string classSymbol = GetMessagePath(options, field->containing_type());
3122+
const std::string adderName = JSGetterName(options, field, BYTES_DEFAULT, /* drop_list = */ true);
3123+
const std::string adderMethodStart = MethodStart(
3124+
options, classSymbol.c_str(), adderName.c_str());
3125+
31133126
printer->Print(
31143127
"/**\n"
31153128
" * @param {!$optionaltype$=} opt_value\n"
31163129
" * @param {number=} opt_index\n"
31173130
" * @return {!$optionaltype$}\n"
31183131
" */\n"
3119-
"$class$.prototype.$addername$ = function(opt_value, opt_index) {\n"
3132+
"$methodstart$(opt_value, opt_index) {\n"
31203133
" return jspb.Message.addTo$repeatedtag$WrapperField(",
3121-
"optionaltype", JSTypeName(options, field, BYTES_DEFAULT), "class",
3122-
GetMessagePath(options, field->containing_type()), "addername",
3134+
"optionaltype", JSTypeName(options, field, BYTES_DEFAULT), "class", classSymbol,
3135+
"methodstart", adderMethodStart,
3136+
"addername",
31233137
"add" + JSGetterName(options, field, BYTES_DEFAULT,
31243138
/* drop_list = */ true),
31253139
"repeatedtag", (field->is_repeated() ? "Repeated" : ""));
@@ -4184,6 +4198,17 @@ void Generator::GenerateMethodEnd(const GeneratorOptions& options,
41844198
}
41854199
}
41864200

4201+
const std::string Generator::StaticMemberAssignmentLhs(
4202+
const GeneratorOptions& options,
4203+
const char * classSymbol,
4204+
const char * fieldName) const {
4205+
if (WantEs6(options)) {
4206+
return std::string("static ") + fieldName;
4207+
} else {
4208+
return std::string("") + classSymbol + "." + fieldName;
4209+
}
4210+
}
4211+
41874212
} // namespace js
41884213
} // namespace compiler
41894214
} // namespace protobuf

generator/js_generator.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,22 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
330330
io::Printer* printer,
331331
const char * classSymbol,
332332
const char * methodName) const;
333-
const std::string MethodStart(const GeneratorOptions& options,
334-
const char * classSymbol,
335-
const char * methodName) const;
336333
void GenerateMethodEnd(const GeneratorOptions& options,
337334
io::Printer* printer) const;
338335

339336
// True if the implementation code should be es6-style classes.
340337
// This is set to true if the import style is set to "es6".
341338
bool WantEs6(const GeneratorOptions& options) const;
342339

340+
const std::string MethodStart(const GeneratorOptions& options,
341+
const char * classSymbol,
342+
const char * methodName) const;
343+
344+
const std::string StaticMemberAssignmentLhs(
345+
const GeneratorOptions& options,
346+
const char * classSymbol,
347+
const char * fieldName) const;
348+
343349
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
344350
};
345351

0 commit comments

Comments
 (0)
0