make-built-in.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var fails = require('../internals/fails');
  2. var isCallable = require('../internals/is-callable');
  3. var hasOwn = require('../internals/has-own-property');
  4. var DESCRIPTORS = require('../internals/descriptors');
  5. var CONFIGURABLE_FUNCTION_NAME = require('../internals/function-name').CONFIGURABLE;
  6. var inspectSource = require('../internals/inspect-source');
  7. var InternalStateModule = require('../internals/internal-state');
  8. var enforceInternalState = InternalStateModule.enforce;
  9. var getInternalState = InternalStateModule.get;
  10. // eslint-disable-next-line es/no-object-defineproperty -- safe
  11. var defineProperty = Object.defineProperty;
  12. var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () {
  13. return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8;
  14. });
  15. var TEMPLATE = String(String).split('String');
  16. var makeBuiltIn = module.exports = function (value, name, options) {
  17. if (String(name).slice(0, 7) === 'Symbol(') {
  18. name = '[' + String(name).replace(/^Symbol\(([^)]*)\)/, '$1') + ']';
  19. }
  20. if (options && options.getter) name = 'get ' + name;
  21. if (options && options.setter) name = 'set ' + name;
  22. if (!hasOwn(value, 'name') || (CONFIGURABLE_FUNCTION_NAME && value.name !== name)) {
  23. if (DESCRIPTORS) defineProperty(value, 'name', { value: name, configurable: true });
  24. else value.name = name;
  25. }
  26. if (CONFIGURABLE_LENGTH && options && hasOwn(options, 'arity') && value.length !== options.arity) {
  27. defineProperty(value, 'length', { value: options.arity });
  28. }
  29. try {
  30. if (options && hasOwn(options, 'constructor') && options.constructor) {
  31. if (DESCRIPTORS) defineProperty(value, 'prototype', { writable: false });
  32. // in V8 ~ Chrome 53, prototypes of some methods, like `Array.prototype.values`, are non-writable
  33. } else if (value.prototype) value.prototype = undefined;
  34. } catch (error) { /* empty */ }
  35. var state = enforceInternalState(value);
  36. if (!hasOwn(state, 'source')) {
  37. state.source = TEMPLATE.join(typeof name == 'string' ? name : '');
  38. } return value;
  39. };
  40. // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
  41. // eslint-disable-next-line no-extend-native -- required
  42. Function.prototype.toString = makeBuiltIn(function toString() {
  43. return isCallable(this) && getInternalState(this).source || inspectSource(this);
  44. }, 'toString');