forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpropertyHelper.js
More file actions
510 lines (456 loc) · 18.5 KB
/
Copy pathpropertyHelper.js
File metadata and controls
510 lines (456 loc) · 18.5 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright (C) 2017 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Collection of functions used to safely verify the correctness of
property descriptors.
defines:
- verifyProperty
- verifyCallableProperty
- verifyAccessorProperty
- verifyEqualTo # deprecated
- verifyWritable # deprecated
- verifyNotWritable # deprecated
- verifyEnumerable # deprecated
- verifyNotEnumerable # deprecated
- verifyConfigurable # deprecated
- verifyNotConfigurable # deprecated
- verifyPrimordialProperty
- verifyPrimordialCallableProperty
- verifyPrimordialAccessorProperty
---*/
// @ts-check
// Capture primordial functions and receiver-uncurried primordial methods that
// are used in verification but might be destroyed *by* that process itself.
var __isArray = Array.isArray;
var __defineProperty = Object.defineProperty;
var __getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var __getOwnPropertyNames = Object.getOwnPropertyNames;
var __join = Function.prototype.call.bind(Array.prototype.join);
var __push = Function.prototype.call.bind(Array.prototype.push);
var __hasOwnProperty = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
var __propertyIsEnumerable = Function.prototype.call.bind(Object.prototype.propertyIsEnumerable);
var nonIndexNumericPropertyName = Math.pow(2, 32) - 1;
/**
* @param {object} obj
* @param {string|symbol} name
* @param {PropertyDescriptor|undefined} desc
* @param {object} [options]
* @param {boolean} [options.label]
* @param {boolean} [options.restore] revert mutations from verifying writable/configurable
*/
function verifyProperty(obj, name, desc, options) {
assert(
arguments.length > 2,
'verifyProperty should receive at least 3 arguments: obj, name, and descriptor'
);
var label = options && options.label || String(name);
var originalDesc = __getOwnPropertyDescriptor(obj, name);
// Allows checking for undefined descriptor if it's explicitly given.
if (desc === undefined) {
assert.sameValue(
originalDesc,
undefined,
label + " descriptor should be undefined"
);
// desc and originalDesc are both undefined, problem solved;
return true;
}
assert(__hasOwnProperty(obj, name), label + " should be an own property");
assert.notSameValue(
desc,
null,
"The desc argument should be an object or undefined, null"
);
assert.sameValue(
typeof desc,
"object",
"The desc argument should be an object or undefined, " + String(desc)
);
var names = __getOwnPropertyNames(desc);
for (var i = 0; i < names.length; i++) {
assert(
names[i] === "value" ||
names[i] === "writable" ||
names[i] === "enumerable" ||
names[i] === "configurable" ||
names[i] === "get" ||
names[i] === "set",
"Invalid descriptor field: " + names[i]
);
}
var failures = [];
if (__hasOwnProperty(desc, 'value')) {
if (!isSameValue(desc.value, originalDesc.value)) {
__push(failures, label + " descriptor value should be " + String(desc.value));
}
if (!isSameValue(desc.value, obj[name])) {
__push(failures, label + " value should be " + String(desc.value));
}
}
if (__hasOwnProperty(desc, 'enumerable') && desc.enumerable !== undefined) {
if (desc.enumerable !== originalDesc.enumerable ||
desc.enumerable !== isEnumerable(obj, name)) {
__push(failures, label + " descriptor should " + (desc.enumerable ? '' : 'not ') + "be enumerable");
}
}
// Operations past this point are potentially destructive!
if (__hasOwnProperty(desc, 'writable') && desc.writable !== undefined) {
if (desc.writable !== originalDesc.writable ||
desc.writable !== isWritable(obj, name)) {
__push(failures, label + " descriptor should " + (desc.writable ? '' : 'not ') + "be writable");
}
}
if (__hasOwnProperty(desc, 'configurable') && desc.configurable !== undefined) {
if (desc.configurable !== originalDesc.configurable ||
desc.configurable !== isConfigurable(obj, name)) {
__push(failures, label + " descriptor should " + (desc.configurable ? '' : 'not ') + "be configurable");
}
}
if (failures.length) {
assert(false, __join(failures, '; '));
}
if (options && options.restore) {
__defineProperty(obj, name, originalDesc);
}
return true;
}
function isConfigurable(obj, name) {
try {
delete obj[name];
} catch (e) {
if (!(e instanceof TypeError)) {
throw new Test262Error("Expected TypeError, got " + e);
}
}
return !__hasOwnProperty(obj, name);
}
function isEnumerable(obj, name) {
var stringCheck = false;
if (typeof name === "string") {
for (var x in obj) {
if (x === name) {
stringCheck = true;
break;
}
}
} else {
// skip it if name is not string, works for Symbol names.
stringCheck = true;
}
return stringCheck && __hasOwnProperty(obj, name) && __propertyIsEnumerable(obj, name);
}
function isSameValue(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return a === b;
}
function isWritable(obj, name, verifyProp, value) {
var unlikelyValue = __isArray(obj) && name === "length" ?
nonIndexNumericPropertyName :
"unlikelyValue";
var newValue = value || unlikelyValue;
var hadValue = __hasOwnProperty(obj, name);
var oldValue = obj[name];
var writeSucceeded;
if (arguments.length < 4 && newValue === oldValue) {
newValue = newValue + "2";
}
try {
obj[name] = newValue;
} catch (e) {
if (!(e instanceof TypeError)) {
throw new Test262Error("Expected TypeError, got " + e);
}
}
writeSucceeded = isSameValue(obj[verifyProp || name], newValue);
// Revert the change only if it was successful (in other cases, reverting
// is unnecessary and may trigger exceptions for certain property
// configurations)
if (writeSucceeded) {
if (hadValue) {
obj[name] = oldValue;
} else {
delete obj[name];
}
}
return writeSucceeded;
}
/**
* Verify that there is a function of specified name, length, and containing
* descriptor associated with `obj[name]` and following the conventions for
* built-in objects.
*
* @param {object} obj
* @param {string|symbol} name
* @param {string} [functionName] defaults to name for strings, `[${name.description}]` for symbols
* @param {number} functionLength
* @param {PropertyDescriptor} [desc] defaults to data property conventions (writable, non-enumerable, configurable)
* @param {object} [options]
* @param {boolean} [options.label]
* @param {typeof verifyProperty} [options.verifyProperty]
* @param {boolean} [options.restore] revert mutations from verifying writable/configurable
*/
function verifyCallableProperty(obj, name, functionName, functionLength, desc, options) {
var label = options && options.label || String(name);
var propertyVerifier = options && options.verifyProperty || verifyProperty;
var value = obj && obj[name];
assert.sameValue(typeof value, "function", label + " should be a function");
// Every other data property described in clauses 19 through 28 and in
// Annex B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
// [[Configurable]]: true } unless otherwise specified.
// https://tc39.es/ecma262/multipage/ecmascript-standard-built-in-objects.html
if (desc === undefined) {
desc = {
writable: true,
enumerable: false,
configurable: true,
value: value
};
} else if (!__hasOwnProperty(desc, "value") && !__hasOwnProperty(desc, "get")) {
desc.value = value;
}
propertyVerifier(obj, name, desc, options);
if (functionName === undefined) {
if (typeof name === "symbol") {
functionName = "[" + name.description + "]";
} else {
functionName = name;
}
}
// Unless otherwise specified, the "name" property of a built-in function
// object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
// [[Configurable]]: true }.
// https://tc39.es/ecma262/multipage/ecmascript-standard-built-in-objects.html#sec-ecmascript-standard-built-in-objects
// https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-setfunctionname
propertyVerifier(value, "name", {
value: functionName,
writable: false,
enumerable: false,
configurable: desc.configurable
}, { label: label + " name", restore: options && options.restore });
// Unless otherwise specified, the "length" property of a built-in function
// object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
// [[Configurable]]: true }.
// https://tc39.es/ecma262/multipage/ecmascript-standard-built-in-objects.html#sec-ecmascript-standard-built-in-objects
// https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-setfunctionlength
propertyVerifier(value, "length", {
value: functionLength,
writable: false,
enumerable: false,
configurable: desc.configurable
}, { label: label + " length", restore: options && options.restore });
}
/**
* Verify that there is an accessor property associated with `obj[name]` and
* following the conventions for built-in objects.
*
* @param {object} obj
* @param {string|symbol} name
* @param {object} desc
* @param {boolean} [desc.enumerable] defaults to accessor property conventions (non-enumerable)
* @param {boolean} [desc.configurable] defaults to accessor property conventions (configurable)
* @param {undefined | Function | {name?: string|symbol, length?: number}} [desc.get] if an object,
* absent fields default to getter conventions (name derived from the property key with a "get "
* prefix, length 0)
* @param {undefined | Function | {name?: string|symbol, length?: number}} [desc.set] if an object,
* absent fields default to getter conventions (name derived from the property key with a "set "
* prefix, length 1)
* @param {object} [options]
* @param {boolean} [options.label]
* @param {typeof verifyProperty} [options.verifyProperty]
* @param {typeof verifyCallableProperty} [options.verifyCallableProperty]
* @param {boolean} [options.restore] revert mutations from verifying property attributes
*/
function verifyAccessorProperty(obj, name, desc, options) {
var checkGet = __hasOwnProperty(desc, "get");
var checkSet = __hasOwnProperty(desc, "set");
assert(
checkGet || checkSet,
'verifyAccessorProperty requires at least one of "get" and "set"'
);
var label = options && options.label || String(name);
var propertyVerifier = options && options.verifyProperty || verifyProperty;
var callabilityVerifier = options && options.verifyCallableProperty || verifyCallableProperty;
var originalDesc = __getOwnPropertyDescriptor(obj, name);
// Every built-in function object, including constructors, has a "name"
// property whose value is a String. Unless otherwise specified, this value is
// the name that is given to the function in this specification. Functions
// that are identified as anonymous functions use the empty String as the
// value of the "name" property. For functions that are specified as
// properties of objects, the name value is the property name string used to
// access the function. Functions that are specified as get or set accessor
// functions of built-in properties have "get" or "set" (respectively) passed
// to the prefix parameter when calling CreateBuiltinFunction.
//
// The value of the "name" property is explicitly specified for each built-in
// functions whose property key is a Symbol value. If such an explicitly
// specified value starts with the prefix "get " or "set " and the function
// for which it is specified is a get or set accessor function of a built-in
// property, the value without the prefix is passed to the name parameter, and
// the value "get" or "set" (respectively) is passed to the prefix parameter
// when calling CreateBuiltinFunction.
// https://tc39.es/ecma262/multipage/ecmascript-standard-built-in-objects.html
if (checkGet) {
var expectGetter = desc.get;
var getterLabel = label + " getter";
if (expectGetter === undefined || typeof expectGetter === "function") {
assert.sameValue(originalDesc.get, expectGetter, getterLabel);
} else {
var getterName = expectGetter.name;
if (getterName === undefined) {
getterName = "get " + (typeof name === "symbol" ? "[" + name.description + "]" : name);
}
var getterLength = expectGetter.length !== undefined ? expectGetter.length : 0;
var getterOptions = { label: getterLabel };
callabilityVerifier(originalDesc, "get", getterName, getterLength, {}, getterOptions);
}
}
if (checkSet) {
var expectSetter = desc.set;
var setterLabel = label + " setter";
if (expectSetter === undefined || typeof expectSetter === "function") {
assert.sameValue(originalDesc.set, expectSetter, setterLabel);
} else {
var setterName = expectSetter.name;
if (setterName === undefined) {
setterName = "set " + (typeof name === "symbol" ? "[" + name.description + "]" : name);
}
var setterLength = expectSetter.length !== undefined ? expectSetter.length : 1;
var setterOptions = { label: setterLabel };
callabilityVerifier(originalDesc, "set", setterName, setterLength, {}, setterOptions);
}
}
// Every accessor property described in clauses 19 through 28 and in Annex B.2
// has the attributes { [[Enumerable]]: false, [[Configurable]]: true } unless
// otherwise specified.
// https://tc39.es/ecma262/multipage/ecmascript-standard-built-in-objects.html
var resolvedDesc = { get: originalDesc.get, set: originalDesc.set };
if (!__hasOwnProperty(desc, "enumerable")) {
resolvedDesc.enumerable = false;
} else if (desc.enumerable !== undefined) {
resolvedDesc.enumerable = desc.enumerable;
}
if (!__hasOwnProperty(desc, "configurable")) {
resolvedDesc.configurable = true;
} else if (desc.configurable !== undefined) {
resolvedDesc.configurable = desc.configurable;
}
propertyVerifier(obj, name, resolvedDesc, options);
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyEqualTo(obj, name, value) {
if (!isSameValue(obj[name], value)) {
throw new Test262Error("Expected obj[" + String(name) + "] to equal " + value +
", actually " + obj[name]);
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyWritable(obj, name, verifyProp, value) {
if (!verifyProp) {
assert(__getOwnPropertyDescriptor(obj, name).writable,
"Expected obj[" + String(name) + "] to have writable:true.");
}
if (!isWritable(obj, name, verifyProp, value)) {
throw new Test262Error("Expected obj[" + String(name) + "] to be writable, but was not.");
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyNotWritable(obj, name, verifyProp, value) {
if (!verifyProp) {
assert(!__getOwnPropertyDescriptor(obj, name).writable,
"Expected obj[" + String(name) + "] to have writable:false.");
}
if (isWritable(obj, name, verifyProp)) {
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be writable, but was.");
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyEnumerable(obj, name) {
assert(__getOwnPropertyDescriptor(obj, name).enumerable,
"Expected obj[" + String(name) + "] to have enumerable:true.");
if (!isEnumerable(obj, name)) {
throw new Test262Error("Expected obj[" + String(name) + "] to be enumerable, but was not.");
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyNotEnumerable(obj, name) {
assert(!__getOwnPropertyDescriptor(obj, name).enumerable,
"Expected obj[" + String(name) + "] to have enumerable:false.");
if (isEnumerable(obj, name)) {
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be enumerable, but was.");
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyConfigurable(obj, name) {
assert(__getOwnPropertyDescriptor(obj, name).configurable,
"Expected obj[" + String(name) + "] to have configurable:true.");
if (!isConfigurable(obj, name)) {
throw new Test262Error("Expected obj[" + String(name) + "] to be configurable, but was not.");
}
}
/**
* Deprecated; please use `verifyProperty` in new tests.
*/
function verifyNotConfigurable(obj, name) {
assert(!__getOwnPropertyDescriptor(obj, name).configurable,
"Expected obj[" + String(name) + "] to have configurable:false.");
if (isConfigurable(obj, name)) {
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be configurable, but was.");
}
}
/**
* Use this function to verify the properties of a primordial object.
* For non-primordial objects, use verifyProperty.
* See: https://github.com/tc39/how-we-work/blob/main/terminology.md#primordial
*/
var verifyPrimordialProperty = verifyProperty;
/**
* Use this function to verify the primordial function-valued properties.
* For non-primordial functions, use verifyCallableProperty.
* See: https://github.com/tc39/how-we-work/blob/main/terminology.md#primordial
*
* @type {typeof verifyCallableProperty}
*/
function verifyPrimordialCallableProperty(obj, name, functionName, functionLength, desc, options) {
var resolvedOptions = {
verifyProperty: options && options.verifyProperty !== undefined
? options.verifyProperty
: verifyPrimordialProperty
};
if (options && options.label !== undefined) resolvedOptions.label = options.label;
if (options && options.restore !== undefined) resolvedOptions.restore = options.restore;
return verifyCallableProperty(obj, name, functionName, functionLength, desc, resolvedOptions);
}
/**
* Use this function to verify the primordial accessor properties.
* For non-primordial functions, use verifyAccessorProperty.
* See: https://github.com/tc39/how-we-work/blob/main/terminology.md#primordial
*
* @type {typeof verifyAccessorProperty}
*/
function verifyPrimordialAccessorProperty(obj, name, desc, options) {
var resolvedOptions = {
verifyProperty: options && options.verifyProperty !== undefined
? options.verifyProperty
: verifyPrimordialProperty,
verifyCallableProperty: options && options.verifyCallableProperty !== undefined
? options.verifyCallableProperty
: verifyPrimordialCallableProperty
};
if (options && options.label !== undefined) resolvedOptions.label = options.label;
if (options && options.restore !== undefined) resolvedOptions.restore = options.restore;
return verifyAccessorProperty(obj, name, desc, resolvedOptions);
}