1 | 'use strict';
|
---|
2 |
|
---|
3 | var hasOwn = require('hasown');
|
---|
4 |
|
---|
5 | var $TypeError = require('es-errors/type');
|
---|
6 |
|
---|
7 | var IsCallable = require('./IsCallable');
|
---|
8 | var ToBoolean = require('./ToBoolean');
|
---|
9 |
|
---|
10 | var isObject = require('../helpers/isObject');
|
---|
11 |
|
---|
12 | // https://262.ecma-international.org/5.1/#sec-8.10.5
|
---|
13 |
|
---|
14 | module.exports = function ToPropertyDescriptor(Obj) {
|
---|
15 | if (!isObject(Obj)) {
|
---|
16 | throw new $TypeError('ToPropertyDescriptor requires an object');
|
---|
17 | }
|
---|
18 |
|
---|
19 | var desc = {};
|
---|
20 | if (hasOwn(Obj, 'enumerable')) {
|
---|
21 | desc['[[Enumerable]]'] = ToBoolean(Obj.enumerable);
|
---|
22 | }
|
---|
23 | if (hasOwn(Obj, 'configurable')) {
|
---|
24 | desc['[[Configurable]]'] = ToBoolean(Obj.configurable);
|
---|
25 | }
|
---|
26 | if (hasOwn(Obj, 'value')) {
|
---|
27 | desc['[[Value]]'] = Obj.value;
|
---|
28 | }
|
---|
29 | if (hasOwn(Obj, 'writable')) {
|
---|
30 | desc['[[Writable]]'] = ToBoolean(Obj.writable);
|
---|
31 | }
|
---|
32 | if (hasOwn(Obj, 'get')) {
|
---|
33 | var getter = Obj.get;
|
---|
34 | if (typeof getter !== 'undefined' && !IsCallable(getter)) {
|
---|
35 | throw new $TypeError('getter must be a function');
|
---|
36 | }
|
---|
37 | desc['[[Get]]'] = getter;
|
---|
38 | }
|
---|
39 | if (hasOwn(Obj, 'set')) {
|
---|
40 | var setter = Obj.set;
|
---|
41 | if (typeof setter !== 'undefined' && !IsCallable(setter)) {
|
---|
42 | throw new $TypeError('setter must be a function');
|
---|
43 | }
|
---|
44 | desc['[[Set]]'] = setter;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if ((hasOwn(desc, '[[Get]]') || hasOwn(desc, '[[Set]]')) && (hasOwn(desc, '[[Value]]') || hasOwn(desc, '[[Writable]]'))) {
|
---|
48 | throw new $TypeError('Invalid property descriptor. Cannot both specify accessors and a value or writable attribute');
|
---|
49 | }
|
---|
50 | return desc;
|
---|
51 | };
|
---|