web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.8 KiB

  1. 'use strict';
  2. var redefineAll = require('../internals/redefine-all');
  3. var getWeakData = require('../internals/internal-metadata').getWeakData;
  4. var anObject = require('../internals/an-object');
  5. var isObject = require('../internals/is-object');
  6. var anInstance = require('../internals/an-instance');
  7. var iterate = require('../internals/iterate');
  8. var ArrayIterationModule = require('../internals/array-iteration');
  9. var $has = require('../internals/has');
  10. var InternalStateModule = require('../internals/internal-state');
  11. var setInternalState = InternalStateModule.set;
  12. var internalStateGetterFor = InternalStateModule.getterFor;
  13. var find = ArrayIterationModule.find;
  14. var findIndex = ArrayIterationModule.findIndex;
  15. var id = 0;
  16. // fallback for uncaught frozen keys
  17. var uncaughtFrozenStore = function (store) {
  18. return store.frozen || (store.frozen = new UncaughtFrozenStore());
  19. };
  20. var UncaughtFrozenStore = function () {
  21. this.entries = [];
  22. };
  23. var findUncaughtFrozen = function (store, key) {
  24. return find(store.entries, function (it) {
  25. return it[0] === key;
  26. });
  27. };
  28. UncaughtFrozenStore.prototype = {
  29. get: function (key) {
  30. var entry = findUncaughtFrozen(this, key);
  31. if (entry) return entry[1];
  32. },
  33. has: function (key) {
  34. return !!findUncaughtFrozen(this, key);
  35. },
  36. set: function (key, value) {
  37. var entry = findUncaughtFrozen(this, key);
  38. if (entry) entry[1] = value;
  39. else this.entries.push([key, value]);
  40. },
  41. 'delete': function (key) {
  42. var index = findIndex(this.entries, function (it) {
  43. return it[0] === key;
  44. });
  45. if (~index) this.entries.splice(index, 1);
  46. return !!~index;
  47. }
  48. };
  49. module.exports = {
  50. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  51. var C = wrapper(function (that, iterable) {
  52. anInstance(that, C, CONSTRUCTOR_NAME);
  53. setInternalState(that, {
  54. type: CONSTRUCTOR_NAME,
  55. id: id++,
  56. frozen: undefined
  57. });
  58. if (iterable != undefined) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  59. });
  60. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  61. var define = function (that, key, value) {
  62. var state = getInternalState(that);
  63. var data = getWeakData(anObject(key), true);
  64. if (data === true) uncaughtFrozenStore(state).set(key, value);
  65. else data[state.id] = value;
  66. return that;
  67. };
  68. redefineAll(C.prototype, {
  69. // 23.3.3.2 WeakMap.prototype.delete(key)
  70. // 23.4.3.3 WeakSet.prototype.delete(value)
  71. 'delete': function (key) {
  72. var state = getInternalState(this);
  73. if (!isObject(key)) return false;
  74. var data = getWeakData(key);
  75. if (data === true) return uncaughtFrozenStore(state)['delete'](key);
  76. return data && $has(data, state.id) && delete data[state.id];
  77. },
  78. // 23.3.3.4 WeakMap.prototype.has(key)
  79. // 23.4.3.4 WeakSet.prototype.has(value)
  80. has: function has(key) {
  81. var state = getInternalState(this);
  82. if (!isObject(key)) return false;
  83. var data = getWeakData(key);
  84. if (data === true) return uncaughtFrozenStore(state).has(key);
  85. return data && $has(data, state.id);
  86. }
  87. });
  88. redefineAll(C.prototype, IS_MAP ? {
  89. // 23.3.3.3 WeakMap.prototype.get(key)
  90. get: function get(key) {
  91. var state = getInternalState(this);
  92. if (isObject(key)) {
  93. var data = getWeakData(key);
  94. if (data === true) return uncaughtFrozenStore(state).get(key);
  95. return data ? data[state.id] : undefined;
  96. }
  97. },
  98. // 23.3.3.5 WeakMap.prototype.set(key, value)
  99. set: function set(key, value) {
  100. return define(this, key, value);
  101. }
  102. } : {
  103. // 23.4.3.1 WeakSet.prototype.add(value)
  104. add: function add(value) {
  105. return define(this, value, true);
  106. }
  107. });
  108. return C;
  109. }
  110. };