SetFunctionLength.js 980 B

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