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