blob: 10c3e8d82cc1b60a7a9a5faa134e696e49ba033f [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
28var realms = [Realm.current(), Realm.create()];
29globals = [Realm.global(0), Realm.global(1)];
30Realm.shared = {}
31
32function 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
39install('return_this', function() { return this; });
40install('return_this_strict', function () { 'use strict'; return this; });
41
42// test behaviour of 'with' scope
43for (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'
64for (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
78for (var i = 0; i < 4; i++) {
79 assertSame(globals[0], return_this());
80 assertSame(undefined, return_this_strict());
81}
82
83// BUG(1547)
84
85Realm.eval(realms[0], "var name = 'o'");
86Realm.eval(realms[1], "var name = 'i'");
87
88install('f', function() { return this.name; });
89install('g', function() { "use strict"; return this ? this.name : "u"; });
90
91for (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}