Skip to content

Commit

Permalink
New: Removed even more 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 dbd19fd commit def7b45
Show file tree
Hide file tree
Showing 20 changed files with 677 additions and 875 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err,

### Descriptors vs. static modules

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.
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.

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.

Expand Down
41 changes: 23 additions & 18 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ function buildFunction(type, functionName, gen, scope) {
delete scope[key];
});

var hasScope = Object.keys(scope).length;

// enclose all but the first and last line in an iife returning our properly scoped function
var lines = code.split(/\n/g);
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
if (hasScope)
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
else
push(name(type.name) + "." + functionName + " = " + lines[0]);
lines.slice(1, lines.length - 1).forEach(function(line) {
var prev = indent;
var i = 0;
Expand All @@ -187,7 +192,10 @@ function buildFunction(type, functionName, gen, scope) {
push(line.trim());
indent = prev;
});
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
if (hasScope)
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
else
push("};");
}

function toJsType(field) {
Expand Down Expand Up @@ -246,14 +254,8 @@ function buildType(ref, type) {
--indent;
push("}");

if (type.fieldsArray.length || type.oneofsArray.length || config.convert) {
push("");
if (config.comments)
push("/** @alias " + fullName + ".prototype */");
push("var $prototype = " + name(type.name) + ".prototype;");
}

// default values
var firstField = true;
type.fieldsArray.forEach(function(field) {
field.resolve();
var jsType = toJsType(field);
Expand All @@ -269,21 +271,24 @@ function buildType(ref, type) {
prop.charAt(0) !== "." ? "@name " + fullName + "#" + field.name : null,
"@type {" + jsType + "}"
]);
} else if (firstField) {
push("");
firstField = false;
}
if (field.repeated)
push("$prototype" + prop + " = $protobuf.util.emptyArray;");
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyArray;");
else if (field.map)
push("$prototype" + prop + " = $protobuf.util.emptyObject;");
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyObject;");
else if (field.long)
push("$prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
+ JSON.stringify(field.typeDefault.low) + ","
+ JSON.stringify(field.typeDefault.high) + ","
+ JSON.stringify(field.typeDefault.unsigned)
+ ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
else if (field.bytes) {
push("$prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
} else
push("$prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
push(name(type.name) + ".prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
});

// virtual oneof fields
Expand All @@ -303,7 +308,7 @@ function buildType(ref, type) {
"@name " + fullName + "#" + name(oneof.name),
"@type {string|undefined}"
]);
push("Object.defineProperty($prototype, " + JSON.stringify(oneof.name) +", {");
push("Object.defineProperty(" + name(type.name) + ".prototype, " + JSON.stringify(oneof.name) +", {");
++indent;
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
Expand All @@ -323,7 +328,7 @@ function buildType(ref, type) {
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
push("");
if (config.comments)
push("// Referenced types");
push("// Lazily resolved referenced types");
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
}

Expand Down Expand Up @@ -461,7 +466,7 @@ function buildType(ref, type) {
"@param {$protobuf.ConversionOptions} [options] Conversion options",
"@returns {Object.<string,*>} Plain object"
]);
push("$prototype.toObject = function toObject(options) {");
push(name(type.name) + ".prototype.toObject = function toObject(options) {");
++indent;
push("return this.constructor.toObject(this, options);");
--indent;
Expand All @@ -472,7 +477,7 @@ function buildType(ref, type) {
"Converts this " + type.name + " to JSON.",
"@returns {Object.<string,*>} JSON object"
]);
push("$prototype.toJSON = function toJSON() {");
push(name(type.name) + ".prototype.toJSON = function toJSON() {");
++indent;
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
--indent;
Expand Down
2 changes: 1 addition & 1 deletion 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.min.js

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

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion 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.min.js

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

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 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.min.js

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

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
28 changes: 11 additions & 17 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ $root.A = (function() {
}
}

/** @alias A.prototype */
var $prototype = A.prototype;

/**
* A whatever.
* @type {string}
*/
$prototype.whatever = "";
A.prototype.whatever = "";

/**
* Creates a new A instance using the specified properties.
Expand Down Expand Up @@ -127,13 +124,13 @@ $root.A = (function() {
* @param {Object.<string,*>} object Plain object
* @returns {A} A
*/
A.fromObject = (function() { return function fromObject(object) {
A.fromObject = function fromObject(object) {
var message = new $root.A();
if (object.whatever !== undefined && object.whatever !== null) {
message.whatever = String(object.whatever);
}
return message;
};})();
};

/**
* Creates a A message from a plain object. Also converts values to their respective internal types.
Expand All @@ -150,7 +147,7 @@ $root.A = (function() {
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} Plain object
*/
A.toObject = (function() { return function toObject(message, options) {
A.toObject = function toObject(message, options) {
if (!options) {
options = {};
}
Expand All @@ -168,22 +165,22 @@ $root.A = (function() {
}
}
return object;
};})();
};

/**
* Creates a plain object from this A message. Also converts values to other types if specified.
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} Plain object
*/
$prototype.toObject = function toObject(options) {
A.prototype.toObject = function toObject(options) {
return this.constructor.toObject(this, options);
};

/**
* Converts this A to JSON.
* @returns {Object.<string,*>} JSON object
*/
$prototype.toJSON = function toJSON() {
A.prototype.toJSON = function toJSON() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

Expand All @@ -206,16 +203,13 @@ $root.B = (function() {
}
}

/** @alias B.prototype */
var $prototype = B.prototype;

/**
* B A.
* @type {A}
*/
$prototype.A = null;
B.prototype.A = null;

// Referenced types
// Lazily resolved referenced types
var $types = {0:"A"}; $lazyTypes.push($types);

/**
Expand Down Expand Up @@ -360,15 +354,15 @@ $root.B = (function() {
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} Plain object
*/
$prototype.toObject = function toObject(options) {
B.prototype.toObject = function toObject(options) {
return this.constructor.toObject(this, options);
};

/**
* Converts this B to JSON.
* @returns {Object.<string,*>} JSON object
*/
$prototype.toJSON = function toJSON() {
B.prototype.toJSON = function toJSON() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

Expand Down
Loading

0 comments on commit def7b45

Please sign in to comment.