typedarray-with.js 11 KB
Newer Older
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --harmony-change-array-by-copy --harmony-rab-gsab
// Flags: --allow-natives-syntax

"use strict";

d8.file.execute('test/mjsunit/typedarray-helpers.js');

function NormalizeValue(array, value) {
  if (array instanceof BigInt64Array || array instanceof BigUint64Array) {
    return BigInt(value);
  }
  return value;
}

function CheckWith(orig, index, indexNotEvil, v, vNotEvil) {
  const origLen = orig.length;
  const absoluteIndex = indexNotEvil < 0
        ? indexNotEvil + orig.length
        : indexNotEvil;
  const origVAtIndex = orig[absoluteIndex];
  const copy = orig.with(index, v);

  assertEquals(origLen, copy.length);
  for (let i = 0; i < origLen; i++) {
    assertEquals(i == absoluteIndex ? vNotEvil : orig[i], copy[i]);
  }
  assertEquals(origVAtIndex, orig[absoluteIndex]);
  assertFalse(copy === orig);
  assertFalse(copy.buffer === orig.buffer);
}

function CheckWithShrunkOrDetached(orig, v, vNotEvil) {
  const origLen = orig.length;
  if (orig instanceof BigInt64Array ||
      orig instanceof BigUint64Array) {
    // This is funny. For BigInt TAs this throws because when shrinking the
    // source array, the remainder of the copy is filled with undefineds because
    // OOB TA loads return undefined, but undefined can't be coerced to BigInt
    // and throw a TypeError.
    assertThrows(() => { orig.with(1, v); }, TypeError);
    return;
  }

  const copy = orig.with(1, v);
  assertEquals(origLen, copy.length);
  assertEquals(NormalizeValue(copy, 0), copy[0]);
  assertEquals(vNotEvil, copy[1]);
  for (let i = 2; i < copy.length; i++) {
    if (copy instanceof Float32Array || copy instanceof Float64Array) {
      assertEquals(NaN, copy[i]);
    } else {
      assertEquals(0, copy[i]);
    }
  }
}

(function TestSurface() {
  for (let TA of ctors) {
    assertEquals(2, TA.prototype.with.length);
    assertEquals("with", TA.prototype.with.name);
  }
})();

(function TestBasic() {
  for (let TA of ctors) {
    let a = new TA(4);
    const v = NormalizeValue(a, 42);
    for (let i = 0; i < 4; i++) {
      WriteToTypedArray(a, i, i);
    }
    CheckWith(a, 1, 1, v, v);
    CheckWith(a, -1, -1, v, v);
  }
})();

(function TestOutOfBounds() {
  for (let TA of ctors) {
    let a = new TA(4);
    const v = NormalizeValue(a, 42);
    assertThrows(() => { a.with(a.length, v); }, RangeError);
    assertThrows(() => { a.with(-a.length - 1, v); }, RangeError);
  }
})();

(function TestNonTypedArray() {
  for (let TA of ctors) {
    assertThrows(() => { TA.prototype.with.call([1,2,3,4], 1, 42); }, TypeError);
  }
})();

