1// Flags: --experimental-vm-modules --expose-internals 2'use strict'; 3const common = require('../common'); 4const assert = require('assert'); 5const { types, inspect } = require('util'); 6const vm = require('vm'); 7const { internalBinding } = require('internal/test/binding'); 8const { JSStream } = internalBinding('js_stream'); 9 10const external = (new JSStream())._externalStream; 11 12for (const [ value, _method ] of [ 13 [ external, 'isExternal' ], 14 [ new Date() ], 15 [ (function() { return arguments; })(), 'isArgumentsObject' ], 16 [ new Boolean(), 'isBooleanObject' ], 17 [ new Number(), 'isNumberObject' ], 18 [ new String(), 'isStringObject' ], 19 [ Object(Symbol()), 'isSymbolObject' ], 20 [ Object(BigInt(0)), 'isBigIntObject' ], 21 [ new Error(), 'isNativeError' ], 22 [ new RegExp() ], 23 [ async function() {}, 'isAsyncFunction' ], 24 [ function*() {}, 'isGeneratorFunction' ], 25 [ (function*() {})(), 'isGeneratorObject' ], 26 [ Promise.resolve() ], 27 [ new Map() ], 28 [ new Set() ], 29 [ (new Map())[Symbol.iterator](), 'isMapIterator' ], 30 [ (new Set())[Symbol.iterator](), 'isSetIterator' ], 31 [ new WeakMap() ], 32 [ new WeakSet() ], 33 [ new ArrayBuffer() ], 34 [ new Uint8Array() ], 35 [ new Uint8ClampedArray() ], 36 [ new Uint16Array() ], 37 [ new Uint32Array() ], 38 [ new Int8Array() ], 39 [ new Int16Array() ], 40 [ new Int32Array() ], 41 [ new Float32Array() ], 42 [ new Float64Array() ], 43 [ new BigInt64Array() ], 44 [ new BigUint64Array() ], 45 [ Object.defineProperty(new Uint8Array(), 46 Symbol.toStringTag, 47 { value: 'foo' }) ], 48 [ new DataView(new ArrayBuffer()) ], 49 [ new SharedArrayBuffer() ], 50 [ new Proxy({}, {}), 'isProxy' ], 51]) { 52 const method = _method || `is${value.constructor.name}`; 53 assert(method in types, `Missing ${method} for ${inspect(value)}`); 54 assert(types[method](value), `Want ${inspect(value)} to match ${method}`); 55 56 for (const key of Object.keys(types)) { 57 if ((types.isArrayBufferView(value) || 58 types.isAnyArrayBuffer(value)) && key.includes('Array') || 59 key === 'isBoxedPrimitive') { 60 continue; 61 } 62 63 assert.strictEqual(types[key](value), 64 key === method, 65 `${inspect(value)}: ${key}, ` + 66 `${method}, ${types[key](value)}`); 67 } 68} 69 70// Check boxed primitives. 71[ 72 new Boolean(), 73 new Number(), 74 new String(), 75 Object(Symbol()), 76 Object(BigInt(0)), 77].forEach((entry) => assert(types.isBoxedPrimitive(entry))); 78 79{ 80 assert(!types.isUint8Array({ [Symbol.toStringTag]: 'Uint8Array' })); 81 assert(types.isUint8Array(vm.runInNewContext('new Uint8Array'))); 82 83 assert(!types.isUint8ClampedArray({ 84 [Symbol.toStringTag]: 'Uint8ClampedArray' 85 })); 86 assert(types.isUint8ClampedArray( 87 vm.runInNewContext('new Uint8ClampedArray') 88 )); 89 90 assert(!types.isUint16Array({ [Symbol.toStringTag]: 'Uint16Array' })); 91 assert(types.isUint16Array(vm.runInNewContext('new Uint16Array'))); 92 93 assert(!types.isUint32Array({ [Symbol.toStringTag]: 'Uint32Array' })); 94 assert(types.isUint32Array(vm.runInNewContext('new Uint32Array'))); 95 96 assert(!types.isInt8Array({ [Symbol.toStringTag]: 'Int8Array' })); 97 assert(types.isInt8Array(vm.runInNewContext('new Int8Array'))); 98 99 assert(!types.isInt16Array({ [Symbol.toStringTag]: 'Int16Array' })); 100 assert(types.isInt16Array(vm.runInNewContext('new Int16Array'))); 101 102 assert(!types.isInt32Array({ [Symbol.toStringTag]: 'Int32Array' })); 103 assert(types.isInt32Array(vm.runInNewContext('new Int32Array'))); 104 105 assert(!types.isFloat32Array({ [Symbol.toStringTag]: 'Float32Array' })); 106 assert(types.isFloat32Array(vm.runInNewContext('new Float32Array'))); 107 108 assert(!types.isFloat64Array({ [Symbol.toStringTag]: 'Float64Array' })); 109 assert(types.isFloat64Array(vm.runInNewContext('new Float64Array'))); 110 111 assert(!types.isBigInt64Array({ [Symbol.toStringTag]: 'BigInt64Array' })); 112 assert(types.isBigInt64Array(vm.runInNewContext('new BigInt64Array'))); 113 114 assert(!types.isBigUint64Array({ [Symbol.toStringTag]: 'BigUint64Array' })); 115 assert(types.isBigUint64Array(vm.runInNewContext('new BigUint64Array'))); 116} 117 118{ 119 const primitive = true; 120 const arrayBuffer = new ArrayBuffer(); 121 const buffer = Buffer.from(arrayBuffer); 122 const dataView = new DataView(arrayBuffer); 123 const uint8Array = new Uint8Array(arrayBuffer); 124 const uint8ClampedArray = new Uint8ClampedArray(arrayBuffer); 125 const uint16Array = new Uint16Array(arrayBuffer); 126 const uint32Array = new Uint32Array(arrayBuffer); 127 const int8Array = new Int8Array(arrayBuffer); 128 const int16Array = new Int16Array(arrayBuffer); 129 const int32Array = new Int32Array(arrayBuffer); 130 const float32Array = new Float32Array(arrayBuffer); 131 const float64Array = new Float64Array(arrayBuffer); 132 const bigInt64Array = new BigInt64Array(arrayBuffer); 133 const bigUint64Array = new BigUint64Array(arrayBuffer); 134 135 const fakeBuffer = Object.create(Buffer.prototype); 136 const fakeDataView = Object.create(DataView.prototype); 137 const fakeUint8Array = Object.create(Uint8Array.prototype); 138 const fakeUint8ClampedArray = Object.create(Uint8ClampedArray.prototype); 139 const fakeUint16Array = Object.create(Uint16Array.prototype); 140 const fakeUint32Array = Object.create(Uint32Array.prototype); 141 const fakeInt8Array = Object.create(Int8Array.prototype); 142 const fakeInt16Array = Object.create(Int16Array.prototype); 143 const fakeInt32Array = Object.create(Int32Array.prototype); 144 const fakeFloat32Array = Object.create(Float32Array.prototype); 145 const fakeFloat64Array = Object.create(Float64Array.prototype); 146 const fakeBigInt64Array = Object.create(BigInt64Array.prototype); 147 const fakeBigUint64Array = Object.create(BigUint64Array.prototype); 148 149 const stealthyDataView = 150 Object.setPrototypeOf(new DataView(arrayBuffer), Uint8Array.prototype); 151 const stealthyUint8Array = 152 Object.setPrototypeOf(new Uint8Array(arrayBuffer), ArrayBuffer.prototype); 153 const stealthyUint8ClampedArray = 154 Object.setPrototypeOf( 155 new Uint8ClampedArray(arrayBuffer), ArrayBuffer.prototype 156 ); 157 const stealthyUint16Array = 158 Object.setPrototypeOf(new Uint16Array(arrayBuffer), Uint16Array.prototype); 159 const stealthyUint32Array = 160 Object.setPrototypeOf(new Uint32Array(arrayBuffer), Uint32Array.prototype); 161 const stealthyInt8Array = 162 Object.setPrototypeOf(new Int8Array(arrayBuffer), Int8Array.prototype); 163 const stealthyInt16Array = 164 Object.setPrototypeOf(new Int16Array(arrayBuffer), Int16Array.prototype); 165 const stealthyInt32Array = 166 Object.setPrototypeOf(new Int32Array(arrayBuffer), Int32Array.prototype); 167 const stealthyFloat32Array = 168 Object.setPrototypeOf( 169 new Float32Array(arrayBuffer), Float32Array.prototype 170 ); 171 const stealthyFloat64Array = 172 Object.setPrototypeOf( 173 new Float64Array(arrayBuffer), Float64Array.prototype 174 ); 175 const stealthyBigInt64Array = 176 Object.setPrototypeOf( 177 new BigInt64Array(arrayBuffer), BigInt64Array.prototype 178 ); 179 const stealthyBigUint64Array = 180 Object.setPrototypeOf( 181 new BigUint64Array(arrayBuffer), BigUint64Array.prototype 182 ); 183 184 const all = [ 185 primitive, arrayBuffer, buffer, fakeBuffer, 186 dataView, fakeDataView, stealthyDataView, 187 uint8Array, fakeUint8Array, stealthyUint8Array, 188 uint8ClampedArray, fakeUint8ClampedArray, stealthyUint8ClampedArray, 189 uint16Array, fakeUint16Array, stealthyUint16Array, 190 uint32Array, fakeUint32Array, stealthyUint32Array, 191 int8Array, fakeInt8Array, stealthyInt8Array, 192 int16Array, fakeInt16Array, stealthyInt16Array, 193 int32Array, fakeInt32Array, stealthyInt32Array, 194 float32Array, fakeFloat32Array, stealthyFloat32Array, 195 float64Array, fakeFloat64Array, stealthyFloat64Array, 196 bigInt64Array, fakeBigInt64Array, stealthyBigInt64Array, 197 bigUint64Array, fakeBigUint64Array, stealthyBigUint64Array, 198 ]; 199 200 const expected = { 201 isArrayBufferView: [ 202 buffer, 203 dataView, stealthyDataView, 204 uint8Array, stealthyUint8Array, 205 uint8ClampedArray, stealthyUint8ClampedArray, 206 uint16Array, stealthyUint16Array, 207 uint32Array, stealthyUint32Array, 208 int8Array, stealthyInt8Array, 209 int16Array, stealthyInt16Array, 210 int32Array, stealthyInt32Array, 211 float32Array, stealthyFloat32Array, 212 float64Array, stealthyFloat64Array, 213 bigInt64Array, stealthyBigInt64Array, 214 bigUint64Array, stealthyBigUint64Array, 215 ], 216 isTypedArray: [ 217 buffer, 218 uint8Array, stealthyUint8Array, 219 uint8ClampedArray, stealthyUint8ClampedArray, 220 uint16Array, stealthyUint16Array, 221 uint32Array, stealthyUint32Array, 222 int8Array, stealthyInt8Array, 223 int16Array, stealthyInt16Array, 224 int32Array, stealthyInt32Array, 225 float32Array, stealthyFloat32Array, 226 float64Array, stealthyFloat64Array, 227 bigInt64Array, stealthyBigInt64Array, 228 bigUint64Array, stealthyBigUint64Array, 229 ], 230 isUint8Array: [ 231 buffer, uint8Array, stealthyUint8Array, 232 ], 233 isUint8ClampedArray: [ 234 uint8ClampedArray, stealthyUint8ClampedArray, 235 ], 236 isUint16Array: [ 237 uint16Array, stealthyUint16Array, 238 ], 239 isUint32Array: [ 240 uint32Array, stealthyUint32Array, 241 ], 242 isInt8Array: [ 243 int8Array, stealthyInt8Array, 244 ], 245 isInt16Array: [ 246 int16Array, stealthyInt16Array, 247 ], 248 isInt32Array: [ 249 int32Array, stealthyInt32Array, 250 ], 251 isFloat32Array: [ 252 float32Array, stealthyFloat32Array, 253 ], 254 isFloat64Array: [ 255 float64Array, stealthyFloat64Array, 256 ], 257 isBigInt64Array: [ 258 bigInt64Array, stealthyBigInt64Array, 259 ], 260 isBigUint64Array: [ 261 bigUint64Array, stealthyBigUint64Array, 262 ] 263 }; 264 265 for (const testedFunc of Object.keys(expected)) { 266 const func = types[testedFunc]; 267 const yup = []; 268 for (const value of all) { 269 if (func(value)) { 270 yup.push(value); 271 } 272 } 273 console.log('Testing', testedFunc); 274 assert.deepStrictEqual(yup, expected[testedFunc]); 275 } 276} 277 278(async () => { 279 const m = new vm.SourceTextModule(''); 280 await m.link(() => 0); 281 await m.evaluate(); 282 assert.ok(types.isModuleNamespaceObject(m.namespace)); 283})().then(common.mustCall()); 284 285{ 286 // eslint-disable-next-line node-core/crypto-check 287 if (common.hasCrypto) { 288 const crypto = require('crypto'); 289 assert.ok(!types.isKeyObject(crypto.createHash('sha1'))); 290 } 291 assert.ok(!types.isCryptoKey()); 292 assert.ok(!types.isKeyObject()); 293} 294