typedarray-proto.js 2.19 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Test that the methods for different TypedArray types have the same
// identity.

8 9 10
'use strict';

let typedArrayConstructors = [
11 12 13 14 15 16 17 18 19 20
  Uint8Array,
  Int8Array,
  Uint16Array,
  Int16Array,
  Uint32Array,
  Int32Array,
  Uint8ClampedArray,
  Float32Array,
  Float64Array];

21 22 23 24 25 26 27 28 29 30
let TypedArray = Uint8Array.__proto__;
let TypedArrayPrototype = TypedArray.prototype;

assertEquals(TypedArray.__proto__, Function.prototype);
assertEquals(TypedArrayPrototype.__proto__, Object.prototype);

// There are extra own class properties due to it simply being a function
let classProperties = new Set([
  "length", "name", "arguments", "caller", "prototype", "BYTES_PER_ELEMENT"
]);
31
let instanceProperties = new Set(["BYTES_PER_ELEMENT", "constructor", "prototype"]);
32

33 34 35
function functionProperties(object) {
  return Object.getOwnPropertyNames(object).filter(function(name) {
    return typeof Object.getOwnPropertyDescriptor(object, name).value
36 37
        == "function"
      && name != 'constructor' && name != 'subarray';
38 39 40
  });
}

41 42
let typedArrayMethods = functionProperties(Uint8Array.prototype);
let typedArrayClassMethods = functionProperties(Uint8Array);
43

44 45 46
for (let constructor of typedArrayConstructors) {
  for (let property of Object.getOwnPropertyNames(constructor.prototype)) {
    assertTrue(instanceProperties.has(property), property);
47
  }
48 49
  for (let property of Object.getOwnPropertyNames(constructor)) {
    assertTrue(classProperties.has(property), property);
50 51
  }
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

// Abstract %TypedArray% class can't be constructed directly

assertThrows(() => new TypedArray(), TypeError);

// The "prototype" property is nonconfigurable, nonenumerable, nonwritable,
// both for %TypedArray% and for all subclasses

let desc = Object.getOwnPropertyDescriptor(TypedArray, "prototype");
assertFalse(desc.writable);
assertFalse(desc.configurable);
assertFalse(desc.enumerable);

for (let constructor of typedArrayConstructors) {
  let desc = Object.getOwnPropertyDescriptor(constructor, "prototype");
  assertFalse(desc.writable);
  assertFalse(desc.configurable);
  assertFalse(desc.enumerable);
}