-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathenumerable.js
135 lines (116 loc) · 3.92 KB
/
enumerable.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// dump obj through for-in enumerator
function enumObj(obj, lines) {
for (var p in obj) {
lines.push(" " + p + ": " + obj[p]);
}
}
// dump obj through for-in enumerator + verify
function enumAndVerifyObj(obj, lines) {
var ctr = 0;
for (var p in obj) {
var thisl = " " + p + ": " + obj[p];
if(lines[ctr] !== thisl) {
telemetryLog(`Failed on ${lines[ctr]} !== ${thisl}`, true);
}
ctr++;
}
}
// add a bunch of data/attribute properties with different attributes
function addProp(o, prefix) {
Object.defineProperty(o, prefix + "10", {
value: "value 10"
});
Object.defineProperty(o, prefix + "11", {
value: "value 11",
enumerable: true
});
Object.defineProperty(o, prefix + "12", {
value: "value 12",
enumerable: true,
configurable: true
});
Object.defineProperty(o, prefix + "13", {
value: "value 13",
enumerable: true,
configurable: true,
writable: true
});
Object.defineProperty(o, prefix + "20", {
get: function() { return "get 20"; },
});
Object.defineProperty(o, prefix + "21", {
get: function () { return "get 21"; },
enumerable: true,
});
Object.defineProperty(o, prefix + "22", {
get: function () { return "get 22"; },
enumerable: true,
configurable: true
});
Object.defineProperty(o, prefix + "25", {
set: function() { echo("do not call 25"); },
});
Object.defineProperty(o, prefix + "26", {
set: function() { echo("do not call 26"); },
enumerable: true,
});
Object.defineProperty(o, prefix + "27", {
set: function() { echo("do not call 27"); },
enumerable: true,
configurable: true
});
}
function testWithObj(o, lines) {
addProp(o, "xx");
addProp(o, "1");
enumObj(o, lines);
}
var s1 = {abc: -12, def: "hello", 1: undefined, 3: null};
var l1 = [];
testWithObj(s1, l1);
var s2 = [-12, "hello", undefined, null];
var l2 = [];
testWithObj(s2, l2);
// Test Object.defineProperties, Object.create
function testPrototype(proto) {
Object.defineProperties(proto, {
name: { value: "SHOULD_NOT_enumerate_prototype" },
0: { get: function() { return "get 0"; } },
3: { value: 3 },
1: { get: function() { return "get 1"; }, enumerable: true },
5: { value: 5, enumerable: true },
2: { get: function() { return this.name; }, enumerable: true },
});
return Object.create(proto, {
name: { value: "correct_original_instance" },
10: { get: function() { return "get 10"; } },
13: { value: 13 },
11: { get: function() { return "get 11"; }, enumerable: true },
15: { value: 15, enumerable: true },
12: { get: function() { return this.name; }, enumerable: true },
});
}
var s3 = testPrototype({});
var l3 = [];
testWithObj(s3, l3);
var s4 = testPrototype([]);
var l4 = [];
testWithObj(s4, l4);
WScript.SetTimeout(testFunction, 50);
/////////////////
function testFunction()
{
telemetryLog('Testing obj enumeration', true);
enumAndVerifyObj(s1, l1);
telemetryLog('Testing array enumeration', true);
enumAndVerifyObj(s2, l2);
telemetryLog('Testing obj proto enumeration', true);
enumAndVerifyObj(s3, l3);
telemetryLog('Testing array proto enumeration', true);
enumAndVerifyObj(s4, l4);
emitTTDLog(ttdLogURI);
}