-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathenumeratingWithES5.js
208 lines (173 loc) · 5.33 KB
/
enumeratingWithES5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
var propName = "d";
var propValue = "dvalue";
function CreateSimpleTypeHandlerObject()
{
var obj = Object.create(null);
obj[propName] = propValue;
return obj;
}
function CreateSimpleDictionaryTypeHandlerObject()
{
var obj = {};
obj[propName] = propValue;
return obj;
}
function CreateDictionaryTypeHandlerObject()
{
var obj = {};
Object.defineProperty(obj, propName,
{
get : function() {},
configurable : true,
enumerable : true
});
delete obj[propName];
obj[propName] = propValue;
return obj;
}
function TestNonWritable(o)
{
var beforeTestValue = null;
var value = 1;
value = TestEnumerations(o, beforeTestValue, value);
SetWritable(o, propName, false);
value = TestEnumerations(o, beforeTestValue, value);
SetWritable(o, propName, true);
value = TestEnumerations(o, beforeTestValue, value);
telemetryLog("Changing writability during enumeration...", true);
beforeTestValue = function(o, i, value)
{
SetWritable(o, propName, false);
return value;
};
value = TestEnumerations(o, beforeTestValue, value);
beforeTestValue = function(o, i, value)
{
SetWritable(o, propName, true);
return value;
};
value = TestEnumerations(o, beforeTestValue, value);
telemetryLog("Freezing object", true);
Object.freeze(o);
beforeTestValue = null;
value = TestEnumerations(o, beforeTestValue, value);
}
function TestAccessors()
{
var o = { a:"aValue" };
DefineAccessor(o, 'b',
function() { return "GETTER FOR b"; },
function(v) { telemetryLog("SETTER FOR b", true); }
);
o.c = "cValue"; // to be deleted
o.d = "dValue";
// Throw in a non-enumerable property
Object.defineProperty(o, 'e',
{
value : "eValue",
configurable : true,
writable : true,
enumerable : false
});
DefineAccessor(o, 'f',
function() { return "GETTER FOR f"; },
function(v) { telemetryLog("SETTER FOR f", true); }
);
o.g = "gValue";
delete o.c;
var value = 1;
var beforeTestValue = null;
value = TestEnumerations(o, beforeTestValue, value);
DefineAccessor(o, propName);
value = TestEnumerations(o, beforeTestValue, value);
DefineDataProperty(o, propName, value++);
value = TestEnumerations(o, beforeTestValue, value);
telemetryLog("Defining accessor property during enumeration...", true);
beforeTestValue = function(o, i, value)
{
if (i === propName) DefineAccessor(o, propName);
return value;
};
value = TestEnumerations(o, beforeTestValue, value);
telemetryLog("Defining data property during enumeration...", true);
beforeTestValue = function(o, i, value)
{
if (i === propName) DefineDataProperty(o, propName, value);
return value + 1;
};
value = TestEnumerations(o, beforeTestValue, value);
}
function SetWritable(o, p, v)
{
telemetryLog("Setting writability of " + p + " to " + v, true);
Object.defineProperty(o, p, { writable : v });
}
function DefineAccessor(o, p, getter, setter)
{
if (!getter) getter = function() { return "GETTER"; };
if (!setter) setter = function(v) { telemetryLog("SETTER", true); }
telemetryLog("Defining accessors for " + p, true);
Object.defineProperty(o, p,
{
get : getter,
set : setter,
configurable : true,
enumerable : true
});
}
function DefineDataProperty(o, p, v)
{
telemetryLog("Defining data property " + p + " with value " + v, true);
Object.defineProperty(o, p,
{
value : v,
writable : true,
configurable : true,
enumerable : true
});
}
function TestEnumerations(o, beforeTestValue, value)
{
telemetryLog("Testing for-in enumeration", true);
for (var i in o)
{
if (beforeTestValue) value = beforeTestValue(o, i, value);
TestValue(o, i, value++);
}
telemetryLog("Testing getOwnPropertyNames enumeration", true);
var names = Object.getOwnPropertyNames(o);
for (var i = 0; i < names.length; i++)
{
if (beforeTestValue) value = beforeTestValue(o, i, value);
TestValue(o, names[i], value++);
}
return value;
}
function TestValue(o, i, value)
{
telemetryLog(i + ": " + o[i], true);
telemetryLog("Setting value to " + value, true);
o[i] = value;
telemetryLog(i + ": " + o[i], true);
}
var th1 = CreateSimpleTypeHandlerObject();
var th2 = CreateSimpleDictionaryTypeHandlerObject();
var th3 = CreateDictionaryTypeHandlerObject();
WScript.SetTimeout(testFunction, 50);
/////////////////
function testFunction()
{
telemetryLog("Test 1: Non-writable, simple type handler", true);
TestNonWritable(th1);
telemetryLog("Test 2: Non-writable, simple dictionary type handler", true);
TestNonWritable(th2);
telemetryLog("Test 3: Non-writable, dictionary type handler", true);
TestNonWritable(th3);
telemetryLog("Test 4: Accessors", true);
TestAccessors();
emitTTDLog(ttdLogURI);
}