ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +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 | // Tests loading of properties across eval calls. |
| 29 | |
| 30 | var x = 1; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 31 | function global_function() { return 'global'; } |
| 32 | const const_uninitialized; |
| 33 | const const_initialized = function() { return "const_global"; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 34 | |
| 35 | // Test loading across an eval call that does not shadow variables. |
| 36 | function testNoShadowing() { |
| 37 | var y = 2; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 38 | function local_function() { return 'local'; } |
| 39 | const local_const_uninitialized; |
| 40 | const local_const_initialized = function() { return "const_local"; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 41 | function f() { |
| 42 | eval('1'); |
| 43 | assertEquals(1, x); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 44 | try { typeof(asdf); } catch(e) { assertUnreachable(); } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 45 | assertEquals(2, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 46 | assertEquals('global', global_function()); |
| 47 | assertEquals('local', local_function()); |
| 48 | try { |
| 49 | const_uninitialized(); |
| 50 | assertUnreachable(); |
| 51 | } catch(e) { |
| 52 | // Ignore. |
| 53 | } |
| 54 | assertEquals('const_global', const_initialized()); |
| 55 | try { |
| 56 | local_const_uninitialized(); |
| 57 | assertUnreachable(); |
| 58 | } catch(e) { |
| 59 | // Ignore. |
| 60 | } |
| 61 | assertEquals('const_local', local_const_initialized()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 62 | function g() { |
| 63 | assertEquals(1, x); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 64 | try { typeof(asdf); } catch(e) { assertUnreachable(); } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 65 | assertEquals(2, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 66 | assertEquals('global', global_function()); |
| 67 | assertEquals('local', local_function()); |
| 68 | try { |
| 69 | const_uninitialized(); |
| 70 | assertUnreachable(); |
| 71 | } catch(e) { |
| 72 | // Ignore. |
| 73 | } |
| 74 | assertEquals('const_global', const_initialized()); |
| 75 | try { |
| 76 | local_const_uninitialized(); |
| 77 | assertUnreachable(); |
| 78 | } catch(e) { |
| 79 | // Ignore. |
| 80 | } |
| 81 | assertEquals('const_local', local_const_initialized()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 82 | } |
| 83 | g(); |
| 84 | } |
| 85 | f(); |
| 86 | } |
| 87 | |
| 88 | testNoShadowing(); |
| 89 | |
| 90 | // Test loading across eval calls that do not shadow variables. |
| 91 | function testNoShadowing2() { |
| 92 | var y = 2; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 93 | function local_function() { return 'local'; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 94 | eval('1'); |
| 95 | function f() { |
| 96 | eval('1'); |
| 97 | assertEquals(1, x); |
| 98 | assertEquals(2, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 99 | assertEquals('global', global_function()); |
| 100 | assertEquals('local', local_function()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 101 | function g() { |
| 102 | assertEquals(1, x); |
| 103 | assertEquals(2, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 104 | assertEquals('global', global_function()); |
| 105 | assertEquals('local', local_function()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 106 | } |
| 107 | g(); |
| 108 | } |
| 109 | f(); |
| 110 | } |
| 111 | |
| 112 | testNoShadowing2(); |
| 113 | |
| 114 | // Test loading across an eval call that shadows variables. |
| 115 | function testShadowing() { |
| 116 | var y = 2; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 117 | function local_function() { return 'local'; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 118 | function f() { |
| 119 | eval('var x = 3; var y = 4;'); |
| 120 | assertEquals(3, x); |
| 121 | assertEquals(4, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 122 | eval('function local_function() { return "new_local"; }'); |
| 123 | eval('function global_function() { return "new_nonglobal"; }'); |
| 124 | assertEquals('new_nonglobal', global_function()); |
| 125 | assertEquals('new_local', local_function()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 126 | function g() { |
| 127 | assertEquals(3, x); |
| 128 | assertEquals(4, y); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 129 | assertEquals('new_nonglobal', global_function()); |
| 130 | assertEquals('new_local', local_function()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 131 | } |
| 132 | g(); |
| 133 | } |
| 134 | f(); |
| 135 | } |
| 136 | |
| 137 | testShadowing(); |