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.
|
|
|
|
---*/
|
2014-11-12 14:41:09 +01:00
|
|
|
|
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,
|
|
|
|
`obj['${nameStr}'] descriptor should be undefined`
|
|
|
|
);
|
|
|
|
|
|
|
|
// desc and originalDesc are both undefined, problem solved;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(
|
|
|
|
Object.prototype.hasOwnProperty.call(obj, name),
|
|
|
|
`obj should have an own property ${nameStr}`
|
|
|
|
);
|
|
|
|
|
|
|
|
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 failures = [];
|
|
|
|
|
2017-06-22 22:45:07 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(desc, 'value')) {
|
|
|
|
if (desc.value !== originalDesc.value) {
|
|
|
|
failures.push(`descriptor value should be ${desc.value}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 18:56:29 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(desc, 'enumerable')) {
|
|
|
|
if (desc.enumerable !== originalDesc.enumerable ||
|
|
|
|
desc.enumerable !== isEnumerable(obj, name)) {
|
|
|
|
failures.push(`descriptor should ${desc.enumerable ? '' : 'not '}be enumerable`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(desc, 'writable')) {
|
|
|
|
if (desc.writable !== originalDesc.writable ||
|
|
|
|
desc.writable !== isWritable(obj, name)) {
|
|
|
|
failures.push(`descriptor should ${desc.writable ? '' : 'not '}be writable`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(desc, 'configurable')) {
|
|
|
|
if (desc.configurable !== originalDesc.configurable ||
|
|
|
|
desc.configurable !== isConfigurable(obj, name)) {
|
|
|
|
failures.push(`descriptor should ${desc.configurable ? '' : 'not '}be configurable`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.sameValue(failures.length, 0, failures.join('; '));
|
|
|
|
|
|
|
|
if (options && options.restore) {
|
|
|
|
Object.defineProperty(obj, name, originalDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
$ERROR("Expected TypeError, got " + e);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
2017-04-13 16:37:32 +02:00
|
|
|
}
|
|
|
|
return !Object.prototype.hasOwnProperty.call(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringCheck &&
|
|
|
|
Object.prototype.hasOwnProperty.call(obj, name) &&
|
|
|
|
Object.prototype.propertyIsEnumerable.call(obj, name);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isEqualTo(obj, name, expectedValue) {
|
2017-04-13 16:37:32 +02:00
|
|
|
var actualValue = obj[name];
|
2014-11-12 14:41:09 +01:00
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
return assert._isSameValue(actualValue, expectedValue);
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isWritable(obj, name, verifyProp, value) {
|
2017-04-13 16:37:32 +02:00
|
|
|
var newValue = value || "unlikelyValue";
|
|
|
|
var hadValue = Object.prototype.hasOwnProperty.call(obj, name);
|
|
|
|
var oldValue = obj[name];
|
|
|
|
var writeSucceeded;
|
|
|
|
|
|
|
|
try {
|
|
|
|
obj[name] = newValue;
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e instanceof TypeError)) {
|
|
|
|
$ERROR("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
|
|
|
|
2017-04-13 16:37:32 +02:00
|
|
|
writeSucceeded = isEqualTo(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) {
|
|
|
|
obj[name] = oldValue;
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function verifyEqualTo(obj, name, value) {
|
2017-04-13 16:37:32 +02:00
|
|
|
if (!isEqualTo(obj, name, value)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] to equal " + value +
|
|
|
|
", actually " + obj[name]);
|
|
|
|
}
|
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.");
|
|
|
|
}
|
|
|
|
if (!isWritable(obj, name, verifyProp, value)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] to be writable, but was not.");
|
|
|
|
}
|
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.");
|
|
|
|
}
|
|
|
|
if (isWritable(obj, name, verifyProp)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] NOT to be writable, but was.");
|
|
|
|
}
|
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)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] to be enumerable, but was not.");
|
|
|
|
}
|
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)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] NOT to be enumerable, but was.");
|
|
|
|
}
|
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)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] to be configurable, but was not.");
|
|
|
|
}
|
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)) {
|
|
|
|
$ERROR("Expected obj[" + String(name) + "] NOT to be configurable, but was.");
|
|
|
|
}
|
2014-11-12 14:41:09 +01:00
|
|
|
}
|