ValidateAndApplyPropertyDescriptor.js 5.6 KB

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