Browse Source

util: Add format for SharedArrayBuffer

PR-URL: https://github.com/nodejs/node/pull/8587
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
Yosuke Furukawa 8 years ago
committed by James M Snell
parent
commit
ce7d3077dc
  1. 10
      lib/util.js
  2. 2
      src/node_util.cc
  3. 10
      test/parallel/test-util-format-shared-arraybuffer.js

10
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;

2
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)

10
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 }');
Loading…
Cancel
Save