(function TestResizableBuffer() {
  for (let TA of ctors) {
    const rab = CreateResizableArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                           8 * TA.BYTES_PER_ELEMENT);
    const fixedLength = new TA(rab, 0, 4);
    const fixedLengthWithOffset = new TA(rab, 2 * TA.BYTES_PER_ELEMENT, 2);
    const lengthTracking = new TA(rab, 0);
    const lengthTrackingWithOffset = new TA(rab, 2 * TA.BYTES_PER_ELEMENT);

    const v = NormalizeValue(fixedLength, 42);

    // Write some data into the array.
    const taWrite = new TA(rab);
    for (let i = 0; i < 4; i++) {
      WriteToTypedArray(taWrite, i, i);
    }

    // Orig. array: [0, 1, 2, 3]
    //              [0, 1, 2, 3] << fixedLength
    //                    [2, 3] << fixedLengthWithOffset
    //              [0, 1, 2, 3, ...] << lengthTracking
    //                    [2, 3, ...] << lengthTrackingWithOffset

    CheckWith(fixedLength, 1, 1, v, v);
    CheckWith(fixedLengthWithOffset, 1, 1, v, v);
    CheckWith(lengthTracking, 1, 1, v, v);
    CheckWith(lengthTrackingWithOffset, 1, 1, v, v);

    CheckWith(fixedLength, -1, -1, v, v);
    CheckWith(fixedLengthWithOffset, -1, -1, v, v);
    CheckWith(lengthTracking, -1, -1, v, v);
    CheckWith(lengthTrackingWithOffset, -1, -1, v, v);

    // Shrink so that the TAs with offset go out of bounds.
    rab.resize(1 * TA.BYTES_PER_ELEMENT);
    WriteToTypedArray(taWrite, 0, 0);

    assertThrows(() => { fixedLength.with(0, v); }, TypeError);
    assertThrows(() => { fixedLengthWithOffset.with(0, v); }, TypeError);
    CheckWith(lengthTracking, 0, 0, v, v);
    assertThrows(() => { lengthTrackingWithOffset.with(0, v); }, TypeError);

    // Shrink to zero.
    rab.resize(0);

    assertThrows(() => { fixedLength.with(0, v); }, TypeError);
    assertThrows(() => { fixedLengthWithOffset.with(0, v); }, TypeError);
    assertThrows(() => { lengthTracking.with(0, v); }, RangeError);
    assertThrows(() => { lengthTrackingWithOffset.copyWithin(0, 1, 1); },
                 TypeError);

    // Grow so that all TAs are back in-bounds.
    rab.resize(6 * TA.BYTES_PER_ELEMENT);
    for (let i = 0; i < 6; ++i) {
      WriteToTypedArray(taWrite, i, i);
    }

    // Orig. array: [0, 1, 2, 3, 4, 5]
    //              [0, 1, 2, 3] << fixedLength
    //                    [2, 3] << fixedLengthWithOffset
    //              [0, 1, 2, 3, 4, 5, ...] << lengthTracking
    //                    [2, 3, 4, 5, ...] << lengthTrackingWithOffset

    CheckWith(fixedLength, 1, 1, v, v);
    CheckWith(fixedLengthWithOffset, 1, 1, v, v);
    CheckWith(lengthTracking, 4, 4, v, v);
    CheckWith(lengthTrackingWithOffset, 1, 1, v, v);

    CheckWith(fixedLength, -1, -1, v, v);
    CheckWith(fixedLengthWithOffset, -1, -1, v, v);
    CheckWith(lengthTracking, -4, -4, v, v);
    CheckWith(lengthTrackingWithOffset, -3, -3, v, v);
  }
})();

(function TestParameterConversionShrinks() {
  for (let TA of ctors) {
    const rab = CreateResizableArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                           8 * TA.BYTES_PER_ELEMENT);
    const fixedLength = new TA(rab, 0, 4);
    const v = NormalizeValue(fixedLength, 42);

    const evilIndex = { valueOf: () => { rab.resize(2 * TA.BYTES_PER_ELEMENT);
                                          return 2; }};
    assertThrows(() => { fixedLength.with(evilIndex, v); },
                 RangeError);

    const evilV = { valueOf: () => { rab.resize(2 * TA.BYTES_PER_ELEMENT);
                                      return v; }};
    rab.resize(4 * TA.BYTES_PER_ELEMENT);
    const lengthTracking = new TA(rab, 0);
    CheckWithShrunkOrDetached(lengthTracking, evilV, v);
  }
})();

(function TestParameterConversionGrows() {
  for (let TA of ctors) {
    const rab = CreateResizableArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                           8 * TA.BYTES_PER_ELEMENT);
    const lengthTracking = new TA(rab, 0);
    const v = NormalizeValue(lengthTracking, 42);

    const evilIndex = { valueOf: () => { rab.resize(6 * TA.BYTES_PER_ELEMENT);
                                          return 1; }};
    const evilV = { valueOf: () => { rab.resize(6 * TA.BYTES_PER_ELEMENT);
                                      return v; }};
    CheckWith(lengthTracking, evilIndex, 1, v, v);
    rab.resize(4 * TA.BYTES_PER_ELEMENT);
    CheckWith(lengthTracking, 1, 1, evilV, v);
  }
})();

(function TestParameterConversionDetaches() {
  for (let TA of ctors) {
    let rab = CreateResizableArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                         8 * TA.BYTES_PER_ELEMENT);
    const fixedLength = new TA(rab, 0, 4);
    const v = NormalizeValue(fixedLength, 42);

    const evilIndex = { valueOf: () => { %ArrayBufferDetach(rab);
                                          return 2; }};
    assertThrows(() => { fixedLength.with(evilIndex, v); },
                 RangeError);

    rab = CreateResizableArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                     8 * TA.BYTES_PER_ELEMENT);
    const lengthTracking = new TA(rab, 0);
    const evilV = { valueOf: () => { %ArrayBufferDetach(rab);
                                      return v; }};
    assertThrows(() => { fixedLength.with(1, evilV); }, TypeError);
  }
})();

