array-set-length.js 1008 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var DESCRIPTORS = require('../internals/descriptors');
  3. var isArray = require('../internals/is-array');
  4. var $TypeError = TypeError;
  5. // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
  6. var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  7. // Safari < 13 does not throw an error in this case
  8. var SILENT_ON_NON_WRITABLE_LENGTH_SET = DESCRIPTORS && !function () {
  9. // makes no sense without proper strict mode support
  10. if (this !== undefined) return true;
  11. try {
  12. // eslint-disable-next-line es/no-object-defineproperty -- safe
  13. Object.defineProperty([], 'length', { writable: false }).length = 1;
  14. } catch (error) {
  15. return error instanceof TypeError;
  16. }
  17. }();
  18. module.exports = SILENT_ON_NON_WRITABLE_LENGTH_SET ? function (O, length) {
  19. if (isArray(O) && !getOwnPropertyDescriptor(O, 'length').writable) {
  20. throw $TypeError('Cannot set read only .length');
  21. } return O.length = length;
  22. } : function (O, length) {
  23. return O.length = length;
  24. };