Skip to content

Commit dbd19fd

Browse files
committed
New: Removed various clutter from generated static code
1 parent c7e14b1 commit dbd19fd

28 files changed

+773
-1028
lines changed

Diff for: cli/targets/static.js

+8-32
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,8 @@ function static_target(root, options, callback) {
3535
buildNamespace(null, root);
3636
push("");
3737
if (config.comments)
38-
push("// Resolve lazy types");
39-
push("$lazyTypes.forEach(function(types) {");
40-
++indent;
41-
push("types.forEach(function(path, i) {");
42-
++indent;
43-
push("if (!path)");
44-
++indent;
45-
push("return;");
46-
--indent;
47-
push("path = path.split(\".\");");
48-
push("var ptr = $root;");
49-
push("while (path.length)");
50-
++indent;
51-
push("ptr = ptr[path.shift()];");
52-
--indent;
53-
push("types[i] = ptr;");
54-
--indent;
55-
push("});");
56-
--indent;
57-
push("});");
38+
push("// Resolve lazy type names to actual types");
39+
push("$protobuf.util.lazyResolve($root, $lazyTypes);");
5840
return callback(null, out.join("\n"));
5941
} catch (err) {
6042
return callback(err);
@@ -330,19 +312,19 @@ function buildType(ref, type) {
330312
});
331313

332314
var hasTypes = false;
333-
var types = type.fieldsArray.map(function(field) {
315+
var types = [];
316+
type.fieldsArray.forEach(function(field, index) {
334317
if (field.resolve().resolvedType) { // including enums!
335318
hasTypes = true;
336-
return JSON.stringify(field.resolvedType.fullName.substring(1));
319+
types.push(index + ":"+JSON.stringify(field.resolvedType.fullName.substring(1)));
337320
}
338-
return "null";
339-
}).join(", ");
321+
});
340322

341323
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
342324
push("");
343325
if (config.comments)
344326
push("// Referenced types");
345-
push("var $types = [" + types + "]; $lazyTypes.push($types);");
327+
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
346328
}
347329

348330
if (config.create) {
@@ -492,13 +474,7 @@ function buildType(ref, type) {
492474
]);
493475
push("$prototype.toJSON = function toJSON() {");
494476
++indent;
495-
push("return this.constructor.toObject(this, {");
496-
++indent;
497-
push("longs: String,");
498-
push("enums: String,");
499-
push("bytes: String");
500-
--indent;
501-
push("});");
477+
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
502478
--indent;
503479
push("};");
504480
}

Diff for: dist/noparse/protobuf.js

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

Diff for: dist/noparse/protobuf.js.map

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

Diff for: dist/noparse/protobuf.min.js

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

Diff for: dist/noparse/protobuf.min.js.gz

72 Bytes
Binary file not shown.

Diff for: dist/noparse/protobuf.min.js.map

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

Diff for: dist/protobuf.js

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

Diff for: dist/protobuf.js.map

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

Diff for: dist/protobuf.min.js

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

Diff for: dist/protobuf.min.js.gz

71 Bytes
Binary file not shown.

Diff for: dist/protobuf.min.js.map

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

Diff for: dist/runtime/protobuf.js

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

Diff for: dist/runtime/protobuf.js.map

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

Diff for: dist/runtime/protobuf.min.js

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

Diff for: dist/runtime/protobuf.min.js.gz

99 Bytes
Binary file not shown.

Diff for: dist/runtime/protobuf.min.js.map

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

Diff for: index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2374,6 +2374,20 @@ export namespace util {
23742374
*/
23752375
function oneOfSetter(fieldNames: string[]): () => any;
23762376

2377+
/**
2378+
* Lazily resolves fully qualified type names against the specified root.
2379+
* @param {Root} root Root instanceof
2380+
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
2381+
* @returns {undefined}
2382+
*/
2383+
function lazyResolve(root: Root, lazyTypes: { [k: number]: (string|ReflectionObject) }): void;
2384+
2385+
/**
2386+
* Default conversion options used for toJSON implementations.
2387+
* @type {ConversionOptions}
2388+
*/
2389+
var toJSONOptions: ConversionOptions;
2390+
23772391
/**
23782392
* A minimal UTF8 implementation for number arrays.
23792393
* @memberof util

Diff for: src/converter.js

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
9696
converter.fromObject = function fromObject(mtype) {
9797
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
9898
var fields = mtype.fieldsArray;
99+
if (!fields.length)
100+
return util.codegen()("return new(this.ctor)");
99101
var gen = util.codegen("d")
100102
("var m=new(this.ctor)");
101103
for (var i = 0; i < fields.length; ++i) {

Diff for: src/message.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Message;
33

4+
var util = require("./util");
5+
46
/**
57
* Constructs a new message instance.
68
*
@@ -127,9 +129,5 @@ Message.prototype.toObject = function toObject(options) {
127129
* @returns {Object.<string,*>} JSON object
128130
*/
129131
Message.prototype.toJSON = function toJSON() {
130-
return this.$type.toObject(this, {
131-
longs: String,
132-
enums: String,
133-
bytes: String
134-
});
132+
return this.$type.toObject(this, util.toJSONOptions);
135133
};

Diff for: src/util/runtime.js

+28
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,31 @@ util.oneOfSetter = function setOneOf(fieldNames) {
222222
delete this[fieldNames[i]];
223223
};
224224
};
225+
226+
/**
227+
* Lazily resolves fully qualified type names against the specified root.
228+
* @param {Root} root Root instanceof
229+
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
230+
* @returns {undefined}
231+
*/
232+
util.lazyResolve = function lazyResolve(root, lazyTypes) {
233+
lazyTypes.forEach(function(types) {
234+
Object.keys(types).forEach(function(index) {
235+
var path = types[index |= 0].split("."),
236+
ptr = root;
237+
while (path.length)
238+
ptr = ptr[path.shift()];
239+
types[index] = ptr || null;
240+
});
241+
});
242+
};
243+
244+
/**
245+
* Default conversion options used for toJSON implementations.
246+
* @type {ConversionOptions}
247+
*/
248+
util.toJSONOptions = {
249+
longs: String,
250+
enums: String,
251+
bytes: String
252+
};

0 commit comments

Comments
 (0)