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