Debian Codegraph @

js/src/jit-test/tests/basic/stringbuffer-5.js

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