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 | // Tests loading of properties across eval calls. |
| 29 | |
| 30 | var x = 1; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 31 | function global_function() { return 'global'; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | |
| 33 | // Test loading across an eval call that does not shadow variables. |
| 34 | function testNoShadowing() { |
| 35 | var y = 2; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 36 | function local_function() { return 'local'; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | function f() { |
| 38 | eval('1'); |
| 39 | assertEquals(1, x); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 40 | try { typeof(asdf); } catch(e) { assertUnreachable(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | assertEquals(2, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 42 | assertEquals('global', global_function()); |
| 43 | assertEquals('local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | function g() { |
| 45 | assertEquals(1, x); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 46 | try { typeof(asdf); } catch(e) { assertUnreachable(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | assertEquals(2, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 48 | assertEquals('global', global_function()); |
| 49 | assertEquals('local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | } |
| 51 | g(); |
| 52 | } |
| 53 | f(); |
| 54 | } |
| 55 | |
| 56 | testNoShadowing(); |
| 57 | |
| 58 | // Test loading across eval calls that do not shadow variables. |
| 59 | function testNoShadowing2() { |
| 60 | var y = 2; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 61 | function local_function() { return 'local'; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | eval('1'); |
| 63 | function f() { |
| 64 | eval('1'); |
| 65 | assertEquals(1, x); |
| 66 | assertEquals(2, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 67 | assertEquals('global', global_function()); |
| 68 | assertEquals('local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | function g() { |
| 70 | assertEquals(1, x); |
| 71 | assertEquals(2, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 72 | assertEquals('global', global_function()); |
| 73 | assertEquals('local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | } |
| 75 | g(); |
| 76 | } |
| 77 | f(); |
| 78 | } |
| 79 | |
| 80 | testNoShadowing2(); |
| 81 | |
| 82 | // Test loading across an eval call that shadows variables. |
| 83 | function testShadowing() { |
| 84 | var y = 2; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 85 | function local_function() { return 'local'; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | function f() { |
| 87 | eval('var x = 3; var y = 4;'); |
| 88 | assertEquals(3, x); |
| 89 | assertEquals(4, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 90 | eval('function local_function() { return "new_local"; }'); |
| 91 | eval('function global_function() { return "new_nonglobal"; }'); |
| 92 | assertEquals('new_nonglobal', global_function()); |
| 93 | assertEquals('new_local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | function g() { |
| 95 | assertEquals(3, x); |
| 96 | assertEquals(4, y); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 97 | assertEquals('new_nonglobal', global_function()); |
| 98 | assertEquals('new_local', local_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | } |
| 100 | g(); |
| 101 | } |
| 102 | f(); |
| 103 | } |
| 104 | |
| 105 | testShadowing(); |