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