From 2c380bd87a3efc02f22e6ef2cda0789c6f2e97e3 Mon Sep 17 00:00:00 2001 From: Jerry Jiang Date: Tue, 23 Aug 2022 19:03:08 -0700 Subject: [PATCH] Alternative fix for int64toString --- packages/runtime/spec/pb-long.spec.ts | 8 ++++++++ packages/runtime/src/goog-varint.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/runtime/spec/pb-long.spec.ts b/packages/runtime/spec/pb-long.spec.ts index 3c3687a5..be0b8076 100644 --- a/packages/runtime/spec/pb-long.spec.ts +++ b/packages/runtime/spec/pb-long.spec.ts @@ -1,4 +1,5 @@ import {PbLong, PbULong} from "../src"; +import {int64toString} from "../src/goog-varint"; describe('PbULong', function () { @@ -72,6 +73,13 @@ describe('PbULong', function () { expect(ulong.lo).toBe(0); }); + it('int64toString should serialize the same was as BigInt.toString()', function () { + let ulong = PbULong.from(1661324400000); + expect(ulong.hi).toBe(386); + expect(ulong.lo).toBe(-827943552); + expect(ulong.toString()).toBe('1661324400000') + expect(int64toString(ulong.lo, ulong.hi)).toBe('1661324400000') + }); }); diff --git a/packages/runtime/src/goog-varint.ts b/packages/runtime/src/goog-varint.ts index 3fd00ab7..89922b55 100644 --- a/packages/runtime/src/goog-varint.ts +++ b/packages/runtime/src/goog-varint.ts @@ -178,7 +178,7 @@ export function int64toString(bitsLow: number, bitsHigh: number): string { // Skip the expensive conversion if the number is small enough to use the // built-in conversions. if (bitsHigh <= 0x1FFFFF) { - return '' + (TWO_PWR_32_DBL * bitsHigh + bitsLow); + return '' + (TWO_PWR_32_DBL * bitsHigh + (bitsLow >>> 0)); } // What this code is doing is essentially converting the input number from