blob: ff5ca305563fded7cfefd85e24f7a0b65f70156b [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 DefineOwnProperty = require('../helpers/DefineOwnProperty');
8
9var FromPropertyDescriptor = require('./FromPropertyDescriptor');
10var OrdinaryGetOwnProperty = require('./OrdinaryGetOwnProperty');
11var IsDataDescriptor = require('./IsDataDescriptor');
12var IsExtensible = require('./IsExtensible');
13var IsPropertyKey = require('./IsPropertyKey');
14var SameValue = require('./SameValue');
15var Type = require('./Type');
16
17// https://ecma-international.org/ecma-262/6.0/#sec-createdataproperty
18
19module.exports = function CreateDataProperty(O, P, V) {
20 if (Type(O) !== 'Object') {
21 throw new $TypeError('Assertion failed: Type(O) is not Object');
22 }
23 if (!IsPropertyKey(P)) {
24 throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
25 }
26 var oldDesc = OrdinaryGetOwnProperty(O, P);
27 var extensible = !oldDesc || IsExtensible(O);
28 var immutable = oldDesc && (!oldDesc['[[Writable]]'] || !oldDesc['[[Configurable]]']);
29 if (immutable || !extensible) {
30 return false;
31 }
32 return DefineOwnProperty(
33 IsDataDescriptor,
34 SameValue,
35 FromPropertyDescriptor,
36 O,
37 P,
38 {
39 '[[Configurable]]': true,
40 '[[Enumerable]]': true,
41 '[[Value]]': V,
42 '[[Writable]]': true
43 }
44 );
45};