source: imaps-frontend/node_modules/es-abstract/2017/ValidateAndApplyPropertyDescriptor.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.9 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var DefineOwnProperty = require('../helpers/DefineOwnProperty');
6var isPropertyDescriptor = require('../helpers/records/property-descriptor');
7var isSamePropertyDescriptor = require('../helpers/isSamePropertyDescriptor');
8
9var FromPropertyDescriptor = require('./FromPropertyDescriptor');
10var IsAccessorDescriptor = require('./IsAccessorDescriptor');
11var IsDataDescriptor = require('./IsDataDescriptor');
12var IsGenericDescriptor = require('./IsGenericDescriptor');
13var IsPropertyKey = require('./IsPropertyKey');
14var SameValue = require('./SameValue');
15var Type = require('./Type');
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
21module.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 }
27 if (typeof extensible !== 'boolean') {
28 throw new $TypeError('Assertion failed: extensible must be a Boolean');
29 }
30 if (!isPropertyDescriptor(Desc)) {
31 throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
32 }
33 if (typeof current !== 'undefined' && !isPropertyDescriptor(current)) {
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 }
39 if (typeof current === 'undefined') {
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};
Note: See TracBrowser for help on using the repository browser.