index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var bind = require('function-bind');
  3. var GetIntrinsic = require('get-intrinsic');
  4. var $apply = GetIntrinsic('%Function.prototype.apply%');
  5. var $call = GetIntrinsic('%Function.prototype.call%');
  6. var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply);
  7. var $gOPD = GetIntrinsic('%Object.getOwnPropertyDescriptor%', true);
  8. var $defineProperty = GetIntrinsic('%Object.defineProperty%', true);
  9. var $max = GetIntrinsic('%Math.max%');
  10. if ($defineProperty) {
  11. try {
  12. $defineProperty({}, 'a', { value: 1 });
  13. } catch (e) {
  14. // IE 8 has a broken defineProperty
  15. $defineProperty = null;
  16. }
  17. }
  18. module.exports = function callBind(originalFunction) {
  19. var func = $reflectApply(bind, $call, arguments);
  20. if ($gOPD && $defineProperty) {
  21. var desc = $gOPD(func, 'length');
  22. if (desc.configurable) {
  23. // original length, plus the receiver, minus any additional arguments (after the receiver)
  24. $defineProperty(
  25. func,
  26. 'length',
  27. { value: 1 + $max(0, originalFunction.length - (arguments.length - 1)) }
  28. );
  29. }
  30. }
  31. return func;
  32. };
  33. var applyBind = function applyBind() {
  34. return $reflectApply(bind, $apply, arguments);
  35. };
  36. if ($defineProperty) {
  37. $defineProperty(module.exports, 'apply', { value: applyBind });
  38. } else {
  39. module.exports.apply = applyBind;
  40. }