Skip to content

Re-enable advanced optimizations and add binary/ tests for CommonJS #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions binary/arith.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ goog.provide('jspb.arith.UInt64');
* @param {number} lo The low 32 bits.
* @param {number} hi The high 32 bits.
* @constructor
* @export
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an example of something that is only being exported for tests

can you open an issue to look into removing export from APIs like this to improve size and just note that this will require a test refactoring.

*/
jspb.arith.UInt64 = function(lo, hi) {
/**
* The low 32 bits.
* @public {number}
* @type {number}
* @export
*/
this.lo = lo;
/**
* The high 32 bits.
* @public {number}
* @type {number}
* @export
*/
this.hi = hi;
};
Expand All @@ -69,6 +72,7 @@ jspb.arith.UInt64 = function(lo, hi) {
* less, +1 if the first is greater, or 0 if both are equal.
* @param {!jspb.arith.UInt64} other
* @return {number}
* @export
*/
jspb.arith.UInt64.prototype.cmp = function(other) {
if (this.hi < other.hi || (this.hi == other.hi && this.lo < other.lo)) {
Expand All @@ -84,6 +88,7 @@ jspb.arith.UInt64.prototype.cmp = function(other) {
/**
* Right-shift this number by one bit.
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.rightShift = function() {
var hi = this.hi >>> 1;
Expand All @@ -95,6 +100,7 @@ jspb.arith.UInt64.prototype.rightShift = function() {
/**
* Left-shift this number by one bit.
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.leftShift = function() {
var lo = this.lo << 1;
Expand All @@ -106,6 +112,7 @@ jspb.arith.UInt64.prototype.leftShift = function() {
/**
* Test the MSB.
* @return {boolean}
* @export
*/
jspb.arith.UInt64.prototype.msb = function() {
return !!(this.hi & 0x80000000);
Expand All @@ -115,6 +122,7 @@ jspb.arith.UInt64.prototype.msb = function() {
/**
* Test the LSB.
* @return {boolean}
* @export
*/
jspb.arith.UInt64.prototype.lsb = function() {
return !!(this.lo & 1);
Expand All @@ -124,6 +132,7 @@ jspb.arith.UInt64.prototype.lsb = function() {
/**
* Test whether this number is zero.
* @return {boolean}
* @export
*/
jspb.arith.UInt64.prototype.zero = function() {
return this.lo == 0 && this.hi == 0;
Expand All @@ -134,6 +143,7 @@ jspb.arith.UInt64.prototype.zero = function() {
* Add two 64-bit numbers to produce a 64-bit number.
* @param {!jspb.arith.UInt64} other
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.add = function(other) {
var lo = ((this.lo + other.lo) & 0xffffffff) >>> 0;
Expand All @@ -148,6 +158,7 @@ jspb.arith.UInt64.prototype.add = function(other) {
* Subtract two 64-bit numbers to produce a 64-bit number.
* @param {!jspb.arith.UInt64} other
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.sub = function(other) {
var lo = ((this.lo - other.lo) & 0xffffffff) >>> 0;
Expand All @@ -163,6 +174,7 @@ jspb.arith.UInt64.prototype.sub = function(other) {
* @param {number} a The first integer: must be in [0, 2^32-1).
* @param {number} b The second integer: must be in [0, 2^32-1).
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.mul32x32 = function(a, b) {
// Directly multiplying two 32-bit numbers may produce up to 64 bits of
Expand Down Expand Up @@ -204,6 +216,7 @@ jspb.arith.UInt64.mul32x32 = function(a, b) {
* truncate the top 32 bits.
* @param {number} a The multiplier.
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.mul = function(a) {
// Produce two parts: at bits 0-63, and 32-95.
Expand All @@ -223,6 +236,7 @@ jspb.arith.UInt64.prototype.mul = function(a) {
* @param {number} _divisor
* @return {Array<jspb.arith.UInt64>} array of [quotient, remainder],
* unless divisor is 0, in which case an empty array is returned.
* @export
*/
jspb.arith.UInt64.prototype.div = function(_divisor) {
if (_divisor == 0) {
Expand Down Expand Up @@ -264,6 +278,7 @@ jspb.arith.UInt64.prototype.div = function(_divisor) {
* Convert a 64-bit number to a string.
* @return {string}
* @override
* @export
*/
jspb.arith.UInt64.prototype.toString = function() {
var result = '';
Expand All @@ -285,6 +300,7 @@ jspb.arith.UInt64.prototype.toString = function() {
* Parse a string into a 64-bit number. Returns `null` on a parse error.
* @param {string} s
* @return {?jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.fromString = function(s) {
var result = new jspb.arith.UInt64(0, 0);
Expand All @@ -305,6 +321,7 @@ jspb.arith.UInt64.fromString = function(s) {
/**
* Make a copy of the uint64.
* @return {!jspb.arith.UInt64}
* @export
*/
jspb.arith.UInt64.prototype.clone = function() {
return new jspb.arith.UInt64(this.lo, this.hi);
Expand All @@ -324,16 +341,19 @@ jspb.arith.UInt64.prototype.clone = function() {
* @param {number} lo The low 32 bits.
* @param {number} hi The high 32 bits.
* @constructor
* @export
*/
jspb.arith.Int64 = function(lo, hi) {
/**
* The low 32 bits.
* @public {number}
* @type {number}
* @export
*/
this.lo = lo;
/**
* The high 32 bits.
* @public {number}
* @type {number}
* @export
*/
this.hi = hi;
};
Expand All @@ -343,6 +363,7 @@ jspb.arith.Int64 = function(lo, hi) {
* Add two 64-bit numbers to produce a 64-bit number.
* @param {!jspb.arith.Int64} other
* @return {!jspb.arith.Int64}
* @export
*/
jspb.arith.Int64.prototype.add = function(other) {
var lo = ((this.lo + other.lo) & 0xffffffff) >>> 0;
Expand All @@ -357,6 +378,7 @@ jspb.arith.Int64.prototype.add = function(other) {
* Subtract two 64-bit numbers to produce a 64-bit number.
* @param {!jspb.arith.Int64} other
* @return {!jspb.arith.Int64}
* @export
*/
jspb.arith.Int64.prototype.sub = function(other) {
var lo = ((this.lo - other.lo) & 0xffffffff) >>> 0;
Expand All @@ -370,6 +392,7 @@ jspb.arith.Int64.prototype.sub = function(other) {
/**
* Make a copy of the int64.
* @return {!jspb.arith.Int64}
* @export
*/
jspb.arith.Int64.prototype.clone = function() {
return new jspb.arith.Int64(this.lo, this.hi);
Expand All @@ -380,6 +403,7 @@ jspb.arith.Int64.prototype.clone = function() {
* Convert a 64-bit number to a string.
* @return {string}
* @override
* @export
*/
jspb.arith.Int64.prototype.toString = function() {
// If the number is negative, find its twos-complement inverse.
Expand All @@ -396,6 +420,7 @@ jspb.arith.Int64.prototype.toString = function() {
* Parse a string into a 64-bit number. Returns `null` on a parse error.
* @param {string} s
* @return {?jspb.arith.Int64}
* @export
*/
jspb.arith.Int64.fromString = function(s) {
var hasNegative = (s.length > 0 && s[0] == '-');
Expand Down
Loading