summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/shell.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--js/src/tests/non262/TypedArray/shell.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/js/src/tests/non262/TypedArray/shell.js b/js/src/tests/non262/TypedArray/shell.js
new file mode 100644
index 0000000000..2fb0ab8f1d
--- /dev/null
+++ b/js/src/tests/non262/TypedArray/shell.js
@@ -0,0 +1,114 @@
+(function(global) {
+ "use strict";
+
+ const {
+ Float32Array, Float64Array, Object, Reflect, SharedArrayBuffer, WeakMap,
+ assertEq
+ } = global;
+ const {
+ apply: Reflect_apply,
+ construct: Reflect_construct,
+ } = Reflect;
+ const {
+ get: WeakMap_prototype_get,
+ has: WeakMap_prototype_has,
+ } = WeakMap.prototype;
+
+ const sharedConstructors = new WeakMap();
+
+ // Synthesize a constructor for a shared memory array from the constructor
+ // for unshared memory. This has "good enough" fidelity for many uses. In
+ // cases where it's not good enough, call isSharedConstructor for local
+ // workarounds.
+ function sharedConstructor(baseConstructor) {
+ // Create SharedTypedArray as a subclass of %TypedArray%, following the
+ // built-in %TypedArray% subclasses.
+ class SharedTypedArray extends Object.getPrototypeOf(baseConstructor) {
+ constructor(...args) {
+ var array = Reflect_construct(baseConstructor, args);
+ var {buffer, byteOffset, length} = array;
+ var sharedBuffer = new SharedArrayBuffer(buffer.byteLength);
+ var sharedArray = Reflect_construct(baseConstructor,
+ [sharedBuffer, byteOffset, length],
+ new.target);
+ for (var i = 0; i < length; i++)
+ sharedArray[i] = array[i];
+ assertEq(sharedArray.buffer, sharedBuffer);
+ return sharedArray;
+ }
+ }
+
+ // 22.2.5.1 TypedArray.BYTES_PER_ELEMENT
+ Object.defineProperty(SharedTypedArray, "BYTES_PER_ELEMENT",
+ {__proto__: null, value: baseConstructor.BYTES_PER_ELEMENT});
+
+ // 22.2.6.1 TypedArray.prototype.BYTES_PER_ELEMENT
+ Object.defineProperty(SharedTypedArray.prototype, "BYTES_PER_ELEMENT",
+ {__proto__: null, value: baseConstructor.BYTES_PER_ELEMENT});
+
+ // Share the same name with the base constructor to avoid calling
+ // isSharedConstructor() in multiple places.
+ Object.defineProperty(SharedTypedArray, "name",
+ {__proto__: null, value: baseConstructor.name});
+
+ sharedConstructors.set(SharedTypedArray, baseConstructor);
+
+ return SharedTypedArray;
+ }
+
+ /**
+ * All TypedArray constructors for unshared memory.
+ */
+ const typedArrayConstructors = Object.freeze([
+ Int8Array,
+ Uint8Array,
+ Uint8ClampedArray,
+ Int16Array,
+ Uint16Array,
+ Int32Array,
+ Uint32Array,
+ Float32Array,
+ Float64Array,
+ ]);
+
+ /**
+ * All TypedArray constructors for shared memory.
+ */
+ const sharedTypedArrayConstructors = Object.freeze(
+ typeof SharedArrayBuffer === "function"
+ ? typedArrayConstructors.map(sharedConstructor)
+ : []
+ );
+
+ /**
+ * All TypedArray constructors for unshared and shared memory.
+ */
+ const anyTypedArrayConstructors = Object.freeze([
+ ...typedArrayConstructors, ...sharedTypedArrayConstructors,
+ ]);
+
+ /**
+ * Returns `true` if `constructor` is a TypedArray constructor for shared
+ * memory.
+ */
+ function isSharedConstructor(constructor) {
+ return Reflect_apply(WeakMap_prototype_has, sharedConstructors, [constructor]);
+ }
+
+ /**
+ * Returns `true` if `constructor` is a TypedArray constructor for shared
+ * or unshared memory, with an underlying element type of either Float32 or
+ * Float64.
+ */
+ function isFloatConstructor(constructor) {
+ if (isSharedConstructor(constructor))
+ constructor = Reflect_apply(WeakMap_prototype_get, sharedConstructors, [constructor]);
+ return constructor == Float32Array || constructor == Float64Array;
+ }
+
+ global.typedArrayConstructors = typedArrayConstructors;
+ global.sharedTypedArrayConstructors = sharedTypedArrayConstructors;
+ global.anyTypedArrayConstructors = anyTypedArrayConstructors;
+ global.isSharedConstructor = isSharedConstructor;
+ global.isFloatConstructor = isFloatConstructor;
+})(this);