GeezerVison is showing github.com — util: make inspect more reliable · bcomnes/node@24012a8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/util.js +4-17Lines changed: 4 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -290,7 +290,7 @@ function formatValue(ctx, value, recurseTimes) { | 290 | 290 | } | 291 | 291 | // Fast path for ArrayBuffer. Can't do the same for DataView because it | 292 | 292 | // has a non-primitive .buffer property that we need to recurse for. | 293 | | - if (value instanceof ArrayBuffer) { | | 293 | + if (binding.isArrayBuffer(value)) { | 294 | 294 | return `${getConstructorOf(value).name}` + | 295 | 295 | ` { byteLength: ${formatNumber(ctx, value.byteLength)} }`; | 296 | 296 | } | @@ -328,18 +328,18 @@ function formatValue(ctx, value, recurseTimes) { | 328 | 328 | keys.unshift('size'); | 329 | 329 | empty = value.size === 0; | 330 | 330 | formatter = formatMap; | 331 | | - } else if (value instanceof ArrayBuffer) { | | 331 | + } else if (binding.isArrayBuffer(value)) { | 332 | 332 | braces = ['{', '}']; | 333 | 333 | keys.unshift('byteLength'); | 334 | 334 | visibleKeys.byteLength = true; | 335 | | - } else if (value instanceof DataView) { | | 335 | + } else if (binding.isDataView(value)) { | 336 | 336 | braces = ['{', '}']; | 337 | 337 | // .buffer goes last, it's not a primitive like the others. | 338 | 338 | keys.unshift('byteLength', 'byteOffset', 'buffer'); | 339 | 339 | visibleKeys.byteLength = true; | 340 | 340 | visibleKeys.byteOffset = true; | 341 | 341 | visibleKeys.buffer = true; | 342 | | - } else if (isTypedArray(value)) { | | 342 | + } else if (binding.isTypedArray(value)) { | 343 | 343 | braces = ['[', ']']; | 344 | 344 | formatter = formatTypedArray; | 345 | 345 | if (ctx.showHidden) { | @@ -679,19 +679,6 @@ function reduceToSingleString(output, base, braces) { | 679 | 679 | } | 680 | 680 |
| 681 | 681 |
| 682 | | -function isTypedArray(value) { | 683 | | - return value instanceof Float32Array || | 684 | | - value instanceof Float64Array || | 685 | | - value instanceof Int16Array || | 686 | | - value instanceof Int32Array || | 687 | | - value instanceof Int8Array || | 688 | | - value instanceof Uint16Array || | 689 | | - value instanceof Uint32Array || | 690 | | - value instanceof Uint8Array || | 691 | | - value instanceof Uint8ClampedArray; | 692 | | -} | 693 | | - | 694 | | - | 695 | 682 | // NOTE: These type checking functions intentionally don't use `instanceof` | 696 | 683 | // because it is fragile and can be easily faked with `Object.create()`. | 697 | 684 | exports.isArray = Array.isArray; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,24 @@ static void IsPromise(const FunctionCallbackInfo<Value>& args) { | 30 | 30 | } | 31 | 31 |
| 32 | 32 |
| | 33 | +static void IsTypedArray(const FunctionCallbackInfo<Value>& args) { | | 34 | + CHECK_EQ(1, args.Length()); | | 35 | + args.GetReturnValue().Set(args[0]->IsTypedArray()); | | 36 | +} | | 37 | + | | 38 | + | | 39 | +static void IsArrayBuffer(const FunctionCallbackInfo<Value>& args) { | | 40 | + CHECK_EQ(1, args.Length()); | | 41 | + args.GetReturnValue().Set(args[0]->IsArrayBuffer()); | | 42 | +} | | 43 | + | | 44 | + | | 45 | +static void IsDataView(const FunctionCallbackInfo<Value>& args) { | | 46 | + CHECK_EQ(1, args.Length()); | | 47 | + args.GetReturnValue().Set(args[0]->IsDataView()); | | 48 | +} | | 49 | + | | 50 | + | 33 | 51 | static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) { | 34 | 52 | Environment* env = Environment::GetCurrent(args); | 35 | 53 |
| @@ -53,6 +71,9 @@ void Initialize(Local<Object> target, | 53 | 71 | env->SetMethod(target, "isMapIterator", IsMapIterator); | 54 | 72 | env->SetMethod(target, "isSetIterator", IsSetIterator); | 55 | 73 | env->SetMethod(target, "isPromise", IsPromise); | | 74 | + env->SetMethod(target, "isTypedArray", IsTypedArray); | | 75 | + env->SetMethod(target, "isArrayBuffer", IsArrayBuffer); | | 76 | + env->SetMethod(target, "isDataView", IsDataView); | 56 | 77 | env->SetMethod(target, "getHiddenValue", GetHiddenValue); | 57 | 78 | } | 58 | 79 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +62Lines changed: 62 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | var common = require('../common'); | 3 | 3 | var assert = require('assert'); | 4 | 4 | var util = require('util'); | | 5 | +const vm = require('vm'); | 5 | 6 |
| 6 | 7 | assert.equal(util.inspect(1), '1'); | 7 | 8 | assert.equal(util.inspect(false), 'false'); | @@ -68,6 +69,35 @@ for (const showHidden of [true, false]) { | 68 | 69 | ' y: 1337 }'); | 69 | 70 | } | 70 | 71 |
| | 72 | +// Now do the same checks but from a different context | | 73 | +for (const showHidden of [true, false]) { | | 74 | + const ab = vm.runInNewContext('new ArrayBuffer(4)'); | | 75 | + const dv = vm.runInNewContext('new DataView(ab, 1, 2)', { ab: ab }); | | 76 | + assert.equal(util.inspect(ab, showHidden), 'ArrayBuffer { byteLength: 4 }'); | | 77 | + assert.equal(util.inspect(new DataView(ab, 1, 2), showHidden), | | 78 | + 'DataView {\n' + | | 79 | + ' byteLength: 2,\n' + | | 80 | + ' byteOffset: 1,\n' + | | 81 | + ' buffer: ArrayBuffer { byteLength: 4 } }'); | | 82 | + assert.equal(util.inspect(ab, showHidden), 'ArrayBuffer { byteLength: 4 }'); | | 83 | + assert.equal(util.inspect(dv, showHidden), | | 84 | + 'DataView {\n' + | | 85 | + ' byteLength: 2,\n' + | | 86 | + ' byteOffset: 1,\n' + | | 87 | + ' buffer: ArrayBuffer { byteLength: 4 } }'); | | 88 | + ab.x = 42; | | 89 | + dv.y = 1337; | | 90 | + assert.equal(util.inspect(ab, showHidden), | | 91 | + 'ArrayBuffer { byteLength: 4, x: 42 }'); | | 92 | + assert.equal(util.inspect(dv, showHidden), | | 93 | + 'DataView {\n' + | | 94 | + ' byteLength: 2,\n' + | | 95 | + ' byteOffset: 1,\n' + | | 96 | + ' buffer: ArrayBuffer { byteLength: 4, x: 42 },\n' + | | 97 | + ' y: 1337 }'); | | 98 | +} | | 99 | + | | 100 | + | 71 | 101 | [ Float32Array, | 72 | 102 | Float64Array, | 73 | 103 | Int16Array, | @@ -94,6 +124,38 @@ for (const showHidden of [true, false]) { | 94 | 124 | assert.equal(util.inspect(array, false), `${constructor.name} [ 65, 97 ]`); | 95 | 125 | }); | 96 | 126 |
| | 127 | +// Now check that declaring a TypedArray in a different context works the same | | 128 | +[ Float32Array, | | 129 | + Float64Array, | | 130 | + Int16Array, | | 131 | + Int32Array, | | 132 | + Int8Array, | | 133 | + Uint16Array, | | 134 | + Uint32Array, | | 135 | + Uint8Array, | | 136 | + Uint8ClampedArray ].forEach(constructor => { | | 137 | + const length = 2; | | 138 | + const byteLength = length * constructor.BYTES_PER_ELEMENT; | | 139 | + const array = vm.runInNewContext('new constructor(new ArrayBuffer(' + | | 140 | + 'byteLength), 0, length)', | | 141 | + { constructor: constructor, | | 142 | + byteLength: byteLength, | | 143 | + length: length | | 144 | + }); | | 145 | + array[0] = 65; | | 146 | + array[1] = 97; | | 147 | + assert.equal(util.inspect(array, true), | | 148 | + `${constructor.name} [\n` + | | 149 | + ` 65,\n` + | | 150 | + ` 97,\n` + | | 151 | + ` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` + | | 152 | + ` [length]: ${length},\n` + | | 153 | + ` [byteLength]: ${byteLength},\n` + | | 154 | + ` [byteOffset]: 0,\n` + | | 155 | + ` [buffer]: ArrayBuffer { byteLength: ${byteLength} } ]`); | | 156 | + assert.equal(util.inspect(array, false), `${constructor.name} [ 65, 97 ]`); | | 157 | + }); | | 158 | + | 97 | 159 | // Due to the hash seed randomization it's not deterministic the order that | 98 | 160 | // the following ways this hash is displayed. | 99 | 161 | // See http://codereview.chromium.org/9124004/ | |
|