blob: 0ec0a3cdda8b84e86fc8c21b2d8a5a181a20e523 [file] [log] [blame]
Tim van der Lippe16aca392020-11-13 11:37:131'use strict';
2
Nikolay Vitkovd76576c2024-12-02 14:10:153var $TypeError = require('es-errors/type');
Tim van der Lippe16aca392020-11-13 11:37:134
5var IsPropertyKey = require('./IsPropertyKey');
6var SameValue = require('./SameValue');
7var Type = require('./Type');
8
9// IE 9 does not throw in strict mode when writability/configurability/extensibility is violated
10var noThrowOnStrictViolation = (function () {
11 try {
12 delete [].length;
13 return true;
14 } catch (e) {
15 return false;
16 }
17}());
18
Nikolay Vitkovd76576c2024-12-02 14:10:1519// https://262.ecma-international.org/6.0/#sec-set-o-p-v-throw
Tim van der Lippe16aca392020-11-13 11:37:1320
21module.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 Vitkovd76576c2024-12-02 14:10:1528 if (typeof Throw !== 'boolean') {
Tim van der Lippe16aca392020-11-13 11:37:1329 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 Lippe16aca392020-11-13 11:37:1337 }
Tim van der Lippebc3a0b72021-11-08 15:22:3738 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 Lippe16aca392020-11-13 11:37:1345};