Skip to content

Commit

Permalink
New: Switched to own property-aware encoders for compatibility, see #639
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 16, 2017
1 parent 3cb8262 commit 9b090bb
Show file tree
Hide file tree
Showing 15 changed files with 1,046 additions and 1,677 deletions.
2 changes: 1 addition & 1 deletion cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var targets = util.requireAll("./targets");
* @returns {number|undefined} Exit code, if known
*/
exports.main = function(args, callback) {
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex";
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins";
var argv = minimist(args, {
alias: {
target : "t",
Expand Down
8 changes: 4 additions & 4 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ function buildFunction(type, functionName, gen, scope) {
var code = gen.str(functionName)
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader")
.replace(/\b(?!\.)util\.\b/g, "$util.")
.replace(/\b(?!\.)types\[\b/g, "$types[");
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
.replace(/\b(?!\.)types\[\b/g, "$types["); // "

if (config.beautify)
code = beautifyCode(code);
Expand Down
32 changes: 11 additions & 21 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,37 +230,27 @@ converter.toObject = function toObject(mtype) {
("d%s=%j", field._prop, field.typeDefault); // also messages (=null)
}); gen
("}");
} gen
("for(var ks=Object.keys(m),i=0;i<ks.length;++i){")
("if(m[ks[i]]!==undefined&&m[ks[i]]!==null){")
("switch(ks[i]){");
}
for (var i = 0; i < fields.length; ++i) {
var field = fields[i],
prop = field._prop; gen
("case%j:", field.name);
("if(m.hasOwnProperty(%j)&&m%s!==undefined&&m%s!==null){", field.name, prop, prop);
if (field.map) { gen
("if(m%s!==util.emptyObject){", prop, prop)
("d%s={}", prop)
("for(var ks2=Object.keys(m%s),j=0;j<ks2.length;++j){", prop);
genValuePartial_toObject(gen, field, i, prop + "[ks2[j]]")
("}")
("}");
("d%s={}", prop)
("for(var ks2=Object.keys(m%s),j=0;j<ks2.length;++j){", prop);
genValuePartial_toObject(gen, field, i, prop + "[ks2[j]]")
("}");
} else if (field.repeated) { gen
("if(m%s.length){", prop)
("d%s=[]", prop)
("for(var j=0;j<m%s.length;++j){", prop);
("d%s=[]", prop)
("for(var j=0;j<m%s.length;++j){", prop);
genValuePartial_toObject(gen, field, i, prop + "[j]")
("}")
("}");
("}");
} else
genValuePartial_toObject(gen, field, i, prop);
genValuePartial_toObject(gen, field, i, prop);
gen
("break");
("}");
}
return gen
("}")
("}")
("}")
("return d");
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
};
22 changes: 12 additions & 10 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ function encoder(mtype) {

var i, ref;
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
var field = fields[i].resolve();
if (field.partOf) // see below for oneofs
continue;
var type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type];
ref = "m" + field._prop;

// Map fields
if (field.map) {
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType; gen
("if(%s&&%s!==util.emptyObject){", ref, ref)
("if(m.hasOwnProperty(%j)&&%s){", field.name, ref)
("for(var ks=Object.keys(%s),i=0;i<ks.length;++i){", ref)
("w.uint32(%d).fork().uint32(%d).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[keyType], keyType);
if (wireType === undefined) gen
Expand All @@ -60,7 +62,7 @@ function encoder(mtype) {
// Packed repeated
if (field.packed && types.packed[type] !== undefined) { gen

("if(%s&&%s.length){", ref, ref)
("if(m.hasOwnProperty(%j)&&%s.length){", field.name, ref)
("w.uint32(%d).fork()", (field.id << 3 | 2) >>> 0)
("for(var i=0;i<%s.length;++i)", ref)
("w.%s(%s[i])", type, ref)
Expand All @@ -70,7 +72,7 @@ function encoder(mtype) {
// Non-packed
} else { gen

("if(%s){", ref)
("if(m.hasOwnProperty(%j)){", field.name)
("for(var i=0;i<%s.length;++i)", ref);
if (wireType === undefined)
genTypePartial(gen, field, i, ref + "[i]");
Expand All @@ -82,18 +84,18 @@ function encoder(mtype) {
}

// Non-repeated
} else if (!field.partOf) { // see below for oneofs
} else {
if (!field.required) {

if (field.long) gen
("if(%s!==undefined&&%s!==null&&util.longNe(%s,%d,%d))", ref, ref, ref, field.defaultValue.low, field.defaultValue.high);
("if(m.hasOwnProperty(%j)&&%s!==undefined&&%s!==null)", field.name, ref, ref);
else if (field.bytes) gen
("if(%s&&%s.length" + (field.defaultValue.length ? "&&util.arrayNe(%s,%j)" : "") + ")", ref, ref, ref, Array.prototype.slice.call(field.defaultValue));
("if(m.hasOwnProperty(%j)&&%s)", field.name, ref);
else gen
("if(%s!==undefined&&%s!==%j)", ref, ref, field.defaultValue);
("if(m.hasOwnProperty(%j)&&%s!==undefined)", field.name, ref);

}

if (wireType === undefined)
genTypePartial(gen, field, i, ref);
else gen
Expand Down
28 changes: 0 additions & 28 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,6 @@ util.newBuffer = function newBuffer(sizeOrArray) {
*/
util.Array = typeof Uint8Array === "undefined" ? Array : Uint8Array;

/**
* Tests if two arrays are not equal.
* @param {Array.<*>} a Array 1
* @param {Array.<*>} b Array 2
* @returns {boolean} `true` if not equal, otherwise `false`
*/
util.arrayNe = function arrayNe(a, b) {
if (a.length === b.length)
for (var i = 0; i < a.length; ++i)
if (a[i] !== b[i])
return true;
return false;
};

util.LongBits = require("./longbits");

/**
Expand Down Expand Up @@ -152,20 +138,6 @@ util.longFromHash = function longFromHash(hash, unsigned) {
return bits.toNumber(Boolean(unsigned));
};

/**
* Tests if a possibily long value equals the specified low and high bits.
* @param {number|string|Long} val Value to test
* @param {number} lo Low bits to test against
* @param {number} hi High bits to test against
* @returns {boolean} `true` if not equal
*/
util.longNe = function longNe(val, lo, hi) {
if (typeof val === "object") // Long-like, null is invalid and throws
return val.low !== lo || val.high !== hi;
var bits = util.LongBits.from(val);
return bits.lo !== lo || bits.hi !== hi;
};

/**
* Merges the properties of the source object into the destination object.
* @param {Object.<string,*>} dst Destination object
Expand Down
24 changes: 7 additions & 17 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex*/
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";

var $protobuf = require("../../runtime");
Expand Down Expand Up @@ -54,7 +54,7 @@ $root.A = (function() {
A.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.whatever !== undefined && message.whatever !== "")
if (message.hasOwnProperty("whatever") && message.whatever !== undefined)
writer.uint32(/* id 1, wireType 2 =*/10).string(message.whatever);
return writer;
};
Expand Down Expand Up @@ -148,13 +148,8 @@ $root.A = (function() {
var object = {};
if (options.defaults)
object.whatever = "";
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i)
if (message[keys[i]] !== undefined && message[keys[i]] !== null)
switch (keys[i]) {
case "whatever":
object.whatever = message.whatever;
break;
}
if (message.hasOwnProperty("whatever") && message.whatever !== undefined && message.whatever !== null)
object.whatever = message.whatever;
return object;
};

Expand Down Expand Up @@ -223,7 +218,7 @@ $root.B = (function() {
B.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.A !== undefined && message.A !== null)
if (message.hasOwnProperty("A") && message.A !== undefined)
$types[0].encode(message.A, writer.uint32(/* id 1, wireType 2 =*/10).fork()).ldelim();
return writer;
};
Expand Down Expand Up @@ -319,13 +314,8 @@ $root.B = (function() {
var object = {};
if (options.defaults)
object.A = null;
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i)
if (message[keys[i]] !== undefined && message[keys[i]] !== null)
switch (keys[i]) {
case "A":
object.A = $types[0].toObject(message.A, options);
break;
}
if (message.hasOwnProperty("A") && message.A !== undefined && message.A !== null)
object.A = $types[0].toObject(message.A, options);
return object;
};

Expand Down
27 changes: 10 additions & 17 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex*/
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";

var $protobuf = require("../../runtime");
Expand Down Expand Up @@ -70,11 +70,11 @@ $root.Test1 = (function() {
Test1.encode = function encode(message, writer) {
if (!writer)
writer = $Writer.create();
if (message.field1 !== undefined && message.field1 !== "")
if (message.hasOwnProperty("field1") && message.field1 !== undefined)
writer.uint32(/* id 1, wireType 2 =*/10).string(message.field1);
if (message.field2 !== undefined && message.field2 !== 0)
if (message.hasOwnProperty("field2") && message.field2 !== undefined)
writer.uint32(/* id 2, wireType 0 =*/16).uint32(message.field2);
if (message.field3 !== undefined && message.field3 !== false)
if (message.hasOwnProperty("field3") && message.field3 !== undefined)
writer.uint32(/* id 3, wireType 0 =*/24).bool(message.field3);
return writer;
};
Expand Down Expand Up @@ -187,19 +187,12 @@ $root.Test1 = (function() {
object.field2 = 0;
object.field3 = false;
}
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i)
if (message[keys[i]] !== undefined && message[keys[i]] !== null)
switch (keys[i]) {
case "field1":
object.field1 = message.field1;
break;
case "field2":
object.field2 = message.field2;
break;
case "field3":
object.field3 = message.field3;
break;
}
if (message.hasOwnProperty("field1") && message.field1 !== undefined && message.field1 !== null)
object.field1 = message.field1;
if (message.hasOwnProperty("field2") && message.field2 !== undefined && message.field2 !== null)
object.field2 = message.field2;
if (message.hasOwnProperty("field3") && message.field3 !== undefined && message.field3 !== null)
object.field3 = message.field3;
return object;
};

Expand Down
Loading

0 comments on commit 9b090bb

Please sign in to comment.