Skip to content

Commit

Permalink
fix: first draft use collection equality
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Mar 6, 2020
1 parent 4adc6be commit 6acbde7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/SwingSet/test/test-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('serialize static data', t => {
body: '{"@qclass":"undefined"}',
slots: [],
});
t.deepEqual(ser(-0), { body: '{"@qclass":"-0"}', slots: [] });
// t.deepEqual(ser(-0), { body: '{"@qclass":"-0"}', slots: [] });
t.deepEqual(ser(NaN), { body: '{"@qclass":"NaN"}', slots: [] });
t.deepEqual(ser(Infinity), {
body: '{"@qclass":"Infinity"}',
Expand Down Expand Up @@ -86,8 +86,8 @@ test('unserialize static data', t => {

// JS primitives that aren't natively representable by JSON
t.deepEqual(uns('{"@qclass":"undefined"}'), undefined);
t.ok(Object.is(uns('{"@qclass":"-0"}'), -0));
t.notOk(Object.is(uns('{"@qclass":"-0"}'), 0));
// t.ok(Object.is(uns('{"@qclass":"-0"}'), -0));
// t.notOk(Object.is(uns('{"@qclass":"-0"}'), 0));
t.ok(Object.is(uns('{"@qclass":"NaN"}'), NaN));
t.deepEqual(uns('{"@qclass":"Infinity"}'), Infinity);
t.deepEqual(uns('{"@qclass":"-Infinity"}'), -Infinity);
Expand Down
16 changes: 12 additions & 4 deletions packages/marshal/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ export function mustPassByPresence(val) {
// ok!
}

// This is the equality comparison used by JavaScript's Map and Set
// abstractions, where NaN is the same as NaN and -0 is the same as
// 0. Marshal serializes -0 as zero, so the semantics of our distributed
// object system does not distinguish 0 from -0.
//
// `sameValueZero` is the EcmaScript spec name for this equality comparison,
// but TODO we need a better name for the API.
export function sameValueZero(x, y) {
return x === y || Object.is(x, y);
}

// How would val be passed? For primitive values, the answer is
// * 'null' for null
// * throwing an error for an unregistered symbol
Expand Down Expand Up @@ -343,7 +354,7 @@ export function makeMarshal(
return harden({ [QCLASS]: 'NaN' });
}
if (Object.is(val, -0)) {
return harden({ [QCLASS]: '-0' });
return 0;
}
if (val === Infinity) {
return harden({ [QCLASS]: 'Infinity' });
Expand Down Expand Up @@ -478,9 +489,6 @@ export function makeMarshal(
case 'undefined': {
return undefined;
}
case '-0': {
return -0;
}
case 'NaN': {
return NaN;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/marshal/test/test-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('serialize static data', t => {
body: '{"@qclass":"undefined"}',
slots: [],
});
t.deepEqual(ser(-0), { body: '{"@qclass":"-0"}', slots: [] });
// t.deepEqual(ser(-0), { body: '{"@qclass":"-0"}', slots: [] });
t.deepEqual(ser(NaN), { body: '{"@qclass":"NaN"}', slots: [] });
t.deepEqual(ser(Infinity), {
body: '{"@qclass":"Infinity"}',
Expand Down Expand Up @@ -75,8 +75,8 @@ test('unserialize static data', t => {

// JS primitives that aren't natively representable by JSON
t.deepEqual(uns('{"@qclass":"undefined"}'), undefined);
t.ok(Object.is(uns('{"@qclass":"-0"}'), -0));
t.notOk(Object.is(uns('{"@qclass":"-0"}'), 0));
//t.ok(Object.is(uns('{"@qclass":"-0"}'), -0));
//t.notOk(Object.is(uns('{"@qclass":"-0"}'), 0));
t.ok(Object.is(uns('{"@qclass":"NaN"}'), NaN));
t.deepEqual(uns('{"@qclass":"Infinity"}'), Infinity);
t.deepEqual(uns('{"@qclass":"-Infinity"}'), -Infinity);
Expand Down
6 changes: 3 additions & 3 deletions packages/same-structure/src/sameStructure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import harden from '@agoric/harden';
import { passStyleOf } from '@agoric/marshal';
import { sameValueZero, passStyleOf } from '@agoric/marshal';
import { assert, details, openDetail } from '@agoric/assert';

// Shim of Object.fromEntries from
Expand Down Expand Up @@ -114,7 +114,7 @@ function sameStructure(left, right) {
case 'symbol':
case 'bigint':
case 'presence': {
return Object.is(left, right);
return sameValueZero(left, right);
}
case 'copyRecord':
case 'copyArray': {
Expand Down Expand Up @@ -193,7 +193,7 @@ function mustBeSameStructureInternal(left, right, message, path) {
case 'symbol':
case 'bigint':
case 'presence': {
if (!Object.is(left, right)) {
if (!sameValueZero(left, right)) {
complain('different');
}
break;
Expand Down

0 comments on commit 6acbde7

Please sign in to comment.