[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var DefineOwnProperty = require('../helpers/DefineOwnProperty');
|
---|
| 6 | var isFullyPopulatedPropertyDescriptor = require('../helpers/isFullyPopulatedPropertyDescriptor');
|
---|
| 7 | var isPropertyDescriptor = require('../helpers/records/property-descriptor');
|
---|
| 8 |
|
---|
| 9 | var FromPropertyDescriptor = require('./FromPropertyDescriptor');
|
---|
| 10 | var IsAccessorDescriptor = require('./IsAccessorDescriptor');
|
---|
| 11 | var IsDataDescriptor = require('./IsDataDescriptor');
|
---|
| 12 | var IsGenericDescriptor = require('./IsGenericDescriptor');
|
---|
[79a0317] | 13 | var isPropertyKey = require('../helpers/isPropertyKey');
|
---|
[d565449] | 14 | var SameValue = require('./SameValue');
|
---|
| 15 | var Type = require('./Type');
|
---|
| 16 |
|
---|
[79a0317] | 17 | var isObject = require('../helpers/isObject');
|
---|
| 18 |
|
---|
[d565449] | 19 | // https://262.ecma-international.org/13.0/#sec-validateandapplypropertydescriptor
|
---|
| 20 |
|
---|
| 21 | // see https://github.com/tc39/ecma262/pull/2468 for ES2022 changes
|
---|
| 22 |
|
---|
| 23 | // eslint-disable-next-line max-lines-per-function, max-statements
|
---|
| 24 | module.exports = function ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current) {
|
---|
| 25 | var oType = Type(O);
|
---|
[79a0317] | 26 | if (typeof O !== 'undefined' && !isObject(O)) {
|
---|
[d565449] | 27 | throw new $TypeError('Assertion failed: O must be undefined or an Object');
|
---|
| 28 | }
|
---|
[79a0317] | 29 | if (!isPropertyKey(P)) {
|
---|
[d565449] | 30 | throw new $TypeError('Assertion failed: P must be a Property Key');
|
---|
| 31 | }
|
---|
| 32 | if (typeof extensible !== 'boolean') {
|
---|
| 33 | throw new $TypeError('Assertion failed: extensible must be a Boolean');
|
---|
| 34 | }
|
---|
| 35 | if (!isPropertyDescriptor(Desc)) {
|
---|
| 36 | throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
|
---|
| 37 | }
|
---|
| 38 | if (typeof current !== 'undefined' && !isPropertyDescriptor(current)) {
|
---|
| 39 | throw new $TypeError('Assertion failed: current must be a Property Descriptor, or undefined');
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if (typeof current === 'undefined') { // step 2
|
---|
| 43 | if (!extensible) {
|
---|
| 44 | return false; // step 2.a
|
---|
| 45 | }
|
---|
| 46 | if (oType === 'Undefined') {
|
---|
| 47 | return true; // step 2.b
|
---|
| 48 | }
|
---|
| 49 | if (IsAccessorDescriptor(Desc)) { // step 2.c
|
---|
| 50 | return DefineOwnProperty(
|
---|
| 51 | IsDataDescriptor,
|
---|
| 52 | SameValue,
|
---|
| 53 | FromPropertyDescriptor,
|
---|
| 54 | O,
|
---|
| 55 | P,
|
---|
| 56 | Desc
|
---|
| 57 | );
|
---|
| 58 | }
|
---|
| 59 | // step 2.d
|
---|
| 60 | return DefineOwnProperty(
|
---|
| 61 | IsDataDescriptor,
|
---|
| 62 | SameValue,
|
---|
| 63 | FromPropertyDescriptor,
|
---|
| 64 | O,
|
---|
| 65 | P,
|
---|
| 66 | {
|
---|
| 67 | '[[Configurable]]': !!Desc['[[Configurable]]'],
|
---|
| 68 | '[[Enumerable]]': !!Desc['[[Enumerable]]'],
|
---|
| 69 | '[[Value]]': Desc['[[Value]]'],
|
---|
| 70 | '[[Writable]]': !!Desc['[[Writable]]']
|
---|
| 71 | }
|
---|
| 72 | );
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | // 3. Assert: current is a fully populated Property Descriptor.
|
---|
| 76 | if (
|
---|
| 77 | !isFullyPopulatedPropertyDescriptor(
|
---|
| 78 | {
|
---|
| 79 | IsAccessorDescriptor: IsAccessorDescriptor,
|
---|
| 80 | IsDataDescriptor: IsDataDescriptor
|
---|
| 81 | },
|
---|
| 82 | current
|
---|
| 83 | )
|
---|
| 84 | ) {
|
---|
| 85 | throw new $TypeError('`current`, when present, must be a fully populated and valid Property Descriptor');
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // 4. If every field in Desc is absent, return true.
|
---|
| 89 | // this can't really match the assertion that it's a Property Descriptor in our JS implementation
|
---|
| 90 |
|
---|
| 91 | // 5. If current.[[Configurable]] is false, then
|
---|
| 92 | if (!current['[[Configurable]]']) {
|
---|
| 93 | if ('[[Configurable]]' in Desc && Desc['[[Configurable]]']) {
|
---|
| 94 | // step 5.a
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 | if ('[[Enumerable]]' in Desc && !SameValue(Desc['[[Enumerable]]'], current['[[Enumerable]]'])) {
|
---|
| 98 | // step 5.b
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 | if (!IsGenericDescriptor(Desc) && !SameValue(IsAccessorDescriptor(Desc), IsAccessorDescriptor(current))) {
|
---|
| 102 | // step 5.c
|
---|
| 103 | return false;
|
---|
| 104 | }
|
---|
| 105 | if (IsAccessorDescriptor(current)) { // step 5.d
|
---|
| 106 | if ('[[Get]]' in Desc && !SameValue(Desc['[[Get]]'], current['[[Get]]'])) {
|
---|
| 107 | return false;
|
---|
| 108 | }
|
---|
| 109 | if ('[[Set]]' in Desc && !SameValue(Desc['[[Set]]'], current['[[Set]]'])) {
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 | } else if (!current['[[Writable]]']) { // step 5.e
|
---|
| 113 | if ('[[Writable]]' in Desc && Desc['[[Writable]]']) {
|
---|
| 114 | return false;
|
---|
| 115 | }
|
---|
| 116 | if ('[[Value]]' in Desc && !SameValue(Desc['[[Value]]'], current['[[Value]]'])) {
|
---|
| 117 | return false;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | // 6. If O is not undefined, then
|
---|
| 123 | if (oType !== 'Undefined') {
|
---|
| 124 | var configurable;
|
---|
| 125 | var enumerable;
|
---|
| 126 | if (IsDataDescriptor(current) && IsAccessorDescriptor(Desc)) { // step 6.a
|
---|
| 127 | configurable = ('[[Configurable]]' in Desc ? Desc : current)['[[Configurable]]'];
|
---|
| 128 | enumerable = ('[[Enumerable]]' in Desc ? Desc : current)['[[Enumerable]]'];
|
---|
| 129 | // Replace the property named P of object O with an accessor property having [[Configurable]] and [[Enumerable]] attributes as described by current and each other attribute set to its default value.
|
---|
| 130 | return DefineOwnProperty(
|
---|
| 131 | IsDataDescriptor,
|
---|
| 132 | SameValue,
|
---|
| 133 | FromPropertyDescriptor,
|
---|
| 134 | O,
|
---|
| 135 | P,
|
---|
| 136 | {
|
---|
| 137 | '[[Configurable]]': !!configurable,
|
---|
| 138 | '[[Enumerable]]': !!enumerable,
|
---|
| 139 | '[[Get]]': ('[[Get]]' in Desc ? Desc : current)['[[Get]]'],
|
---|
| 140 | '[[Set]]': ('[[Set]]' in Desc ? Desc : current)['[[Set]]']
|
---|
| 141 | }
|
---|
| 142 | );
|
---|
| 143 | } else if (IsAccessorDescriptor(current) && IsDataDescriptor(Desc)) {
|
---|
| 144 | configurable = ('[[Configurable]]' in Desc ? Desc : current)['[[Configurable]]'];
|
---|
| 145 | enumerable = ('[[Enumerable]]' in Desc ? Desc : current)['[[Enumerable]]'];
|
---|
| 146 | // i. Replace the property named P of object O with a data property having [[Configurable]] and [[Enumerable]] attributes as described by current and each other attribute set to its default value.
|
---|
| 147 | return DefineOwnProperty(
|
---|
| 148 | IsDataDescriptor,
|
---|
| 149 | SameValue,
|
---|
| 150 | FromPropertyDescriptor,
|
---|
| 151 | O,
|
---|
| 152 | P,
|
---|
| 153 | {
|
---|
| 154 | '[[Configurable]]': !!configurable,
|
---|
| 155 | '[[Enumerable]]': !!enumerable,
|
---|
| 156 | '[[Value]]': ('[[Value]]' in Desc ? Desc : current)['[[Value]]'],
|
---|
| 157 | '[[Writable]]': !!('[[Writable]]' in Desc ? Desc : current)['[[Writable]]']
|
---|
| 158 | }
|
---|
| 159 | );
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | // For each field of Desc that is present, set the corresponding attribute of the property named P of object O to the value of the field.
|
---|
| 163 | return DefineOwnProperty(
|
---|
| 164 | IsDataDescriptor,
|
---|
| 165 | SameValue,
|
---|
| 166 | FromPropertyDescriptor,
|
---|
| 167 | O,
|
---|
| 168 | P,
|
---|
| 169 | Desc
|
---|
| 170 | );
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | return true; // step 7
|
---|
| 174 | };
|
---|