Skip to content

Commit

Permalink
[New] add support for SharedArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 5, 2023
1 parent 0360ee1 commit 0ef51c7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"files": "example/**",
"rules": {
"no-console": 0,
"no-magic-numbers": 0,
"no-magic-numbers": 0,
}
},
{
Expand Down
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
'use strict';

var objectKeys = require('object-keys');
var isArguments = require('is-arguments');
var is = require('object-is');
var isRegex = require('is-regex');
var assign = require('object.assign');
var callBound = require('call-bind/callBound');
var flags = require('regexp.prototype.flags');
var GetIntrinsic = require('get-intrinsic');
var getIterator = require('es-get-iterator');
var getSideChannel = require('side-channel');
var is = require('object-is');
var isArguments = require('is-arguments');
var isArray = require('isarray');
var isArrayBuffer = require('is-array-buffer');
var isDate = require('is-date-object');
var isRegex = require('is-regex');
var isSharedArrayBuffer = require('is-shared-array-buffer');
var objectKeys = require('object-keys');
var whichBoxedPrimitive = require('which-boxed-primitive');
var GetIntrinsic = require('get-intrinsic');
var callBound = require('call-bind/callBound');
var whichCollection = require('which-collection');
var getIterator = require('es-get-iterator');
var getSideChannel = require('side-channel');
var whichTypedArray = require('which-typed-array');
var assign = require('object.assign');
var isArrayBuffer = require('is-array-buffer');

var byteLength = callBound('%ArrayBuffer.prototype.byteLength%', true)
var byteLength = callBound('ArrayBuffer.prototype.byteLength', true)
|| function byteLength(ab) { return ab.byteLength; }; // in node < 0.11, byteLength is an own nonconfigurable property
var sabByteLength = callBound('SharedArrayBuffer.prototype.byteLength', true);

var $getTime = callBound('Date.prototype.getTime');
var gPO = Object.getPrototypeOf;
Expand Down Expand Up @@ -335,6 +337,14 @@ function objEquiv(a, b, opts, channel) {
return typeof Uint8Array === 'function' && internalDeepEqual(new Uint8Array(a), new Uint8Array(b), opts, channel);
}

var aIsSAB = isSharedArrayBuffer(a);
var bIsSAB = isSharedArrayBuffer(b);
if (aIsSAB !== bIsSAB) { return false; }
if (aIsSAB || bIsSAB) { // && would work too, because both are true or both false here
if (sabByteLength(a) !== sabByteLength(b)) { return false; }
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"is-array-buffer": "^3.0.1",
"is-date-object": "^1.0.5",
"is-regex": "^1.1.4",
"is-shared-array-buffer": "^1.0.2",
"isarray": "^2.0.5",
"object-is": "^1.1.5",
"object-keys": "^1.1.1",
Expand Down
61 changes: 61 additions & 0 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,5 +1236,66 @@ test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
st.end();
});

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

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

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

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

for (var i = 0; i < view1.byteLength; i += 1) {
view1[i] = 9; // change all values to 9's
}

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

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

t.test('lies about byteLength', { skip: !('byteLength' in SharedArrayBuffer.prototype) }, function (s2t) {
var empty4 = new SharedArrayBuffer(4);
var empty6 = new SharedArrayBuffer(6);
Object.defineProperty(empty6, 'byteLength', { value: 4 });

s2t.deepEqualTest(
empty4,
empty6,
'different-length SharedArrayBuffers, one lying',
false,
false
);
s2t.end();
});

st.end();
});

t.end();
});

0 comments on commit 0ef51c7

Please sign in to comment.