Skip to content

Commit

Permalink
New: Removed various clutter from generated static code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 14, 2017
1 parent c7e14b1 commit dbd19fd
Show file tree
Hide file tree
Showing 28 changed files with 773 additions and 1,028 deletions.
40 changes: 8 additions & 32 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,8 @@ function static_target(root, options, callback) {
buildNamespace(null, root);
push("");
if (config.comments)
push("// Resolve lazy types");
push("$lazyTypes.forEach(function(types) {");
++indent;
push("types.forEach(function(path, i) {");
++indent;
push("if (!path)");
++indent;
push("return;");
--indent;
push("path = path.split(\".\");");
push("var ptr = $root;");
push("while (path.length)");
++indent;
push("ptr = ptr[path.shift()];");
--indent;
push("types[i] = ptr;");
--indent;
push("});");
--indent;
push("});");
push("// Resolve lazy type names to actual types");
push("$protobuf.util.lazyResolve($root, $lazyTypes);");
return callback(null, out.join("\n"));
} catch (err) {
return callback(err);
Expand Down Expand Up @@ -330,19 +312,19 @@ function buildType(ref, type) {
});

var hasTypes = false;
var types = type.fieldsArray.map(function(field) {
var types = [];
type.fieldsArray.forEach(function(field, index) {
if (field.resolve().resolvedType) { // including enums!
hasTypes = true;
return JSON.stringify(field.resolvedType.fullName.substring(1));
types.push(index + ":"+JSON.stringify(field.resolvedType.fullName.substring(1)));
}
return "null";
}).join(", ");
});

if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
push("");
if (config.comments)
push("// Referenced types");
push("var $types = [" + types + "]; $lazyTypes.push($types);");
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
}

if (config.create) {
Expand Down Expand Up @@ -492,13 +474,7 @@ function buildType(ref, type) {
]);
push("$prototype.toJSON = function toJSON() {");
++indent;
push("return this.constructor.toObject(this, {");
++indent;
push("longs: String,");
push("enums: String,");
push("bytes: String");
--indent;
push("});");
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
--indent;
push("};");
}
Expand Down
50 changes: 39 additions & 11 deletions dist/noparse/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/noparse/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/noparse/protobuf.min.js.map

Large diffs are not rendered by default.

50 changes: 39 additions & 11 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,20 @@ export namespace util {
*/
function oneOfSetter(fieldNames: string[]): () => any;

/**
* Lazily resolves fully qualified type names against the specified root.
* @param {Root} root Root instanceof
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
* @returns {undefined}
*/
function lazyResolve(root: Root, lazyTypes: { [k: number]: (string|ReflectionObject) }): void;

/**
* Default conversion options used for toJSON implementations.
* @type {ConversionOptions}
*/
var toJSONOptions: ConversionOptions;

/**
* A minimal UTF8 implementation for number arrays.
* @memberof util
Expand Down
2 changes: 2 additions & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
converter.fromObject = function fromObject(mtype) {
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
var fields = mtype.fieldsArray;
if (!fields.length)
return util.codegen()("return new(this.ctor)");
var gen = util.codegen("d")
("var m=new(this.ctor)");
for (var i = 0; i < fields.length; ++i) {
Expand Down
8 changes: 3 additions & 5 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Message;

var util = require("./util");

/**
* Constructs a new message instance.
*
Expand Down Expand Up @@ -127,9 +129,5 @@ Message.prototype.toObject = function toObject(options) {
* @returns {Object.<string,*>} JSON object
*/
Message.prototype.toJSON = function toJSON() {
return this.$type.toObject(this, {
longs: String,
enums: String,
bytes: String
});
return this.$type.toObject(this, util.toJSONOptions);
};
28 changes: 28 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,31 @@ util.oneOfSetter = function setOneOf(fieldNames) {
delete this[fieldNames[i]];
};
};

/**
* Lazily resolves fully qualified type names against the specified root.
* @param {Root} root Root instanceof
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
* @returns {undefined}
*/
util.lazyResolve = function lazyResolve(root, lazyTypes) {
lazyTypes.forEach(function(types) {
Object.keys(types).forEach(function(index) {
var path = types[index |= 0].split("."),
ptr = root;
while (path.length)
ptr = ptr[path.shift()];
types[index] = ptr || null;
});
});
};

/**
* Default conversion options used for toJSON implementations.
* @type {ConversionOptions}
*/
util.toJSONOptions = {
longs: String,
enums: String,
bytes: String
};
Loading

0 comments on commit dbd19fd

Please sign in to comment.