DefinePropertyOrThrow.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isObject = require('es-object-atoms/isObject');
  4. var isPropertyDescriptor = require('../helpers/records/property-descriptor');
  5. var DefineOwnProperty = require('../helpers/DefineOwnProperty');
  6. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  7. var IsDataDescriptor = require('./IsDataDescriptor');
  8. var isPropertyKey = require('../helpers/isPropertyKey');
  9. var SameValue = require('./SameValue');
  10. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  11. // https://262.ecma-international.org/6.0/#sec-definepropertyorthrow
  12. module.exports = function DefinePropertyOrThrow(O, P, desc) {
  13. if (!isObject(O)) {
  14. throw new $TypeError('Assertion failed: Type(O) is not Object');
  15. }
  16. if (!isPropertyKey(P)) {
  17. throw new $TypeError('Assertion failed: P is not a Property Key');
  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. };