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