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