| 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 |
function representation(s) { |
| 8 |
return JSON.parse(stringRepresentation(s)); |
| 9 |
} |
| 10 |
|
| 11 |
function testBasic(tenured) { |
| 12 |
var s = newString("abcdefghijklmnopqrstuvwxyz", {newStringBuffer: true, tenured}); |
| 13 |
assertEq(representation(s).bufferRefCount, 1); |
| 14 |
assertEq(s, "abcdefghijklmnopqrstuvwxyz"); |
| 15 |
assertEq(s.substring(1), "bcdefghijklmnopqrstuvwxyz"); |
| 16 |
assertEq(s + s + s, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); |
| 17 |
} |
| 18 |
testBasic(false); |
| 19 |
testBasic(true); |
| 20 |
|
| 21 |
function testAtomRef(tenured) { |
| 22 |
var s = newString("abcdefghijklmnopqrstuvwxyz", {newStringBuffer: true, tenured}); |
| 23 |
var s2 = newString(s, {shareStringBuffer: true}); |
| 24 |
assertEq(representation(s).bufferRefCount, 2); |
| 25 |
var o = {[s2]: 1}; |
| 26 |
for (var i = 0; i < 10; i++) { |
| 27 |
o[s2]++; |
| 28 |
} |
| 29 |
minorgc(); |
| 30 |
minorgc(); |
| 31 |
finishBackgroundFree(); |
| 32 |
// If s2 is now an AtomRef string, then only s holds a reference to |
| 33 |
// the buffer. |
| 34 |
if (representation(s2).flags.includes("ATOM_REF_BIT")) { |
| 35 |
assertEq(representation(s).bufferRefCount, 1); |
| 36 |
} else { |
| 37 |
assertEq(representation(s).bufferRefCount, 2); |
| 38 |
} |
| 39 |
return o; |
| 40 |
} |
| 41 |
testAtomRef(false); |
| 42 |
testAtomRef(true); |
| 43 |
|
| 44 |
function testDeduplication(tenured) { |
| 45 |
var arr = []; |
| 46 |
var s = newString("abcdefghijklmnopqrstuvwxyz" + "012345".substring(1), {newStringBuffer: true, tenured}); |
| 47 |
for (var i = 0; i < 100; i++) { |
| 48 |
arr.push(newString(s, {shareStringBuffer: true, tenured})); |
| 49 |
} |
| 50 |
assertEq(representation(s).bufferRefCount, 101); |
| 51 |
gc() |
| 52 |
finishBackgroundFree(); |
| 53 |
assertEq(representation(s).bufferRefCount, tenured ? 101 : 1); |
| 54 |
return arr; |
| 55 |
} |
| 56 |
testDeduplication(false); |
| 57 |
testDeduplication(true); |