diff --git a/lib/util.js b/lib/util.js index caab2651f3532d..f742e38433927f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -435,9 +435,10 @@ function formatValue(ctx, value, recurseTimes) { formatted = formatPrimitiveNoColor(ctx, raw); return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean'); } - // Fast path for ArrayBuffer. Can't do the same for DataView because it - // has a non-primitive .buffer property that we need to recurse for. - if (binding.isArrayBuffer(value)) { + // Fast path for ArrayBuffer and SharedArrayBuffer. + // Can't do the same for DataView because it has a non-primitive + // .buffer property that we need to recurse for. + if (binding.isArrayBuffer(value) || binding.isSharedArrayBuffer(value)) { return `${getConstructorOf(value).name}` + ` { byteLength: ${formatNumber(ctx, value.byteLength)} }`; } @@ -475,7 +476,8 @@ function formatValue(ctx, value, recurseTimes) { keys.unshift('size'); empty = value.size === 0; formatter = formatMap; - } else if (binding.isArrayBuffer(value)) { + } else if (binding.isArrayBuffer(value) || + binding.isSharedArrayBuffer(value)) { braces = ['{', '}']; keys.unshift('byteLength'); visibleKeys.byteLength = true; diff --git a/src/node_util.cc b/src/node_util.cc index 000d1e6a410292..0db7ab40fbf609 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -20,7 +20,6 @@ using v8::Value; #define VALUE_METHOD_MAP(V) \ V(isArrayBuffer, IsArrayBuffer) \ - V(isSharedArrayBuffer, IsSharedArrayBuffer) \ V(isDataView, IsDataView) \ V(isDate, IsDate) \ V(isMap, IsMap) \ @@ -29,6 +28,7 @@ using v8::Value; V(isRegExp, IsRegExp) \ V(isSet, IsSet) \ V(isSetIterator, IsSetIterator) \ + V(isSharedArrayBuffer, IsSharedArrayBuffer) \ V(isTypedArray, IsTypedArray) diff --git a/test/parallel/test-util-format-shared-arraybuffer.js b/test/parallel/test-util-format-shared-arraybuffer.js new file mode 100644 index 00000000000000..d0c7dfe8c4c7f2 --- /dev/null +++ b/test/parallel/test-util-format-shared-arraybuffer.js @@ -0,0 +1,10 @@ +// Flags: --harmony_sharedarraybuffer + +'use strict'; +require('../common'); +const assert = require('assert'); +const util = require('util'); + +/* global SharedArrayBuffer */ +const sab = new SharedArrayBuffer(4); +assert.strictEqual(util.format(sab), 'SharedArrayBuffer { byteLength: 4 }');