diff --git a/src/util/longbits.js b/src/util/longbits.js index 305437152..64aaa6332 100644 --- a/src/util/longbits.js +++ b/src/util/longbits.js @@ -67,13 +67,23 @@ LongBits.fromNumber = function fromNumber(value) { /** * Constrcuts new long bits from a number or long. - * @param {Long|number} value Value + * @param {Long|number|string} value Value * @returns {util.LongBits} Instance */ LongBits.from = function from(value) { - return typeof value === 'number' - ? LongBits.fromNumber(value) - : new LongBits(value.low >>> 0, value.high >>> 0); + var type = typeof value, result=zero; + if(type === 'number') + result = LongBits.fromNumber(value); + else + { + if(type === 'string' && util.Long) + value = util.Long.fromString(value); + + if(value.low || value.high) + result = new LongBits(value.low >>> 0, value.high >>> 0); + } + + return result; }; /** diff --git a/src/writer.js b/src/writer.js index 65c4a0fe6..645e47ec3 100644 --- a/src/writer.js +++ b/src/writer.js @@ -214,31 +214,25 @@ function writeVarint64(buf, pos, val) { /** * Writes an unsigned 64 bit value as a varint. - * @param {Long|number} value Value to write + * @param {Long|number|string} value Value to write * @returns {Writer} `this` */ WriterPrototype.uint64 = function write_uint64(value) { - var bits; - if (typeof value === 'number') - bits = value ? LongBits.fromNumber(value) : LongBits.zero; - else if (value.low || value.high) - bits = new LongBits(value.low >>> 0, value.high >>> 0); - else - bits = LongBits.zero; + var bits = LongBits.from(value); return this.push(writeVarint64, bits.length(), bits); }; /** * Writes a signed 64 bit value as a varint. * @function - * @param {Long|number} value Value to write + * @param {Long|number|string} value Value to write * @returns {Writer} `this` */ WriterPrototype.int64 = WriterPrototype.uint64; /** * Writes a signed 64 bit value as a varint, zig-zag encoded. - * @param {Long|number} value Value to write + * @param {Long|number|string} value Value to write * @returns {Writer} `this` */ WriterPrototype.sint64 = function sint64(value) { @@ -246,6 +240,7 @@ WriterPrototype.sint64 = function sint64(value) { return this.push(writeVarint64, bits.length(), bits); }; + /** * Writes a boolish value as a varint. * @param {boolean} value Value to write