1
|
'use strict';
|
2
|
|
3
|
var GetIntrinsic = require('../GetIntrinsic');
|
4
|
|
5
|
var $gOPD = require('../helpers/getOwnPropertyDescriptor');
|
6
|
var $SyntaxError = GetIntrinsic('%SyntaxError%');
|
7
|
var $TypeError = GetIntrinsic('%TypeError%');
|
8
|
|
9
|
var isPropertyDescriptor = require('../helpers/isPropertyDescriptor');
|
10
|
|
11
|
var IsAccessorDescriptor = require('./IsAccessorDescriptor');
|
12
|
var IsDataDescriptor = require('./IsDataDescriptor');
|
13
|
var IsExtensible = require('./IsExtensible');
|
14
|
var IsPropertyKey = require('./IsPropertyKey');
|
15
|
var ToPropertyDescriptor = require('./ToPropertyDescriptor');
|
16
|
var SameValue = require('./SameValue');
|
17
|
var Type = require('./Type');
|
18
|
var ValidateAndApplyPropertyDescriptor = require('./ValidateAndApplyPropertyDescriptor');
|
19
|
|
20
|
// https://www.ecma-international.org/ecma-262/6.0/#sec-ordinarydefineownproperty
|
21
|
|
22
|
module.exports = function OrdinaryDefineOwnProperty(O, P, Desc) {
|
23
|
if (Type(O) !== 'Object') {
|
24
|
throw new $TypeError('Assertion failed: O must be an Object');
|
25
|
}
|
26
|
if (!IsPropertyKey(P)) {
|
27
|
throw new $TypeError('Assertion failed: P must be a Property Key');
|
28
|
}
|
29
|
if (!isPropertyDescriptor({
|
30
|
Type: Type,
|
31
|
IsDataDescriptor: IsDataDescriptor,
|
32
|
IsAccessorDescriptor: IsAccessorDescriptor
|
33
|
}, Desc)) {
|
34
|
throw new $TypeError('Assertion failed: Desc must be a Property Descriptor');
|
35
|
}
|
36
|
if (!$gOPD) {
|
37
|
// ES3/IE 8 fallback
|
38
|
if (IsAccessorDescriptor(Desc)) {
|
39
|
throw new $SyntaxError('This environment does not support accessor property descriptors.');
|
40
|
}
|
41
|
var creatingNormalDataProperty = !(P in O)
|
42
|
&& Desc['[[Writable]]']
|
43
|
&& Desc['[[Enumerable]]']
|
44
|
&& Desc['[[Configurable]]']
|
45
|
&& '[[Value]]' in Desc;
|
46
|
var settingExistingDataProperty = (P in O)
|
47
|
&& (!('[[Configurable]]' in Desc) || Desc['[[Configurable]]'])
|
48
|
&& (!('[[Enumerable]]' in Desc) || Desc['[[Enumerable]]'])
|
49
|
&& (!('[[Writable]]' in Desc) || Desc['[[Writable]]'])
|
50
|
&& '[[Value]]' in Desc;
|
51
|
if (creatingNormalDataProperty || settingExistingDataProperty) {
|
52
|
O[P] = Desc['[[Value]]']; // eslint-disable-line no-param-reassign
|
53
|
return SameValue(O[P], Desc['[[Value]]']);
|
54
|
}
|
55
|
throw new $SyntaxError('This environment does not support defining non-writable, non-enumerable, or non-configurable properties');
|
56
|
}
|
57
|
var desc = $gOPD(O, P);
|
58
|
var current = desc && ToPropertyDescriptor(desc);
|
59
|
var extensible = IsExtensible(O);
|
60
|
return ValidateAndApplyPropertyDescriptor(O, P, extensible, Desc, current);
|
61
|
};
|