es6.weak-map.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var global = require('./_global');
  3. var each = require('./_array-methods')(0);
  4. var redefine = require('./_redefine');
  5. var meta = require('./_meta');
  6. var assign = require('./_object-assign');
  7. var weak = require('./_collection-weak');
  8. var isObject = require('./_is-object');
  9. var validate = require('./_validate-collection');
  10. var NATIVE_WEAK_MAP = require('./_validate-collection');
  11. var IS_IE11 = !global.ActiveXObject && 'ActiveXObject' in global;
  12. var WEAK_MAP = 'WeakMap';
  13. var getWeak = meta.getWeak;
  14. var isExtensible = Object.isExtensible;
  15. var uncaughtFrozenStore = weak.ufstore;
  16. var InternalMap;
  17. var wrapper = function (get) {
  18. return function WeakMap() {
  19. return get(this, arguments.length > 0 ? arguments[0] : undefined);
  20. };
  21. };
  22. var methods = {
  23. // 23.3.3.3 WeakMap.prototype.get(key)
  24. get: function get(key) {
  25. if (isObject(key)) {
  26. var data = getWeak(key);
  27. if (data === true) return uncaughtFrozenStore(validate(this, WEAK_MAP)).get(key);
  28. return data ? data[this._i] : undefined;
  29. }
  30. },
  31. // 23.3.3.5 WeakMap.prototype.set(key, value)
  32. set: function set(key, value) {
  33. return weak.def(validate(this, WEAK_MAP), key, value);
  34. }
  35. };
  36. // 23.3 WeakMap Objects
  37. var $WeakMap = module.exports = require('./_collection')(WEAK_MAP, wrapper, methods, weak, true, true);
  38. // IE11 WeakMap frozen keys fix
  39. if (NATIVE_WEAK_MAP && IS_IE11) {
  40. InternalMap = weak.getConstructor(wrapper, WEAK_MAP);
  41. assign(InternalMap.prototype, methods);
  42. meta.NEED = true;
  43. each(['delete', 'has', 'get', 'set'], function (key) {
  44. var proto = $WeakMap.prototype;
  45. var method = proto[key];
  46. redefine(proto, key, function (a, b) {
  47. // store frozen objects on internal weakmap shim
  48. if (isObject(a) && !isExtensible(a)) {
  49. if (!this._f) this._f = new InternalMap();
  50. var result = this._f[key](a, b);
  51. return key == 'set' ? this : result;
  52. // store all the rest on native weakmap
  53. } return method.call(this, a, b);
  54. });
  55. });
  56. }