Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | // Flags: --expose-debug-as debug |
| 29 | |
| 30 | // Get the Debug object exposed from the debug context global object. |
| 31 | var Debug = debug.Debug; |
| 32 | |
| 33 | function CheckScope(scope_mirror, scope_expectations, expected_scope_type) { |
| 34 | assertEquals(expected_scope_type, scope_mirror.scopeType()); |
| 35 | |
| 36 | var scope_object = scope_mirror.scopeObject().value(); |
| 37 | |
| 38 | for (var name in scope_expectations) { |
| 39 | var actual = scope_object[name]; |
| 40 | var expected = scope_expectations[name]; |
| 41 | assertEquals(expected, actual); |
| 42 | } |
| 43 | } |
| 44 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | // A copy of the scope types from debug/mirrors.js. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | var ScopeType = { Global: 0, |
| 47 | Local: 1, |
| 48 | With: 2, |
| 49 | Closure: 3, |
| 50 | Catch: 4, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 51 | Block: 5, |
| 52 | Script: 6}; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 53 | |
| 54 | var f1 = (function F1(x) { |
| 55 | function F2(y) { |
| 56 | var z = x + y; |
| 57 | with ({w: 5, v: "Capybara"}) { |
| 58 | var F3 = function(a, b) { |
| 59 | function F4(p) { |
| 60 | return p + a + b + z + w + v.length; |
| 61 | } |
| 62 | return F4; |
| 63 | } |
| 64 | return F3(4, 5); |
| 65 | } |
| 66 | } |
| 67 | return F2(17); |
| 68 | })(5); |
| 69 | |
| 70 | var mirror = Debug.MakeMirror(f1); |
| 71 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 72 | assertEquals(6, mirror.scopeCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | |
| 74 | CheckScope(mirror.scope(0), { a: 4, b: 5 }, ScopeType.Closure); |
| 75 | CheckScope(mirror.scope(1), { w: 5, v: "Capybara" }, ScopeType.With); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 76 | CheckScope(mirror.scope(2), { z: 22 }, ScopeType.Closure); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | CheckScope(mirror.scope(3), { x: 5 }, ScopeType.Closure); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 78 | CheckScope(mirror.scope(4), {}, ScopeType.Script); |
| 79 | CheckScope(mirror.scope(5), {}, ScopeType.Global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | |
| 81 | var f2 = function() { return 5; } |
| 82 | |
| 83 | var mirror = Debug.MakeMirror(f2); |
| 84 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 85 | assertEquals(2, mirror.scopeCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 87 | CheckScope(mirror.scope(0), {}, ScopeType.Script); |
| 88 | CheckScope(mirror.scope(1), {}, ScopeType.Global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 89 | |
| 90 | var f3 = (function F1(invisible_parameter) { |
| 91 | var invisible1 = 1; |
| 92 | var visible1 = 10; |
| 93 | return (function F2() { |
| 94 | var invisible2 = 2; |
| 95 | return (function F3() { |
| 96 | var visible2 = 20; |
| 97 | var invisible2 = 3; |
| 98 | return (function () {return visible1 + visible2 + visible1a;}); |
| 99 | })(); |
| 100 | })(); |
| 101 | })(5); |
| 102 | |
| 103 | var mirror = Debug.MakeMirror(f3); |
| 104 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 105 | assertEquals(4, mirror.scopeCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | |
| 107 | CheckScope(mirror.scope(0), { visible2: 20 }, ScopeType.Closure); |
| 108 | CheckScope(mirror.scope(1), { visible1: 10 }, ScopeType.Closure); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | CheckScope(mirror.scope(2), {}, ScopeType.Script); |
| 110 | CheckScope(mirror.scope(3), {}, ScopeType.Global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | var f4 = (function One() { |
| 114 | try { |
| 115 | throw "I'm error 1"; |
| 116 | } catch (e1) { |
| 117 | try { |
| 118 | throw "I'm error 2"; |
| 119 | } catch (e2) { |
| 120 | return function GetError() { |
| 121 | return e1 + e2; |
| 122 | }; |
| 123 | } |
| 124 | } |
| 125 | })(); |
| 126 | |
| 127 | var mirror = Debug.MakeMirror(f4); |
| 128 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 129 | assertEquals(4, mirror.scopeCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 130 | |
| 131 | CheckScope(mirror.scope(0), { e2: "I'm error 2" }, ScopeType.Catch); |
| 132 | CheckScope(mirror.scope(1), { e1: "I'm error 1" }, ScopeType.Catch); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 133 | CheckScope(mirror.scope(2), {}, ScopeType.Script); |
| 134 | CheckScope(mirror.scope(3), {}, ScopeType.Global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | var f5 = (function Raz(p1, p2) { |
| 138 | var p3 = p1 + p2; |
| 139 | return (function() { |
| 140 | var p4 = 20; |
| 141 | var p5 = 21; |
| 142 | var p6 = 22; |
| 143 | return eval("(function(p7){return p1 + p4 + p6 + p7})"); |
| 144 | })(); |
| 145 | })(1,2); |
| 146 | |
| 147 | var mirror = Debug.MakeMirror(f5); |
| 148 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 149 | assertEquals(4, mirror.scopeCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | |
| 151 | CheckScope(mirror.scope(0), { p4: 20, p6: 22 }, ScopeType.Closure); |
| 152 | CheckScope(mirror.scope(1), { p1: 1 }, ScopeType.Closure); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 153 | CheckScope(mirror.scope(2), {}, ScopeType.Script); |
| 154 | CheckScope(mirror.scope(3), {}, ScopeType.Global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | function CheckNoScopeVisible(f) { |
| 158 | var mirror = Debug.MakeMirror(f); |
| 159 | assertEquals(0, mirror.scopeCount()); |
| 160 | } |
| 161 | |
| 162 | CheckNoScopeVisible(Number); |
| 163 | |
| 164 | CheckNoScopeVisible(Function.toString); |
| 165 | |
| 166 | // This getter is known to be implemented as closure. |
| 167 | CheckNoScopeVisible(new Error().__lookupGetter__("stack")); |