All files / near-membrane-shared/src Array.ts

100% Statements 44/44
100% Branches 0/0
100% Functions 1/1
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        14x   14x                                                         14x   14x                     14x   14x   14x     7x 7x 7x                     7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x    
import { ObjectAssign, ObjectFreeze } from './Object';
import { ReflectSetPrototypeOf } from './Reflect';
import { SymbolIterator, SymbolUnscopables } from './Symbol';
 
export const ArrayCtor = Array;
 
const { prototype: ArrayProto } = ArrayCtor;
 
const {
    at: ArrayProtoAt,
    concat: ArrayProtoConcat,
    copyWithin: ArrayProtoCopyWithin,
    entries: ArrayProtoEntries,
    every: ArrayProtoEvery,
    fill: ArrayProtoFill,
    findIndex: ArrayProtoFindIndex,
    flat: ArrayProtoFlat,
    flatMap: ArrayProtoFlatMap,
    forEach: ArrayProtoForEach,
    indexOf: ArrayProtoIndexOf,
    join: ArrayProtoJoin,
    keys: ArrayProtoKeys,
    lastIndexOf: ArrayProtoLastIndexOf,
    map: ArrayProtoMap,
    pop: ArrayProtoPop,
    reduce: ArrayProtoReduce,
    reduceRight: ArrayProtoReduceRight,
    reverse: ArrayProtoReverse,
    slice: ArrayProtoSlice,
    some: ArrayProtoSome,
    splice: ArrayProtoSplice,
    toLocaleString: ArrayProtoToLocaleString,
    toString: ArrayProtoToString,
    values: ArrayProtoValues,
    [SymbolIterator as any]: ArrayProtoSymbolIterator,
} = ArrayProto;
 
const ArrayUnscopables = ObjectFreeze(
    ObjectAssign({ __proto__: null }, ArrayProto[SymbolUnscopables as any])
);
 
export const {
    filter: ArrayProtoFilter,
    find: ArrayProtoFind,
    includes: ArrayProtoIncludes,
    shift: ArrayProtoShift,
    sort: ArrayProtoSort,
    unshift: ArrayProtoUnshift,
} = ArrayProto;
 
export const { push: ArrayProtoPush } = ArrayProto;
 
export const { isArray: ArrayIsArray } = ArrayCtor;
 
export function toSafeArray<T extends any[]>(array: T): T {
    ReflectSetPrototypeOf(array, null);
    array.at = ArrayProtoAt;
    array.concat = ArrayProtoConcat;
    // *** DO NOT SET THE ARRAY CONSTRUCTOR PROPERTY ***
    // https://bugs.chromium.org/p/v8/issues/detail?id=13202
    // https://source.chromium.org/chromium/chromium/src/+/main:v8/src/objects/lookup.cc;l=196-215?q=IsArraySpeciesLookupChainIntact
    //
    // In V8 setting the constructor property of an array, promise, regexp, or
    // typed array triggers a de-opt because it could change an instance's
    // @@species. This de-opt affects at least `Array#splice` and occurs even
    // if the prototype of the array is change or nulled beforehand. Further,
    // the de-opt persists after a page refresh. It is not until navigating to
    // a different page that the performance of `Array#splice` is restored.
    array.copyWithin = ArrayProtoCopyWithin as any;
    array.entries = ArrayProtoEntries;
    array.every = ArrayProtoEvery;
    array.fill = ArrayProtoFill as any;
    array.filter = ArrayProtoFilter;
    array.find = ArrayProtoFind;
    array.findIndex = ArrayProtoFindIndex;
    array.flat = ArrayProtoFlat;
    array.flatMap = ArrayProtoFlatMap;
    array.forEach = ArrayProtoForEach;
    array.includes = ArrayProtoIncludes;
    array.indexOf = ArrayProtoIndexOf;
    array.join = ArrayProtoJoin;
    array.keys = ArrayProtoKeys;
    array.lastIndexOf = ArrayProtoLastIndexOf;
    array.map = ArrayProtoMap;
    array.pop = ArrayProtoPop;
    array.push = ArrayProtoPush;
    array.reduce = ArrayProtoReduce;
    array.reduceRight = ArrayProtoReduceRight;
    array.reverse = ArrayProtoReverse;
    array.shift = ArrayProtoShift;
    array.slice = ArrayProtoSlice;
    array.some = ArrayProtoSome;
    array.sort = ArrayProtoSort as any;
    array.splice = ArrayProtoSplice;
    array.toLocaleString = ArrayProtoToLocaleString;
    array.toString = ArrayProtoToString;
    array.unshift = ArrayProtoUnshift;
    array.values = ArrayProtoValues;
    array[SymbolIterator as any] = ArrayProtoSymbolIterator;
    array[SymbolUnscopables as any] = ArrayUnscopables;
    ReflectSetPrototypeOf(array, ArrayProto);
    return array;
}