Skip to content

Commit def7b45

Browse files
committed
New: Removed even more clutter from generated static code
1 parent dbd19fd commit def7b45

20 files changed

+677
-875
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err,
345345

346346
### Descriptors vs. static modules
347347

348-
While .proto and JSON files require the full library (about 17.5kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.
348+
While .proto and JSON files require the full library (about 18.5kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.
349349

350350
Static code, on the other hand, requires just the minimal runtime (about 5.5kb gzipped), but generates additional, albeit editable, source code without any reflection features.
351351

cli/targets/static.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ function buildFunction(type, functionName, gen, scope) {
176176
delete scope[key];
177177
});
178178

179+
var hasScope = Object.keys(scope).length;
180+
179181
// enclose all but the first and last line in an iife returning our properly scoped function
180182
var lines = code.split(/\n/g);
181-
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
183+
if (hasScope)
184+
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
185+
else
186+
push(name(type.name) + "." + functionName + " = " + lines[0]);
182187
lines.slice(1, lines.length - 1).forEach(function(line) {
183188
var prev = indent;
184189
var i = 0;
@@ -187,7 +192,10 @@ function buildFunction(type, functionName, gen, scope) {
187192
push(line.trim());
188193
indent = prev;
189194
});
190-
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
195+
if (hasScope)
196+
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
197+
else
198+
push("};");
191199
}
192200

193201
function toJsType(field) {
@@ -246,14 +254,8 @@ function buildType(ref, type) {
246254
--indent;
247255
push("}");
248256

249-
if (type.fieldsArray.length || type.oneofsArray.length || config.convert) {
250-
push("");
251-
if (config.comments)
252-
push("/** @alias " + fullName + ".prototype */");
253-
push("var $prototype = " + name(type.name) + ".prototype;");
254-
}
255-
256257
// default values
258+
var firstField = true;
257259
type.fieldsArray.forEach(function(field) {
258260
field.resolve();
259261
var jsType = toJsType(field);
@@ -269,21 +271,24 @@ function buildType(ref, type) {
269271
prop.charAt(0) !== "." ? "@name " + fullName + "#" + field.name : null,
270272
"@type {" + jsType + "}"
271273
]);
274+
} else if (firstField) {
275+
push("");
276+
firstField = false;
272277
}
273278
if (field.repeated)
274-
push("$prototype" + prop + " = $protobuf.util.emptyArray;");
279+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyArray;");
275280
else if (field.map)
276-
push("$prototype" + prop + " = $protobuf.util.emptyObject;");
281+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyObject;");
277282
else if (field.long)
278-
push("$prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
283+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
279284
+ JSON.stringify(field.typeDefault.low) + ","
280285
+ JSON.stringify(field.typeDefault.high) + ","
281286
+ JSON.stringify(field.typeDefault.unsigned)
282287
+ ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
283288
else if (field.bytes) {
284-
push("$prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
289+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
285290
} else
286-
push("$prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
291+
push(name(type.name) + ".prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
287292
});
288293

289294
// virtual oneof fields
@@ -303,7 +308,7 @@ function buildType(ref, type) {
303308
"@name " + fullName + "#" + name(oneof.name),
304309
"@type {string|undefined}"
305310
]);
306-
push("Object.defineProperty($prototype, " + JSON.stringify(oneof.name) +", {");
311+
push("Object.defineProperty(" + name(type.name) + ".prototype, " + JSON.stringify(oneof.name) +", {");
307312
++indent;
308313
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
309314
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
@@ -323,7 +328,7 @@ function buildType(ref, type) {
323328
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
324329
push("");
325330
if (config.comments)
326-
push("// Referenced types");
331+
push("// Lazily resolved referenced types");
327332
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
328333
}
329334

@@ -461,7 +466,7 @@ function buildType(ref, type) {
461466
"@param {$protobuf.ConversionOptions} [options] Conversion options",
462467
"@returns {Object.<string,*>} Plain object"
463468
]);
464-
push("$prototype.toObject = function toObject(options) {");
469+
push(name(type.name) + ".prototype.toObject = function toObject(options) {");
465470
++indent;
466471
push("return this.constructor.toObject(this, options);");
467472
--indent;
@@ -472,7 +477,7 @@ function buildType(ref, type) {
472477
"Converts this " + type.name + " to JSON.",
473478
"@returns {Object.<string,*>} JSON object"
474479
]);
475-
push("$prototype.toJSON = function toJSON() {");
480+
push(name(type.name) + ".prototype.toJSON = function toJSON() {");
476481
++indent;
477482
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
478483
--indent;

dist/noparse/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

2 Bytes
Binary file not shown.

dist/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

0 Bytes
Binary file not shown.

dist/runtime/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

-1 Bytes
Binary file not shown.

tests/data/ambiguous-names.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ $root.A = (function() {
2525
}
2626
}
2727

28-
/** @alias A.prototype */
29-
var $prototype = A.prototype;
30-
3128
/**
3229
* A whatever.
3330
* @type {string}
3431
*/
35-
$prototype.whatever = "";
32+
A.prototype.whatever = "";
3633

3734
/**
3835
* Creates a new A instance using the specified properties.
@@ -127,13 +124,13 @@ $root.A = (function() {
127124
* @param {Object.<string,*>} object Plain object
128125
* @returns {A} A
129126
*/
130-
A.fromObject = (function() { return function fromObject(object) {
127+
A.fromObject = function fromObject(object) {
131128
var message = new $root.A();
132129
if (object.whatever !== undefined && object.whatever !== null) {
133130
message.whatever = String(object.whatever);
134131
}
135132
return message;
136-
};})();
133+
};
137134

138135
/**
139136
* Creates a A message from a plain object. Also converts values to their respective internal types.
@@ -150,7 +147,7 @@ $root.A = (function() {
150147
* @param {$protobuf.ConversionOptions} [options] Conversion options
151148
* @returns {Object.<string,*>} Plain object
152149
*/
153-
A.toObject = (function() { return function toObject(message, options) {
150+
A.toObject = function toObject(message, options) {
154151
if (!options) {
155152
options = {};
156153
}
@@ -168,22 +165,22 @@ $root.A = (function() {
168165
}
169166
}
170167
return object;
171-
};})();
168+
};
172169

173170
/**
174171
* Creates a plain object from this A message. Also converts values to other types if specified.
175172
* @param {$protobuf.ConversionOptions} [options] Conversion options
176173
* @returns {Object.<string,*>} Plain object
177174
*/
178-
$prototype.toObject = function toObject(options) {
175+
A.prototype.toObject = function toObject(options) {
179176
return this.constructor.toObject(this, options);
180177
};
181178

182179
/**
183180
* Converts this A to JSON.
184181
* @returns {Object.<string,*>} JSON object
185182
*/
186-
$prototype.toJSON = function toJSON() {
183+
A.prototype.toJSON = function toJSON() {
187184
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
188185
};
189186

@@ -206,16 +203,13 @@ $root.B = (function() {
206203
}
207204
}
208205

209-
/** @alias B.prototype */
210-
var $prototype = B.prototype;
211-
212206
/**
213207
* B A.
214208
* @type {A}
215209
*/
216-
$prototype.A = null;
210+
B.prototype.A = null;
217211

218-
// Referenced types
212+
// Lazily resolved referenced types
219213
var $types = {0:"A"}; $lazyTypes.push($types);
220214

221215
/**
@@ -360,15 +354,15 @@ $root.B = (function() {
360354
* @param {$protobuf.ConversionOptions} [options] Conversion options
361355
* @returns {Object.<string,*>} Plain object
362356
*/
363-
$prototype.toObject = function toObject(options) {
357+
B.prototype.toObject = function toObject(options) {
364358
return this.constructor.toObject(this, options);
365359
};
366360

367361
/**
368362
* Converts this B to JSON.
369363
* @returns {Object.<string,*>} JSON object
370364
*/
371-
$prototype.toJSON = function toJSON() {
365+
B.prototype.toJSON = function toJSON() {
372366
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
373367
};
374368

0 commit comments

Comments
 (0)