| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 | var isObject = require('es-object-atoms/isObject');
|
|---|
| 5 |
|
|---|
| 6 | var DefineOwnProperty = require('../helpers/DefineOwnProperty');
|
|---|
| 7 | var isPropertyDescriptor = require('../helpers/records/property-descriptor');
|
|---|
| 8 | var isSamePropertyDescriptor = require('../helpers/isSamePropertyDescriptor');
|
|---|
| 9 |
|
|---|
| 10 | var FromPropertyDescriptor = require('./FromPropertyDescriptor');
|
|---|
| 11 | var IsAccessorDescriptor = require('./IsAccessorDescriptor');
|
|---|
| 12 | var IsDataDescriptor = require('./IsDataDescriptor');
|
|---|
| 13 | var IsGenericDescriptor = require('./IsGenericDescriptor');
|
|---|
| 14 | var isPropertyKey = require('../helpers/isPropertyKey');
|
|---|
| 15 | var SameValue = require('./SameValue');
|
|---|
| 16 |
|
|---|
| 17 | // https://262.ecma-international.org/6.0/#sec-validateandapplypropertydescriptor
|
|---|
| 18 | // https://262.ecma-international.org/8.0/#sec-validateandapplypropertydescriptor
|
|---|
| 19 |
|
|---|
| 20 | // eslint-disable-next-line max-lines-per-function, max-statements
|
|---|
| 21 | module.exports = function ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current) {
|
|---|
| 22 | // this uses the ES2017+ logic, since it fixes a number of bugs in the ES2015 logic.
|
|---|
| 23 | if (typeof O !== 'undefined' && !isObject(O)) {
|
|---|
| 24 | throw new $TypeError('Assertion failed: O must be undefined or an Object');
|
|---|
| 25 | }
|
|---|
| 26 | if (typeof extensible !== 'boolean') {
|
|---|
| 27 | throw new $TypeError('Assertion failed: extensible must be a Boolean');
|
|---|
| 28 | }
|
|---|
| 29 | if (!isPropertyDescriptor(Desc)) {
|
|---|
| 30 | throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
|
|---|
| 31 | }
|
|---|
| 32 | if (typeof current !== 'undefined' && !isPropertyDescriptor(current)) {
|
|---|
| 33 | throw new $TypeError('Assertion failed: current must be a Property Descriptor, or undefined');
|
|---|
| 34 | }
|
|---|
| 35 | if (typeof O !== 'undefined' && !isPropertyKey(P)) {
|
|---|
| 36 | throw new $TypeError('Assertion failed: if O is not undefined, P must be a Property Key');
|
|---|
| 37 | }
|
|---|
| 38 | if (typeof current === 'undefined') {
|
|---|
| 39 | if (!extensible) {
|
|---|
| 40 | return false;
|
|---|
| 41 | }
|
|---|
| 42 | if (IsGenericDescriptor(Desc) || IsDataDescriptor(Desc)) {
|
|---|
| 43 | if (typeof O !== 'undefined') {
|
|---|
| 44 | DefineOwnProperty(
|
|---|
| 45 | IsDataDescriptor,
|
|---|
| 46 | SameValue,
|
|---|
| 47 | FromPropertyDescriptor,
|
|---|
| 48 | O,
|
|---|
| 49 | P,
|
|---|
| 50 | {
|
|---|
| 51 | '[[Configurable]]': Desc['[[Configurable]]'],
|
|---|
| 52 | '[[Enumerable]]': Desc['[[Enumerable]]'],
|
|---|
| 53 | '[[Value]]': Desc['[[Value]]'],
|
|---|
| 54 | '[[Writable]]': Desc['[[Writable]]']
|
|---|
| 55 | }
|
|---|
| 56 | );
|
|---|
| 57 | }
|
|---|
| 58 | } else {
|
|---|
| 59 | if (!IsAccessorDescriptor(Desc)) {
|
|---|
| 60 | throw new $TypeError('Assertion failed: Desc is not an accessor descriptor');
|
|---|
| 61 | }
|
|---|
| 62 | if (typeof O !== 'undefined') {
|
|---|
| 63 | return DefineOwnProperty(
|
|---|
| 64 | IsDataDescriptor,
|
|---|
| 65 | SameValue,
|
|---|
| 66 | FromPropertyDescriptor,
|
|---|
| 67 | O,
|
|---|
| 68 | P,
|
|---|
| 69 | Desc
|
|---|
| 70 | );
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | return true;
|
|---|
| 74 | }
|
|---|
| 75 | if (IsGenericDescriptor(Desc) && !('[[Configurable]]' in Desc) && !('[[Enumerable]]' in Desc)) {
|
|---|
| 76 | return true;
|
|---|
| 77 | }
|
|---|
| 78 | if (isSamePropertyDescriptor({ SameValue: SameValue }, Desc, current)) {
|
|---|
| 79 | return true; // removed by ES2017, but should still be correct
|
|---|
| 80 | }
|
|---|
| 81 | // "if every field in Desc is absent, return true" can't really match the assertion that it's a Property Descriptor
|
|---|
| 82 | if (!current['[[Configurable]]']) {
|
|---|
| 83 | if (Desc['[[Configurable]]']) {
|
|---|
| 84 | return false;
|
|---|
| 85 | }
|
|---|
| 86 | if ('[[Enumerable]]' in Desc && !Desc['[[Enumerable]]'] === !!current['[[Enumerable]]']) {
|
|---|
| 87 | return false;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | if (IsGenericDescriptor(Desc)) {
|
|---|
| 91 | // no further validation is required.
|
|---|
| 92 | } else if (IsDataDescriptor(current) !== IsDataDescriptor(Desc)) {
|
|---|
| 93 | if (!current['[[Configurable]]']) {
|
|---|
| 94 | return false;
|
|---|
| 95 | }
|
|---|
| 96 | if (IsDataDescriptor(current)) {
|
|---|
| 97 | if (typeof O !== 'undefined') {
|
|---|
| 98 | DefineOwnProperty(
|
|---|
| 99 | IsDataDescriptor,
|
|---|
| 100 | SameValue,
|
|---|
| 101 | FromPropertyDescriptor,
|
|---|
| 102 | O,
|
|---|
| 103 | P,
|
|---|
| 104 | {
|
|---|
| 105 | '[[Configurable]]': current['[[Configurable]]'],
|
|---|
| 106 | '[[Enumerable]]': current['[[Enumerable]]'],
|
|---|
| 107 | '[[Get]]': void undefined
|
|---|
| 108 | }
|
|---|
| 109 | );
|
|---|
| 110 | }
|
|---|
| 111 | } else if (typeof O !== 'undefined') {
|
|---|
| 112 | DefineOwnProperty(
|
|---|
| 113 | IsDataDescriptor,
|
|---|
| 114 | SameValue,
|
|---|
| 115 | FromPropertyDescriptor,
|
|---|
| 116 | O,
|
|---|
| 117 | P,
|
|---|
| 118 | {
|
|---|
| 119 | '[[Configurable]]': current['[[Configurable]]'],
|
|---|
| 120 | '[[Enumerable]]': current['[[Enumerable]]'],
|
|---|
| 121 | '[[Value]]': void undefined
|
|---|
| 122 | }
|
|---|
| 123 | );
|
|---|
| 124 | }
|
|---|
| 125 | } else if (IsDataDescriptor(current) && IsDataDescriptor(Desc)) {
|
|---|
| 126 | if (!current['[[Configurable]]'] && !current['[[Writable]]']) {
|
|---|
| 127 | if ('[[Writable]]' in Desc && Desc['[[Writable]]']) {
|
|---|
| 128 | return false;
|
|---|
| 129 | }
|
|---|
| 130 | if ('[[Value]]' in Desc && !SameValue(Desc['[[Value]]'], current['[[Value]]'])) {
|
|---|
| 131 | return false;
|
|---|
| 132 | }
|
|---|
| 133 | return true;
|
|---|
| 134 | }
|
|---|
| 135 | } else if (IsAccessorDescriptor(current) && IsAccessorDescriptor(Desc)) {
|
|---|
| 136 | if (!current['[[Configurable]]']) {
|
|---|
| 137 | if ('[[Set]]' in Desc && !SameValue(Desc['[[Set]]'], current['[[Set]]'])) {
|
|---|
| 138 | return false;
|
|---|
| 139 | }
|
|---|
| 140 | if ('[[Get]]' in Desc && !SameValue(Desc['[[Get]]'], current['[[Get]]'])) {
|
|---|
| 141 | return false;
|
|---|
| 142 | }
|
|---|
| 143 | return true;
|
|---|
| 144 | }
|
|---|
| 145 | } else {
|
|---|
| 146 | throw new $TypeError('Assertion failed: current and Desc are not both data, both accessors, or one accessor and one data.');
|
|---|
| 147 | }
|
|---|
| 148 | if (typeof O !== 'undefined') {
|
|---|
| 149 | return DefineOwnProperty(
|
|---|
| 150 | IsDataDescriptor,
|
|---|
| 151 | SameValue,
|
|---|
| 152 | FromPropertyDescriptor,
|
|---|
| 153 | O,
|
|---|
| 154 | P,
|
|---|
| 155 | Desc
|
|---|
| 156 | );
|
|---|
| 157 | }
|
|---|
| 158 | return true;
|
|---|
| 159 | };
|
|---|