es6.regexp.constructor.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var $ = require('./$')
  2. , global = require('./$.global')
  3. , isRegExp = require('./$.is-regexp')
  4. , $flags = require('./$.flags')
  5. , $RegExp = global.RegExp
  6. , Base = $RegExp
  7. , proto = $RegExp.prototype
  8. , re1 = /a/g
  9. , re2 = /a/g
  10. // "new" creates a new object, old webkit buggy here
  11. , CORRECT_NEW = new $RegExp(re1) !== re1;
  12. if(require('./$.descriptors') && (!CORRECT_NEW || require('./$.fails')(function(){
  13. re2[require('./$.wks')('match')] = false;
  14. // RegExp constructor can alter flags and IsRegExp works correct with @@match
  15. return $RegExp(re1) != re1 || $RegExp(re2) == re2 || $RegExp(re1, 'i') != '/a/i';
  16. }))){
  17. $RegExp = function RegExp(p, f){
  18. var piRE = isRegExp(p)
  19. , fiU = f === undefined;
  20. return !(this instanceof $RegExp) && piRE && p.constructor === $RegExp && fiU ? p
  21. : CORRECT_NEW
  22. ? new Base(piRE && !fiU ? p.source : p, f)
  23. : Base((piRE = p instanceof $RegExp) ? p.source : p, piRE && fiU ? $flags.call(p) : f);
  24. };
  25. $.each.call($.getNames(Base), function(key){
  26. key in $RegExp || $.setDesc($RegExp, key, {
  27. configurable: true,
  28. get: function(){ return Base[key]; },
  29. set: function(it){ Base[key] = it; }
  30. });
  31. });
  32. proto.constructor = $RegExp;
  33. $RegExp.prototype = proto;
  34. require('./$.redefine')(global, 'RegExp', $RegExp);
  35. }
  36. require('./$.set-species')('RegExp');