DefinePropertyOrThrow.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isPropertyDescriptor = require('../helpers/records/property-descriptor');
  4. var DefineOwnProperty = require('../helpers/DefineOwnProperty');
  5. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  6. var IsDataDescriptor = require('./IsDataDescriptor');
  7. var IsPropertyKey = require('./IsPropertyKey');
  8. var SameValue = require('./SameValue');
  9. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  10. var Type = require('./Type');
  11. // https://262.ecma-international.org/6.0/#sec-definepropertyorthrow
  12. module.exports = function DefinePropertyOrThrow(O, P, desc) {
  13. if (Type(O) !== 'Object') {
  14. throw new $TypeError('Assertion failed: Type(O) is not Object');
  15. }
  16. if (!IsPropertyKey(P)) {
  17. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  18. }
  19. var Desc = isPropertyDescriptor(desc) ? desc : ToPropertyDescriptor(desc);
  20. if (!isPropertyDescriptor(Desc)) {
  21. throw new $TypeError('Assertion failed: Desc is not a valid Property Descriptor');
  22. }
  23. return DefineOwnProperty(
  24. IsDataDescriptor,
  25. SameValue,
  26. FromPropertyDescriptor,
  27. O,
  28. P,
  29. Desc
  30. );
  31. };