DefinePropertyOrThrow.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var isPropertyDescriptor = require('../helpers/isPropertyDescriptor');
  5. var DefineOwnProperty = require('../helpers/DefineOwnProperty');
  6. var FromPropertyDescriptor = require('./FromPropertyDescriptor');
  7. var IsAccessorDescriptor = require('./IsAccessorDescriptor');
  8. var IsDataDescriptor = require('./IsDataDescriptor');
  9. var IsPropertyKey = require('./IsPropertyKey');
  10. var SameValue = require('./SameValue');
  11. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  12. var Type = require('./Type');
  13. // https://262.ecma-international.org/6.0/#sec-definepropertyorthrow
  14. module.exports = function DefinePropertyOrThrow(O, P, desc) {
  15. if (Type(O) !== 'Object') {
  16. throw new $TypeError('Assertion failed: Type(O) is not Object');
  17. }
  18. if (!IsPropertyKey(P)) {
  19. throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
  20. }
  21. var Desc = isPropertyDescriptor({
  22. Type: Type,
  23. IsDataDescriptor: IsDataDescriptor,
  24. IsAccessorDescriptor: IsAccessorDescriptor
  25. }, desc) ? desc : ToPropertyDescriptor(desc);
  26. if (!isPropertyDescriptor({
  27. Type: Type,
  28. IsDataDescriptor: IsDataDescriptor,
  29. IsAccessorDescriptor: IsAccessorDescriptor
  30. }, Desc)) {
  31. throw new $TypeError('Assertion failed: Desc is not a valid Property Descriptor');
  32. }
  33. return DefineOwnProperty(
  34. IsDataDescriptor,
  35. SameValue,
  36. FromPropertyDescriptor,
  37. O,
  38. P,
  39. Desc
  40. );
  41. };