]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* The contents of this file are subject to the Netscape Public |
2 | * License Version 1.1 (the "License"); you may not use this file | |
3 | * except in compliance with the License. You may obtain a copy of | |
4 | * the License at http://www.mozilla.org/NPL/ | |
5 | * | |
6 | * Software distributed under the License is distributed on an "AS | |
7 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
8 | * implied. See the License for the specific language governing | |
9 | * rights and limitations under the License. | |
10 | * | |
11 | * The Original Code is Mozilla Communicator client code, released March | |
12 | * 31, 1998. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Netscape Communications | |
15 | * Corporation. Portions created by Netscape are | |
16 | * Copyright (C) 1998 Netscape Communications Corporation. All | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | * | |
21 | */ | |
22 | /** | |
23 | File Name: 10.1.4-1.js | |
24 | ECMA Section: 10.1.4 Scope Chain and Identifier Resolution | |
25 | Description: | |
26 | Every execution context has associated with it a scope chain. This is | |
27 | logically a list of objects that are searched when binding an Identifier. | |
28 | When control enters an execution context, the scope chain is created and | |
29 | is populated with an initial set of objects, depending on the type of | |
30 | code. When control leaves the execution context, the scope chain is | |
31 | destroyed. | |
32 | ||
33 | During execution, the scope chain of the execution context is affected | |
34 | only by WithStatement. When execution enters a with block, the object | |
35 | specified in the with statement is added to the front of the scope chain. | |
36 | When execution leaves a with block, whether normally or via a break or | |
37 | continue statement, the object is removed from the scope chain. The object | |
38 | being removed will always be the first object in the scope chain. | |
39 | ||
40 | During execution, the syntactic production PrimaryExpression : Identifier | |
41 | is evaluated using the following algorithm: | |
42 | ||
43 | 1. Get the next object in the scope chain. If there isn't one, go to step 5. | |
44 | 2. Call the [[HasProperty]] method of Result(l), passing the Identifier as | |
45 | the property. | |
46 | 3. If Result(2) is true, return a value of type Reference whose base object | |
47 | is Result(l) and whose property name is the Identifier. | |
48 | 4. Go to step 1. | |
49 | 5. Return a value of type Reference whose base object is null and whose | |
50 | property name is the Identifier. | |
51 | The result of binding an identifier is always a value of type Reference with | |
52 | its member name component equal to the identifier string. | |
53 | Author: christine@netscape.com | |
54 | Date: 12 november 1997 | |
55 | */ | |
56 | var SECTION = "10.1.4-2"; | |
57 | var VERSION = "ECMA_1"; | |
58 | startTest(); | |
59 | ||
60 | writeHeaderToLog( SECTION + " Scope Chain and Identifier Resolution"); | |
61 | ||
62 | var testcases = getTestCases(); | |
63 | ||
64 | test(); | |
65 | ||
66 | function test() { | |
67 | for ( tc=0; tc < testcases.length; tc++ ) { | |
68 | ||
69 | var MYOBJECT = new MyObject(); | |
70 | var INPUT = 2; | |
71 | testcases[tc].description += "( "+INPUT +" )" ; | |
72 | ||
73 | with ( this ) { | |
74 | with ( MYOBJECT ) { | |
75 | testcases[tc].actual = eval( INPUT ); | |
76 | testcases[tc].expect = Math.pow(INPUT,2); | |
77 | } | |
78 | } | |
79 | ||
80 | testcases[tc].passed = writeTestCaseResult( | |
81 | testcases[tc].expect, | |
82 | testcases[tc].actual, | |
83 | testcases[tc].description +" = "+ | |
84 | testcases[tc].actual ); | |
85 | ||
86 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; | |
87 | } | |
88 | stopTest(); | |
89 | return ( testcases ); | |
90 | } | |
91 | function getTestCases() { | |
92 | var array = new Array(); | |
93 | var item = 0; | |
94 | ||
95 | array[item++] = new TestCase( "SECTION", "with MyObject, eval should return square of " ); | |
96 | ||
97 | return ( array ); | |
98 | } | |
99 | ||
100 | function MyObject() { | |
101 | this.eval = new Function( "x", "return(Math.pow(Number(x),2))" ); | |
102 | } |