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"Tests variable resolution rules for named function expressions."
26);
27
28function Call(lambda) { return lambda(); }
29
30debug("anonymous function expression");
31shouldBe("var x = (function(a,b){ return a + b; }); x(1,2)", "3");
32
33debug("named function expression");
34shouldBe("var x = (function Named(a,b){ return a + b; }); x(2,3)", "5");
35
36debug("eval'd code should be able to access scoped variables");
37shouldBe("var z = 6; var x = eval('(function(a,b){ return a + b + z; })'); x(3,4)", "13");
38
39debug("eval'd code + self-check");
40shouldBe("var z = 10; var x = eval('(function Named(a,b){ return (!!Named) ? (a + b + z) : -999; })'); x(4,5)", "19");
41
42debug("named function expressions are not saved in the current context");
43shouldBe('(function Foo(){ return 1; }); try { Foo(); throw "FuncExpr was stored"; } catch(e) { if(typeof(e)=="string") throw e; } 1', "1");
44
45debug("recursion is possible, though");
46shouldBe("var ctr = 3; var x = (function Named(a,b){ if(--ctr) return 2 * Named(a,b); else return a + b; }); x(5,6)", "44");
47
48debug("regression test where kjs regarded an anonymous function declaration (which is illegal) as a FunctionExpr");
49shouldBe('var hadError = 0; try { eval("function(){ return 2; };"); } catch(e) { hadError = 1; }; hadError;', "1");
50
51debug("\n-----\n");
52
53function shouldBeTrueWithDescription(x, xDescription)
54{
55        if (x) {
56                debug("PASS: " + xDescription + " should be true and is.");
57                return;
58        }
59
60        debug("FAIL: " + xDescription + " should be true but isn't.");
61}
62
63// Recursion.
64shouldBeTrueWithDescription(
65        (function closure() { return closure == arguments.callee && !this.closure; })(),
66        "(function closure() { return closure == arguments.callee && !this.closure; })()"
67);
68
69// Assignment.
70shouldBeTrueWithDescription(
71        (function closure() { closure = 1; return closure == arguments.callee && !this.closure; })(),
72        "(function closure() { closure = 1; return closure == arguments.callee && !this.closure; })()"
73);
74
75// Function name vs parameter.
76shouldBeTrueWithDescription(
77        (function closure(closure) { return closure == 1 && !this.closure; })(1),
78        "(function closure(closure) { return closure == 1 && !this.closure; })(1)"
79);
80
81// Function name vs var.
82shouldBeTrueWithDescription(
83        (function closure() { var closure = 1; return closure == 1 && !this.closure; })(),
84        "(function closure() { var closure = 1; return closure == 1 && !this.closure; })()"
85);
86
87// Function name vs declared function.
88shouldBeTrueWithDescription(
89        (function closure() { function closure() { }; return closure != arguments.callee && !this.closure; })(),
90        "(function closure() { function closure() { }; return closure != arguments.callee && !this.closure; })()"
91);
92
93// Resolve before tear-off.
94shouldBeTrueWithDescription(
95        (function closure() { return (function() { return closure && !this.closure; })(); })(),
96        "(function closure() { return (function() { return closure && !this.closure; })(); })()"
97);
98
99// Resolve assignment before tear-off.
100shouldBeTrueWithDescription(
101        (function closure() { return (function() { closure = null; return closure && !this.closure; })(); })(),
102        "(function closure() { return (function() { closure = null; return closure && !this.closure; })(); })()"
103);
104
105// Resolve after tear-off.
106shouldBeTrueWithDescription(
107        (function closure() { return (function() { return closure && !this.closure; }); })()(),
108        "(function closure() { return (function() { return closure && !this.closure; }); })()()"
109);
110
111// Resolve assignment after tear-off.
112shouldBeTrueWithDescription(
113        (function closure() { return (function() { closure = null; return closure && !this.closure; }); })()(),
114        "(function closure() { return (function() { closure = null; return closure && !this.closure; }); })()()"
115);
116
117// Eval var shadowing (should overwrite).
118shouldBeTrueWithDescription(
119        (function closure() { eval("var closure"); return closure == undefined && !this.closure; })(),
120        "(function closure() { eval(\"var closure\"); return closure == undefined && !this.closure; })()"
121);
122
123// Eval function shadowing (should overwrite).
124shouldBeTrueWithDescription(
125        (function closure() { eval("function closure() { }"); return closure != arguments.callee && !this.closure; })(),
126        "(function closure() { eval(\"function closure() { }\"); return closure != arguments.callee && !this.closure; })()"
127);
128
129// Eval shadowing (should overwrite), followed by put (should overwrite).
130shouldBeTrueWithDescription(
131        (function closure() { eval("var closure;"); closure = 1; return closure == 1 && !this.closure; })(),
132        "(function closure() { eval(\"var closure;\"); closure = 1; return closure == 1 && !this.closure; })()"
133);
134
135// Eval var shadowing, followed by delete (should not overwrite).
136shouldBeTrueWithDescription(
137        (function closure() { eval("var closure"); delete closure; return closure == arguments.callee && !this.closure; })(),
138        "(function closure() { eval(\"var closure\"); delete closure; return closure == arguments.callee && !this.closure; })()"
139);
140
141// Eval function shadowing, followed by delete (should not overwrite).
142shouldBeTrueWithDescription(
143        (function closure() { eval("function closure() { }"); delete closure; return closure == arguments.callee && !this.closure; })(),
144        "(function closure() { eval(\"function closure() { }\"); delete closure; return closure == arguments.callee && !this.closure; })()"
145);
146
147// Eval assignment (should not overwrite).
148shouldBeTrueWithDescription(
149        (function closure() { eval("closure = 1;"); return closure == arguments.callee && !this.closure; })(),
150        "(function closure() { eval(\"closure = 1;\"); return closure == arguments.callee && !this.closure; })()"
151);
152