Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / Function / call-001.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 /*
40  * Date: 2001-07-13
41  *
42  * SUMMARY: Applying Function.prototype.call to the Function object itself
43  *
44  *
45  * ECMA-262 15.3.4.4 Function.prototype.call (thisArg [,arg1 [,arg2,\85] ] )
46  *
47  * When applied to the Function object itself, thisArg should be ignored.
48  * As explained by Waldemar (waldemar@netscape.com):
49  *
50  * Function.call(obj, "print(this)") is equivalent to invoking
51  * Function("print(this)") with this set to obj. Now, Function("print(this)")
52  * is equivalent to new Function("print(this)") (see 15.3.1.1), and the latter
53  * ignores the this value that you passed it and constructs a function
54  * (which we'll call F) which will print the value of the this that will be
55  * passed in when F will be invoked.
56  *
57  * With the last set of () you're invoking F(), which means you're calling it
58  * with no this value. When you don't provide a this value, it defaults to the
59  * global object.
60  *
61  */
62
63 //-----------------------------------------------------------------------------
64 var UBound = 0;
65 var BUGNUMBER = '(none)';
66 var summary = 'Applying Function.prototype.call to the Function object itself';
67 var status = '';
68 var statusitems = [];
69 var actual = '';
70 var actualvalues = [];
71 var expect= '';
72 var expectedvalues = [];
73 var self = this; // capture a reference to the global object
74 var cnOBJECT_GLOBAL = self.toString();
75 var cnOBJECT_OBJECT = (new Object).toString();
76 var cnHello = 'Hello';
77 var cnRed = 'red';
78 var objTEST = {color:cnRed};
79 var f = new Function();
80 var g = new Function();
81
82
83 f = Function.call(self, 'return cnHello');
84 g = Function.call(objTEST, 'return cnHello');
85
86 status = 'Section A of test';
87 actual = f();
88 expect = cnHello;
89 captureThis();
90
91 status = 'Section B of test';
92 actual = g();
93 expect = cnHello;
94 captureThis();
95
96
97 f = Function.call(self, 'return this.toString()');
98 g = Function.call(objTEST, 'return this.toString()');
99
100 status = 'Section C of test';
101 actual = f();
102 expect = cnOBJECT_GLOBAL;
103 captureThis();
104
105 status = 'Section D of test';
106 actual = g();
107 expect = cnOBJECT_GLOBAL;
108 captureThis();
109
110
111 f = Function.call(self, 'return this.color');
112 g = Function.call(objTEST, 'return this.color');
113
114 status = 'Section E of test';
115 actual = f();
116 expect = undefined;
117 captureThis();
118
119 status = 'Section F of test';
120 actual = g();
121 expect = undefined;
122 captureThis();
123
124
125
126 //-----------------------------------------------------------------------------
127 test();
128 //-----------------------------------------------------------------------------
129
130
131 function captureThis()
132 {
133   statusitems[UBound] = status;
134   actualvalues[UBound] = actual;
135   expectedvalues[UBound] = expect;
136   UBound++;
137 }
138
139
140 function test()
141 {
142   enterFunc ('test');
143   printBugNumber(BUGNUMBER);
144   printStatus (summary);
145
146   for (var i = 0; i < UBound; i++)
147   {
148     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
149   }
150
151   exitFunc ('test');
152 }