Skip to content

Commit

Permalink
fix global case of #262
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed May 4, 2018
1 parent 63d71b5 commit c23d86f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
14 changes: 14 additions & 0 deletions packages/core-js/internals/copy-constructor-properties.js
Original file line number Diff line number Diff line change
@@ -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));
}
};
14 changes: 10 additions & 4 deletions packages/core-js/internals/export.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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);
}
};
8 changes: 4 additions & 4 deletions packages/core-js/modules/es.regexp.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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);
}

Expand Down
29 changes: 12 additions & 17 deletions packages/core-js/modules/esnext.symbol.description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
}

0 comments on commit c23d86f

Please sign in to comment.