ValidateAndApplyPropertyDescriptor.js 5.6 KB

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