es6.weak-map.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var $ = require('./$')
  3. , redefine = require('./$.redefine')
  4. , weak = require('./$.collection-weak')
  5. , isObject = require('./$.is-object')
  6. , has = require('./$.has')
  7. , frozenStore = weak.frozenStore
  8. , WEAK = weak.WEAK
  9. , isExtensible = Object.isExtensible || isObject
  10. , tmp = {};
  11. // 23.3 WeakMap Objects
  12. var $WeakMap = require('./$.collection')('WeakMap', function(get){
  13. return function WeakMap(){ return get(this, arguments.length > 0 ? arguments[0] : undefined); };
  14. }, {
  15. // 23.3.3.3 WeakMap.prototype.get(key)
  16. get: function get(key){
  17. if(isObject(key)){
  18. if(!isExtensible(key))return frozenStore(this).get(key);
  19. if(has(key, WEAK))return key[WEAK][this._i];
  20. }
  21. },
  22. // 23.3.3.5 WeakMap.prototype.set(key, value)
  23. set: function set(key, value){
  24. return weak.def(this, key, value);
  25. }
  26. }, weak, true, true);
  27. // IE11 WeakMap frozen keys fix
  28. if(new $WeakMap().set((Object.freeze || Object)(tmp), 7).get(tmp) != 7){
  29. $.each.call(['delete', 'has', 'get', 'set'], function(key){
  30. var proto = $WeakMap.prototype
  31. , method = proto[key];
  32. redefine(proto, key, function(a, b){
  33. // store frozen objects on leaky map
  34. if(isObject(a) && !isExtensible(a)){
  35. var result = frozenStore(this)[key](a, b);
  36. return key == 'set' ? this : result;
  37. // store all the rest on native weakmap
  38. } return method.call(this, a, b);
  39. });
  40. });
  41. }