1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
|