blob: ea49e813f23b7ca9d0a6b31ac27cd7a521056be9 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = GetIntrinsic('%TypeError%');
6
7var IsPropertyKey = require('./IsPropertyKey');
8var SameValue = require('./SameValue');
9var Type = require('./Type');
10
11// IE 9 does not throw in strict mode when writability/configurability/extensibility is violated
12var 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
23module.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};