1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var call = require('../internals/function-call');
|
---|
4 | var anObject = require('../internals/an-object');
|
---|
5 | var isObject = require('../internals/is-object');
|
---|
6 | var isDataDescriptor = require('../internals/is-data-descriptor');
|
---|
7 | var fails = require('../internals/fails');
|
---|
8 | var definePropertyModule = require('../internals/object-define-property');
|
---|
9 | var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
|
---|
10 | var getPrototypeOf = require('../internals/object-get-prototype-of');
|
---|
11 | var createPropertyDescriptor = require('../internals/create-property-descriptor');
|
---|
12 |
|
---|
13 | // `Reflect.set` method
|
---|
14 | // https://tc39.es/ecma262/#sec-reflect.set
|
---|
15 | function set(target, propertyKey, V /* , receiver */) {
|
---|
16 | var receiver = arguments.length < 4 ? target : arguments[3];
|
---|
17 | var ownDescriptor = getOwnPropertyDescriptorModule.f(anObject(target), propertyKey);
|
---|
18 | var existingDescriptor, prototype, setter;
|
---|
19 | if (!ownDescriptor) {
|
---|
20 | if (isObject(prototype = getPrototypeOf(target))) {
|
---|
21 | return set(prototype, propertyKey, V, receiver);
|
---|
22 | }
|
---|
23 | ownDescriptor = createPropertyDescriptor(0);
|
---|
24 | }
|
---|
25 | if (isDataDescriptor(ownDescriptor)) {
|
---|
26 | if (ownDescriptor.writable === false || !isObject(receiver)) return false;
|
---|
27 | if (existingDescriptor = getOwnPropertyDescriptorModule.f(receiver, propertyKey)) {
|
---|
28 | if (existingDescriptor.get || existingDescriptor.set || existingDescriptor.writable === false) return false;
|
---|
29 | existingDescriptor.value = V;
|
---|
30 | definePropertyModule.f(receiver, propertyKey, existingDescriptor);
|
---|
31 | } else definePropertyModule.f(receiver, propertyKey, createPropertyDescriptor(0, V));
|
---|
32 | } else {
|
---|
33 | setter = ownDescriptor.set;
|
---|
34 | if (setter === undefined) return false;
|
---|
35 | call(setter, receiver, V);
|
---|
36 | } return true;
|
---|
37 | }
|
---|
38 |
|
---|
39 | // MS Edge 17-18 Reflect.set allows setting the property to object
|
---|
40 | // with non-writable property on the prototype
|
---|
41 | var MS_EDGE_BUG = fails(function () {
|
---|
42 | var Constructor = function () { /* empty */ };
|
---|
43 | var object = definePropertyModule.f(new Constructor(), 'a', { configurable: true });
|
---|
44 | // eslint-disable-next-line es/no-reflect -- required for testing
|
---|
45 | return Reflect.set(Constructor.prototype, 'a', 1, object) !== false;
|
---|
46 | });
|
---|
47 |
|
---|
48 | $({ target: 'Reflect', stat: true, forced: MS_EDGE_BUG }, {
|
---|
49 | set: set
|
---|
50 | });
|
---|