Skip to content

Commit ee319b7

Browse files
ojssFishrock123
authored andcommitted
buffer: add isSharedArrayBuffer checks
Fixes: #8440 PR-URL: #8510 Reviewed-By: Brian White <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent b10467c commit ee319b7

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

lib/buffer.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
const binding = process.binding('buffer');
5-
const { isArrayBuffer } = process.binding('util');
5+
const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util');
66
const bindingObj = {};
77
const internalUtil = require('internal/util');
88

@@ -88,7 +88,7 @@ Buffer.from = function(value, encodingOrOffset, length) {
8888
if (typeof value === 'number')
8989
throw new TypeError('"value" argument must not be a number');
9090

91-
if (isArrayBuffer(value))
91+
if (isArrayBuffer(value) || isSharedArrayBuffer(value))
9292
return fromArrayBuffer(value, encodingOrOffset, length);
9393

9494
if (typeof value === 'string')
@@ -242,7 +242,8 @@ function fromObject(obj) {
242242
}
243243

244244
if (obj) {
245-
if (isArrayBuffer(obj.buffer) || 'length' in obj) {
245+
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
246+
isSharedArrayBuffer(obj)) {
246247
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
247248
return new FastBuffer();
248249
}
@@ -337,8 +338,10 @@ function base64ByteLength(str, bytes) {
337338

338339
function byteLength(string, encoding) {
339340
if (typeof string !== 'string') {
340-
if (ArrayBuffer.isView(string) || isArrayBuffer(string))
341+
if (ArrayBuffer.isView(string) || isArrayBuffer(string) ||
342+
isSharedArrayBuffer(string)) {
341343
return string.byteLength;
344+
}
342345

343346
string = '' + string;
344347
}

src/node_util.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using v8::Value;
2020

2121
#define VALUE_METHOD_MAP(V) \
2222
V(isArrayBuffer, IsArrayBuffer) \
23+
V(isSharedArrayBuffer, IsSharedArrayBuffer) \
2324
V(isDataView, IsDataView) \
2425
V(isDate, IsDate) \
2526
V(isMap, IsMap) \
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*global SharedArrayBuffer*/
2+
'use strict';
3+
// Flags: --harmony-sharedarraybuffer
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const Buffer = require('buffer').Buffer;
8+
9+
const sab = new SharedArrayBuffer(24);
10+
const arr1 = new Uint16Array(sab);
11+
const arr2 = new Uint16Array(12);
12+
arr2[0] = 5000;
13+
arr1[0] = 5000;
14+
arr1[1] = 4000;
15+
arr2[1] = 4000;
16+
17+
const arr_buf = Buffer.from(arr1.buffer);
18+
const ar_buf = Buffer.from(arr2.buffer);
19+
20+
assert.deepStrictEqual(arr_buf, ar_buf, 0);
21+
22+
arr1[1] = 6000;
23+
arr2[1] = 6000;
24+
25+
assert.deepStrictEqual(arr_buf, ar_buf, 0);
26+
27+
// Checks for calling Buffer.byteLength on a SharedArrayBuffer
28+
29+
assert.strictEqual(Buffer.byteLength(sab), sab.byteLength, 0);

0 commit comments

Comments
 (0)