From 722d68874889e24f6d1f2de4eb0434a3eecc45c0 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sun, 11 Feb 2018 12:28:06 +0700 Subject: [PATCH] fix global case of #262 --- .../internals/copy-constructor-properties.js | 14 +++++++++ packages/core-js/internals/export.js | 14 ++++++--- .../core-js/modules/es.regexp.constructor.js | 8 ++--- .../modules/esnext.symbol.description.js | 29 ++++++++----------- 4 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 packages/core-js/internals/copy-constructor-properties.js diff --git a/packages/core-js/internals/copy-constructor-properties.js b/packages/core-js/internals/copy-constructor-properties.js new file mode 100644 index 000000000000..3b5a267ea9d1 --- /dev/null +++ b/packages/core-js/internals/copy-constructor-properties.js @@ -0,0 +1,14 @@ +var has = require('core-js-internals/has'); +var ownKeys = require('../internals/own-keys'); +var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor'); +var definePropertyModule = require('../internals/object-define-property'); + +module.exports = function (target, source) { + var keys = ownKeys(source); + var defineProperty = definePropertyModule.f; + var getOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f; + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + if (!has(target, key)) defineProperty(target, key, getOwnPropertyDescriptor(source, key)); + } +}; diff --git a/packages/core-js/internals/export.js b/packages/core-js/internals/export.js index f4c46c20a2e0..126f287247fd 100644 --- a/packages/core-js/internals/export.js +++ b/packages/core-js/internals/export.js @@ -1,6 +1,7 @@ var global = require('core-js-internals/global'); var redefine = require('../internals/redefine'); var setGlobal = require('../internals/set-global'); +var copyConstructorProperties = require('../internals/copy-constructor-properties'); /* options.target - name of the target object @@ -15,7 +16,7 @@ var setGlobal = require('../internals/set-global'); */ module.exports = function (options, source) { var name = options.target; - var target, key; + var target, key, targetProperty, sourceProperty; if (options.global) { target = global; } else if (options.stat) { @@ -24,9 +25,14 @@ module.exports = function (options, source) { target = (global[name] || {}).prototype; } if (target) for (key in source) { - // contains in native - if (!options.forced && target[key] !== undefined) continue; + targetProperty = target[key]; + sourceProperty = source[key]; + // contained in target + if (!options.forced && targetProperty !== undefined) { + if (typeof sourceProperty === typeof targetProperty) continue; + copyConstructorProperties(sourceProperty, targetProperty); + } // extend global - redefine(target, key, source[key], options.unsafe); + redefine(target, key, sourceProperty, options.unsafe); } }; diff --git a/packages/core-js/modules/es.regexp.constructor.js b/packages/core-js/modules/es.regexp.constructor.js index 31b00162ba06..268441c23153 100644 --- a/packages/core-js/modules/es.regexp.constructor.js +++ b/packages/core-js/modules/es.regexp.constructor.js @@ -6,7 +6,7 @@ var isRegExp = require('core-js-internals/is-regexp'); var getFlags = require('core-js-internals/regexp-flags'); var redefine = require('../internals/redefine'); var NativeRegExp = global.RegExp; -var proto = NativeRegExp.prototype; +var RegExpPrototype = NativeRegExp.prototype; var re1 = /a/g; var re2 = /a/g; // "new" should create a new object, old webkit bug @@ -29,7 +29,7 @@ if (require('core-js-internals/descriptors') && (!CORRECT_NEW || require('core-j : NativeRegExp((patternIsRegExp = pattern instanceof RegExpWrapper) ? pattern.source : pattern, patternIsRegExp && flagsAreUndefined ? getFlags.call(pattern) : flags) - , thisIsRegExp ? this : proto, RegExpWrapper); + , thisIsRegExp ? this : RegExpPrototype, RegExpWrapper); }; var proxy = function (key) { key in RegExpWrapper || defineProperty(RegExpWrapper, key, { @@ -39,8 +39,8 @@ if (require('core-js-internals/descriptors') && (!CORRECT_NEW || require('core-j }); }; for (var keys = getOwnPropertyNames(NativeRegExp), i = 0; keys.length > i;) proxy(keys[i++]); - proto.constructor = RegExpWrapper; - RegExpWrapper.prototype = proto; + RegExpPrototype.constructor = RegExpWrapper; + RegExpWrapper.prototype = RegExpPrototype; redefine(global, 'RegExp', RegExpWrapper); } diff --git a/packages/core-js/modules/esnext.symbol.description.js b/packages/core-js/modules/esnext.symbol.description.js index cc7a3cdadb67..4153dd9544cd 100644 --- a/packages/core-js/modules/esnext.symbol.description.js +++ b/packages/core-js/modules/esnext.symbol.description.js @@ -3,33 +3,28 @@ var DESCRIPTORS = require('core-js-internals/descriptors'); var has = require('core-js-internals/has'); var isObject = require('core-js-internals/is-object'); -var getOwnPropertyNames = require('../internals/object-get-own-property-names').f; -var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f; var defineProperty = require('../internals/object-define-property').f; -var Base = require('core-js-internals/global').Symbol; +var copyConstructorProperties = require('../internals/copy-constructor-properties'); +var NativeSymbol = require('core-js-internals/global').Symbol; -if (DESCRIPTORS && typeof Base == 'function' && !('description' in Base.prototype)) { +if (DESCRIPTORS && typeof NativeSymbol == 'function' && !('description' in NativeSymbol.prototype)) { var emptyStringDescriptionStore = {}; // wrap Symbol constructor for correct work with undefined description - var $Symbol = function Symbol() { + var SymbolWrapper = function Symbol() { var description = arguments.length < 1 || arguments[0] === undefined ? undefined : String(arguments[0]); - var result = this instanceof $Symbol - ? new Base(description) + var result = this instanceof SymbolWrapper + ? new NativeSymbol(description) // in Edge 13, String(Symbol(undefined)) === 'Symbol(undefined)' - : description === undefined ? Base() : Base(description); + : description === undefined ? NativeSymbol() : NativeSymbol(description); if (description === '') emptyStringDescriptionStore[result] = true; return result; }; - for (var keys = getOwnPropertyNames(Base), i = 0, key; keys.length > i; i++) { - if (!has($Symbol, key = keys[i])) { - defineProperty($Symbol, key, getOwnPropertyDescriptor(Base, key)); - } - } - var symbolPrototype = $Symbol.prototype = Base.prototype; - symbolPrototype.constructor = $Symbol; + copyConstructorProperties(SymbolWrapper, NativeSymbol); + var symbolPrototype = SymbolWrapper.prototype = NativeSymbol.prototype; + symbolPrototype.constructor = SymbolWrapper; var symbolToString = symbolPrototype.toString; - var native = String(Base('test')) == 'Symbol(test)'; + var native = String(NativeSymbol('test')) == 'Symbol(test)'; var regexp = /^Symbol\((.*)\)[^)]+$/; defineProperty(symbolPrototype, 'description', { configurable: true, @@ -42,5 +37,5 @@ if (DESCRIPTORS && typeof Base == 'function' && !('description' in Base.prototyp } }); - require('../internals/export')({ global: true, forced: true }, { Symbol: $Symbol }); + require('../internals/export')({ global: true, forced: true }, { Symbol: SymbolWrapper }); }