1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This test checks that toString() round-trip on a function that has prefix, postfix and typeof operators applied to group expression will not remove the grouping. Also checks that evaluation of such a expression produces run-time exception"
26);
27
28/* These have become obsolete, since they are not syntactically well-formed ES5+.
29
30function postfix_should_preserve_parens(x, y, z) {
31    (x, y)++;
32    return y;
33}
34
35function prefix_should_preserve_parens(x, y, z) {
36    ++(x, y);
37    return x;
38
39}
40
41function both_should_preserve_parens(x, y, z) {
42    ++(x, y)--;
43    return x;
44
45}
46
47function postfix_should_preserve_parens_multi(x, y, z) {
48    (((x, y)))--;
49    return x;
50}
51
52function prefix_should_preserve_parens_multi(x, y, z) {
53    --(((x, y)));
54    return x;
55}
56
57function both_should_preserve_parens_multi(x, y, z) {
58    ++(((x, y)))--;
59    return x;
60}
61
62function postfix_should_preserve_parens_multi1(x, y, z) {
63    (((x)), y)--;
64    return x;
65}
66
67function prefix_should_preserve_parens_multi1(x, y, z) {
68    --(((x)), y);
69    return x;
70}
71
72function prefix_should_preserve_parens_multi2(x, y, z) {
73    var z = 0;
74    --(((x), y), z);
75    return x;
76}
77
78function postfix_should_preserve_parens_multi2(x, y, z) {
79    var z = 0;
80    (((x), y) ,z)++;
81    return x;
82}
83*/
84
85// if these return a variable (such as y) instead of
86// the result of typeof, this means that the parenthesis
87// got lost somewhere.
88function typeof_should_preserve_parens(x, y, z) {
89    return typeof (x, y);
90}
91
92function typeof_should_preserve_parens1(x, y, z) {
93    return typeof ((x, y));
94}
95
96function typeof_should_preserve_parens2(x, y, z) {
97    var z = 33;
98    return typeof (z, (x, y));
99}
100
101function typeof_should_preserve_parens_multi(x, y, z) {
102    var z = 33;
103    return typeof ((z,(((x, y)))));
104}
105
106unevalf = function(x) { return '(' + x.toString() + ')'; };
107
108function testToString(fn) {
109    // check that toString result evaluates to code that can be evaluated
110    // this doesn't actually reveal the bug that this test is testing
111    shouldBe("unevalf(eval(unevalf("+fn+")))", "unevalf(" + fn + ")");
112
113    // check that grouping operator is still there (this test reveals the bug
114    // but will create possible false negative if toString output changes in
115    // the future)
116    shouldBeTrue("/.*\\(+x\\)*, y\\)/.test(unevalf("+fn+"))");
117
118}
119
120function testToStringAndRTFailure(fn)
121{
122    testToString(fn);
123
124    // check that function call produces run-time exception
125    shouldThrow(""+fn+ "(1, 2, 3);");
126
127    // check that function call produces run-time exception after eval(unevalf)
128    shouldThrow("eval(unevalf("+fn+ "))(1, 2, 3);");
129}
130
131function testToStringAndReturn(fn, p1, p2, retval)
132{
133
134    testToString(fn);
135
136    // check that function call produces correct result
137    shouldBe("" + fn + "(" + p1 + ", " + p2 +");", retval);
138
139    // check that function call produces correct result after eval(unevalf)
140    shouldBe("eval(unevalf("+fn+ "))" + "(" + p1 + ", " + p2 +");", retval);
141}
142
143
144/*
145testToStringAndRTFailure("prefix_should_preserve_parens");
146testToStringAndRTFailure("postfix_should_preserve_parens");
147testToStringAndRTFailure("both_should_preserve_parens");
148testToStringAndRTFailure("prefix_should_preserve_parens_multi");
149testToStringAndRTFailure("postfix_should_preserve_parens_multi");
150testToStringAndRTFailure("prefix_should_preserve_parens_multi1");
151testToStringAndRTFailure("postfix_should_preserve_parens_multi1");
152testToStringAndRTFailure("prefix_should_preserve_parens_multi2");
153testToStringAndRTFailure("postfix_should_preserve_parens_multi2");
154*/
155
156testToStringAndReturn("typeof_should_preserve_parens", "'a'", 1, "'number'");
157testToStringAndReturn("typeof_should_preserve_parens1", "'a'", 1, "'number'");
158testToStringAndReturn("typeof_should_preserve_parens2", "'a'", 1, "'number'");
159testToStringAndReturn("typeof_should_preserve_parens_multi", "'a'", 1, "'number'");
160