[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
|
---|
| 6 | var IsExtensible = require('./IsExtensible');
|
---|
[79a0317] | 7 |
|
---|
| 8 | var isObject = require('../helpers/isObject');
|
---|
| 9 | var isPropertyKey = require('../helpers/isPropertyKey');
|
---|
[d565449] | 10 |
|
---|
| 11 | // https://262.ecma-international.org/13.0/#sec-definemethodproperty
|
---|
| 12 |
|
---|
| 13 | module.exports = function DefineMethodProperty(homeObject, key, closure, enumerable) {
|
---|
[79a0317] | 14 | if (!isObject(homeObject)) {
|
---|
[d565449] | 15 | throw new $TypeError('Assertion failed: `homeObject` is not an Object');
|
---|
| 16 | }
|
---|
[79a0317] | 17 | if (!isPropertyKey(key)) {
|
---|
[d565449] | 18 | throw new $TypeError('Assertion failed: `key` is not a Property Key or a Private Name');
|
---|
| 19 | }
|
---|
| 20 | if (typeof closure !== 'function') {
|
---|
| 21 | throw new $TypeError('Assertion failed: `closure` is not a function');
|
---|
| 22 | }
|
---|
| 23 | if (typeof enumerable !== 'boolean') {
|
---|
| 24 | throw new $TypeError('Assertion failed: `enumerable` is not a Boolean');
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | // 1. Assert: homeObject is an ordinary, extensible object with no non-configurable properties.
|
---|
| 28 | if (!IsExtensible(homeObject)) {
|
---|
| 29 | throw new $TypeError('Assertion failed: `homeObject` is not an ordinary, extensible object, with no non-configurable properties');
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | // 2. If key is a Private Name, then
|
---|
| 33 | // a. Return PrivateElement { [[Key]]: key, [[Kind]]: method, [[Value]]: closure }.
|
---|
| 34 | // 3. Else,
|
---|
| 35 | var desc = { // step 3.a
|
---|
| 36 | '[[Value]]': closure,
|
---|
| 37 | '[[Writable]]': true,
|
---|
| 38 | '[[Enumerable]]': enumerable,
|
---|
| 39 | '[[Configurable]]': true
|
---|
| 40 | };
|
---|
| 41 | DefinePropertyOrThrow(homeObject, key, desc); // step 3.b
|
---|
| 42 | };
|
---|