shim.js 779 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var supportsDescriptors = require('define-properties').supportsDescriptors;
  3. var getPolyfill = require('./polyfill');
  4. var gOPD = Object.getOwnPropertyDescriptor;
  5. var defineProperty = Object.defineProperty;
  6. var TypeErr = TypeError;
  7. var getProto = Object.getPrototypeOf;
  8. var regex = /a/;
  9. module.exports = function shimFlags() {
  10. if (!supportsDescriptors || !getProto) {
  11. throw new TypeErr('RegExp.prototype.flags requires a true ES5 environment that supports property descriptors');
  12. }
  13. var polyfill = getPolyfill();
  14. var proto = getProto(regex);
  15. var descriptor = gOPD(proto, 'flags');
  16. if (!descriptor || descriptor.get !== polyfill) {
  17. defineProperty(proto, 'flags', {
  18. configurable: true,
  19. enumerable: false,
  20. get: polyfill
  21. });
  22. }
  23. return polyfill;
  24. };