2017-07-14 17:37:24 +02:00
|
|
|
// 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.
|
2019-09-25 02:22:26 +02:00
|
|
|
defines:
|
|
|
|
- verifyProperty
|
2022-03-26 00:47:14 +01:00
|
|
|
- verifyEqualTo # deprecated
|
|
|
|
- verifyWritable # deprecated
|
|
|
|
- verifyNotWritable # deprecated
|
|
|
|
- verifyEnumerable # deprecated
|
|
|
|
- verifyNotEnumerable # deprecated
|
|
|
|
- verifyConfigurable # deprecated
|
|
|
|
- verifyNotConfigurable # deprecated
|
2024-09-22 02:51:42 +02:00
|
|
|
- verifyPrimordialProperty
|
2017-07-14 17:37:24 +02:00
|
|
|
---*/
|
2014-11-12 14:41:09 +01:00
|
|
|
|
2019-09-18 18:39:12 +02:00
|
|
|
// @ts-check
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
// 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 __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;
|
|
|
|
|
2019-09-18 18:39:12 +02:00
|
|
|
/**
|
|
|
|
* @param {object} obj
|
|
|
|
* @param {string|symbol} name
|
|
|
|
* @param {PropertyDescriptor|undefined} desc
|
|
|
|
* @param {object} [options]
|
|
|
|
* @param {boolean} [options.restore]
|
|
|
|
*/
|
2017-04-15 18:56:29 +02:00
|
|
|
function verifyProperty(obj, name, desc, options) {
|
|
|
|
assert(
|
|
|
|
arguments.length > 2,
|
|
|
|
'verifyProperty should receive at least 3 arguments: obj, name, and descriptor'
|
|
|
|
);
|
|
|
|
|
|
|
|
var originalDesc = Object.getOwnPropertyDescriptor(obj, name);
|
|
|
|
var nameStr = String(name);
|
|
|
|
|
|
|
|
// Allows checking for undefined descriptor if it's explicitly given.
|
|
|
|
if (desc === undefined) {
|
|
|
|
assert.sameValue(
|
|
|
|
originalDesc,
|
|
|
|
undefined,
|
2017-09-08 18:25:44 +02:00
|
|
|
"obj['" + nameStr + "'] descriptor should be undefined"
|
2017-04-15 18:56:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// desc and originalDesc are both undefined, problem solved;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(
|
2024-09-23 18:09:59 +02:00
|
|
|
__hasOwnProperty(obj, name),
|
2017-09-08 18:25:44 +02:00
|
|
|
"obj should have an own property " + nameStr
|
2017-04-15 18:56:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.notSameValue(
|
|
|
|
desc,
|
|
|
|
null,
|
2017-09-08 18:25:44 +02:00
|
|
|
"The desc argument should be an object or undefined, null"
|
2017-04-15 18:56:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.sameValue(
|
|
|
|
typeof desc,
|
|
|
|
"object",
|
2017-09-08 18:25:44 +02:00
|
|
|
"The desc argument should be an object or undefined, " + String(desc)
|
2017-04-15 18:56:29 +02:00
|
|
|
);
|
|
|
|
|
2023-09-22 09:11:45 +02:00
|
|
|
var names = Object.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],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-15 18:56:29 +02:00
|
|
|
var failures = [];
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
if (__hasOwnProperty(desc, 'value')) {
|
2019-06-12 20:14:14 +02:00
|
|
|
if (!isSameValue(desc.value, originalDesc.value)) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__push(failures, "descriptor value should be " + desc.value);
|
2017-06-22 22:45:07 +02:00
|
|
|
}
|
2023-09-11 15:47:59 +02:00
|
|
|
if (!isSameValue(desc.value, obj[name])) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__push(failures, "object value should be " + desc.value);
|
2023-09-11 15:47:59 +02:00
|
|
|
}
|
2017-06-22 22:45:07 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
if (__hasOwnProperty(desc, 'enumerable')) {
|
2017-04-15 18:56:29 +02:00
|
|
|
if (desc.enumerable !== originalDesc.enumerable ||
|
|
|
|
desc.enumerable !== isEnumerable(obj, name)) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__push(failures, 'descriptor should ' + (desc.enumerable ? '' : 'not ') + 'be enumerable');
|
2017-04-15 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
// Operations past this point are potentially destructive!
|
|
|
|
|
|
|
|
if (__hasOwnProperty(desc, 'writable')) {
|
2017-04-15 18:56:29 +02:00
|
|
|
if (desc.writable !== originalDesc.writable ||
|
|
|
|
desc.writable !== isWritable(obj, name)) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__push(failures, 'descriptor should ' + (desc.writable ? '' : 'not ') + 'be writable');
|
2017-04-15 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
if (__hasOwnProperty(desc, 'configurable')) {
|
2017-04-15 18:56:29 +02:00
|
|
|
if (desc.configurable !== originalDesc.configurable ||
|
|
|
|
desc.configurable !== isConfigurable(obj, name)) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__push(failures, 'descriptor should ' + (desc.configurable ? '' : 'not ') + 'be configurable');
|
2017-04-15 18:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
assert(!failures.length, __join(failures, '; '));
|
2017-04-15 18:56:29 +02:00
|
|
|
|
|
|
|
if (options && options.restore) {
|
2024-09-23 18:09:59 +02:00
|
|
|
__defineProperty(obj, name, originalDesc);
|
2017-04-15 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 14:41:09 +01:00
|
|
|
function isConfigurable(obj, name) {
|
2017-04-13 16:37:32 +02:00
|
|
|
try {
|
|
|
|
delete obj[name];
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof TypeError)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected TypeError, got " + e);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2024-09-23 18:09:59 +02:00
|
|
|
return !__hasOwnProperty(obj, name);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isEnumerable(obj, name) {
|
2017-04-15 18:56:29 +02:00
|
|
|
var stringCheck = false;
|
2017-04-13 16:37:32 +02:00
|
|
|
|
|
|
|
if (typeof name === "string") {
|
|
|
|
for (var x in obj) {
|
|
|
|
if (x === name) {
|
2017-03-13 18:48:33 +01:00
|
|
|
stringCheck = true;
|
2017-04-13 16:37:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-03-13 18:48:33 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
} else {
|
|
|
|
// skip it if name is not string, works for Symbol names.
|
|
|
|
stringCheck = true;
|
|
|
|
}
|
|
|
|
|
2024-09-23 18:09:59 +02:00
|
|
|
return stringCheck && __hasOwnProperty(obj, name) && __propertyIsEnumerable(obj, name);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 20:14:14 +02:00
|
|
|
function isSameValue(a, b) {
|
|
|
|
if (a === 0 && b === 0) return 1 / a === 1 / b;
|
|
|
|
if (a !== a && b !== b) return true;
|
2014-11-12 14:41:09 +01:00
|
|
|
|
2019-06-12 20:14:14 +02:00
|
|
|
return a === b;
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:39:49 +01:00
|
|
|
function isWritable(obj, name, verifyProp, value) {
|
2020-03-25 13:45:14 +01:00
|
|
|
var unlikelyValue = __isArray(obj) && name === "length" ?
|
2024-09-23 18:09:59 +02:00
|
|
|
nonIndexNumericPropertyName :
|
2020-03-23 19:44:01 +01:00
|
|
|
"unlikelyValue";
|
2020-03-13 15:17:40 +01:00
|
|
|
var newValue = value || unlikelyValue;
|
2024-09-23 18:09:59 +02:00
|
|
|
var hadValue = __hasOwnProperty(obj, name);
|
2017-04-13 16:37:32 +02:00
|
|
|
var oldValue = obj[name];
|
|
|
|
var writeSucceeded;
|
|
|
|
|
2024-09-23 18:15:23 +02:00
|
|
|
if (arguments.length < 4 && newValue === oldValue) {
|
|
|
|
newValue = newValue + "2";
|
|
|
|
}
|
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
try {
|
|
|
|
obj[name] = newValue;
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof TypeError)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected TypeError, got " + e);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
|
2020-03-23 22:39:49 +01:00
|
|
|
writeSucceeded = isSameValue(obj[verifyProp || name], newValue);
|
2015-03-05 00:38:37 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
// 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) {
|
2017-08-31 20:05:15 +02:00
|
|
|
obj[name] = oldValue;
|
2017-04-13 16:37:32 +02:00
|
|
|
} else {
|
2017-08-31 20:05:15 +02:00
|
|
|
delete obj[name];
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
return writeSucceeded;
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyEqualTo(obj, name, value) {
|
2019-06-12 20:14:14 +02:00
|
|
|
if (!isSameValue(obj[name], value)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] to equal " + value +
|
2017-04-13 16:37:32 +02:00
|
|
|
", actually " + obj[name]);
|
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyWritable(obj, name, verifyProp, value) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (!verifyProp) {
|
|
|
|
assert(Object.getOwnPropertyDescriptor(obj, name).writable,
|
|
|
|
"Expected obj[" + String(name) + "] to have writable:true.");
|
|
|
|
}
|
2020-03-23 22:39:49 +01:00
|
|
|
if (!isWritable(obj, name, verifyProp, value)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] to be writable, but was not.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyNotWritable(obj, name, verifyProp, value) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (!verifyProp) {
|
|
|
|
assert(!Object.getOwnPropertyDescriptor(obj, name).writable,
|
|
|
|
"Expected obj[" + String(name) + "] to have writable:false.");
|
|
|
|
}
|
2020-03-23 22:39:49 +01:00
|
|
|
if (isWritable(obj, name, verifyProp)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be writable, but was.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyEnumerable(obj, name) {
|
2017-04-13 16:37:32 +02:00
|
|
|
assert(Object.getOwnPropertyDescriptor(obj, name).enumerable,
|
|
|
|
"Expected obj[" + String(name) + "] to have enumerable:true.");
|
|
|
|
if (!isEnumerable(obj, name)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] to be enumerable, but was not.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyNotEnumerable(obj, name) {
|
2017-04-13 16:37:32 +02:00
|
|
|
assert(!Object.getOwnPropertyDescriptor(obj, name).enumerable,
|
|
|
|
"Expected obj[" + String(name) + "] to have enumerable:false.");
|
|
|
|
if (isEnumerable(obj, name)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be enumerable, but was.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyConfigurable(obj, name) {
|
2017-04-13 16:37:32 +02:00
|
|
|
assert(Object.getOwnPropertyDescriptor(obj, name).configurable,
|
|
|
|
"Expected obj[" + String(name) + "] to have configurable:true.");
|
|
|
|
if (!isConfigurable(obj, name)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] to be configurable, but was not.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:47:14 +01:00
|
|
|
/**
|
|
|
|
* Deprecated; please use `verifyProperty` in new tests.
|
|
|
|
*/
|
2014-11-12 14:41:09 +01:00
|
|
|
function verifyNotConfigurable(obj, name) {
|
2017-04-13 16:37:32 +02:00
|
|
|
assert(!Object.getOwnPropertyDescriptor(obj, name).configurable,
|
|
|
|
"Expected obj[" + String(name) + "] to have configurable:false.");
|
|
|
|
if (isConfigurable(obj, name)) {
|
2021-07-20 21:17:52 +02:00
|
|
|
throw new Test262Error("Expected obj[" + String(name) + "] NOT to be configurable, but was.");
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
2024-09-22 02:51:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|