stringbuffer-5.js (3085B)
1 // |jit-test| --setpref=objectfuse_for_global=false 2 3 // Don't use object fuses for this test because atomizing global constants 4 // affects what we're testing. 5 6 // Test structured cloning of StringBuffer references. 7 8 gczeal(0); 9 10 var strLatin1 = newString("abcdefghijklmnopqrstuvwxyz".repeat(10), {newStringBuffer: true}); 11 var strTwoByte = newString("abcdefghijklmnopqrstuvwx\u3210\u1234".repeat(10), {newStringBuffer: true}); 12 13 function checkRefCount(s, expected) { 14 // stringRepresentation and the bufferRefCount field aren't available in 15 // all builds. 16 if (getBuildConfiguration("debug")) { 17 var repr = JSON.parse(stringRepresentation(s)); 18 assertEq(repr.bufferRefCount, expected); 19 } 20 } 21 22 function test() { 23 checkRefCount(strLatin1, 1); 24 checkRefCount(strTwoByte, 1); 25 26 // With SameProcess, we should transfer the reference so the resulting buffer 27 // should be relatively small. The buffer contains 120 bytes currently so use 28 // 200 as a reasonable upper limit. 29 var clonebufferSameProcess = serialize([strLatin1, strTwoByte, strLatin1, strTwoByte], 30 [], {scope: "SameProcess"}); 31 assertEq(clonebufferSameProcess.arraybuffer.byteLength < 200, true); 32 33 // JS string + 2 refs from clone buffer 34 checkRefCount(strLatin1, 3); 35 checkRefCount(strTwoByte, 3); 36 37 // Test deserialization. 38 var arr1 = deserialize(clonebufferSameProcess); 39 assertEq(arr1.length, 4); 40 assertEq(arr1[0], strLatin1); 41 assertEq(arr1[1], strTwoByte); 42 assertEq(arr1[2], strLatin1); 43 assertEq(arr1[3], strTwoByte); 44 45 // JS string + 2 refs from clone buffer + 2 refs from |arr| 46 checkRefCount(strLatin1, 5); 47 checkRefCount(strTwoByte, 5); 48 49 // With DifferentProcess, the string contents are serialized so we have a 50 // larger buffer. 51 var clonebufferDifferentProcess = serialize([strLatin1, strTwoByte, strLatin1, strTwoByte], 52 [], {scope: "DifferentProcess"}); 53 assertEq(clonebufferDifferentProcess.arraybuffer.byteLength > 500, true); 54 55 // Test deserialization. 56 var arr2 = deserialize(clonebufferDifferentProcess); 57 assertEq(arr2.length, 4); 58 assertEq(arr2[0], strLatin1); 59 assertEq(arr2[1], strTwoByte); 60 assertEq(arr2[2], strLatin1); 61 assertEq(arr2[3], strTwoByte); 62 63 // Unchanged from before. 64 checkRefCount(strLatin1, 5); 65 checkRefCount(strTwoByte, 5); 66 } 67 test(); 68 69 // Trigger GC. This should drop all references except for the JS strings. 70 gc(); 71 finishBackgroundFree(); 72 checkRefCount(strLatin1, 1); 73 checkRefCount(strTwoByte, 1); 74 75 function testAtom() { 76 var sourceLatin1 = "abcde".repeat(200); 77 var reLatin1 = new RegExp(sourceLatin1); 78 79 var sourceTwoByte = "abcd\u1234".repeat(200); 80 var reTwoByte = new RegExp(sourceTwoByte); 81 82 var clonebuffer = serialize([reLatin1, reTwoByte], [], {scope: "SameProcess"}); 83 var arr = deserialize(clonebuffer); 84 assertEq(arr.length, 2); 85 assertEq(arr[0].source, sourceLatin1); 86 assertEq(arr[1].source, sourceTwoByte); 87 } 88 testAtom();