forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-bi-array-proto-some.js
More file actions
326 lines (277 loc) · 8.46 KB
/
test-bi-array-proto-some.js
File metadata and controls
326 lines (277 loc) · 8.46 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function retFalse(val, key, obj) {
print(typeof this, this, typeof val, val, typeof key, key, typeof obj, obj);
return false;
}
function test(this_value, args) {
var t;
try {
t = Array.prototype.some.apply(this_value, args);
print(typeof t, t);
} catch (e) {
print(e.name);
}
}
/*===
basic
boolean false
object [object global] number 1 number 0 object 1
boolean false
object [object global] number 1 number 0 object 1,2
object [object global] number 2 number 1 object 1,2
boolean false
object [object global] number 1 number 0 object 1,2,3,4,5
object [object global] number 2 number 1 object 1,2,3,4,5
object [object global] number 3 number 2 object 1,2,3,4,5
object [object global] number 4 number 3 object 1,2,3,4,5
object [object global] number 5 number 4 object 1,2,3,4,5
boolean false
object [object global] number 1 number 0 object 1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3
object [object global] number 2 number 50 object 1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3
object [object global] number 3 number 100 object 1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3
boolean false
object [object global] string foo number 0 object [object Object]
object [object global] string bar number 5 object [object Object]
object [object global] string quux number 20 object [object Object]
boolean false
callback 0
callback 1
callback 2
callback 3
boolean true
callback 1
CallbackError
nonstrict object
boolean true
nonstrict object
boolean true
nonstrict object
boolean true
strict undefined undefined
boolean true
strict object null
boolean true
strict string foo
boolean true
===*/
print('basic');
function basicTest() {
var obj;
var count;
// simple cases
test([], [ retFalse ]);
test([1], [ retFalse ]);
test([1,2], [ retFalse ]);
// dense
test([1,2,3,4,5], [ retFalse ]);
// sparse
obj = [1];
obj[100] = 3;
obj[50] = 2;
test(obj, [ retFalse ]);
// non-array
obj = { '0': 'foo', '5': 'bar', '20': 'quux', '100': 'baz', length: 35 };
test(obj, [ retFalse ]);
// first true terminates; return value is ToBoolean coerced (use ints here)
count = 3;
test([1,2,3,4,5,6,7,8,9,10], [ function(val, key, obj) {
print('callback', key); if (count == 0) { return 1; }; count--; return 0;
}]);
// error in callback propagates outwards
test([1,2,3], [ function(val, key, obj) {
var e;
print('callback', val);
e = new Error('callback error');
e.name = 'CallbackError';
throw e;
}]);
// this binding, non-strict callbacks gets a coerced binding
test([1,2,3], [ function(val, key, obj) {
print('nonstrict', typeof this);
return true;
}]);
test([1,2,3], [ function(val, key, obj) {
print('nonstrict', typeof this);
return true;
}, null]);
test([1,2,3], [ function(val, key, obj) {
print('nonstrict', typeof this);
return true;
}, 'foo']);
test([1,2,3], [ function(val, key, obj) {
'use strict';
print('strict', typeof this, this);
return true;
}]);
test([1,2,3], [ function(val, key, obj) {
'use strict';
print('strict', typeof this, this);
return true;
}, null]); // Note: typeof null -> 'object'
test([1,2,3], [ function(val, key, obj) {
'use strict';
print('strict', typeof this, this);
return true;
}, 'foo']);
}
try {
basicTest();
} catch (e) {
print(e);
}
/*===
mutation
foo 0 foo,bar,quux
bar 1 foo,bar,quux,baz
quux 2 foo,bar,quux,baz
boolean false
foo 0 foo,bar,quux
quux 2 foo,,quux
boolean false
foo 0 [object Object]
bar 1 [object Object]
quux 2 [object Object]
boolean false
foo 0 [object Object]
quux 2 [object Object]
boolean false
===*/
print('mutation');
function mutationTest() {
var obj;
// added element not recognized
obj = [ 'foo', 'bar', 'quux' ];
test(obj, [ function (val, key, obj) {
print(val, key, obj);
obj[3] = 'baz';
return false;
}]);
// deleted element not processed
obj = [ 'foo', 'bar', 'quux' ];
test(obj, [ function (val, key, obj) {
print(val, key, obj);
delete obj[1];
return false;
}]);
// same for non-array
obj = { '0': 'foo', '1': 'bar', '2': 'quux', '3': 'baz', length: 3 };
test(obj, [ function (val, key, obj) {
print(val, key, obj);
obj[4] = 'quuux';
obj.length = 10;
return false;
}]);
obj = { '0': 'foo', '1': 'bar', '2': 'quux', '3': 'baz', length: 3 };
test(obj, [ function (val, key, obj) {
print(val, key, obj);
delete obj[3]; delete obj[1];
obj.length = 0;
return false;
}]);
}
try {
mutationTest();
} catch (e) {
print(e);
}
/*===
coercion
TypeError
TypeError
boolean false
boolean false
boolean false
object [object global] string f number 0 object foo
object [object global] string o number 1 object foo
object [object global] string o number 2 object foo
boolean false
object [object global] number 1 number 0 object 1,2,3
object [object global] number 2 number 1 object 1,2,3
object [object global] number 3 number 2 object 1,2,3
boolean false
boolean false
object [object global] string foo number 0 object [object Object]
object [object global] string bar number 1 object [object Object]
object [object global] string quux number 2 object [object Object]
boolean false
object [object global] string foo number 0 object [object Object]
object [object global] string bar number 1 object [object Object]
object [object global] string quux number 2 object [object Object]
boolean false
object [object global] string foo number 0 object [object Object]
object [object global] string bar number 1 object [object Object]
object [object global] string quux number 2 object [object Object]
object [object global] string baz number 3 object [object Object]
boolean false
length valueOf
object [object global] string foo number 0 object [object Object]
object [object global] string bar number 1 object [object Object]
object [object global] string quux number 2 object [object Object]
boolean false
length valueOf
TypeError
callback 1 0 1,2,3,4,5,6,7,8,9,10
callback 2 1 1,2,3,4,5,6,7,8,9,10
callback 3 2 1,2,3,4,5,6,7,8,9,10
boolean true
===*/
print('coercion');
function coercionTest() {
var obj;
// this
test(undefined, [ retFalse ]);
test(null, [ retFalse ]);
test(true, [ retFalse ]);
test(false, [ retFalse ]);
test(123, [ retFalse ]);
test('foo', [ retFalse ]);
test([1,2,3], [ retFalse ]);
test({ foo: 1, bar: 2 }, [ retFalse ]);
// length
obj = { '0': 'foo', '1': 'bar', '2': 'quux', '3': 'baz', '4': 'quux', length: '3.9' };
test(obj, [ retFalse ]);
obj = { '0': 'foo', '1': 'bar', '2': 'quux', '3': 'baz', '4': 'quux', length: 256*256*256*256 + 3.9 }; // coerces to 3
test(obj, [ retFalse ]);
obj = { '0': 'foo', '1': 'bar', '2': 'quux', '3': 'baz', '4': 'quux', length: -256*256*256*256 + 3.9 }; // coerces to 4
test(obj, [ retFalse ]);
obj = { '0': 'foo', '1': 'bar', '2': 'quux', 'length': {
toString: function() {
print('length toString');
return 4;
},
valueOf: function() {
print('length valueOf');
return 3;
}
}};
test(obj, [ retFalse ]);
// callable check is done after length coercion
obj = { '0': 'foo', '1': 'bar', '2': 'quux', 'length': {
toString: function() {
print('length toString');
return 4;
},
valueOf: function() {
print('length valueOf');
return 3;
}
}};
test(obj, [ null ]);
// ToBoolean of callback return value
test([1,2,3,4,5,6,7,8,9,10], [ function (val, key, obj) {
print('callback', val, key, obj);
if (key == 0) { return 0.0; } /*false*/
else if (key == 1) { return ''; } /*false*/
else if (key == 2) {
// Note: object is always 'true', no coercion related calls are made
return {
toString: function() { print('callback retval toString'); return 0; },
valueOf: function() { print('callback retval valueOf'); return key == 1 ? '' /*false*/ : 'foo' /*true*/; }
};
}
} ]);
}
try {
coercionTest();
} catch (e) {
print(e);
}