Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | // Flags: --nocrankshaft |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 29 | |
| 30 | var functionToCatch; |
| 31 | var lineNumber; |
| 32 | |
| 33 | function catchLineNumber () { |
| 34 | var x = {}; |
| 35 | |
| 36 | Error.prepareStackTrace = function (error, stackTrace) { |
| 37 | stackTrace.some(function (frame) { |
| 38 | if (frame.getFunction() == functionToCatch) { |
| 39 | lineNumber = frame.getLineNumber(); |
| 40 | return true; |
| 41 | } |
| 42 | return false; |
| 43 | }); |
| 44 | return lineNumber; |
| 45 | }; |
| 46 | |
| 47 | Error.captureStackTrace(x); |
| 48 | return x.stack; |
| 49 | } |
| 50 | |
| 51 | function log() { |
| 52 | catchLineNumber(); |
| 53 | } |
| 54 | |
| 55 | function foo() {} |
| 56 | |
| 57 | function test1() { |
| 58 | log(foo() == foo() |
| 59 | ? 'a' |
| 60 | : 'b'); |
| 61 | } |
| 62 | |
| 63 | function test2() { |
| 64 | var o = { foo: function () {}} |
| 65 | log(o.foo() == o.foo() |
| 66 | ? 'a' |
| 67 | : 'b'); |
| 68 | } |
| 69 | |
| 70 | function test3() { |
| 71 | var o = { log: log, foo: function() { } }; |
| 72 | o.log(o.foo() == o.foo() |
| 73 | ? 'a' |
| 74 | : 'b'); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | function test(f, expectedLineNumber) { |
| 79 | functionToCatch = f; |
| 80 | f(); |
| 81 | |
| 82 | assertEquals(expectedLineNumber, lineNumber); |
| 83 | } |
| 84 | |
| 85 | test(test1, 58); |
| 86 | test(test2, 65); |
| 87 | test(test3, 72); |
| 88 | |
| 89 | eval(test1.toString() + "//@ sourceUrl=foo"); |
| 90 | eval(test2.toString() + "//@ sourceUrl=foo"); |
| 91 | eval(test3.toString() + "//@ sourceUrl=foo"); |
| 92 | |
| 93 | test(test1, 2); |
| 94 | test(test2, 3); |
| 95 | test(test3, 3); |