(function TestGrowableSAB() {
  for (let TA of ctors) {
    const gsab = CreateGrowableSharedArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                                 8 * TA.BYTES_PER_ELEMENT);
    const fixedLength = new TA(gsab, 0, 4);
    const fixedLengthWithOffset = new TA(gsab, 2 * TA.BYTES_PER_ELEMENT, 2);
    const lengthTracking = new TA(gsab, 0);
    const lengthTrackingWithOffset = new TA(gsab, 2 * TA.BYTES_PER_ELEMENT);

    const v = NormalizeValue(fixedLength, 42);

    // Write some data into the array.
    const taWrite = new TA(gsab);
    for (let i = 0; i < 4; i++) {
      WriteToTypedArray(taWrite, i, 2 * i);
    }

    // Orig. array: [0, 2, 4, 6]
    //              [0, 2, 4, 6] << fixedLength
    //                    [4, 6] << fixedLengthWithOffset
    //              [0, 2, 4, 6, ...] << lengthTracking
    //                    [4, 6, ...] << lengthTrackingWithOffset

    CheckWith(fixedLength, 1, 1, v, v);
    CheckWith(fixedLengthWithOffset, 1, 1, v, v);
    CheckWith(lengthTracking, 1, 1, v, v);
    CheckWith(lengthTrackingWithOffset, 1, 1, v, v);

    CheckWith(fixedLength, -1, -1, v, v);
    CheckWith(fixedLengthWithOffset, -1, -1, v, v);
    CheckWith(lengthTracking, -1, -1, v, v);
    CheckWith(lengthTrackingWithOffset, -1, -1, v, v);

    // Grow.
    gsab.grow(6 * TA.BYTES_PER_ELEMENT);
    for (let i = 0; i < 6; ++i) {
      WriteToTypedArray(taWrite, i, 2 * i);
    }

    // Orig. array: [0, 2, 4, 6, 8, 10]
    //              [0, 2, 4, 6] << fixedLength
    //                    [4, 6] << fixedLengthWithOffset
    //              [0, 2, 4, 6, 8, 10, ...] << lengthTracking
    //                    [4, 6, 8, 10, ...] << lengthTrackingWithOffset
    CheckWith(fixedLength, 1, 1, v, v);
    CheckWith(fixedLengthWithOffset, 1, 1, v, v);
    CheckWith(lengthTracking, 4, 4, v, v);
    CheckWith(lengthTrackingWithOffset, 1, 1, v, v);

    CheckWith(fixedLength, -1, -1, v, v);
    CheckWith(fixedLengthWithOffset, -1, -1, v, v);
    CheckWith(lengthTracking, -4, -4, v, v);
    CheckWith(lengthTrackingWithOffset, -3, -3, v, v);
  }
})();

(function TestParameterConversionGrowsSAB() {
  for (let TA of ctors) {
    let gsab = CreateGrowableSharedArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                               8 * TA.BYTES_PER_ELEMENT);
    let lengthTracking = new TA(gsab, 0);
    const v = NormalizeValue(lengthTracking, 42);

    const evilIndex = { valueOf: () => { gsab.grow(6 * TA.BYTES_PER_ELEMENT);
                                          return 1; }};
    const evilV = { valueOf: () => { gsab.grow(6 * TA.BYTES_PER_ELEMENT);
                                      return v; }};
    CheckWith(lengthTracking, evilIndex, 1, v, v);

    gsab = CreateGrowableSharedArrayBuffer(4 * TA.BYTES_PER_ELEMENT,
                                           8 * TA.BYTES_PER_ELEMENT);
    lengthTracking = new TA(gsab, 0);
    CheckWith(lengthTracking, 1, 1, evilV, v);
  }
})();

(function TestNoSpecies() {
  class MyUint8Array extends Uint8Array {
    constructor(len) { super(len); }
    static get [Symbol.species]() { return MyUint8Array; }
  }
  assertEquals(Uint8Array, (new MyUint8Array(4)).with(0, 42).constructor);
})();