Skip to content

Commit d270706

Browse files
bnoordhuisrvagg
authored andcommitted
util: pretty-print SIMD types
Before: Bool32x4 {} Float32x4 {} Int32x4 {} // etc. After: Bool32x4 [ false, false, false, false ] Float32x4 [ NaN, NaN, NaN, NaN ] Int32x4 [ 0, 0, 0, 0 ] // etc. PR-URL: #6917 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Jefe Lindstaedt <robert.lindstaedt@gmail.com>
1 parent 32cc43a commit d270706

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lib/util.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,56 @@ const isError = internalUtil.isError;
99
const kDefaultMaxLength = 100;
1010

1111
var Debug;
12+
var simdFormatters;
13+
14+
// SIMD is only available when --harmony_simd is specified on the command line
15+
// and the set of available types differs between v5 and v6, that's why we use
16+
// a map to look up and store the formatters. It also provides a modicum of
17+
// protection against users monkey-patching the SIMD object.
18+
if (typeof global.SIMD === 'object' && global.SIMD !== null) {
19+
simdFormatters = new Map();
20+
21+
const make = (extractLane, count) => {
22+
return (ctx, value, recurseTimes, visibleKeys, keys) => {
23+
const output = new Array(count);
24+
for (let i = 0; i < count; i += 1)
25+
output[i] = formatPrimitive(ctx, extractLane(value, i));
26+
return output;
27+
};
28+
};
29+
30+
const SIMD = global.SIMD; // Pacify eslint.
31+
32+
if (typeof SIMD.Bool16x8 === 'function')
33+
simdFormatters.set(SIMD.Bool16x8, make(SIMD.Bool16x8.extractLane, 8));
34+
35+
if (typeof SIMD.Bool32x4 === 'function')
36+
simdFormatters.set(SIMD.Bool32x4, make(SIMD.Bool32x4.extractLane, 4));
37+
38+
if (typeof SIMD.Bool8x16 === 'function')
39+
simdFormatters.set(SIMD.Bool8x16, make(SIMD.Bool8x16.extractLane, 16));
40+
41+
if (typeof SIMD.Float32x4 === 'function')
42+
simdFormatters.set(SIMD.Float32x4, make(SIMD.Float32x4.extractLane, 4));
43+
44+
if (typeof SIMD.Int16x8 === 'function')
45+
simdFormatters.set(SIMD.Int16x8, make(SIMD.Int16x8.extractLane, 8));
46+
47+
if (typeof SIMD.Int32x4 === 'function')
48+
simdFormatters.set(SIMD.Int32x4, make(SIMD.Int32x4.extractLane, 4));
49+
50+
if (typeof SIMD.Int8x16 === 'function')
51+
simdFormatters.set(SIMD.Int8x16, make(SIMD.Int8x16.extractLane, 16));
52+
53+
if (typeof SIMD.Uint16x8 === 'function')
54+
simdFormatters.set(SIMD.Uint16x8, make(SIMD.Uint16x8.extractLane, 8));
55+
56+
if (typeof SIMD.Uint32x4 === 'function')
57+
simdFormatters.set(SIMD.Uint32x4, make(SIMD.Uint32x4.extractLane, 4));
58+
59+
if (typeof SIMD.Uint8x16 === 'function')
60+
simdFormatters.set(SIMD.Uint8x16, make(SIMD.Uint8x16.extractLane, 16));
61+
}
1262

1363
function tryStringify(arg) {
1464
try {
@@ -432,6 +482,10 @@ function formatValue(ctx, value, recurseTimes) {
432482
'byteOffset',
433483
'buffer');
434484
}
485+
} else if (simdFormatters &&
486+
typeof value.constructor === 'function' &&
487+
(formatter = simdFormatters.get(value.constructor))) {
488+
braces = ['[', ']'];
435489
} else {
436490
var promiseInternals = inspectPromise(value);
437491
if (promiseInternals) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Flags: --harmony_simd
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
const inspect = require('util').inspect;
7+
8+
const SIMD = global.SIMD; // Pacify eslint.
9+
10+
assert.strictEqual(
11+
inspect(SIMD.Bool16x8()),
12+
'Bool16x8 [ false, false, false, false, false, false, false, false ]');
13+
14+
assert.strictEqual(
15+
inspect(SIMD.Bool32x4()),
16+
'Bool32x4 [ false, false, false, false ]');
17+
18+
assert.strictEqual(
19+
inspect(SIMD.Bool8x16()),
20+
'Bool8x16 [\n false,\n false,\n false,\n false,\n false,\n' +
21+
' false,\n false,\n false,\n false,\n false,\n false,\n' +
22+
' false,\n false,\n false,\n false,\n false ]');
23+
24+
assert.strictEqual(
25+
inspect(SIMD.Bool32x4()),
26+
'Bool32x4 [ false, false, false, false ]');
27+
28+
assert.strictEqual(
29+
inspect(SIMD.Float32x4()),
30+
'Float32x4 [ NaN, NaN, NaN, NaN ]');
31+
32+
assert.strictEqual(
33+
inspect(SIMD.Int16x8()),
34+
'Int16x8 [ 0, 0, 0, 0, 0, 0, 0, 0 ]');
35+
36+
assert.strictEqual(
37+
inspect(SIMD.Int32x4()),
38+
'Int32x4 [ 0, 0, 0, 0 ]');
39+
40+
assert.strictEqual(
41+
inspect(SIMD.Int8x16()),
42+
'Int8x16 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]');
43+
44+
// The SIMD types below are not available in v5.
45+
if (typeof SIMD.Uint16x8 === 'function') {
46+
assert.strictEqual(
47+
inspect(SIMD.Uint16x8()),
48+
'Uint16x8 [ 0, 0, 0, 0, 0, 0, 0, 0 ]');
49+
}
50+
51+
if (typeof SIMD.Uint32x4 === 'function') {
52+
assert.strictEqual(
53+
inspect(SIMD.Uint32x4()),
54+
'Uint32x4 [ 0, 0, 0, 0 ]');
55+
}
56+
57+
if (typeof SIMD.Uint8x16 === 'function') {
58+
assert.strictEqual(
59+
inspect(SIMD.Uint8x16()),
60+
'Uint8x16 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]');
61+
}

0 commit comments

Comments
 (0)