Skip to content

Commit

Permalink
Fix tracing -0
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 9, 2023
1 parent 9e800a2 commit 14cc078
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/serialize/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const {

const bigIntToString = BigInt.prototype.toString;

module.exports = {tracePrimitive};
module.exports = {tracePrimitive, traceMinusZero};

/**
* Trace primitive value.
Expand Down Expand Up @@ -81,8 +81,7 @@ function traceBoolean(val, record) {
* @returns {number} - Type ID
*/
function traceNumber(val, record) {
// `Object.is()` required to differentiate between 0 and -0
if (val < 0 || Object.is(val, -0)) {
if (val < 0) {
record.name = `minus${-val}`;
record.extra = {positive: this.traceDependency(-val, null, null, record)};
return NEGATIVE_TYPE;
Expand All @@ -93,6 +92,18 @@ function traceNumber(val, record) {
return NUMBER_TYPE;
}

/**
* Trace minus zero.
* @this {Object} Serializer
* @param {Object} record - Record
* @returns {number} - Type ID
*/
function traceMinusZero(record) {
record.name = 'minus0';
record.extra = {positive: this.traceDependency(0, null, null, record)};
return NEGATIVE_TYPE;
}

/**
* Trace `BigInt`.
* @this {Object} Serializer
Expand Down
10 changes: 10 additions & 0 deletions lib/serialize/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
*/
initTrace() {
this.traceStack = [];
this.minusZeroRecord = null;
},

/**
Expand All @@ -52,6 +53,15 @@ module.exports = {
},

traceValueInner(val, name) {
// Special case -0 because 0 and -0 are treated as equal as Map keys
if (Object.is(val, -0)) {
if (this.minusZeroRecord) return this.minusZeroRecord;
const record = createRecord(name);
record.type = this.traceMinusZero(record);
this.minusZeroRecord = record;
return record;
}

// Use existing record
const {records} = this;
let record = records.get(val);
Expand Down

0 comments on commit 14cc078

Please sign in to comment.