| 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 // Do initialization work in an inner function to avoid references to |
| 13 // Do not pass the object directly to observeGC function. This may | 13 // objects remaining live on this function's stack frame. |
| 14 // remain live on this function's stack preventing GC from collecting | 14 // (http://crbug.com/595672/). |
| 15 // it. Accessing the object inside an inner function will prevent any | 15 var valueA, observationA; |
| 16 // unneeded references on this function's stack. | 16 function initializeA() { |
| 17 observationA = internals.observeGC((() => {return valueA;})()); | 17 valueA = {}; |
| 18 observationA = internals.observeGC(valueA); |
| 19 } |
| 20 |
| 21 initializeA(); |
| 18 gc(); | 22 gc(); |
| 19 shouldBeFalse('observationA.wasCollected'); | 23 shouldBeFalse('observationA.wasCollected'); |
| 20 // value ineligible for GC should not be flagged as collected | 24 // value ineligible for GC should not be flagged as collected |
| 21 valueA = null; | 25 valueA = null; |
| 22 observationA = null; | 26 observationA = null; |
| 23 | 27 |
| 24 // "Romeo and GCuliet": Romeo JavaScript finds G.uliet C.apulet and dies. | 28 // "Romeo and GCuliet": Romeo JavaScript finds G.uliet C.apulet and dies. |
| 25 | 29 |
| 26 var valueB = {}; | 30 // Do initialization work in an inner function to avoid references to |
| 27 // Do not pass the object directly to observeGC function. This may | 31 // objects remaining live on this function's stack frame. |
| 28 // remain live on this function's stack preventing GC from collecting | 32 // (http://crbug.com/595672/). |
| 29 // it. Accessing the object inside an inner function will prevent any | 33 var valueB, observationB; |
| 30 // unneeded references on this function's stack. | 34 function initializeB() { |
| 31 observationB = internals.observeGC((() => {return valueB;})()); | 35 valueB = {}; |
| 32 valueB = null; | 36 observationB = internals.observeGC(valueB); |
| 37 valueB = null; |
| 38 } |
| 39 |
| 40 initializeB(); |
| 33 gc(); | 41 gc(); |
| 34 shouldBeTrue('observationB.wasCollected'); | 42 shouldBeTrue('observationB.wasCollected'); |
| 35 // value eligible for GC should be flagged as collected | 43 // value eligible for GC should be flagged as collected |
| 36 observationB = null; | 44 observationB = null; |
| 37 | 45 |
| 38 // "One DOM Tree Hill": A family struggles with the loss of | 46 // "One DOM Tree Hill": A family struggles with the loss of |
| 39 // innocence. And a DOM node. | 47 // innocence. And a DOM node. |
| 40 | 48 |
| 41 var valueC = document.createElement('div'); | 49 // Do initialization work in an inner function to avoid references to |
| 42 // Do not pass the object directly to observeGC function. This may | 50 // objects remaining live on this function's stack frame. |
| 43 // remain live on this function's stack preventing GC from collecting | 51 // (http://crbug.com/595672/). |
| 44 // it. Accessing the object inside an inner function will prevent any | 52 var valueC, observationC; |
| 45 // unneeded references on this function's stack. | 53 function initializeC() { |
| 46 observationC = internals.observeGC((() => {return valueC;})()); | 54 valueC = document.createElement('div'); |
| 47 valueC = null; | 55 observationC = internals.observeGC(valueC); |
| 56 valueC = null; |
| 57 } |
| 58 |
| 59 initializeC(); |
| 48 gc(); | 60 gc(); |
| 49 shouldBeTrue('observationC.wasCollected'); | 61 shouldBeTrue('observationC.wasCollected'); |
| 50 // DOM node eligible for GC should be flagged as collected | 62 // DOM node eligible for GC should be flagged as collected |
| 51 observationC = null; | 63 observationC = null; |
| 52 | 64 |
| 53 // Now, movies that failed: | 65 // Now, movies that failed: |
| 54 | 66 |
| 55 shouldThrow('internals.observeGC(undefined)', | 67 shouldThrow('internals.observeGC(undefined)', |
| 56 '"TypeError: value to observe is null or undefined"'); | 68 '"TypeError: value to observe is null or undefined"'); |
| 57 shouldThrow('internals.observeGC(null)', | 69 shouldThrow('internals.observeGC(null)', |
| 58 '"TypeError: value to observe is null or undefined"'); | 70 '"TypeError: value to observe is null or undefined"'); |
| 59 | 71 |
| 60 // Try to create objects and observers that will die at once | 72 // Try to create objects and observers that will die at once |
| 61 | 73 |
| 62 var valueD = {}; | 74 // Do initialization work in an inner function to avoid references to |
| 63 // Do not pass the object directly to observeGC function. This may | 75 // objects remaining live on this function's stack frame. |
| 64 // remain live on this function's stack preventing GC from collecting | 76 // (http://crbug.com/595672/). |
| 65 // it. Accessing the object inside an inner function will prevent any | 77 var valueD; |
| 66 // unneeded references on this function's stack. | 78 var observerD; |
| 67 var observerD = internals.observeGC((() => {return valueD;})()); | 79 function initializeD() { |
| 68 valueD.observer = observerD; | 80 valueD = {}; |
| 69 observerD.observed = valueD; | 81 observerD = internals.observeGC(valueD); |
| 70 valueD = observerD = null; | 82 valueD.observer = observerD; |
| 83 observerD.observed = valueD; |
| 84 valueD = observerD = null; |
| 85 } |
| 86 |
| 87 initializeD(); |
| 71 gc(); | 88 gc(); |
| 72 testPassed('did not crash'); | 89 testPassed('did not crash'); |
| 73 | 90 |
| 74 var successfullyParsed = true; | 91 var successfullyParsed = true; |
| 75 </script> | 92 </script> |
| OLD | NEW |