OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --allow-natives-syntax --no-always-opt |
| 6 |
| 7 // Test precise code coverage. |
| 8 |
| 9 function GetCoverage(source) { |
| 10 var scripts = %DebugGetLoadedScripts(); |
| 11 for (var script of scripts) { |
| 12 if (script.source == source) { |
| 13 var coverage = %DebugCollectCoverage(); |
| 14 for (var data of coverage) { |
| 15 if (data.script_id == script.id) return data.entries; |
| 16 } |
| 17 } |
| 18 } |
| 19 return undefined; |
| 20 } |
| 21 |
| 22 function ApplyCoverageToSource(source, coverage) { |
| 23 var result = ""; |
| 24 var cursor = 0; |
| 25 for (var entry of coverage) { |
| 26 var chunk = source.substring(cursor, entry.end_position); |
| 27 cursor = entry.end_position; |
| 28 result += `[${chunk}[${entry.count}]]`; |
| 29 } |
| 30 return result; |
| 31 } |
| 32 |
| 33 function TestCoverage(name, source, expectation) { |
| 34 source = source.trim(); |
| 35 eval(source); |
| 36 var coverage = GetCoverage(source); |
| 37 if (expectation === undefined) { |
| 38 assertEquals(undefined, coverage); |
| 39 } else { |
| 40 expectation = expectation.trim(); |
| 41 var result = ApplyCoverageToSource(source, coverage); |
| 42 print(result); |
| 43 assertEquals(expectation, result, name + " failed"); |
| 44 } |
| 45 } |
| 46 |
| 47 |
| 48 // Without precise coverage enabled, we lose coverage data to the GC. |
| 49 TestCoverage( |
| 50 "call an IIFE", |
| 51 ` |
| 52 (function f() {})(); |
| 53 `, |
| 54 undefined // The IIFE has been garbage-collected. |
| 55 ); |
| 56 |
| 57 TestCoverage( |
| 58 "call locally allocated function", |
| 59 ` |
| 60 for (var i = 0; i < 10; i++) { |
| 61 let f = () => 1; |
| 62 i += f(); |
| 63 } |
| 64 `, |
| 65 undefined |
| 66 ); |
| 67 |
| 68 // This does not happen with precise coverage enabled. |
| 69 %DebugTogglePreciseCoverage(true); |
| 70 |
| 71 TestCoverage( |
| 72 "call an IIFE", |
| 73 ` |
| 74 (function f() {})(); |
| 75 `, |
| 76 ` |
| 77 [(function f() {})();[1]] |
| 78 ` |
| 79 ); |
| 80 |
| 81 TestCoverage( |
| 82 "call locally allocated function", |
| 83 ` |
| 84 for (var i = 0; i < 10; i++) { |
| 85 let f = () => 1; |
| 86 i += f(); |
| 87 } |
| 88 `, |
| 89 ` |
| 90 [for (var i = 0; i < 10; i++) { |
| 91 let f = [1]][() => 1[5]][; |
| 92 i += f(); |
| 93 }[1]] |
| 94 ` |
| 95 ); |
| 96 |
| 97 %DebugTogglePreciseCoverage(false); |
OLD | NEW |