From e5c6df2b719a1755b49915725832920a0bc91b7a Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Sat, 17 Sep 2016 11:01:39 +0200 Subject: [PATCH] util: Add format for SharedArrayBuffer --- lib/util.js | 10 ++++++---- src/node_util.cc | 2 +- test/parallel/test-util-format-shared-arraybuffer.js | 10 ++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-util-format-shared-arraybuffer.js diff --git a/lib/util.js b/lib/util.js index 245b19639324df..a679ca8d580731 100644 --- a/lib/util.js +++ b/lib/util.js @@ -447,9 +447,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)} }`; } @@ -487,7 +488,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 b228af005077a4..c231983e57a2df 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 }');