Skip to content

Commit

Permalink
[New] add support for ArrayBuffer
Browse files Browse the repository at this point in the history
Fixes #94.
  • Loading branch information
ljharb committed Sep 22, 2021
1 parent 492e159 commit b47d59e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"func-style": [2, "declaration"],
"id-length": 0,
"indent": [2, 2],
"max-lines": 0,
"max-params": [2, 6],
"max-statements-per-line": [2, { "max": 2 }],
"new-cap": [2, { "capIsNewExceptions": ["GetIntrinsic"] }],
"no-magic-numbers": [2, { "ignore": [0, 1] }],
"operator-linebreak": [2, "before"],
},
"globals": {
"ArrayBuffer": false,
"WeakMap": false,
"WeakSet": false,
},
Expand Down
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ var getSideChannel = require('side-channel');
var whichTypedArray = require('which-typed-array');
var assign = require('object.assign');

// TODO: use extracted package
var byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
function isArrayBuffer(buffer) {
if (!buffer || typeof buffer !== 'object' || !byteLength) {
return false;
}
try {
byteLength(buffer);
return true;
} catch (e) {
return false;
}
}

var $getTime = callBound('Date.prototype.getTime');
var gPO = Object.getPrototypeOf;
var $objToString = callBound('Object.prototype.toString');
Expand Down Expand Up @@ -321,6 +335,15 @@ function objEquiv(a, b, opts, channel) {
return true;
}

var aIsArrayBuffer = isArrayBuffer(a);
var bIsArrayBuffer = isArrayBuffer(b);
if (aIsArrayBuffer !== bIsArrayBuffer) { return false; }
if (aIsArrayBuffer || bIsArrayBuffer) { // && would work too, because both are true or both false here
if (byteLength(a) !== byteLength(b)) { return false; }
/* global Uint8Array */
return typeof Uint8Array === 'function' && internalDeepEqual(new Uint8Array(a), new Uint8Array(b), opts, channel);
}

if (typeof a !== typeof b) { return false; }

var ka = objectKeys(a);
Expand Down
55 changes: 55 additions & 0 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,5 +1175,60 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
st.end();
});

t.test('ArrayBuffers', { skip: typeof ArrayBuffer !== 'function' }, function (st) {
var buffer1 = new ArrayBuffer(8); // initial value of 0's
var buffer2 = new ArrayBuffer(8); // initial value of 0's

var view1 = new Int8Array(buffer1);
var view2 = new Int8Array(buffer2);

st.deepEqualTest(
view1,
view2,
'Int8Arrays of similar ArrayBuffers',
true,
true
);

st.deepEqualTest(
buffer1,
buffer2,
'similar ArrayBuffers',
true,
true
);

view1.fill(9); // change all values to 9's

st.deepEqualTest(
view1,
view2,
'Int8Arrays of different ArrayBuffers',
false,
false
);

st.deepEqualTest(
buffer1,
buffer2,
'different ArrayBuffers',
false,
false
);

var empty4 = new ArrayBuffer(4);
var empty6 = new ArrayBuffer(6);
Object.defineProperty(empty6, 'byteLength', { value: 4 });
st.deepEqualTest(
empty4,
empty6,
'different-length ArrayBuffers, one lying',
false,
false
);

st.end();
});

t.end();
});

0 comments on commit b47d59e

Please sign in to comment.