Debian Codegraph @

js/src/tests/test262/built-ins/ArrayBuffer/isView/shell.js

raw
1 // GENERATED, DO NOT EDIT
2 // file: testTypedArray.js
3 // Copyright (C) 2015 André Bargull. All rights reserved.
4 // This code is governed by the BSD license found in the LICENSE file.
5 /*---
6 description: |
7 Collection of functions used to assert the correctness of TypedArray objects.
8 defines:
9 - floatArrayConstructors
10 - nonClampedIntArrayConstructors
11 - intArrayConstructors
12 - typedArrayConstructors
13 - TypedArray
14 - testWithTypedArrayConstructors
15 - nonAtomicsFriendlyTypedArrayConstructors
16 - testWithAtomicsFriendlyTypedArrayConstructors
17 - testWithNonAtomicsFriendlyTypedArrayConstructors
18 - testTypedArrayConversions
19 ---*/
20
21 var floatArrayConstructors = [
22 Float64Array,
23 Float32Array
24 ];
25
26 var nonClampedIntArrayConstructors = [
27 Int32Array,
28 Int16Array,
29 Int8Array,
30 Uint32Array,
31 Uint16Array,
32 Uint8Array
33 ];
34
35 var intArrayConstructors = nonClampedIntArrayConstructors.concat([Uint8ClampedArray]);
36
37 // Float16Array is a newer feature
38 // adding it to this list unconditionally would cause implementations lacking it to fail every test which uses it
39 if (typeof Float16Array !== 'undefined') {
40 floatArrayConstructors.push(Float16Array);
41 }
42
43 /**
44 * Array containing every non-bigint typed array constructor.
45 */
46
47 var typedArrayConstructors = floatArrayConstructors.concat(intArrayConstructors);
48
49 /**
50 * The %TypedArray% intrinsic constructor function.
51 */
52 var TypedArray = Object.getPrototypeOf(Int8Array);
53
54 /**
55 * Callback for testing a typed array constructor.
56 *
57 * @callback typedArrayConstructorCallback
58 * @param {Function} Constructor the constructor object to test with.
59 */
60
61 /**
62 * Calls the provided function for every typed array constructor.
63 *
64 * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
65 * @param {Array} selected - An optional Array with filtered typed arrays
66 */
67 function testWithTypedArrayConstructors(f, selected) {
68 var constructors = selected || typedArrayConstructors;
69 for (var i = 0; i < constructors.length; ++i) {
70 var constructor = constructors[i];
71 try {
72 f(constructor);
73 } catch (e) {
74 e.message += " (Testing with " + constructor.name + ".)";
75 throw e;
76 }
77 }
78 }
79
80 var nonAtomicsFriendlyTypedArrayConstructors = floatArrayConstructors.concat([Uint8ClampedArray]);
81 /**
82 * Calls the provided function for every non-"Atomics Friendly" typed array constructor.
83 *
84 * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
85 * @param {Array} selected - An optional Array with filtered typed arrays
86 */
87 function testWithNonAtomicsFriendlyTypedArrayConstructors(f) {
88 testWithTypedArrayConstructors(f, nonAtomicsFriendlyTypedArrayConstructors);
89 }
90
91 /**
92 * Calls the provided function for every "Atomics Friendly" typed array constructor.
93 *
94 * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
95 * @param {Array} selected - An optional Array with filtered typed arrays
96 */
97 function testWithAtomicsFriendlyTypedArrayConstructors(f) {
98 testWithTypedArrayConstructors(f, [
99 Int32Array,
100 Int16Array,
101 Int8Array,
102 Uint32Array,
103 Uint16Array,
104 Uint8Array,
105 ]);
106 }
107
108 /**
109 * Helper for conversion operations on TypedArrays, the expected values
110 * properties are indexed in order to match the respective value for each
111 * TypedArray constructor
112 * @param {Function} fn - the function to call for each constructor and value.
113 * will be called with the constructor, value, expected
114 * value, and a initial value that can be used to avoid
115 * a false positive with an equivalent expected value.
116 */
117 function testTypedArrayConversions(byteConversionValues, fn) {
118 var values = byteConversionValues.values;
119 var expected = byteConversionValues.expected;
120
121 testWithTypedArrayConstructors(function(TA) {
122 var name = TA.name.slice(0, -5);
123
124 return values.forEach(function(value, index) {
125 var exp = expected[name][index];
126 var initial = 0;
127 if (exp === 0) {
128 initial = 1;
129 }
130 fn(TA, value, exp, initial);
131 });
132 });
133 }
134
135 /**
136 * Checks if the given argument is one of the float-based TypedArray constructors.
137 *
138 * @param {constructor} ctor - the value to check
139 * @returns {boolean}
140 */
141 function isFloatTypedArrayConstructor(arg) {
142 return floatArrayConstructors.indexOf(arg) !== -1;
143 }
144
145 /**
146 * Determines the precision of the given float-based TypedArray constructor.
147 *
148 * @param {constructor} ctor - the value to check
149 * @returns {string} "half", "single", or "double" for Float16Array, Float32Array, and Float64Array respectively.
150 */
151 function floatTypedArrayConstructorPrecision(FA) {
152 if (typeof Float16Array !== "undefined" && FA === Float16Array) {
153 return "half";
154 } else if (FA === Float32Array) {
155 return "single";
156 } else if (FA === Float64Array) {
157 return "double";
158 } else {
159 throw new Error("Malformed test - floatTypedArrayConstructorPrecision called with non-float TypedArray");
160 }
161 }