util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.wrapNativeSuper = void 0;
  4. //@ts-ignore
  5. function wrapNativeSuper(Class) {
  6. var _cache = typeof Map === "function" ? new Map() : undefined;
  7. //@ts-ignore
  8. exports.wrapNativeSuper = wrapNativeSuper = function wrapNativeSuper(Class) {
  9. if (Class === null || !_isNativeFunction(Class))
  10. return Class;
  11. if (typeof Class !== "function") {
  12. throw new TypeError("Super expression must either be null or a function");
  13. }
  14. if (typeof _cache !== "undefined") {
  15. if (_cache.has(Class))
  16. return _cache.get(Class);
  17. _cache.set(Class, Wrapper);
  18. }
  19. function Wrapper() {
  20. //@ts-ignore
  21. return _construct(Class, arguments, _getPrototypeOf(this).constructor);
  22. }
  23. Wrapper.prototype = Object.create(Class.prototype, {
  24. constructor: {
  25. value: Wrapper,
  26. enumerable: false,
  27. writable: true,
  28. configurable: true
  29. }
  30. });
  31. return _setPrototypeOf(Wrapper, Class);
  32. };
  33. return wrapNativeSuper(Class);
  34. }
  35. exports.wrapNativeSuper = wrapNativeSuper;
  36. function isNativeReflectConstruct() {
  37. if (typeof Reflect === "undefined" || !Reflect.construct)
  38. return false;
  39. //@ts-ignore
  40. if (Reflect.construct.sham)
  41. return false;
  42. if (typeof Proxy === "function")
  43. return true;
  44. try {
  45. Date.prototype.toString.call(Reflect.construct(Date, [], function () { }));
  46. return true;
  47. }
  48. catch (e) {
  49. return false;
  50. }
  51. }
  52. //@ts-ignore
  53. function _construct(Parent, args, Class) {
  54. if (isNativeReflectConstruct()) {
  55. //@ts-ignore
  56. _construct = Reflect.construct;
  57. }
  58. else {
  59. //@ts-ignore
  60. _construct = function _construct(Parent, args, Class) {
  61. var a = [null];
  62. a.push.apply(a, args);
  63. //@ts-ignore
  64. var Constructor = Function.bind.apply(Parent, a);
  65. //@ts-ignore
  66. var instance = new Constructor();
  67. if (Class)
  68. _setPrototypeOf(instance, Class.prototype);
  69. return instance;
  70. };
  71. }
  72. //@ts-ignore
  73. return _construct.apply(null, arguments);
  74. }
  75. function _isNativeFunction(fn) {
  76. return Function.toString.call(fn).indexOf("[native code]") !== -1;
  77. }
  78. //@ts-ignore
  79. function _setPrototypeOf(o, p) {
  80. //@ts-ignore
  81. _setPrototypeOf =
  82. Object.setPrototypeOf ||
  83. //@ts-ignore
  84. function _setPrototypeOf(o, p) {
  85. o.__proto__ = p;
  86. return o;
  87. };
  88. return _setPrototypeOf(o, p);
  89. }
  90. //@ts-ignore
  91. function _getPrototypeOf(o) {
  92. //@ts-ignore
  93. _getPrototypeOf = Object.setPrototypeOf
  94. ? Object.getPrototypeOf
  95. : function _getPrototypeOf(
  96. //@ts-ignore
  97. o) {
  98. return o.__proto__ || Object.getPrototypeOf(o);
  99. };
  100. return _getPrototypeOf(o);
  101. }
  102. module.exports = {
  103. wrapNativeSuper
  104. };