mirror of https://github.com/lukechilds/node.git
Browse Source
PR-URL: https://github.com/nodejs/node/pull/15663 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>v9.x-staging
Timothy Gu
7 years ago
committed by
Ruben Bridgewater
20 changed files with 140 additions and 30 deletions
@ -0,0 +1,36 @@ |
|||
'use strict'; |
|||
|
|||
const ReflectApply = Reflect.apply; |
|||
|
|||
// This function is borrowed from the function with the same name on V8 Extras'
|
|||
// `utils` object. V8 implements Reflect.apply very efficiently in conjunction
|
|||
// with the spread syntax, such that no additional special case is needed for
|
|||
// function calls w/o arguments.
|
|||
// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156
|
|||
function uncurryThis(func) { |
|||
return (thisArg, ...args) => ReflectApply(func, thisArg, args); |
|||
} |
|||
|
|||
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype); |
|||
|
|||
const TypedArrayProto_toStringTag = |
|||
uncurryThis( |
|||
Object.getOwnPropertyDescriptor(TypedArrayPrototype, |
|||
Symbol.toStringTag).get); |
|||
|
|||
// Cached to make sure no userland code can tamper with it.
|
|||
const isArrayBufferView = ArrayBuffer.isView; |
|||
|
|||
function isTypedArray(value) { |
|||
return TypedArrayProto_toStringTag(value) !== undefined; |
|||
} |
|||
|
|||
function isUint8Array(value) { |
|||
return TypedArrayProto_toStringTag(value) === 'Uint8Array'; |
|||
} |
|||
|
|||
module.exports = { |
|||
isArrayBufferView, |
|||
isTypedArray, |
|||
isUint8Array |
|||
}; |
@ -0,0 +1,57 @@ |
|||
'use strict'; |
|||
|
|||
// Flags: --expose-internals
|
|||
|
|||
require('../common'); |
|||
const assert = require('assert'); |
|||
const types = require('internal/util/types'); |
|||
|
|||
const primitive = true; |
|||
const arrayBuffer = new ArrayBuffer(); |
|||
const dataView = new DataView(arrayBuffer); |
|||
const int32Array = new Int32Array(arrayBuffer); |
|||
const uint8Array = new Uint8Array(arrayBuffer); |
|||
const buffer = Buffer.from(arrayBuffer); |
|||
|
|||
const fakeDataView = Object.create(DataView.prototype); |
|||
const fakeInt32Array = Object.create(Int32Array.prototype); |
|||
const fakeUint8Array = Object.create(Uint8Array.prototype); |
|||
const fakeBuffer = Object.create(Buffer.prototype); |
|||
|
|||
const stealthyDataView = |
|||
Object.setPrototypeOf(new DataView(arrayBuffer), Uint8Array.prototype); |
|||
const stealthyInt32Array = |
|||
Object.setPrototypeOf(new Int32Array(arrayBuffer), uint8Array); |
|||
const stealthyUint8Array = |
|||
Object.setPrototypeOf(new Uint8Array(arrayBuffer), ArrayBuffer.prototype); |
|||
|
|||
const all = [ |
|||
primitive, arrayBuffer, dataView, int32Array, uint8Array, buffer, |
|||
fakeDataView, fakeInt32Array, fakeUint8Array, fakeBuffer, |
|||
stealthyDataView, stealthyInt32Array, stealthyUint8Array |
|||
]; |
|||
|
|||
const expected = { |
|||
isArrayBufferView: [ |
|||
dataView, int32Array, uint8Array, buffer, |
|||
stealthyDataView, stealthyInt32Array, stealthyUint8Array |
|||
], |
|||
isTypedArray: [ |
|||
int32Array, uint8Array, buffer, stealthyInt32Array, stealthyUint8Array |
|||
], |
|||
isUint8Array: [ |
|||
uint8Array, buffer, stealthyUint8Array |
|||
] |
|||
}; |
|||
|
|||
for (const testedFunc of Object.keys(expected)) { |
|||
const func = types[testedFunc]; |
|||
const yup = []; |
|||
for (const value of all) { |
|||
if (func(value)) { |
|||
yup.push(value); |
|||
} |
|||
} |
|||
console.log('Testing', testedFunc); |
|||
assert.deepStrictEqual(yup, expected[testedFunc]); |
|||
} |
Loading…
Reference in new issue