144d362409d5469aed47d19e7908d19bd194493aThomas Graf// Copyright 2013 the V8 project authors. All rights reserved.
244d362409d5469aed47d19e7908d19bd194493aThomas Graf// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
344d362409d5469aed47d19e7908d19bd194493aThomas Graf//
444d362409d5469aed47d19e7908d19bd194493aThomas Graf// Redistribution and use in source and binary forms, with or without
544d362409d5469aed47d19e7908d19bd194493aThomas Graf// modification, are permitted provided that the following conditions
644d362409d5469aed47d19e7908d19bd194493aThomas Graf// are met:
744d362409d5469aed47d19e7908d19bd194493aThomas Graf// 1.  Redistributions of source code must retain the above copyright
844d362409d5469aed47d19e7908d19bd194493aThomas Graf//     notice, this list of conditions and the following disclaimer.
98a3efffa5b3fde252675239914118664d36a2c24Thomas Graf// 2.  Redistributions in binary form must reproduce the above copyright
1044d362409d5469aed47d19e7908d19bd194493aThomas Graf//     notice, this list of conditions and the following disclaimer in the
1144d362409d5469aed47d19e7908d19bd194493aThomas Graf//     documentation and/or other materials provided with the distribution.
1244d362409d5469aed47d19e7908d19bd194493aThomas Graf//
1344d362409d5469aed47d19e7908d19bd194493aThomas Graf// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
1444d362409d5469aed47d19e7908d19bd194493aThomas Graf// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1544d362409d5469aed47d19e7908d19bd194493aThomas Graf// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1644d362409d5469aed47d19e7908d19bd194493aThomas Graf// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
1744d362409d5469aed47d19e7908d19bd194493aThomas Graf// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1844d362409d5469aed47d19e7908d19bd194493aThomas Graf// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1944d362409d5469aed47d19e7908d19bd194493aThomas Graf// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2044d362409d5469aed47d19e7908d19bd194493aThomas Graf// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2144d362409d5469aed47d19e7908d19bd194493aThomas Graf// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2244d362409d5469aed47d19e7908d19bd194493aThomas Graf// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2344d362409d5469aed47d19e7908d19bd194493aThomas Graf
2444d362409d5469aed47d19e7908d19bd194493aThomas Grafdescription(
2544d362409d5469aed47d19e7908d19bd194493aThomas Graf'Tests whether eval() works inside statements that read and modify a value.'
2644d362409d5469aed47d19e7908d19bd194493aThomas Graf);
2744d362409d5469aed47d19e7908d19bd194493aThomas Graf
2844d362409d5469aed47d19e7908d19bd194493aThomas Graffunction multTest()
2944d362409d5469aed47d19e7908d19bd194493aThomas Graf{
3044d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 1;
3144d362409d5469aed47d19e7908d19bd194493aThomas Graf    x *= eval('2');
3244d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 2;
3344d362409d5469aed47d19e7908d19bd194493aThomas Graf}
3444d362409d5469aed47d19e7908d19bd194493aThomas Graf
3544d362409d5469aed47d19e7908d19bd194493aThomas Graffunction divTest()
3644d362409d5469aed47d19e7908d19bd194493aThomas Graf{
3744d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 2;
3844d362409d5469aed47d19e7908d19bd194493aThomas Graf    x /= eval('2');
3944d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 1;
4044d362409d5469aed47d19e7908d19bd194493aThomas Graf}
4144d362409d5469aed47d19e7908d19bd194493aThomas Graf
4244d362409d5469aed47d19e7908d19bd194493aThomas Graffunction addTest()
4344d362409d5469aed47d19e7908d19bd194493aThomas Graf{
4444d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 0;
4544d362409d5469aed47d19e7908d19bd194493aThomas Graf    x += eval('1');
4644d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 1;
4744d362409d5469aed47d19e7908d19bd194493aThomas Graf}
4844d362409d5469aed47d19e7908d19bd194493aThomas Graf
4944d362409d5469aed47d19e7908d19bd194493aThomas Graffunction subTest()
5044d362409d5469aed47d19e7908d19bd194493aThomas Graf{
5144d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 0;
5244d362409d5469aed47d19e7908d19bd194493aThomas Graf    x -= eval('1');
5344d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == -1;
54c48a17694b6719606fa76fdad8a5cef3289cb42cThomas Graf}
5544d362409d5469aed47d19e7908d19bd194493aThomas Graf
5644d362409d5469aed47d19e7908d19bd194493aThomas Graffunction lshiftTest()
5744d362409d5469aed47d19e7908d19bd194493aThomas Graf{
5844d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 1;
5944d362409d5469aed47d19e7908d19bd194493aThomas Graf    x <<= eval('1');
6044d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 2;
6144d362409d5469aed47d19e7908d19bd194493aThomas Graf}
6244d362409d5469aed47d19e7908d19bd194493aThomas Graf
6344d362409d5469aed47d19e7908d19bd194493aThomas Graffunction rshiftTest()
6444d362409d5469aed47d19e7908d19bd194493aThomas Graf{
6544d362409d5469aed47d19e7908d19bd194493aThomas Graf    var x = 1;
6644d362409d5469aed47d19e7908d19bd194493aThomas Graf    x >>= eval('1');
6744d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 0;
6844d362409d5469aed47d19e7908d19bd194493aThomas Graf}
69508685c269275cb7ba3471c75abc689b4e3839b1Thomas Graf
70662887c052f0fcd98287e2a7fa5843a4f66ca256Thomas Graffunction urshiftTest()
71508685c269275cb7ba3471c75abc689b4e3839b1Thomas Graf{
72662887c052f0fcd98287e2a7fa5843a4f66ca256Thomas Graf    var x = 1;
73508685c269275cb7ba3471c75abc689b4e3839b1Thomas Graf    x >>>= eval('1');
7444d362409d5469aed47d19e7908d19bd194493aThomas Graf    return x == 0;
7544d362409d5469aed47d19e7908d19bd194493aThomas Graf}
7644d362409d5469aed47d19e7908d19bd194493aThomas Graf
7744d362409d5469aed47d19e7908d19bd194493aThomas Graffunction andTest()
7844d362409d5469aed47d19e7908d19bd194493aThomas Graf{
79    var x = 1;
80    x &= eval('1');
81    return x == 1;
82}
83
84function xorTest()
85{
86    var x = 0;
87    x ^= eval('1');
88    return x == 1;
89}
90
91function orTest()
92{
93    var x = 0;
94    x |= eval('1');
95    return x == 1;
96}
97
98function modTest()
99{
100    var x = 4;
101    x %= eval('3');
102    return x == 1;
103}
104
105function preIncTest()
106{
107    var x = { value: 0 };
108    ++eval('x').value;
109    return x.value == 1;
110}
111
112function preDecTest()
113{
114    var x = { value: 0 };
115    --eval('x').value;
116    return x.value == -1;
117}
118
119function postIncTest()
120{
121    var x = { value: 0 };
122    eval('x').value++;
123    return x.value == 1;
124}
125
126function postDecTest()
127{
128    var x = { value: 0 };
129    eval('x').value--;
130    return x.value == -1;
131}
132
133function primitiveThisTest()
134{
135    // Test that conversion of this is persistant over multiple calls to eval,
136    // even where 'this' is not directly used within the function.
137    eval('this.value = "Seekrit message";');
138    return eval('this.value') === "Seekrit message";
139}
140
141function strictThisTest()
142{
143    // In a strict mode function primitive this values are not converted, so
144    // the property access in the first eval is writing a value to a temporary
145    // object. This throws, per section 8.7.2.
146    "use strict";
147    eval('this.value = "Seekrit message";');
148    return eval('this.value') === undefined;
149}
150
151shouldBeTrue('multTest();');
152shouldBeTrue('divTest();');
153shouldBeTrue('addTest();');
154shouldBeTrue('subTest();');
155shouldBeTrue('lshiftTest();');
156shouldBeTrue('rshiftTest();');
157shouldBeTrue('urshiftTest();');
158shouldBeTrue('andTest();');
159shouldBeTrue('xorTest();');
160shouldBeTrue('orTest();');
161shouldBeTrue('modTest();');
162
163shouldBeTrue('preIncTest();');
164shouldBeTrue('preDecTest();');
165shouldBeTrue('postIncTest();');
166shouldBeTrue('postDecTest();');
167
168shouldBeTrue('primitiveThisTest.call(1);');
169shouldThrow('strictThisTest.call(1);');
170