ValidateAndApplyPropertyDescriptor.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var DefineOwnProperty = require('../helpers/DefineOwnProperty');
  5. var isFullyPopulatedPropertyDescriptor = require('../helpers/isFullyPopulatedPropertyDescriptor');
  6. var isPropertyDescriptor = require('../helpers/isPropertyDescriptor');
  7. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  8. var IsAccessorDescriptor = require('./IsAccessorDescriptor');
  9. var IsDataDescriptor = require('./IsDataDescriptor');
  10. var IsGenericDescriptor = require('./IsGenericDescriptor');
  11. var IsPropertyKey = require('./IsPropertyKey');
  12. var SameValue = require('./SameValue');
  13. var Type = require('./Type');
  14. // https://262.ecma-international.org/13.0/#sec-validateandapplypropertydescriptor
  15. // see https://github.com/tc39/ecma262/pull/2468 for ES2022 changes
  16. // eslint-disable-next-line max-lines-per-function, max-statements, max-params
  17. module.exports = function ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current) {
  18. var oType = Type(O);
  19. if (oType !== 'Undefined' && oType !== 'Object') {
  20. throw new $TypeError('Assertion failed: O must be undefined or an Object');
  21. }
  22. if (!IsPropertyKey(P)) {
  23. throw new $TypeError('Assertion failed: P must be a Property Key');
  24. }
  25. if (Type(extensible) !== 'Boolean') {
  26. throw new $TypeError('Assertion failed: extensible must be a Boolean');
  27. }
  28. if (!isPropertyDescriptor({
  29. Type: Type,
  30. IsDataDescriptor: IsDataDescriptor,
  31. IsAccessorDescriptor: IsAccessorDescriptor
  32. }, Desc)) {
  33. throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
  34. }
  35. if (Type(current) !== 'Undefined' && !isPropertyDescriptor({
  36. Type: Type,
  37. IsDataDescriptor: IsDataDescriptor,
  38. IsAccessorDescriptor: IsAccessorDescriptor
  39. }, current)) {
  40. throw new $TypeError('Assertion failed: current must be a Property Descriptor, or undefined');
  41. }
  42. if (Type(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. // 3. Assert: current is a fully populated Property Descriptor.
  75. if (!isFullyPopulatedPropertyDescriptor({
  76. IsAccessorDescriptor: IsAccessorDescriptor,
  77. IsDataDescriptor: IsDataDescriptor
  78. }, current)) {
  79. throw new $TypeError('`current`, when present, must be a fully populated and valid Property Descriptor');
  80. }
  81. // 4. If every field in Desc is absent, return true.
  82. // this can't really match the assertion that it's a Property Descriptor in our JS implementation
  83. // 5. If current.[[Configurable]] is false, then
  84. if (!current['[[Configurable]]']) {
  85. if ('[[Configurable]]' in Desc && Desc['[[Configurable]]']) {
  86. // step 5.a
  87. return false;
  88. }
  89. if ('[[Enumerable]]' in Desc && !SameValue(Desc['[[Enumerable]]'], current['[[Enumerable]]'])) {
  90. // step 5.b
  91. return false;
  92. }
  93. if (!IsGenericDescriptor(Desc) && !SameValue(IsAccessorDescriptor(Desc), IsAccessorDescriptor(current))) {
  94. // step 5.c
  95. return false;
  96. }
  97. if (IsAccessorDescriptor(current)) { // step 5.d
  98. if ('[[Get]]' in Desc && !SameValue(Desc['[[Get]]'], current['[[Get]]'])) {
  99. return false;
  100. }
  101. if ('[[Set]]' in Desc && !SameValue(Desc['[[Set]]'], current['[[Set]]'])) {
  102. return false;
  103. }
  104. } else if (!current['[[Writable]]']) { // step 5.e
  105. if ('[[Writable]]' in Desc && Desc['[[Writable]]']) {
  106. return false;
  107. }
  108. if ('[[Value]]' in Desc && !SameValue(Desc['[[Value]]'], current['[[Value]]'])) {
  109. return false;
  110. }
  111. }
  112. }
  113. // 6. If O is not undefined, then
  114. if (oType !== 'Undefined') {
  115. var configurable;
  116. var enumerable;
  117. if (IsDataDescriptor(current) && IsAccessorDescriptor(Desc)) { // step 6.a
  118. configurable = ('[[Configurable]]' in Desc ? Desc : current)['[[Configurable]]'];
  119. enumerable = ('[[Enumerable]]' in Desc ? Desc : current)['[[Enumerable]]'];
  120. // 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.
  121. return DefineOwnProperty(
  122. IsDataDescriptor,
  123. SameValue,
  124. FromPropertyDescriptor,
  125. O,
  126. P,
  127. {
  128. '[[Configurable]]': !!configurable,
  129. '[[Enumerable]]': !!enumerable,
  130. '[[Get]]': ('[[Get]]' in Desc ? Desc : current)['[[Get]]'],
  131. '[[Set]]': ('[[Set]]' in Desc ? Desc : current)['[[Set]]']
  132. }
  133. );
  134. } else if (IsAccessorDescriptor(current) && IsDataDescriptor(Desc)) {
  135. configurable = ('[[Configurable]]' in Desc ? Desc : current)['[[Configurable]]'];
  136. enumerable = ('[[Enumerable]]' in Desc ? Desc : current)['[[Enumerable]]'];
  137. // 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.
  138. return DefineOwnProperty(
  139. IsDataDescriptor,
  140. SameValue,
  141. FromPropertyDescriptor,
  142. O,
  143. P,
  144. {
  145. '[[Configurable]]': !!configurable,
  146. '[[Enumerable]]': !!enumerable,
  147. '[[Value]]': ('[[Value]]' in Desc ? Desc : current)['[[Value]]'],
  148. '[[Writable]]': !!('[[Writable]]' in Desc ? Desc : current)['[[Writable]]']
  149. }
  150. );
  151. }
  152. // 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.
  153. return DefineOwnProperty(
  154. IsDataDescriptor,
  155. SameValue,
  156. FromPropertyDescriptor,
  157. O,
  158. P,
  159. Desc
  160. );
  161. }
  162. return true; // step 7
  163. };