Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / contextual-calls.js
1 // Copyright 2013 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 var realms = [Realm.current(), Realm.create()];
29 globals = [Realm.global(0), Realm.global(1)];
30 Realm.shared = {}
31
32 function install(name, value) {
33   Realm.shared[name] = value;
34   for (i in realms) {
35     Realm.eval(realms[i], name + " = Realm.shared['" + name + "'];");
36   }
37 }
38
39 install('return_this', function() { return this; });
40 install('return_this_strict', function () { 'use strict'; return this; });
41
42 // test behaviour of 'with' scope
43 for (i in realms) {
44   Realm.shared.results = [];
45   // in the second case, 'this' is found in the with scope,
46   // so the receiver is 'this'
47   Realm.eval(realms[i],"                                                       \
48       with('irrelevant') {                                                     \
49         Realm.shared.results.push(return_this());                              \
50         Realm.shared.results.push(return_this_strict());                       \
51       }                                                                        \
52       with(this) {                                                             \
53         Realm.shared.results.push(return_this());                              \
54         Realm.shared.results.push(return_this_strict());                       \
55       }                                                                        \
56     ");
57   assertSame(globals[0], Realm.shared.results[0]);
58   assertSame(undefined, Realm.shared.results[1]);
59   assertSame(globals[i], Realm.shared.results[2]);
60   assertSame(globals[i], Realm.shared.results[3]);
61 }
62
63 // test 'apply' and 'call'
64 for (i in realms) {
65   // 'apply' without a receiver is a contextual call
66   assertSame(globals[0], Realm.eval(realms[i],'return_this.apply()')) ;
67   assertSame(undefined, Realm.eval(realms[i],'return_this_strict.apply()'));
68   assertSame(globals[0], Realm.eval(realms[i],'return_this.apply(null)')) ;
69   assertSame(null, Realm.eval(realms[i],'return_this_strict.apply(null)'));
70   // 'call' without a receiver is a contextual call
71   assertSame(globals[0], Realm.eval(realms[i],'return_this.call()')) ;
72   assertSame(undefined, Realm.eval(realms[i],'return_this_strict.call()'));
73   assertSame(globals[0], Realm.eval(realms[i],'return_this.call(null)')) ;
74   assertSame(null, Realm.eval(realms[i],'return_this_strict.call(null)'));
75 }
76
77 // test ics
78 for (var i = 0; i < 4; i++) {
79   assertSame(globals[0], return_this());
80   assertSame(undefined, return_this_strict());
81 }
82
83 // BUG(1547)
84
85 Realm.eval(realms[0], "var name = 'o'");
86 Realm.eval(realms[1], "var name = 'i'");
87
88 install('f', function() { return this.name; });
89 install('g', function() { "use strict"; return this ? this.name : "u"; });
90
91 for (i in realms) {
92   result = Realm.eval(realms[i], "                                             \
93       (function(){return f();})() +                                            \
94       (function(){return (1,f)();})() +                                        \
95       (function(){'use strict'; return f();})() +                              \
96       (function(){'use strict'; return (1,f)();})() +                          \
97       (function(){return g();})() +                                            \
98       (function(){return (1,g)();})() +                                        \
99       (function(){'use strict'; return g();})() +                              \
100       (function(){'use strict'; return (1,g)();})();                           \
101     ");
102   assertSame("oooouuuu", result);
103 }