| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var GetIntrinsic = require('get-intrinsic'); |
| 4 | |
| 5 | var $TypeError = GetIntrinsic('%TypeError%'); |
| 6 | |
| 7 | var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); |
| 8 | var HasOwnProperty = require('./HasOwnProperty'); |
| 9 | var IsExtensible = require('./IsExtensible'); |
| 10 | var IsInteger = require('./IsInteger'); |
| 11 | var Type = require('./Type'); |
| 12 | |
| 13 | // https://262.ecma-international.org/9.0/#sec-setfunctionlength |
| 14 | |
| 15 | module.exports = function SetFunctionLength(F, length) { |
| 16 | if (typeof F !== 'function' || !IsExtensible(F) || HasOwnProperty(F, 'length')) { |
| 17 | throw new $TypeError('Assertion failed: `F` must be an extensible function and lack an own `length` property'); |
| 18 | } |
| 19 | if (Type(length) !== 'Number') { |
| 20 | throw new $TypeError('Assertion failed: `length` must be a Number'); |
| 21 | } |
| 22 | if (length < 0 || !IsInteger(length)) { |
| 23 | throw new $TypeError('Assertion failed: `length` must be an integer >= 0'); |
| 24 | } |
| 25 | return DefinePropertyOrThrow(F, 'length', { |
| 26 | '[[Configurable]]': true, |
| 27 | '[[Enumerable]]': false, |
| 28 | '[[Value]]': length, |
| 29 | '[[Writable]]': false |
| 30 | }); |
| 31 | }; |