TypedArrayConstructor.js [plain text]
function of()
{
"use strict";
let len = arguments.length;
let constructFunction = @getByIdDirectPrivate(this, "allocateTypedArray");
if (constructFunction === @undefined)
@throwTypeError("TypedArray.of requires its this argument to subclass a TypedArray constructor");
let result = constructFunction(len);
for (let i = 0; i < len; i++)
result[i] = arguments[i];
return result;
}
function from(items )
{
"use strict";
let mapFn = @argument(1);
let thisArg;
if (mapFn !== @undefined) {
if (typeof mapFn !== "function")
@throwTypeError("TypedArray.from requires that the second argument, when provided, be a function");
thisArg = @argument(2);
}
let arrayLike = @toObject(items, "TypedArray.from requires an array-like object - not null or undefined");
let iteratorMethod = items.@iteratorSymbol;
if (iteratorMethod != null) {
if (typeof iteratorMethod !== "function")
@throwTypeError("TypedArray.from requires that the property of the first argument, items[Symbol.iterator], when exists, be a function");
let accumulator = [];
let k = 0;
let iterator = iteratorMethod.@call(items);
let wrapper = {};
wrapper.@iteratorSymbol = function() { return iterator; }
for (let value of wrapper) {
if (mapFn)
@putByValDirect(accumulator, k, thisArg === @undefined ? mapFn(value, k) : mapFn.@call(thisArg, value, k));
else
@putByValDirect(accumulator, k, value);
k++;
}
let constructFunction = @getByIdDirectPrivate(this, "allocateTypedArray");
if (constructFunction === @undefined)
@throwTypeError("TypedArray.from requires its this argument subclass a TypedArray constructor");
let result = constructFunction(k);
for (let i = 0; i < k; i++)
result[i] = accumulator[i];
return result;
}
let arrayLikeLength = @toLength(arrayLike.length);
let constructFunction = @getByIdDirectPrivate(this, "allocateTypedArray");
if (constructFunction === @undefined)
@throwTypeError("this does not subclass a TypedArray constructor");
let result = constructFunction(arrayLikeLength);
let k = 0;
while (k < arrayLikeLength) {
let value = arrayLike[k];
if (mapFn)
result[k] = thisArg === @undefined ? mapFn(value, k) : mapFn.@call(thisArg, value, k);
else
result[k] = value;
k++;
}
return result;
}
function allocateInt8Array(length)
{
return new @Int8Array(length);
}
function allocateInt16Array(length)
{
return new @Int16Array(length);
}
function allocateInt32Array(length)
{
return new @Int32Array(length);
}
function allocateUint32Array(length)
{
return new @Uint32Array(length);
}
function allocateUint16Array(length)
{
return new @Uint16Array(length);
}
function allocateUint8Array(length)
{
return new @Uint8Array(length);
}
function allocateUint8ClampedArray(length)
{
return new @Uint8ClampedArray(length);
}
function allocateFloat32Array(length)
{
return new @Float32Array(length);
}
function allocateFloat64Array(length)
{
return new @Float64Array(length);
}