index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var define = require('define-data-property');
  4. var hasDescriptors = require('has-property-descriptors')();
  5. var gOPD = require('gopd');
  6. var $TypeError = require('es-errors/type');
  7. var $floor = GetIntrinsic('%Math.floor%');
  8. /** @type {import('.')} */
  9. module.exports = function setFunctionLength(fn, length) {
  10. if (typeof fn !== 'function') {
  11. throw new $TypeError('`fn` is not a function');
  12. }
  13. if (typeof length !== 'number' || length < 0 || length > 0xFFFFFFFF || $floor(length) !== length) {
  14. throw new $TypeError('`length` must be a positive 32-bit integer');
  15. }
  16. var loose = arguments.length > 2 && !!arguments[2];
  17. var functionLengthIsConfigurable = true;
  18. var functionLengthIsWritable = true;
  19. if ('length' in fn && gOPD) {
  20. var desc = gOPD(fn, 'length');
  21. if (desc && !desc.configurable) {
  22. functionLengthIsConfigurable = false;
  23. }
  24. if (desc && !desc.writable) {
  25. functionLengthIsWritable = false;
  26. }
  27. }
  28. if (functionLengthIsConfigurable || functionLengthIsWritable || !loose) {
  29. if (hasDescriptors) {
  30. define(/** @type {Parameters<define>[0]} */ (fn), 'length', length, true, true);
  31. } else {
  32. define(/** @type {Parameters<define>[0]} */ (fn), 'length', length);
  33. }
  34. }
  35. return fn;
  36. };