2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
|
|
|
// ECMAScript 6 symbols shim
|
2017-04-20 03:09:25 +02:00
|
|
|
var $ = require('./$')
|
|
|
|
, global = require('./$.global')
|
|
|
|
, has = require('./$.has')
|
|
|
|
, DESCRIPTORS = require('./$.descriptors')
|
|
|
|
, $export = require('./$.export')
|
|
|
|
, redefine = require('./$.redefine')
|
|
|
|
, $fails = require('./$.fails')
|
|
|
|
, shared = require('./$.shared')
|
|
|
|
, setToStringTag = require('./$.set-to-string-tag')
|
|
|
|
, uid = require('./$.uid')
|
|
|
|
, wks = require('./$.wks')
|
|
|
|
, keyOf = require('./$.keyof')
|
|
|
|
, $names = require('./$.get-names')
|
|
|
|
, enumKeys = require('./$.enum-keys')
|
|
|
|
, isArray = require('./$.is-array')
|
|
|
|
, anObject = require('./$.an-object')
|
|
|
|
, toIObject = require('./$.to-iobject')
|
|
|
|
, createDesc = require('./$.property-desc')
|
|
|
|
, getDesc = $.getDesc
|
|
|
|
, setDesc = $.setDesc
|
|
|
|
, _create = $.create
|
|
|
|
, getNames = $names.get
|
2016-10-10 03:43:44 +02:00
|
|
|
, $Symbol = global.Symbol
|
|
|
|
, $JSON = global.JSON
|
|
|
|
, _stringify = $JSON && $JSON.stringify
|
2017-04-20 03:09:25 +02:00
|
|
|
, setter = false
|
2016-10-10 03:43:44 +02:00
|
|
|
, HIDDEN = wks('_hidden')
|
2017-04-20 03:09:25 +02:00
|
|
|
, isEnum = $.isEnum
|
2016-10-10 03:43:44 +02:00
|
|
|
, SymbolRegistry = shared('symbol-registry')
|
|
|
|
, AllSymbols = shared('symbols')
|
2017-04-20 03:09:25 +02:00
|
|
|
, useNative = typeof $Symbol == 'function'
|
|
|
|
, ObjectProto = Object.prototype;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// fallback for old Android, https://code.google.com/p/v8/issues/detail?id=687
|
|
|
|
var setSymbolDesc = DESCRIPTORS && $fails(function(){
|
2017-04-20 03:09:25 +02:00
|
|
|
return _create(setDesc({}, 'a', {
|
|
|
|
get: function(){ return setDesc(this, 'a', {value: 7}).a; }
|
2016-10-10 03:43:44 +02:00
|
|
|
})).a != 7;
|
|
|
|
}) ? function(it, key, D){
|
2017-04-20 03:09:25 +02:00
|
|
|
var protoDesc = getDesc(ObjectProto, key);
|
2016-10-10 03:43:44 +02:00
|
|
|
if(protoDesc)delete ObjectProto[key];
|
2017-04-20 03:09:25 +02:00
|
|
|
setDesc(it, key, D);
|
|
|
|
if(protoDesc && it !== ObjectProto)setDesc(ObjectProto, key, protoDesc);
|
|
|
|
} : setDesc;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
var wrap = function(tag){
|
2017-04-20 03:09:25 +02:00
|
|
|
var sym = AllSymbols[tag] = _create($Symbol.prototype);
|
2016-10-10 03:43:44 +02:00
|
|
|
sym._k = tag;
|
2017-04-20 03:09:25 +02:00
|
|
|
DESCRIPTORS && setter && setSymbolDesc(ObjectProto, tag, {
|
|
|
|
configurable: true,
|
|
|
|
set: function(value){
|
|
|
|
if(has(this, HIDDEN) && has(this[HIDDEN], tag))this[HIDDEN][tag] = false;
|
|
|
|
setSymbolDesc(this, tag, createDesc(1, value));
|
|
|
|
}
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
return sym;
|
|
|
|
};
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
var isSymbol = function(it){
|
2016-10-10 03:43:44 +02:00
|
|
|
return typeof it == 'symbol';
|
|
|
|
};
|
|
|
|
|
|
|
|
var $defineProperty = function defineProperty(it, key, D){
|
2017-04-20 03:09:25 +02:00
|
|
|
if(D && has(AllSymbols, key)){
|
2016-10-10 03:43:44 +02:00
|
|
|
if(!D.enumerable){
|
2017-04-20 03:09:25 +02:00
|
|
|
if(!has(it, HIDDEN))setDesc(it, HIDDEN, createDesc(1, {}));
|
2016-10-10 03:43:44 +02:00
|
|
|
it[HIDDEN][key] = true;
|
|
|
|
} else {
|
|
|
|
if(has(it, HIDDEN) && it[HIDDEN][key])it[HIDDEN][key] = false;
|
|
|
|
D = _create(D, {enumerable: createDesc(0, false)});
|
|
|
|
} return setSymbolDesc(it, key, D);
|
2017-04-20 03:09:25 +02:00
|
|
|
} return setDesc(it, key, D);
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
|
|
|
var $defineProperties = function defineProperties(it, P){
|
|
|
|
anObject(it);
|
|
|
|
var keys = enumKeys(P = toIObject(P))
|
|
|
|
, i = 0
|
|
|
|
, l = keys.length
|
|
|
|
, key;
|
|
|
|
while(l > i)$defineProperty(it, key = keys[i++], P[key]);
|
|
|
|
return it;
|
|
|
|
};
|
|
|
|
var $create = function create(it, P){
|
|
|
|
return P === undefined ? _create(it) : $defineProperties(_create(it), P);
|
|
|
|
};
|
|
|
|
var $propertyIsEnumerable = function propertyIsEnumerable(key){
|
2017-04-20 03:09:25 +02:00
|
|
|
var E = isEnum.call(this, key);
|
|
|
|
return E || !has(this, key) || !has(AllSymbols, key) || has(this, HIDDEN) && this[HIDDEN][key]
|
|
|
|
? E : true;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
|
|
|
var $getOwnPropertyDescriptor = function getOwnPropertyDescriptor(it, key){
|
2017-04-20 03:09:25 +02:00
|
|
|
var D = getDesc(it = toIObject(it), key);
|
2016-10-10 03:43:44 +02:00
|
|
|
if(D && has(AllSymbols, key) && !(has(it, HIDDEN) && it[HIDDEN][key]))D.enumerable = true;
|
|
|
|
return D;
|
|
|
|
};
|
|
|
|
var $getOwnPropertyNames = function getOwnPropertyNames(it){
|
2017-04-20 03:09:25 +02:00
|
|
|
var names = getNames(toIObject(it))
|
2016-10-10 03:43:44 +02:00
|
|
|
, result = []
|
|
|
|
, i = 0
|
|
|
|
, key;
|
2017-04-20 03:09:25 +02:00
|
|
|
while(names.length > i)if(!has(AllSymbols, key = names[i++]) && key != HIDDEN)result.push(key);
|
|
|
|
return result;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
|
|
|
var $getOwnPropertySymbols = function getOwnPropertySymbols(it){
|
2017-04-20 03:09:25 +02:00
|
|
|
var names = getNames(toIObject(it))
|
2016-10-10 03:43:44 +02:00
|
|
|
, result = []
|
|
|
|
, i = 0
|
|
|
|
, key;
|
2017-04-20 03:09:25 +02:00
|
|
|
while(names.length > i)if(has(AllSymbols, key = names[i++]))result.push(AllSymbols[key]);
|
|
|
|
return result;
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
2017-04-20 03:09:25 +02:00
|
|
|
var $stringify = function stringify(it){
|
|
|
|
if(it === undefined || isSymbol(it))return; // IE8 returns string on undefined
|
|
|
|
var args = [it]
|
|
|
|
, i = 1
|
|
|
|
, $$ = arguments
|
|
|
|
, replacer, $replacer;
|
|
|
|
while($$.length > i)args.push($$[i++]);
|
|
|
|
replacer = args[1];
|
|
|
|
if(typeof replacer == 'function')$replacer = replacer;
|
|
|
|
if($replacer || !isArray(replacer))replacer = function(key, value){
|
|
|
|
if($replacer)value = $replacer.call(this, key, value);
|
|
|
|
if(!isSymbol(value))return value;
|
|
|
|
};
|
|
|
|
args[1] = replacer;
|
|
|
|
return _stringify.apply($JSON, args);
|
|
|
|
};
|
|
|
|
var buggyJSON = $fails(function(){
|
|
|
|
var S = $Symbol();
|
|
|
|
// MS Edge converts symbol values to JSON as {}
|
|
|
|
// WebKit converts symbol values to JSON as null
|
|
|
|
// V8 throws on boxed symbols
|
|
|
|
return _stringify([S]) != '[null]' || _stringify({a: S}) != '{}' || _stringify(Object(S)) != '{}';
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// 19.4.1.1 Symbol([description])
|
2017-04-20 03:09:25 +02:00
|
|
|
if(!useNative){
|
2016-10-10 03:43:44 +02:00
|
|
|
$Symbol = function Symbol(){
|
2017-04-20 03:09:25 +02:00
|
|
|
if(isSymbol(this))throw TypeError('Symbol is not a constructor');
|
|
|
|
return wrap(uid(arguments.length > 0 ? arguments[0] : undefined));
|
2016-10-10 03:43:44 +02:00
|
|
|
};
|
2017-04-20 03:09:25 +02:00
|
|
|
redefine($Symbol.prototype, 'toString', function toString(){
|
2016-10-10 03:43:44 +02:00
|
|
|
return this._k;
|
|
|
|
});
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
isSymbol = function(it){
|
|
|
|
return it instanceof $Symbol;
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
$.create = $create;
|
|
|
|
$.isEnum = $propertyIsEnumerable;
|
|
|
|
$.getDesc = $getOwnPropertyDescriptor;
|
|
|
|
$.setDesc = $defineProperty;
|
|
|
|
$.setDescs = $defineProperties;
|
|
|
|
$.getNames = $names.get = $getOwnPropertyNames;
|
|
|
|
$.getSymbols = $getOwnPropertySymbols;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
if(DESCRIPTORS && !require('./$.library')){
|
|
|
|
redefine(ObjectProto, 'propertyIsEnumerable', $propertyIsEnumerable, true);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
var symbolStatics = {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 19.4.2.1 Symbol.for(key)
|
|
|
|
'for': function(key){
|
|
|
|
return has(SymbolRegistry, key += '')
|
|
|
|
? SymbolRegistry[key]
|
|
|
|
: SymbolRegistry[key] = $Symbol(key);
|
|
|
|
},
|
|
|
|
// 19.4.2.5 Symbol.keyFor(sym)
|
|
|
|
keyFor: function keyFor(key){
|
2017-04-20 03:09:25 +02:00
|
|
|
return keyOf(SymbolRegistry, key);
|
2016-10-10 03:43:44 +02:00
|
|
|
},
|
|
|
|
useSetter: function(){ setter = true; },
|
|
|
|
useSimple: function(){ setter = false; }
|
2017-04-20 03:09:25 +02:00
|
|
|
};
|
|
|
|
// 19.4.2.2 Symbol.hasInstance
|
|
|
|
// 19.4.2.3 Symbol.isConcatSpreadable
|
|
|
|
// 19.4.2.4 Symbol.iterator
|
|
|
|
// 19.4.2.6 Symbol.match
|
|
|
|
// 19.4.2.8 Symbol.replace
|
|
|
|
// 19.4.2.9 Symbol.search
|
|
|
|
// 19.4.2.10 Symbol.species
|
|
|
|
// 19.4.2.11 Symbol.split
|
|
|
|
// 19.4.2.12 Symbol.toPrimitive
|
|
|
|
// 19.4.2.13 Symbol.toStringTag
|
|
|
|
// 19.4.2.14 Symbol.unscopables
|
|
|
|
$.each.call((
|
|
|
|
'hasInstance,isConcatSpreadable,iterator,match,replace,search,' +
|
|
|
|
'species,split,toPrimitive,toStringTag,unscopables'
|
|
|
|
).split(','), function(it){
|
|
|
|
var sym = wks(it);
|
|
|
|
symbolStatics[it] = useNative ? sym : wrap(sym);
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
setter = true;
|
|
|
|
|
|
|
|
$export($export.G + $export.W, {Symbol: $Symbol});
|
|
|
|
|
|
|
|
$export($export.S, 'Symbol', symbolStatics);
|
|
|
|
|
|
|
|
$export($export.S + $export.F * !useNative, 'Object', {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 19.1.2.2 Object.create(O [, Properties])
|
|
|
|
create: $create,
|
|
|
|
// 19.1.2.4 Object.defineProperty(O, P, Attributes)
|
|
|
|
defineProperty: $defineProperty,
|
|
|
|
// 19.1.2.3 Object.defineProperties(O, Properties)
|
|
|
|
defineProperties: $defineProperties,
|
|
|
|
// 19.1.2.6 Object.getOwnPropertyDescriptor(O, P)
|
|
|
|
getOwnPropertyDescriptor: $getOwnPropertyDescriptor,
|
|
|
|
// 19.1.2.7 Object.getOwnPropertyNames(O)
|
|
|
|
getOwnPropertyNames: $getOwnPropertyNames,
|
|
|
|
// 19.1.2.8 Object.getOwnPropertySymbols(O)
|
|
|
|
getOwnPropertySymbols: $getOwnPropertySymbols
|
|
|
|
});
|
|
|
|
|
|
|
|
// 24.3.2 JSON.stringify(value [, replacer [, space]])
|
2017-04-20 03:09:25 +02:00
|
|
|
$JSON && $export($export.S + $export.F * (!useNative || buggyJSON), 'JSON', {stringify: $stringify});
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// 19.4.3.5 Symbol.prototype[@@toStringTag]
|
|
|
|
setToStringTag($Symbol, 'Symbol');
|
|
|
|
// 20.2.1.9 Math[@@toStringTag]
|
|
|
|
setToStringTag(Math, 'Math', true);
|
|
|
|
// 24.3.3 JSON[@@toStringTag]
|
|
|
|
setToStringTag(global.JSON, 'JSON', true);
|