SetFunctionLength.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var DefinePropertyOrThrow = require('./DefinePropertyOrThrow');
  5. var HasOwnProperty = require('./HasOwnProperty');
  6. var IsExtensible = require('./IsExtensible');
  7. var IsIntegralNumber = require('./IsIntegralNumber');
  8. var Type = require('./Type');
  9. // https://ecma-international.org/ecma-262/12.0/#sec-setfunctionlength
  10. module.exports = function SetFunctionLength(F, length) {
  11. if (typeof F !== 'function' || !IsExtensible(F) || HasOwnProperty(F, 'length')) {
  12. throw new $TypeError('Assertion failed: `F` must be an extensible function and lack an own `length` property');
  13. }
  14. if (Type(length) !== 'Number') {
  15. throw new $TypeError('Assertion failed: `length` must be a Number');
  16. }
  17. if (length !== Infinity && (!IsIntegralNumber(length) || length < 0)) {
  18. throw new $TypeError('Assertion failed: `length` must be ∞, or an integer >= 0');
  19. }
  20. return DefinePropertyOrThrow(F, 'length', {
  21. '[[Configurable]]': true,
  22. '[[Enumerable]]': false,
  23. '[[Value]]': length,
  24. '[[Writable]]': false
  25. });
  26. };