| 1 |
/*--- |
| 2 |
defines: [typedArrayConstructors, sharedTypedArrayConstructors, anyTypedArrayConstructors, isSharedConstructor, isFloatConstructor, isUnsignedConstructor] |
| 3 |
allow_unused: True |
| 4 |
---*/ |
| 5 |
(function(global) { |
| 6 |
"use strict"; |
| 7 |
|
| 8 |
const { |
| 9 |
Float16Array, Float32Array, Float64Array, Object, Reflect, SharedArrayBuffer, WeakMap, |
| 10 |
assertEq |
| 11 |
} = global; |
| 12 |
const { |
| 13 |
apply: Reflect_apply, |
| 14 |
construct: Reflect_construct, |
| 15 |
} = Reflect; |
| 16 |
const { |
| 17 |
get: WeakMap_prototype_get, |
| 18 |
has: WeakMap_prototype_has, |
| 19 |
} = WeakMap.prototype; |
| 20 |
|
| 21 |
const sharedConstructors = new WeakMap(); |
| 22 |
|
| 23 |
// Synthesize a constructor for a shared memory array from the constructor |
| 24 |
// for unshared memory. This has "good enough" fidelity for many uses. In |
| 25 |
// cases where it's not good enough, call isSharedConstructor for local |
| 26 |
// workarounds. |
| 27 |
function sharedConstructor(baseConstructor) { |
| 28 |
// Create SharedTypedArray as a subclass of %TypedArray%, following the |
| 29 |
// built-in %TypedArray% subclasses. |
| 30 |
class SharedTypedArray extends Object.getPrototypeOf(baseConstructor) { |
| 31 |
constructor(...args) { |
| 32 |
var array = Reflect_construct(baseConstructor, args); |
| 33 |
var {buffer, byteOffset, length} = array; |
| 34 |
var sharedBuffer = new SharedArrayBuffer(buffer.byteLength); |
| 35 |
var sharedArray = Reflect_construct(baseConstructor, |
| 36 |
[sharedBuffer, byteOffset, length], |
| 37 |
new.target); |
| 38 |
for (var i = 0; i < length; i++) |
| 39 |
sharedArray[i] = array[i]; |
| 40 |
assertEq(sharedArray.buffer, sharedBuffer); |
| 41 |
return sharedArray; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// 22.2.5.1 TypedArray.BYTES_PER_ELEMENT |
| 46 |
Object.defineProperty(SharedTypedArray, "BYTES_PER_ELEMENT", |
| 47 |
{__proto__: null, value: baseConstructor.BYTES_PER_ELEMENT}); |
| 48 |
|
| 49 |
// 22.2.6.1 TypedArray.prototype.BYTES_PER_ELEMENT |
| 50 |
Object.defineProperty(SharedTypedArray.prototype, "BYTES_PER_ELEMENT", |
| 51 |
{__proto__: null, value: baseConstructor.BYTES_PER_ELEMENT}); |
| 52 |
|
| 53 |
// Share the same name with the base constructor to avoid calling |
| 54 |
// isSharedConstructor() in multiple places. |
| 55 |
Object.defineProperty(SharedTypedArray, "name", |
| 56 |
{__proto__: null, value: baseConstructor.name}); |
| 57 |
|
| 58 |
sharedConstructors.set(SharedTypedArray, baseConstructor); |
| 59 |
|
| 60 |
return SharedTypedArray; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* All TypedArray constructors for unshared memory. |
| 65 |
*/ |
| 66 |
const typedArrayConstructors = Object.freeze([ |
| 67 |
Int8Array, |
| 68 |
Uint8Array, |
| 69 |
Uint8ClampedArray, |
| 70 |
Int16Array, |
| 71 |
Uint16Array, |
| 72 |
Int32Array, |
| 73 |
Uint32Array, |
| 74 |
Float32Array, |
| 75 |
Float64Array, |
| 76 |
].concat(Float16Array ?? [])); |
| 77 |
|
| 78 |
/** |
| 79 |
* All TypedArray constructors for shared memory. |
| 80 |
*/ |
| 81 |
const sharedTypedArrayConstructors = Object.freeze( |
| 82 |
typeof SharedArrayBuffer === "function" |
| 83 |
? typedArrayConstructors.map(sharedConstructor) |
| 84 |
: [] |
| 85 |
); |
| 86 |
|
| 87 |
/** |
| 88 |
* All TypedArray constructors for unshared and shared memory. |
| 89 |
*/ |
| 90 |
const anyTypedArrayConstructors = Object.freeze([ |
| 91 |
...typedArrayConstructors, ...sharedTypedArrayConstructors, |
| 92 |
]); |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns `true` if `constructor` is a TypedArray constructor for shared |
| 96 |
* memory. |
| 97 |
*/ |
| 98 |
function isSharedConstructor(constructor) { |
| 99 |
return Reflect_apply(WeakMap_prototype_has, sharedConstructors, [constructor]); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns `true` if `constructor` is a TypedArray constructor for shared |
| 104 |
* or unshared memory, with an underlying element type of one of Float16, Float32 |
| 105 |
* or Float64. |
| 106 |
*/ |
| 107 |
function isFloatConstructor(constructor) { |
| 108 |
if (isSharedConstructor(constructor)) |
| 109 |
constructor = Reflect_apply(WeakMap_prototype_get, sharedConstructors, [constructor]); |
| 110 |
return constructor == Float32Array || constructor == Float64Array || (Float16Array && constructor == Float16Array); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns `true` if `constructor` is a TypedArray constructor for shared |
| 115 |
* or unshared memory, with an underlying element type of one of Uint8, |
| 116 |
* Uint8Clamped, Uint16, or Uint32. |
| 117 |
*/ |
| 118 |
function isUnsignedConstructor(constructor) { |
| 119 |
if (isSharedConstructor(constructor)) |
| 120 |
constructor = Reflect_apply(WeakMap_prototype_get, sharedConstructors, [constructor]); |
| 121 |
return constructor == Uint8Array || |
| 122 |
constructor == Uint8ClampedArray || |
| 123 |
constructor == Uint16Array || |
| 124 |
constructor == Uint32Array; |
| 125 |
} |
| 126 |
|
| 127 |
global.typedArrayConstructors = typedArrayConstructors; |
| 128 |
global.sharedTypedArrayConstructors = sharedTypedArrayConstructors; |
| 129 |
global.anyTypedArrayConstructors = anyTypedArrayConstructors; |
| 130 |
global.isSharedConstructor = isSharedConstructor; |
| 131 |
global.isFloatConstructor = isFloatConstructor; |
| 132 |
global.isUnsignedConstructor = isUnsignedConstructor; |
| 133 |
})(this); |