2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
2017-04-20 03:09:25 +02:00
|
|
|
var $ = require('./$')
|
|
|
|
, redefine = require('./$.redefine')
|
|
|
|
, weak = require('./$.collection-weak')
|
|
|
|
, isObject = require('./$.is-object')
|
|
|
|
, has = require('./$.has')
|
|
|
|
, frozenStore = weak.frozenStore
|
|
|
|
, WEAK = weak.WEAK
|
|
|
|
, isExtensible = Object.isExtensible || isObject
|
|
|
|
, tmp = {};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
// 23.3 WeakMap Objects
|
|
|
|
var $WeakMap = require('./$.collection')('WeakMap', function(get){
|
|
|
|
return function WeakMap(){ return get(this, arguments.length > 0 ? arguments[0] : undefined); };
|
|
|
|
}, {
|
2016-10-10 03:43:44 +02:00
|
|
|
// 23.3.3.3 WeakMap.prototype.get(key)
|
|
|
|
get: function get(key){
|
|
|
|
if(isObject(key)){
|
2017-04-20 03:09:25 +02:00
|
|
|
if(!isExtensible(key))return frozenStore(this).get(key);
|
|
|
|
if(has(key, WEAK))return key[WEAK][this._i];
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// 23.3.3.5 WeakMap.prototype.set(key, value)
|
|
|
|
set: function set(key, value){
|
|
|
|
return weak.def(this, key, value);
|
|
|
|
}
|
2017-04-20 03:09:25 +02:00
|
|
|
}, weak, true, true);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
// IE11 WeakMap frozen keys fix
|
|
|
|
if(new $WeakMap().set((Object.freeze || Object)(tmp), 7).get(tmp) != 7){
|
2017-04-20 03:09:25 +02:00
|
|
|
$.each.call(['delete', 'has', 'get', 'set'], function(key){
|
2016-10-10 03:43:44 +02:00
|
|
|
var proto = $WeakMap.prototype
|
|
|
|
, method = proto[key];
|
|
|
|
redefine(proto, key, function(a, b){
|
2017-04-20 03:09:25 +02:00
|
|
|
// store frozen objects on leaky map
|
2016-10-10 03:43:44 +02:00
|
|
|
if(isObject(a) && !isExtensible(a)){
|
2017-04-20 03:09:25 +02:00
|
|
|
var result = frozenStore(this)[key](a, b);
|
2016-10-10 03:43:44 +02:00
|
|
|
return key == 'set' ? this : result;
|
|
|
|
// store all the rest on native weakmap
|
|
|
|
} return method.call(this, a, b);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|