Permalink
Browse files

New: Removed even more clutter from generated static code

  • Loading branch information...
dcodeIO committed Jan 14, 2017
1 parent dbd19fd commit def7b45fb9b5e01028cfa3bf2ecd8272575feb4d
@@ -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.
@@ -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;
@@ -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) {
@@ -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);
@@ -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
@@ -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)");
@@ -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);");
}
@@ -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;
@@ -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;

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +2 Bytes (100%) dist/noparse/protobuf.min.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +0 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN -1 Byte (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.
@@ -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.
@@ -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.
@@ -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 = {};
}
@@ -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);
};
@@ -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);
/**
@@ -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);
};
Oops, something went wrong.

0 comments on commit def7b45

Please sign in to comment.