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