blob: 26f2714bb1f5d3b5c3a152b4c402f3a10dc4a412 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = GetIntrinsic('%TypeError%');
6
7var isPropertyDescriptor = require('../helpers/isPropertyDescriptor');
8var DefineOwnProperty = require('../helpers/DefineOwnProperty');
9
10var FromPropertyDescriptor = require('./FromPropertyDescriptor');
11var IsAccessorDescriptor = require('./IsAccessorDescriptor');
12var IsDataDescriptor = require('./IsDataDescriptor');
13var IsPropertyKey = require('./IsPropertyKey');
14var SameValue = require('./SameValue');
15var ToPropertyDescriptor = require('./ToPropertyDescriptor');
16var Type = require('./Type');
17
18// https://ecma-international.org/ecma-262/6.0/#sec-definepropertyorthrow
19
20module.exports = function DefinePropertyOrThrow(O, P, desc) {
21 if (Type(O) !== 'Object') {
22 throw new $TypeError('Assertion failed: Type(O) is not Object');
23 }
24
25 if (!IsPropertyKey(P)) {
26 throw new $TypeError('Assertion failed: IsPropertyKey(P) is not true');
27 }
28
29 var Desc = isPropertyDescriptor({
30 Type: Type,
31 IsDataDescriptor: IsDataDescriptor,
32 IsAccessorDescriptor: IsAccessorDescriptor
33 }, desc) ? desc : ToPropertyDescriptor(desc);
34 if (!isPropertyDescriptor({
35 Type: Type,
36 IsDataDescriptor: IsDataDescriptor,
37 IsAccessorDescriptor: IsAccessorDescriptor
38 }, Desc)) {
39 throw new $TypeError('Assertion failed: Desc is not a valid Property Descriptor');
40 }
41
42 return DefineOwnProperty(
43 IsDataDescriptor,
44 SameValue,
45 FromPropertyDescriptor,
46 O,
47 P,
48 Desc
49 );
50};