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 // Test some expression contexts involving short-circuit boolean 29 // operations that did not otherwise have test coverage. 30 31 32 var x = 42; 33 34 // Literals in value/test context. 35 assertEquals(x, function () { return 0 || x }()); 36 assertEquals(1, function () { return 1 || x }()); 37 38 // Literals in test/value context. 39 assertEquals(0, function () { return 0 && x }()); 40 assertEquals(x, function () { return 1 && x }()); 41 42 // A value on top of the stack in value/test context. 43 assertEquals(x, function(y) { return y++ || x }(0)); 44 assertEquals(1, function(y) { return y++ || x }(1)); 45 46 // A value on top of the stack in a test/value context. 47 assertEquals(0, function(y) { return y++ && x }(0)); 48 assertEquals(x, function(y) { return y++ && x }(1)); 49 50 // An object literal in value context. 51 assertEquals(0, function () { return {x: 0}}().x); 52 53 // An object literal in value/test context. 54 assertEquals(0, function () { return {x: 0} || this }().x); 55 56 // An object literal in test/value context. 57 assertEquals(x, function () { return {x: 0} && this }().x); 58 59 // An array literal in value/test context. 60 assertEquals(0, function () { return [0,1] || new Array(x,1) }()[0]); 61 62 // An array literal in test/value context. 63 assertEquals(x, function () { return [0,1] && new Array(x,1) }()[0]); 64 65 // Slot assignment in value/test context. 66 assertEquals(x, function (y) { return (y = 0) || x }("?")); 67 assertEquals(1, function (y) { return (y = 1) || x }("?")); 68 69 // Slot assignment in test/value context. 70 assertEquals(0, function (y) { return (y = 0) && x }("?")); 71 assertEquals(x, function (y) { return (y = 1) && x }("?")); 72 73 // void in value context. 74 assertEquals(void 0, function () { return void x }()); 75 76 // void in value/test context. 77 assertEquals(x, function () { return (void x) || x }()); 78 79 // void in test/value context. 80 assertEquals(void 0, function () { return (void x) && x }()); 81 82 // Unary not in value context. 83 assertEquals(false, function () { return !x }()); 84 85 // Unary not in value/test context. 86 assertEquals(true, function (y) { return !y || x }(0)); 87 assertEquals(x, function (y) { return !y || x }(1)); 88 89 // Unary not in test/value context. 90 assertEquals(x, function (y) { return !y && x }(0)); 91 assertEquals(false, function (y) { return !y && x }(1)); 92 93 // Comparison in value context. 94 assertEquals(false, function () { return x < x; }()); 95 96 // Comparison in value/test context. 97 assertEquals(x, function () { return x < x || x; }()); 98 assertEquals(true, function () { return x <= x || x; }()); 99 100 // Comparison in test/value context. 101 assertEquals(false, function () { return x < x && x; }()); 102 assertEquals(x, function () { return x <= x && x; }()); 103