CreateDataProperty.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var DefineOwnProperty = require('../helpers/DefineOwnProperty');
  5. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  6. var OrdinaryGetOwnProperty = require('./OrdinaryGetOwnProperty');
  7. var IsDataDescriptor = require('./IsDataDescriptor');
  8. var IsExtensible = require('./IsExtensible');
  9. var IsPropertyKey = require('./IsPropertyKey');
  10. var SameValue = require('./SameValue');
  11. var Type = require('./Type');
  12. // https://ecma-international.org/ecma-262/6.0/#sec-createdataproperty
  13. module.exports = function CreateDataProperty(O, P, V) {
  14. if (Type(O) !== 'Object') {
  15. throw new $TypeError('Assertion failed: Type(O) is not Object');
  16. }
  17. if (!IsPropertyKey(P)) {
  18. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  19. }
  20. var oldDesc = OrdinaryGetOwnProperty(O, P);
  21. var extensible = !oldDesc || IsExtensible(O);
  22. var nonConfigurable = oldDesc && !oldDesc['[[Configurable]]'];
  23. if (nonConfigurable || !extensible) {
  24. return false;
  25. }
  26. return DefineOwnProperty(
  27. IsDataDescriptor,
  28. SameValue,
  29. FromPropertyDescriptor,
  30. O,
  31. P,
  32. {
  33. '[[Configurable]]': true,
  34. '[[Enumerable]]': true,
  35. '[[Value]]': V,
  36. '[[Writable]]': true
  37. }
  38. );
  39. };