ValidateAndApplyPropertyDescriptor.js 5.6 KB

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