-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: improve typed array formatting #3793
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,10 +284,17 @@ 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 (value instanceof ArrayBuffer) { | ||
return `${getConstructorOf(value).name}` + | ||
` { byteLength: ${formatNumber(ctx, value.byteLength)} }`; | ||
} | ||
} | ||
|
||
var constructor = getConstructorOf(value); | ||
var base = '', empty = false, braces, formatter; | ||
var base = '', empty = false, braces; | ||
var formatter = formatObject; | ||
|
||
if (Array.isArray(value)) { | ||
// We can't use `constructor === Array` because this could | ||
|
@@ -314,6 +321,28 @@ function formatValue(ctx, value, recurseTimes) { | |
keys.unshift('size'); | ||
empty = value.size === 0; | ||
formatter = formatMap; | ||
} else if (value instanceof ArrayBuffer) { | ||
braces = ['{', '}']; | ||
keys.unshift('byteLength'); | ||
visibleKeys.byteLength = true; | ||
} else if (value instanceof DataView) { | ||
braces = ['{', '}']; | ||
// .buffer goes last, it's not a primitive like the others. | ||
keys.unshift('byteLength', 'byteOffset', 'buffer'); | ||
visibleKeys.byteLength = true; | ||
visibleKeys.byteOffset = true; | ||
visibleKeys.buffer = true; | ||
} else if (isTypedArray(value)) { | ||
braces = ['[', ']']; | ||
formatter = formatTypedArray; | ||
if (ctx.showHidden) { | ||
// .buffer goes last, it's not a primitive like the others. | ||
keys.unshift('BYTES_PER_ELEMENT', | ||
'length', | ||
'byteLength', | ||
'byteOffset', | ||
'buffer'); | ||
} | ||
} else { | ||
// Only create a mirror if the object superficially looks like a Promise. | ||
var promiseInternals = value instanceof Promise && inspectPromise(value); | ||
|
@@ -336,7 +365,6 @@ function formatValue(ctx, value, recurseTimes) { | |
constructor = null; | ||
braces = ['{', '}']; | ||
empty = true; // No other data than keys. | ||
formatter = formatObject; | ||
} | ||
} | ||
} | ||
|
@@ -408,6 +436,15 @@ function formatValue(ctx, value, recurseTimes) { | |
} | ||
|
||
|
||
function formatNumber(ctx, value) { | ||
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0, | ||
// so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 . | ||
if (value === 0 && 1 / value < 0) | ||
return ctx.stylize('-0', 'number'); | ||
return ctx.stylize('' + value, 'number'); | ||
} | ||
|
||
|
||
function formatPrimitive(ctx, value) { | ||
if (value === undefined) | ||
return ctx.stylize('undefined', 'undefined'); | ||
|
@@ -424,13 +461,8 @@ function formatPrimitive(ctx, value) { | |
.replace(/\\"/g, '"') + '\''; | ||
return ctx.stylize(simple, 'string'); | ||
} | ||
if (type === 'number') { | ||
// Format -0 as '-0'. Strict equality won't distinguish 0 from -0, | ||
// so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 . | ||
if (value === 0 && 1 / value < 0) | ||
return ctx.stylize('-0', 'number'); | ||
return ctx.stylize('' + value, 'number'); | ||
} | ||
if (type === 'number') | ||
return formatNumber(ctx, value); | ||
if (type === 'boolean') | ||
return ctx.stylize('' + value, 'boolean'); | ||
// es6 symbol primitive | ||
|
@@ -480,6 +512,20 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { | |
} | ||
|
||
|
||
function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) { | ||
var output = new Array(value.length); | ||
for (var i = 0, l = value.length; i < l; ++i) | ||
output[i] = formatNumber(ctx, value[i]); | ||
for (const key of keys) { | ||
if (typeof key === 'symbol' || !key.match(/^\d+$/)) { | ||
output.push( | ||
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)); | ||
} | ||
} | ||
return output; | ||
} | ||
|
||
|
||
function formatSet(ctx, value, recurseTimes, visibleKeys, keys) { | ||
var output = []; | ||
value.forEach(function(v) { | ||
|
@@ -626,6 +672,19 @@ function reduceToSingleString(output, base, braces) { | |
} | ||
|
||
|
||
function isTypedArray(value) { | ||
return value instanceof Float32Array || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder, would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, not for a buffer anyways...nevermind me |
||
value instanceof Float64Array || | ||
value instanceof Int16Array || | ||
value instanceof Int32Array || | ||
value instanceof Int8Array || | ||
value instanceof Uint16Array || | ||
value instanceof Uint32Array || | ||
value instanceof Uint8Array || | ||
value instanceof Uint8ClampedArray; | ||
} | ||
|
||
|
||
// NOTE: These type checking functions intentionally don't use `instanceof` | ||
// because it is fragile and can be easily faked with `Object.create()`. | ||
exports.isArray = Array.isArray; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.keys((new ArrayBuffer(5))).length === 0
. Should this be placed in the shortcut path above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nm. overlooked the other keys additions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hah. so I wasn't loosing it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it here because it adds a key for
.byteLength
but I can probably move it up and callformatProperty()
directly, likewise for DataView. Good idea / bad idea?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a fast path for ArrayBuffer, PTAL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it.