Skip to content

Commit

Permalink
buffer: add isSharedArrayBuffer checks
Browse files Browse the repository at this point in the history
Fixes: #8440
PR-URL: #8510
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
ojss authored and mscdex committed Sep 17, 2016
1 parent a89c23f commit 2a2ec9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const binding = process.binding('buffer');
const { isArrayBuffer } = process.binding('util');
const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util');
const bindingObj = {};
const internalUtil = require('internal/util');

Expand Down Expand Up @@ -103,7 +103,7 @@ Buffer.from = function(value, encodingOrOffset, length) {
if (typeof value === 'number')
throw new TypeError('"value" argument must not be a number');

if (isArrayBuffer(value))
if (isArrayBuffer(value) || isSharedArrayBuffer(value))
return fromArrayBuffer(value, encodingOrOffset, length);

if (typeof value === 'string')
Expand Down Expand Up @@ -264,7 +264,8 @@ function fromObject(obj) {
}

if (obj) {
if (isArrayBuffer(obj.buffer) || 'length' in obj) {
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
isSharedArrayBuffer(obj)) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer();
}
Expand Down Expand Up @@ -351,8 +352,10 @@ function base64ByteLength(str, bytes) {

function byteLength(string, encoding) {
if (typeof string !== 'string') {
if (ArrayBuffer.isView(string) || isArrayBuffer(string))
if (ArrayBuffer.isView(string) || isArrayBuffer(string) ||
isSharedArrayBuffer(string)) {
return string.byteLength;
}

string = '' + string;
}
Expand Down
1 change: 1 addition & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using v8::Value;

#define VALUE_METHOD_MAP(V) \
V(isArrayBuffer, IsArrayBuffer) \
V(isSharedArrayBuffer, IsSharedArrayBuffer) \
V(isDataView, IsDataView) \
V(isDate, IsDate) \
V(isMap, IsMap) \
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-buffer-sharedarraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*global SharedArrayBuffer*/
'use strict';
// Flags: --harmony-sharedarraybuffer

require('../common');
const assert = require('assert');
const Buffer = require('buffer').Buffer;

const sab = new SharedArrayBuffer(24);
const arr1 = new Uint16Array(sab);
const arr2 = new Uint16Array(12);
arr2[0] = 5000;
arr1[0] = 5000;
arr1[1] = 4000;
arr2[1] = 4000;

const arr_buf = Buffer.from(arr1.buffer);
const ar_buf = Buffer.from(arr2.buffer);

assert.deepStrictEqual(arr_buf, ar_buf, 0);

arr1[1] = 6000;
arr2[1] = 6000;

assert.deepStrictEqual(arr_buf, ar_buf, 0);

// Checks for calling Buffer.byteLength on a SharedArrayBuffer

assert.strictEqual(Buffer.byteLength(sab), sab.byteLength, 0);

0 comments on commit 2a2ec9d

Please sign in to comment.