Debian Codegraph @

rhino/testsrc/tests/js1_5/Object/regress-192105.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 *
8 * Date: 06 February 2003
9 * SUMMARY: Using |instanceof| to check if function is called as a constructor
10 *
11 * See http://bugzilla.mozilla.org/show_bug.cgi?id=192105
12 *
13 */
14 //-----------------------------------------------------------------------------
15 var gTestfile = 'regress-192105.js';
16 var UBound = 0;
17 var BUGNUMBER = 192105;
18 var summary = 'Using |instanceof| to check if f() is called as constructor';
19 var status = '';
20 var statusitems = [];
21 var actual = '';
22 var actualvalues = [];
23 var expect= '';
24 var expectedvalues = [];
25
26
27 /*
28 * This function is the heart of the test. It sets the result
29 * variable |actual|, which we will compare against |expect|.
30 *
31 * Note |actual| will be set to |true| or |false| according
32 * to whether or not this function is called as a constructor;
33 * i.e. whether it is called via the |new| keyword or not -
34 */
35 function f()
36 {
37 actual = (this instanceof f);
38 }
39
40
41 /*
42 * Call f as a constructor from global scope
43 */
44 status = inSection(1);
45 new f(); // sets |actual|
46 expect = true;
47 addThis();
48
49 /*
50 * Now, not as a constructor
51 */
52 status = inSection(2);
53 f(); // sets |actual|
54 expect = false;
55 addThis();
56
57
58 /*
59 * Call f as a constructor from function scope
60 */
61 function F()
62 {
63 new f();
64 }
65 status = inSection(3);
66 F(); // sets |actual|
67 expect = true;
68 addThis();
69
70 /*
71 * Now, not as a constructor
72 */
73 function G()
74 {
75 f();
76 }
77 status = inSection(4);
78 G(); // sets |actual|
79 expect = false;
80 addThis();
81
82
83 /*
84 * Now make F() and G() methods of an object
85 */
86 var obj = {F:F, G:G};
87 status = inSection(5);
88 obj.F(); // sets |actual|
89 expect = true;
90 addThis();
91
92 status = inSection(6);
93 obj.G(); // sets |actual|
94 expect = false;
95 addThis();
96
97
98 /*
99 * Now call F() and G() from yet other functions, and use eval()
100 */
101 function A()
102 {
103 eval('F();');
104 }
105 status = inSection(7);
106 A(); // sets |actual|
107 expect = true;
108 addThis();
109
110
111 function B()
112 {
113 eval('G();');
114 }
115 status = inSection(8);
116 B(); // sets |actual|
117 expect = false;
118 addThis();
119
120
121
122
123 //-----------------------------------------------------------------------------
124 test();
125 //-----------------------------------------------------------------------------
126
127
128
129 function addThis()
130 {
131 statusitems[UBound] = status;
132 actualvalues[UBound] = actual;
133 expectedvalues[UBound] = expect;
134 UBound++;
135 }
136
137
138 function test()
139 {
140 enterFunc('test');
141 printBugNumber(BUGNUMBER);
142 printStatus(summary);
143
144 for (var i=0; i<UBound; i++)
145 {
146 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
147 }
148
149 exitFunc ('test');
150 }