tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

arraybuffer-transfer.js (1707B)


      1 // |jit-test| heavy
      2 const gb = 1 * 1024 * 1024 * 1024;
      3 
      4 const smallByteLength = 1024;
      5 const largeByteLength = 4 * gb;
      6 
      7 // Fast alternative to |assertEq| to avoid a slow VM-call.
      8 // Should only be used when looping over the TypedArray contents to avoid
      9 // unnecessary test slowdowns.
     10 function assertEqNum(e, a) {
     11  if (e !== a) {
     12    assertEq(e, a);
     13  }
     14 }
     15 
     16 {
     17  let smallBuffer = new ArrayBuffer(smallByteLength);
     18 
     19  let i8 = new Uint8Array(smallBuffer);
     20  for (let i = 0; i < smallByteLength; ++i) {
     21    assertEqNum(i8[i], 0);
     22    i8[i] = i;
     23  }
     24 
     25  assertEq(smallBuffer.byteLength, smallByteLength);
     26  assertEq(smallBuffer.detached, false);
     27 
     28  // Copy from a small into a large buffer.
     29  let largeBuffer = smallBuffer.transfer(largeByteLength);
     30 
     31  assertEq(smallBuffer.byteLength, 0);
     32  assertEq(smallBuffer.detached, true);
     33 
     34  assertEq(largeBuffer.byteLength, largeByteLength);
     35  assertEq(largeBuffer.detached, false);
     36 
     37  i8 = new Uint8Array(largeBuffer);
     38  for (let i = 0; i < smallByteLength; ++i) {
     39    assertEqNum(i8[i], i & 0xff);
     40  }
     41 
     42  // Test the first 100 new bytes.
     43  for (let i = smallByteLength; i < smallByteLength + 100; ++i) {
     44    assertEqNum(i8[i], 0);
     45  }
     46 
     47  // And the last 100 new bytes.
     48  for (let i = largeByteLength - 100; i < largeByteLength; ++i) {
     49    assertEqNum(i8[i], 0);
     50  }
     51 
     52  // Copy back from a large into a small buffer.
     53  smallBuffer = largeBuffer.transfer(smallByteLength);
     54 
     55  assertEq(largeBuffer.byteLength, 0);
     56  assertEq(largeBuffer.detached, true);
     57 
     58  assertEq(smallBuffer.byteLength, smallByteLength);
     59  assertEq(smallBuffer.detached, false);
     60 
     61  i8 = new Uint8Array(smallBuffer);
     62  for (let i = 0; i < smallByteLength; ++i) {
     63    assertEqNum(i8[i], i & 0xff);
     64  }
     65 }