tor-browser

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

stringbuffer-4.js (1907B)


      1 // |jit-test| skip-if: !getBuildConfiguration("debug")
      2 // stringRepresentation and the bufferRefCount field aren't available in
      3 // all builds.
      4 
      5 gczeal(0);
      6 
      7 // Long strings allocated by JSON.parse have a string buffer.
      8 function testJSON() {
      9    var json = `["${"a".repeat(2000)}"]`;
     10    var s = JSON.parse(json)[0];
     11    var repr = JSON.parse(stringRepresentation(s));
     12    assertEq(repr.flags.includes("HAS_STRING_BUFFER_BIT"), true);
     13    assertEq(repr.bufferRefCount, 1);
     14 }
     15 testJSON();
     16 
     17 // Long atoms also have a string buffer.
     18 function testAtom() {
     19    var str = "b".repeat(2000);
     20    var obj = {[str]: 1};
     21    var atom = Object.keys(obj)[0];
     22    var repr = JSON.parse(stringRepresentation(atom));
     23    assertEq(repr.flags.includes("HAS_STRING_BUFFER_BIT"), true);
     24    assertEq(repr.bufferRefCount, 1);
     25 }
     26 testAtom();
     27 
     28 // Strings created by js::StringBuffer.
     29 function testJoin() {
     30    var str = Array(1000).fill("a").join(",");
     31    var repr = JSON.parse(stringRepresentation(str));
     32    assertEq(repr.flags.includes("HAS_STRING_BUFFER_BIT"), true);
     33    assertEq(repr.bufferRefCount, 1);
     34 }
     35 testJoin();
     36 
     37 // Strings created by StringChars.
     38 function testLowerCase() {
     39    var str = "A".repeat(2000).toLowerCase();
     40    var repr = JSON.parse(stringRepresentation(str));
     41    assertEq(repr.flags.includes("HAS_STRING_BUFFER_BIT"), true);
     42    assertEq(repr.bufferRefCount, 1);
     43 }
     44 testLowerCase();
     45 
     46 function testUpperCase(N) {
     47    // Use ß to cover reallocation. The initial buffer has the same size as the
     48    // input string, but because Upper(ß) = SS, the final buffer has twice the
     49    // size.
     50    var str = "ß".repeat(N).toUpperCase();
     51    var repr = JSON.parse(stringRepresentation(str));
     52    assertEq(repr.flags.includes("HAS_STRING_BUFFER_BIT"), true);
     53    assertEq(repr.bufferRefCount, 1);
     54 }
     55 testUpperCase(1000);  // Initial allocation not a string buffer.
     56 testUpperCase(2000);  // Reallocate string buffer.