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