Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 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 | function testMethodNameInference() { |
| 29 | function Foo() { } |
| 30 | Foo.prototype.bar = function () { FAIL; }; |
| 31 | (new Foo).bar(); |
| 32 | } |
| 33 | |
| 34 | function testNested() { |
| 35 | function one() { |
| 36 | function two() { |
| 37 | function three() { |
| 38 | FAIL; |
| 39 | } |
| 40 | three(); |
| 41 | } |
| 42 | two(); |
| 43 | } |
| 44 | one(); |
| 45 | } |
| 46 | |
| 47 | function testArrayNative() { |
| 48 | [1, 2, 3].map(function () { FAIL; }); |
| 49 | } |
| 50 | |
| 51 | function testImplicitConversion() { |
| 52 | function Nirk() { } |
| 53 | Nirk.prototype.valueOf = function () { FAIL; }; |
| 54 | return 1 + (new Nirk); |
| 55 | } |
| 56 | |
| 57 | function testEval() { |
| 58 | eval("function Doo() { FAIL; }; Doo();"); |
| 59 | } |
| 60 | |
| 61 | function testNestedEval() { |
| 62 | var x = "FAIL"; |
| 63 | eval("function Outer() { eval('function Inner() { eval(x); }'); Inner(); }; Outer();"); |
| 64 | } |
| 65 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 66 | function testEvalWithSourceURL() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | eval("function Doo() { FAIL; }; Doo();\n//# sourceURL=res://name"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | function testNestedEvalWithSourceURL() { |
| 71 | var x = "FAIL"; |
| 72 | var innerEval = 'function Inner() { eval(x); }\n//@ sourceURL=res://inner-eval'; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | eval("function Outer() { eval(innerEval); Inner(); }; Outer();\n//# sourceURL=res://outer-eval"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | function testValue() { |
| 77 | Number.prototype.causeError = function () { FAIL; }; |
| 78 | (1).causeError(); |
| 79 | } |
| 80 | |
| 81 | function testConstructor() { |
| 82 | function Plonk() { FAIL; } |
| 83 | new Plonk(); |
| 84 | } |
| 85 | |
| 86 | function testRenamedMethod() { |
| 87 | function a$b$c$d() { return FAIL; } |
| 88 | function Wookie() { } |
| 89 | Wookie.prototype.d = a$b$c$d; |
| 90 | (new Wookie).d(); |
| 91 | } |
| 92 | |
| 93 | function testAnonymousMethod() { |
| 94 | (function () { FAIL }).call([1, 2, 3]); |
| 95 | } |
| 96 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | function testFunctionName() { |
| 98 | function gen(name, counter) { |
| 99 | var f = function foo() { |
| 100 | if (counter === 0) { |
| 101 | FAIL; |
| 102 | } |
| 103 | gen(name, counter - 1)(); |
| 104 | } |
| 105 | if (counter === 4) { |
| 106 | Object.defineProperty(f, 'name', {get: function(){ throw 239; }}); |
| 107 | } else if (counter == 3) { |
| 108 | Object.defineProperty(f, 'name', {value: 'boo' + '_' + counter}); |
| 109 | } else { |
| 110 | Object.defineProperty(f, 'name', {writable: true}); |
| 111 | if (counter === 2) |
| 112 | f.name = 42; |
| 113 | else |
| 114 | f.name = name + '_' + counter; |
| 115 | } |
| 116 | return f; |
| 117 | } |
| 118 | gen('foo', 4)(); |
| 119 | } |
| 120 | |
| 121 | function testFunctionInferredName() { |
| 122 | var f = function() { |
| 123 | FAIL; |
| 124 | } |
| 125 | f(); |
| 126 | } |
| 127 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | function CustomError(message, stripPoint) { |
| 129 | this.message = message; |
| 130 | Error.captureStackTrace(this, stripPoint); |
| 131 | } |
| 132 | |
| 133 | CustomError.prototype.toString = function () { |
| 134 | return "CustomError: " + this.message; |
| 135 | }; |
| 136 | |
| 137 | function testDefaultCustomError() { |
| 138 | throw new CustomError("hep-hey", undefined); |
| 139 | } |
| 140 | |
| 141 | function testStrippedCustomError() { |
| 142 | throw new CustomError("hep-hey", CustomError); |
| 143 | } |
| 144 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | MyObj = function() { FAIL; } |
| 146 | |
| 147 | MyObjCreator = function() {} |
| 148 | |
| 149 | MyObjCreator.prototype.Create = function() { |
| 150 | return new MyObj(); |
| 151 | } |
| 152 | |
| 153 | function testClassNames() { |
| 154 | (new MyObjCreator).Create(); |
| 155 | } |
| 156 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | // Utility function for testing that the expected strings occur |
| 158 | // in the stack trace produced when running the given function. |
| 159 | function testTrace(name, fun, expected, unexpected) { |
| 160 | var threw = false; |
| 161 | try { |
| 162 | fun(); |
| 163 | } catch (e) { |
| 164 | for (var i = 0; i < expected.length; i++) { |
| 165 | assertTrue(e.stack.indexOf(expected[i]) != -1, |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 166 | name + " doesn't contain expected[" + i + "] stack = " + e.stack); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | } |
| 168 | if (unexpected) { |
| 169 | for (var i = 0; i < unexpected.length; i++) { |
| 170 | assertEquals(e.stack.indexOf(unexpected[i]), -1, |
| 171 | name + " contains unexpected[" + i + "]"); |
| 172 | } |
| 173 | } |
| 174 | threw = true; |
| 175 | } |
| 176 | assertTrue(threw, name + " didn't throw"); |
| 177 | } |
| 178 | |
| 179 | // Test that the error constructor is not shown in the trace |
| 180 | function testCallerCensorship() { |
| 181 | var threw = false; |
| 182 | try { |
| 183 | FAIL; |
| 184 | } catch (e) { |
| 185 | assertEquals(-1, e.stack.indexOf('at new ReferenceError'), |
| 186 | "CallerCensorship contained new ReferenceError"); |
| 187 | threw = true; |
| 188 | } |
| 189 | assertTrue(threw, "CallerCensorship didn't throw"); |
| 190 | } |
| 191 | |
| 192 | // Test that the explicit constructor call is shown in the trace |
| 193 | function testUnintendedCallerCensorship() { |
| 194 | var threw = false; |
| 195 | try { |
| 196 | new ReferenceError({ |
| 197 | toString: function () { |
| 198 | FAIL; |
| 199 | } |
| 200 | }); |
| 201 | } catch (e) { |
| 202 | assertTrue(e.stack.indexOf('at new ReferenceError') != -1, |
| 203 | "UnintendedCallerCensorship didn't contain new ReferenceError"); |
| 204 | threw = true; |
| 205 | } |
| 206 | assertTrue(threw, "UnintendedCallerCensorship didn't throw"); |
| 207 | } |
| 208 | |
| 209 | // If an error occurs while the stack trace is being formatted it should |
| 210 | // be handled gracefully. |
| 211 | function testErrorsDuringFormatting() { |
| 212 | function Nasty() { } |
| 213 | Nasty.prototype.foo = function () { throw new RangeError(); }; |
| 214 | var n = new Nasty(); |
| 215 | n.__defineGetter__('constructor', function () { CONS_FAIL; }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | assertThrows(()=>n.foo(), RangeError); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | // Now we can't even format the message saying that we couldn't format |
| 218 | // the stack frame. Put that in your pipe and smoke it! |
| 219 | ReferenceError.prototype.toString = function () { NESTED_FAIL; }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | assertThrows(()=>n.foo(), RangeError); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | // Poisonous object that throws a reference error if attempted converted to |
| 225 | // a primitive values. |
| 226 | var thrower = { valueOf: function() { FAIL; }, |
| 227 | toString: function() { FAIL; } }; |
| 228 | |
| 229 | // Tests that a native constructor function is included in the |
| 230 | // stack trace. |
| 231 | function testTraceNativeConstructor(nativeFunc) { |
| 232 | var nativeFuncName = nativeFunc.name; |
| 233 | try { |
| 234 | new nativeFunc(thrower); |
| 235 | assertUnreachable(nativeFuncName); |
| 236 | } catch (e) { |
| 237 | assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Tests that a native conversion function is included in the |
| 242 | // stack trace. |
| 243 | function testTraceNativeConversion(nativeFunc) { |
| 244 | var nativeFuncName = nativeFunc.name; |
| 245 | try { |
| 246 | nativeFunc(thrower); |
| 247 | assertUnreachable(nativeFuncName); |
| 248 | } catch (e) { |
| 249 | assertTrue(e.stack.indexOf(nativeFuncName) >= 0, nativeFuncName); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | function testOmittedBuiltin(throwing, omitted) { |
| 255 | try { |
| 256 | throwing(); |
| 257 | assertUnreachable(omitted); |
| 258 | } catch (e) { |
| 259 | assertTrue(e.stack.indexOf(omitted) < 0, omitted); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]); |
| 265 | testTrace("testNested", testNested, ["at one", "at two", "at three"]); |
| 266 | testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]); |
| 267 | testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"]); |
| 268 | testTrace("testEval", testEval, ["at Doo (eval at testEval"]); |
| 269 | testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 270 | testTrace("testEvalWithSourceURL", testEvalWithSourceURL, |
| 271 | [ "at Doo (res://name:1:18)" ]); |
| 272 | testTrace("testNestedEvalWithSourceURL", testNestedEvalWithSourceURL, |
| 273 | [" at Inner (res://inner-eval:1:20)", |
| 274 | " at Outer (res://outer-eval:1:37)"]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 275 | testTrace("testValue", testValue, ["at Number.causeError"]); |
| 276 | testTrace("testConstructor", testConstructor, ["new Plonk"]); |
| 277 | testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]); |
| 278 | testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 279 | testTrace("testFunctionName", testFunctionName, |
| 280 | [" at foo_0 ", " at foo_1", " at foo ", " at boo_3 ", " at foo "]); |
| 281 | testTrace("testFunctionInferredName", testFunctionInferredName, [" at f "]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | testTrace("testDefaultCustomError", testDefaultCustomError, |
| 283 | ["hep-hey", "new CustomError"], |
| 284 | ["collectStackTrace"]); |
| 285 | testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"], |
| 286 | ["new CustomError", "collectStackTrace"]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | testTrace("testClassNames", testClassNames, |
| 288 | ["new MyObj", "MyObjCreator.Create"], ["as Create"]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | testCallerCensorship(); |
| 290 | testUnintendedCallerCensorship(); |
| 291 | testErrorsDuringFormatting(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 292 | |
| 293 | testTraceNativeConversion(String); // Does ToString on argument. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 294 | testTraceNativeConversion(RegExp); // Does ToString on argument. |
| 295 | |
| 296 | testTraceNativeConstructor(String); // Does ToString on argument. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 297 | testTraceNativeConstructor(RegExp); // Does ToString on argument. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 298 | |
| 299 | // Omitted because QuickSort has builtins object as receiver, and is non-native |
| 300 | // builtin. |
| 301 | testOmittedBuiltin(function(){ [thrower, 2].sort(function (a,b) { |
| 302 | (b < a) - (a < b); }); |
| 303 | }, "QuickSort"); |
| 304 | |
| 305 | // Omitted because ADD from runtime.js is non-native builtin. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | testOmittedBuiltin(function(){ thrower + 2; }, "ADD"); |
| 307 | |
| 308 | var error = new Error(); |
| 309 | error.toString = function() { assertUnreachable(); }; |
| 310 | error.stack; |
| 311 | |
| 312 | error = new Error(); |
| 313 | error.name = { toString: function() { assertUnreachable(); }}; |
| 314 | error.message = { toString: function() { assertUnreachable(); }}; |
| 315 | error.stack; |
| 316 | |
| 317 | error = new Error(); |
| 318 | Array.prototype.push = function(x) { assertUnreachable(); }; |
| 319 | Array.prototype.join = function(x) { assertUnreachable(); }; |
| 320 | error.stack; |
| 321 | |
| 322 | var fired = false; |
| 323 | error = new Error({ toString: function() { fired = true; } }); |
| 324 | assertTrue(fired); |
| 325 | error.stack; |
| 326 | assertTrue(fired); |
| 327 | |
| 328 | // Check that throwing exception in a custom stack trace formatting function |
| 329 | // does not lead to recursion. |
| 330 | Error.prepareStackTrace = function() { throw new Error("abc"); }; |
| 331 | var message; |
| 332 | try { |
| 333 | try { |
| 334 | throw new Error(); |
| 335 | } catch (e) { |
| 336 | e.stack; |
| 337 | } |
| 338 | } catch (e) { |
| 339 | message = e.message; |
| 340 | } |
| 341 | |
| 342 | assertEquals("abc", message); |
| 343 | |
| 344 | // Test that modifying Error.prepareStackTrace by itself works. |
| 345 | Error.prepareStackTrace = function() { Error.prepareStackTrace = "custom"; }; |
| 346 | new Error().stack; |
| 347 | |
| 348 | assertEquals("custom", Error.prepareStackTrace); |
| 349 | |
| 350 | // Check that the formatted stack trace can be set to undefined. |
| 351 | error = new Error(); |
| 352 | error.stack = undefined; |
| 353 | assertEquals(undefined, error.stack); |
| 354 | |
| 355 | // Check that the stack trace accessors are not forcibly set. |
| 356 | var my_error = {}; |
| 357 | Object.freeze(my_error); |
| 358 | assertThrows(function() { Error.captureStackTrace(my_error); }); |
| 359 | |
| 360 | my_error = {}; |
| 361 | Object.preventExtensions(my_error); |
| 362 | assertThrows(function() { Error.captureStackTrace(my_error); }); |
| 363 | |
| 364 | var fake_error = {}; |
| 365 | my_error = new Error(); |
| 366 | var stolen_getter = Object.getOwnPropertyDescriptor(my_error, 'stack').get; |
| 367 | Object.defineProperty(fake_error, 'stack', { get: stolen_getter }); |
| 368 | assertEquals(undefined, fake_error.stack); |