diff --git a/library/modules/$.fix-re-wks.js b/library/modules/$.fix-re-wks.js new file mode 100644 index 000000000000..9920ec77ac29 --- /dev/null +++ b/library/modules/$.fix-re-wks.js @@ -0,0 +1,16 @@ +module.exports = function(KEY, exec){ + var SYMBOL = require('./$.wks')(KEY) + , methods = exec(SYMBOL, ''[KEY]); + if(function(){ + try { + var O = {}; + O[SYMBOL] = function(){ return 7; }; + return ''[KEY](O) != 7; + } catch(e){ + return true; + } + }()){ + require('./$.redef')(String.prototype, KEY, methods[0]); + require('./$').hide(RegExp.prototype, SYMBOL, methods[1]); + } +}; \ No newline at end of file diff --git a/library/modules/$.need-fix-re-wks.js b/library/modules/$.need-fix-re-wks.js deleted file mode 100644 index 61e99848393d..000000000000 --- a/library/modules/$.need-fix-re-wks.js +++ /dev/null @@ -1,10 +0,0 @@ -var wks = require('./$.wks'); -module.exports = function(KEY){ - try { - var O = {}; - O[wks(KEY)] = function(){ return 7; }; - return ''[KEY](O) != 7; - } catch(e){ - return true; - } -}; \ No newline at end of file diff --git a/modules/$.fix-re-wks.js b/modules/$.fix-re-wks.js new file mode 100644 index 000000000000..9920ec77ac29 --- /dev/null +++ b/modules/$.fix-re-wks.js @@ -0,0 +1,16 @@ +module.exports = function(KEY, exec){ + var SYMBOL = require('./$.wks')(KEY) + , methods = exec(SYMBOL, ''[KEY]); + if(function(){ + try { + var O = {}; + O[SYMBOL] = function(){ return 7; }; + return ''[KEY](O) != 7; + } catch(e){ + return true; + } + }()){ + require('./$.redef')(String.prototype, KEY, methods[0]); + require('./$').hide(RegExp.prototype, SYMBOL, methods[1]); + } +}; \ No newline at end of file diff --git a/modules/$.need-fix-re-wks.js b/modules/$.need-fix-re-wks.js deleted file mode 100644 index 61e99848393d..000000000000 --- a/modules/$.need-fix-re-wks.js +++ /dev/null @@ -1,10 +0,0 @@ -var wks = require('./$.wks'); -module.exports = function(KEY){ - try { - var O = {}; - O[wks(KEY)] = function(){ return 7; }; - return ''[KEY](O) != 7; - } catch(e){ - return true; - } -}; \ No newline at end of file diff --git a/modules/es6.regexp.match.js b/modules/es6.regexp.match.js index 33a9ae7c7284..6ef1f943c351 100644 --- a/modules/es6.regexp.match.js +++ b/modules/es6.regexp.match.js @@ -1,14 +1,15 @@ -if(require('./$.need-fix-re-wks')('match')){ - var $match = ''.match - , MATCH = require('./$.wks')('match'); - // 21.1.3.11 String.prototype.match(regexp) - require('./$.redef')(String.prototype, 'match', function match(regexp){ - var str = String(this) - , fn = regexp == undefined ? undefined : regexp[MATCH]; - return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[MATCH](str); - }); - // 21.2.5.6 RegExp.prototype[@@match](string) - require('./$').hide(RegExp.prototype, MATCH, function(string){ - return $match.call(string, this); - }); -} \ No newline at end of file +// @@match logic +require('./$.fix-re-wks')('match', function(MATCH, $match){ + return [ + // 21.1.3.11 String.prototype.match(regexp) + function match(regexp){ + var str = String(this) + , fn = regexp == undefined ? undefined : regexp[MATCH]; + return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[MATCH](str); + }, + // 21.2.5.6 RegExp.prototype[@@match](string) + function(string){ + return $match.call(string, this); + } + ]; +}); \ No newline at end of file diff --git a/modules/es6.regexp.replace.js b/modules/es6.regexp.replace.js index ee945be8038f..b2f3f90f5f3d 100644 --- a/modules/es6.regexp.replace.js +++ b/modules/es6.regexp.replace.js @@ -1,16 +1,17 @@ -if(require('./$.need-fix-re-wks')('replace')){ - var $replace = ''.replace - , REPLACE = require('./$.wks')('replace'); - // 21.1.3.14 String.prototype.replace(searchValue, replaceValue) - require('./$.redef')(String.prototype, 'replace', function replace(searchValue, replaceValue){ - var str = String(this) - , fn = searchValue == undefined ? undefined : searchValue[REPLACE]; - return fn !== undefined - ? fn.call(searchValue, str, replaceValue) - : $replace.call(str, searchValue, replaceValue); - }); - // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue) - require('./$').hide(RegExp.prototype, REPLACE, function(string, replaceValue){ - return $replace.call(string, this, replaceValue); - }); -} \ No newline at end of file +// @@replace logic +require('./$.fix-re-wks')('replace', function(REPLACE, $replace){ + return [ + // 21.1.3.14 String.prototype.replace(searchValue, replaceValue) + function replace(searchValue, replaceValue){ + var str = String(this) + , fn = searchValue == undefined ? undefined : searchValue[REPLACE]; + return fn !== undefined + ? fn.call(searchValue, str, replaceValue) + : $replace.call(str, searchValue, replaceValue); + }, + // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue) + function(string, replaceValue){ + return $replace.call(string, this, replaceValue); + } + ]; +}); \ No newline at end of file diff --git a/modules/es6.regexp.search.js b/modules/es6.regexp.search.js index f1ef8d87af01..c6caed452212 100644 --- a/modules/es6.regexp.search.js +++ b/modules/es6.regexp.search.js @@ -1,14 +1,15 @@ -if(require('./$.need-fix-re-wks')('search')){ - var $search = ''.search - , SEARCH = require('./$.wks')('search'); - // 21.1.3.15 String.prototype.search(regexp) - require('./$.redef')(String.prototype, 'search', function search(regexp){ - var str = String(this) - , fn = regexp == undefined ? undefined : regexp[SEARCH]; - return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[SEARCH](str); - }); - // 21.2.5.9 RegExp.prototype[@@search](string) - require('./$').hide(RegExp.prototype, SEARCH, function(string){ - return $search.call(string, this); - }); -} \ No newline at end of file +// @@search logic +require('./$.fix-re-wks')('search', function(SEARCH, $search){ + return [ + // 21.1.3.15 String.prototype.search(regexp) + function search(regexp){ + var str = String(this) + , fn = regexp == undefined ? undefined : regexp[SEARCH]; + return fn !== undefined ? fn.call(regexp, str) : new RegExp(regexp)[SEARCH](str); + }, + // 21.2.5.9 RegExp.prototype[@@search](string) + function(string){ + return $search.call(string, this); + } + ]; +}); \ No newline at end of file diff --git a/modules/es6.regexp.split.js b/modules/es6.regexp.split.js index da79fcf9d8fb..94faac48a9be 100644 --- a/modules/es6.regexp.split.js +++ b/modules/es6.regexp.split.js @@ -1,14 +1,15 @@ -if(require('./$.need-fix-re-wks')('split')){ - var $split = ''.split - , SPLIT = require('./$.wks')('split'); - // 21.1.3.17 String.prototype.split(separator, limit) - require('./$.redef')(String.prototype, 'split', function split(separator, limit){ - var str = String(this) - , fn = separator == undefined ? undefined : separator[SPLIT]; - return fn !== undefined ? fn.call(separator, str, limit) : $split.call(str, separator, limit); - }); - // 21.2.5.11 RegExp.prototype[@@split](string, limit) - require('./$').hide(RegExp.prototype, SPLIT, function(string, limit){ - return $split.call(string, this, limit); - }); -} \ No newline at end of file +// @@split logic +require('./$.fix-re-wks')('split', function(SPLIT, $split){ + return [ + // 21.1.3.17 String.prototype.split(separator, limit) + function split(separator, limit){ + var str = String(this) + , fn = separator == undefined ? undefined : separator[SPLIT]; + return fn !== undefined ? fn.call(separator, str, limit) : $split.call(str, separator, limit); + }, + // 21.2.5.11 RegExp.prototype[@@split](string, limit) + function(string, limit){ + return $split.call(string, this, limit); + } + ]; +}); \ No newline at end of file