OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // Return the stack frames of an Error object. |
| 29 Error.prototype.getFrames = function() { |
| 30 Error.prepareStackTrace = function(error, frames) { |
| 31 return frames; |
| 32 } |
| 33 var frames = this.stack; |
| 34 Error.prepareStackTrace = undefined; |
| 35 return frames; |
| 36 } |
| 37 |
| 38 String.prototype.contains = function(pattern) { |
| 39 return this.indexOf(pattern) > -1; |
| 40 } |
| 41 |
| 42 // Check for every frame that a certain method returns the |
| 43 // expected value for every frame. |
| 44 Array.prototype.verifyEquals = function(frames, func_name) { |
| 45 this.forEach( |
| 46 function(element, index) { |
| 47 var frame = frames[index]; |
| 48 if (element === null) return; |
| 49 assertEquals(element, (frame[func_name])()); |
| 50 } |
| 51 ); |
| 52 } |
| 53 |
| 54 // Check for every frame that a certain method has a return value |
| 55 // that contains the expected pattern for every frame. |
| 56 Array.prototype.verifyContains = function(frames, func_name) { |
| 57 this.forEach( |
| 58 function(element, index) { |
| 59 var frame = frames[index]; |
| 60 if (element === null) return; |
| 61 assertTrue((frame[func_name])().contains(element)); |
| 62 } |
| 63 ); |
| 64 } |
| 65 |
| 66 // Check for every frame that a certain method returns undefined |
| 67 // when expected. |
| 68 Array.prototype.verifyUndefined = function(frames, func_name) { |
| 69 this.forEach( |
| 70 function(element, index) { |
| 71 var frame = frames[index]; |
| 72 if (element === null) return; |
| 73 assertEquals(element, (frame[func_name])() === undefined); |
| 74 } |
| 75 ); |
| 76 } |
| 77 |
| 78 |
| 79 // Simple eval. |
| 80 var code1 = "function f() { \n" + |
| 81 " throw new Error(3); \n" + // Line 2 |
| 82 "} \n" + |
| 83 "f(); \n"; // Line 4 |
| 84 |
| 85 function g() { |
| 86 eval(code1); |
| 87 } |
| 88 |
| 89 try { |
| 90 g(); |
| 91 } catch (e) { |
| 92 // We expect something like |
| 93 // f (eval at g (eval-stack.js:87:8), <anonymous>:2:9) |
| 94 // eval (eval at g (eval-stack.js:87:8), <anonymous>:4:1) |
| 95 // g (eval-stack.js:87:3) |
| 96 // eval-stack.js:94:3 |
| 97 var frames = e.getFrames(); |
| 98 assertEquals(4, frames.length); |
| 99 ["f", "eval", "g"] |
| 100 .verifyEquals(frames, "getFunctionName"); |
| 101 [2, 4] |
| 102 .verifyEquals(frames, "getLineNumber"); |
| 103 ["<anonymous>:2:", "<anonymous>:4:"] |
| 104 .verifyContains(frames, "toString"); |
| 105 [true, true, false, false] |
| 106 .verifyUndefined(frames, "getFileName"); |
| 107 ["eval at g", "eval at g"] |
| 108 .verifyContains(frames, "getEvalOrigin"); |
| 109 } |
| 110 |
| 111 |
| 112 // Nested eval. |
| 113 var code2 = "function h() { \n" + |
| 114 " // Empty \n" + |
| 115 " eval(code1); \n" + // Line 3 |
| 116 "} \n" + |
| 117 "h(); \n"; // Line 5 |
| 118 |
| 119 try { |
| 120 eval(code2); |
| 121 } catch (e) { |
| 122 // We expect something like |
| 123 // f (eval at h (eval at <anonymous> (eval-stack.js:116:8)), |
| 124 // <anonymous>:2:9) |
| 125 // eval (eval at h (eval at <anonymous> (eval-stack.js:116:8)), |
| 126 // <anonymous>:4:1) |
| 127 // h (eval at <anonymous> (eval-stack.js:116:8), <anonymous>:3:3) |
| 128 // eval (eval at <anonymous> (eval-stack.js:116:8), <anonymous>:5:1) |
| 129 // eval-stack.js:116:3 |
| 130 var frames = e.getFrames(); |
| 131 assertEquals(5, frames.length); |
| 132 ["f", "eval", "h", "eval"] |
| 133 .verifyEquals(frames, "getFunctionName"); |
| 134 [2, 4, 3, 5] |
| 135 .verifyEquals(frames, "getLineNumber"); |
| 136 ["<anonymous>:2:", "<anonymous>:4:", "<anonymous>:3:", "<anonymous>:5:"] |
| 137 .verifyContains(frames, "toString"); |
| 138 [true, true, true, true, false] |
| 139 .verifyUndefined(frames, "getFileName"); |
| 140 ["eval at h (eval at <anonymous> (", |
| 141 "eval at h (eval at <anonymous> (", |
| 142 "eval at <anonymous> (", |
| 143 "eval at <anonymous> ("] |
| 144 .verifyContains(frames, "getEvalOrigin"); |
| 145 } |
| 146 |
| 147 |
| 148 // Nested eval calling through non-eval defined function. |
| 149 var code3 = "function h() { \n" + |
| 150 " // Empty \n" + |
| 151 " g(); \n" + // Line 3 |
| 152 "} \n" + |
| 153 "h(); \n"; // Line 5 |
| 154 |
| 155 try { |
| 156 eval(code3); |
| 157 } catch (e) { |
| 158 // We expect something like |
| 159 // f (eval at g (test.js:83:8), <anonymous>:2:9) |
| 160 // eval (eval at g (test.js:83:8), <anonymous>:4:1) |
| 161 // g (test.js:83:3) |
| 162 // h (eval at <anonymous> (test.js:149:8), <anonymous>:3:3) |
| 163 // eval (eval at <anonymous> (test.js:149:8), <anonymous>:5:1) |
| 164 // test.js:149:3 |
| 165 var frames = e.getFrames(); |
| 166 assertEquals(6, frames.length); |
| 167 ["f", "eval", "g", "h", "eval"] |
| 168 .verifyEquals(frames, "getFunctionName"); |
| 169 [2, 4, null, 3, 5] |
| 170 .verifyEquals(frames, "getLineNumber"); |
| 171 ["<anonymous>:2:", "<anonymous>:4:", null, "<anonymous>:3:", "<anonymous>:5:"] |
| 172 .verifyContains(frames, "toString"); |
| 173 [true, true, false, true, true, false] |
| 174 .verifyUndefined(frames, "getFileName"); |
| 175 ["eval at g (", |
| 176 "eval at g (", |
| 177 null, |
| 178 "eval at <anonymous> (", |
| 179 "eval at <anonymous> ("] |
| 180 .verifyContains(frames, "getEvalOrigin"); |
| 181 } |
| 182 |
| 183 |
| 184 // Calling function defined in eval. |
| 185 eval("function f() { \n" + |
| 186 " throw new Error(3); \n" + |
| 187 "} \n"); |
| 188 |
| 189 try { |
| 190 f(); |
| 191 } catch (e) { |
| 192 // We expect something like |
| 193 // f (eval at <anonymous> (test.js:182:40), <anonymous>:2:9) |
| 194 // test.js:186:3 |
| 195 var frames = e.getFrames(); |
| 196 assertEquals(2, frames.length); |
| 197 ["f"].verifyEquals(frames, "getFunctionName"); |
| 198 [2].verifyEquals(frames, "getLineNumber"); |
| 199 ["<anonymous>:2:"].verifyContains(frames, "toString"); |
| 200 [true, false].verifyUndefined(frames, "getFileName"); |
| 201 ["eval at <anonymous> ("].verifyContains(frames, "getEvalOrigin"); |
| 202 } |
| 203 |
OLD | NEW |