Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Test that the methods for different TypedArray types have the same |
| 6 | // identity. |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | let typedArrayConstructors = [ |
| 11 | Uint8Array, |
| 12 | Int8Array, |
| 13 | Uint16Array, |
| 14 | Int16Array, |
| 15 | Uint32Array, |
| 16 | Int32Array, |
| 17 | Uint8ClampedArray, |
| 18 | Float32Array, |
| 19 | Float64Array]; |
| 20 | |
| 21 | let TypedArray = Uint8Array.__proto__; |
| 22 | let TypedArrayPrototype = TypedArray.prototype; |
| 23 | |
| 24 | assertEquals(TypedArray.__proto__, Function.prototype); |
| 25 | assertEquals(TypedArrayPrototype.__proto__, Object.prototype); |
| 26 | |
| 27 | // There are extra own class properties due to it simply being a function |
| 28 | let classProperties = new Set([ |
| 29 | "length", "name", "arguments", "caller", "prototype", "BYTES_PER_ELEMENT" |
| 30 | ]); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 31 | let instanceProperties = new Set(["BYTES_PER_ELEMENT", "constructor", "prototype"]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | |
| 33 | function functionProperties(object) { |
| 34 | return Object.getOwnPropertyNames(object).filter(function(name) { |
| 35 | return typeof Object.getOwnPropertyDescriptor(object, name).value |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 36 | == "function" |
| 37 | && name != 'constructor' && name != 'subarray'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | }); |
| 39 | } |
| 40 | |
| 41 | let typedArrayMethods = functionProperties(Uint8Array.prototype); |
| 42 | let typedArrayClassMethods = functionProperties(Uint8Array); |
| 43 | |
| 44 | for (let constructor of typedArrayConstructors) { |
| 45 | for (let property of Object.getOwnPropertyNames(constructor.prototype)) { |
| 46 | assertTrue(instanceProperties.has(property), property); |
| 47 | } |
| 48 | for (let property of Object.getOwnPropertyNames(constructor)) { |
| 49 | assertTrue(classProperties.has(property), property); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Abstract %TypedArray% class can't be constructed directly |
| 54 | |
| 55 | assertThrows(() => new TypedArray(), TypeError); |
| 56 | |
| 57 | // The "prototype" property is nonconfigurable, nonenumerable, nonwritable, |
| 58 | // both for %TypedArray% and for all subclasses |
| 59 | |
| 60 | let desc = Object.getOwnPropertyDescriptor(TypedArray, "prototype"); |
| 61 | assertFalse(desc.writable); |
| 62 | assertFalse(desc.configurable); |
| 63 | assertFalse(desc.enumerable); |
| 64 | |
| 65 | for (let constructor of typedArrayConstructors) { |
| 66 | let desc = Object.getOwnPropertyDescriptor(constructor, "prototype"); |
| 67 | assertFalse(desc.writable); |
| 68 | assertFalse(desc.configurable); |
| 69 | assertFalse(desc.enumerable); |
| 70 | } |