Skip to content

Commit

Permalink
util: Add format for SharedArrayBuffer
Browse files Browse the repository at this point in the history
PR-URL: #8587
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
yosuke-furukawa authored and jasnell committed Sep 26, 2016
1 parent e65a2d7 commit ce7d307
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)} }`;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -29,6 +28,7 @@ using v8::Value;
V(isRegExp, IsRegExp) \
V(isSet, IsSet) \
V(isSetIterator, IsSetIterator) \
V(isSharedArrayBuffer, IsSharedArrayBuffer) \
V(isTypedArray, IsTypedArray)


Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-format-shared-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -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 }');

0 comments on commit ce7d307

Please sign in to comment.