| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 6 | |
| 7 | var IsPropertyKey = require('./IsPropertyKey'); |
| 8 | var SameValue = require('./SameValue'); |
| 9 | var Type = require('./Type'); |
| 10 | |
| 11 | // IE 9 does not throw in strict mode when writability/configurability/extensibility is violated |
| 12 | var noThrowOnStrictViolation = (function () { |
| 13 | try { |
| 14 | delete [].length; |
| 15 | return true; |
| 16 | } catch (e) { |
| 17 | return false; |
| 18 | } |
| 19 | }()); |
| 20 | |
| 21 | // https://ecma-international.org/ecma-262/6.0/#sec-set-o-p-v-throw |
| 22 | |
| 23 | module.exports = function Set(O, P, V, Throw) { |
| 24 | if (Type(O) !== 'Object') { |
| 25 | throw new $TypeError('Assertion failed: `O` must be an Object'); |
| 26 | } |
| 27 | if (!IsPropertyKey(P)) { |
| 28 | throw new $TypeError('Assertion failed: `P` must be a Property Key'); |
| 29 | } |
| 30 | if (Type(Throw) !== 'Boolean') { |
| 31 | throw new $TypeError('Assertion failed: `Throw` must be a Boolean'); |
| 32 | } |
| 33 | if (Throw) { |
| 34 | O[P] = V; // eslint-disable-line no-param-reassign |
| 35 | if (noThrowOnStrictViolation && !SameValue(O[P], V)) { |
| 36 | throw new $TypeError('Attempted to assign to readonly property.'); |
| 37 | } |
| 38 | return true; |
| 39 | } else { |
| 40 | try { |
| 41 | O[P] = V; // eslint-disable-line no-param-reassign |
| 42 | return noThrowOnStrictViolation ? SameValue(O[P], V) : true; |
| 43 | } catch (e) { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | }; |