OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../../resources/js-test.js"></script> | 2 <script src="../../resources/js-test.js"></script> |
3 <body> | 3 <body> |
4 <script> | 4 <script> |
5 description('Tests that the window.internals.observeGC hook works.'); | 5 description('Tests that the window.internals.observeGC hook works.'); |
6 | 6 |
7 shouldBe('typeof window.internals.observeGC', '"function"', | 7 shouldBe('typeof window.internals.observeGC', '"function"', |
8 'this test requires window.internals'); | 8 'this test requires window.internals'); |
9 | 9 |
10 // "Generic Kid's Movie III": ... where nobody dies. | 10 // "Generic Kid's Movie III": ... where nobody dies. |
11 | 11 |
12 var valueA = {}; | 12 var valueA = {}; |
13 observationA = internals.observeGC(valueA); | 13 // Do not pass the object directly to observeGC function. This may |
| 14 // remain live on this function's stack preventing GC from collecting |
| 15 // it. Accessing the object inside an inner function will prevent any |
| 16 // unneeded references on this function's stack. |
| 17 observationA = internals.observeGC((() => {return valueA;})()); |
14 gc(); | 18 gc(); |
15 shouldBeFalse('observationA.wasCollected'); | 19 shouldBeFalse('observationA.wasCollected'); |
16 // value ineligible for GC should not be flagged as collected | 20 // value ineligible for GC should not be flagged as collected |
17 valueA = null; | 21 valueA = null; |
18 observationA = null; | 22 observationA = null; |
19 | 23 |
20 // "Romeo and GCuliet": Romeo JavaScript finds G.uliet C.apulet and dies. | 24 // "Romeo and GCuliet": Romeo JavaScript finds G.uliet C.apulet and dies. |
21 | 25 |
22 var valueB = {}; | 26 var valueB = {}; |
23 observationB = internals.observeGC(valueB); | 27 // Do not pass the object directly to observeGC function. This may |
| 28 // remain live on this function's stack preventing GC from collecting |
| 29 // it. Accessing the object inside an inner function will prevent any |
| 30 // unneeded references on this function's stack. |
| 31 observationB = internals.observeGC((() => {return valueB;})()); |
24 valueB = null; | 32 valueB = null; |
25 gc(); | 33 gc(); |
26 shouldBeTrue('observationB.wasCollected'); | 34 shouldBeTrue('observationB.wasCollected'); |
27 // value eligible for GC should be flagged as collected | 35 // value eligible for GC should be flagged as collected |
28 observationB = null; | 36 observationB = null; |
29 | 37 |
30 // "One DOM Tree Hill": A family struggles with the loss of | 38 // "One DOM Tree Hill": A family struggles with the loss of |
31 // innocence. And a DOM node. | 39 // innocence. And a DOM node. |
32 | 40 |
33 var valueC = document.createElement('div'); | 41 var valueC = document.createElement('div'); |
34 observationC = internals.observeGC(valueC); | 42 // Do not pass the object directly to observeGC function. This may |
| 43 // remain live on this function's stack preventing GC from collecting |
| 44 // it. Accessing the object inside an inner function will prevent any |
| 45 // unneeded references on this function's stack. |
| 46 observationC = internals.observeGC((() => {return valueC;})()); |
35 valueC = null; | 47 valueC = null; |
36 gc(); | 48 gc(); |
37 shouldBeTrue('observationC.wasCollected'); | 49 shouldBeTrue('observationC.wasCollected'); |
38 // DOM node eligible for GC should be flagged as collected | 50 // DOM node eligible for GC should be flagged as collected |
39 observationC = null; | 51 observationC = null; |
40 | 52 |
41 // Now, movies that failed: | 53 // Now, movies that failed: |
42 | 54 |
43 shouldThrow('internals.observeGC(undefined)', '"TypeError: value to observe is n
ull or undefined"'); | 55 shouldThrow('internals.observeGC(undefined)', |
44 shouldThrow('internals.observeGC(null)', '"TypeError: value to observe is null o
r undefined"'); | 56 '"TypeError: value to observe is null or undefined"'); |
| 57 shouldThrow('internals.observeGC(null)', |
| 58 '"TypeError: value to observe is null or undefined"'); |
45 | 59 |
46 // Try to create objects and observers that will die at once | 60 // Try to create objects and observers that will die at once |
47 | 61 |
48 var valueD = {}; | 62 var valueD = {}; |
49 var observerD = internals.observeGC(valueD); | 63 // Do not pass the object directly to observeGC function. This may |
| 64 // remain live on this function's stack preventing GC from collecting |
| 65 // it. Accessing the object inside an inner function will prevent any |
| 66 // unneeded references on this function's stack. |
| 67 var observerD = internals.observeGC((() => {return valueD;})()); |
50 valueD.observer = observerD; | 68 valueD.observer = observerD; |
51 observerD.observed = valueD; | 69 observerD.observed = valueD; |
52 valueD = observerD = null; | 70 valueD = observerD = null; |
53 gc(); | 71 gc(); |
54 testPassed('did not crash'); | 72 testPassed('did not crash'); |
55 | 73 |
56 var successfullyParsed = true; | 74 var successfullyParsed = true; |
57 </script> | 75 </script> |
OLD | NEW |