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.

186 lines
6.6 KiB

  1. 'use strict';
  2. var defineProperty = require('../internals/object-define-property').f;
  3. var create = require('../internals/object-create');
  4. var redefineAll = require('../internals/redefine-all');
  5. var bind = require('../internals/function-bind-context');
  6. var anInstance = require('../internals/an-instance');
  7. var iterate = require('../internals/iterate');
  8. var defineIterator = require('../internals/define-iterator');
  9. var setSpecies = require('../internals/set-species');
  10. var DESCRIPTORS = require('../internals/descriptors');
  11. var fastKey = require('../internals/internal-metadata').fastKey;
  12. var InternalStateModule = require('../internals/internal-state');
  13. var setInternalState = InternalStateModule.set;
  14. var internalStateGetterFor = InternalStateModule.getterFor;
  15. module.exports = {
  16. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  17. var C = wrapper(function (that, iterable) {
  18. anInstance(that, C, CONSTRUCTOR_NAME);
  19. setInternalState(that, {
  20. type: CONSTRUCTOR_NAME,
  21. index: create(null),
  22. first: undefined,
  23. last: undefined,
  24. size: 0
  25. });
  26. if (!DESCRIPTORS) that.size = 0;
  27. if (iterable != undefined) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  28. });
  29. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  30. var define = function (that, key, value) {
  31. var state = getInternalState(that);
  32. var entry = getEntry(that, key);
  33. var previous, index;
  34. // change existing entry
  35. if (entry) {
  36. entry.value = value;
  37. // create new entry
  38. } else {
  39. state.last = entry = {
  40. index: index = fastKey(key, true),
  41. key: key,
  42. value: value,
  43. previous: previous = state.last,
  44. next: undefined,
  45. removed: false
  46. };
  47. if (!state.first) state.first = entry;
  48. if (previous) previous.next = entry;
  49. if (DESCRIPTORS) state.size++;
  50. else that.size++;
  51. // add to index
  52. if (index !== 'F') state.index[index] = entry;
  53. } return that;
  54. };
  55. var getEntry = function (that, key) {
  56. var state = getInternalState(that);
  57. // fast case
  58. var index = fastKey(key);
  59. var entry;
  60. if (index !== 'F') return state.index[index];
  61. // frozen object case
  62. for (entry = state.first; entry; entry = entry.next) {
  63. if (entry.key == key) return entry;
  64. }
  65. };
  66. redefineAll(C.prototype, {
  67. // 23.1.3.1 Map.prototype.clear()
  68. // 23.2.3.2 Set.prototype.clear()
  69. clear: function clear() {
  70. var that = this;
  71. var state = getInternalState(that);
  72. var data = state.index;
  73. var entry = state.first;
  74. while (entry) {
  75. entry.removed = true;
  76. if (entry.previous) entry.previous = entry.previous.next = undefined;
  77. delete data[entry.index];
  78. entry = entry.next;
  79. }
  80. state.first = state.last = undefined;
  81. if (DESCRIPTORS) state.size = 0;
  82. else that.size = 0;
  83. },
  84. // 23.1.3.3 Map.prototype.delete(key)
  85. // 23.2.3.4 Set.prototype.delete(value)
  86. 'delete': function (key) {
  87. var that = this;
  88. var state = getInternalState(that);
  89. var entry = getEntry(that, key);
  90. if (entry) {
  91. var next = entry.next;
  92. var prev = entry.previous;
  93. delete state.index[entry.index];
  94. entry.removed = true;
  95. if (prev) prev.next = next;
  96. if (next) next.previous = prev;
  97. if (state.first == entry) state.first = next;
  98. if (state.last == entry) state.last = prev;
  99. if (DESCRIPTORS) state.size--;
  100. else that.size--;
  101. } return !!entry;
  102. },
  103. // 23.2.3.6 Set.prototype.forEach(callbackfn, thisArg = undefined)
  104. // 23.1.3.5 Map.prototype.forEach(callbackfn, thisArg = undefined)
  105. forEach: function forEach(callbackfn /* , that = undefined */) {
  106. var state = getInternalState(this);
  107. var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3);
  108. var entry;
  109. while (entry = entry ? entry.next : state.first) {
  110. boundFunction(entry.value, entry.key, this);
  111. // revert to the last existing entry
  112. while (entry && entry.removed) entry = entry.previous;
  113. }
  114. },
  115. // 23.1.3.7 Map.prototype.has(key)
  116. // 23.2.3.7 Set.prototype.has(value)
  117. has: function has(key) {
  118. return !!getEntry(this, key);
  119. }
  120. });
  121. redefineAll(C.prototype, IS_MAP ? {
  122. // 23.1.3.6 Map.prototype.get(key)
  123. get: function get(key) {
  124. var entry = getEntry(this, key);
  125. return entry && entry.value;
  126. },
  127. // 23.1.3.9 Map.prototype.set(key, value)
  128. set: function set(key, value) {
  129. return define(this, key === 0 ? 0 : key, value);
  130. }
  131. } : {
  132. // 23.2.3.1 Set.prototype.add(value)
  133. add: function add(value) {
  134. return define(this, value = value === 0 ? 0 : value, value);
  135. }
  136. });
  137. if (DESCRIPTORS) defineProperty(C.prototype, 'size', {
  138. get: function () {
  139. return getInternalState(this).size;
  140. }
  141. });
  142. return C;
  143. },
  144. setStrong: function (C, CONSTRUCTOR_NAME, IS_MAP) {
  145. var ITERATOR_NAME = CONSTRUCTOR_NAME + ' Iterator';
  146. var getInternalCollectionState = internalStateGetterFor(CONSTRUCTOR_NAME);
  147. var getInternalIteratorState = internalStateGetterFor(ITERATOR_NAME);
  148. // add .keys, .values, .entries, [@@iterator]
  149. // 23.1.3.4, 23.1.3.8, 23.1.3.11, 23.1.3.12, 23.2.3.5, 23.2.3.8, 23.2.3.10, 23.2.3.11
  150. defineIterator(C, CONSTRUCTOR_NAME, function (iterated, kind) {
  151. setInternalState(this, {
  152. type: ITERATOR_NAME,
  153. target: iterated,
  154. state: getInternalCollectionState(iterated),
  155. kind: kind,
  156. last: undefined
  157. });
  158. }, function () {
  159. var state = getInternalIteratorState(this);
  160. var kind = state.kind;
  161. var entry = state.last;
  162. // revert to the last existing entry
  163. while (entry && entry.removed) entry = entry.previous;
  164. // get next entry
  165. if (!state.target || !(state.last = entry = entry ? entry.next : state.state.first)) {
  166. // or finish the iteration
  167. state.target = undefined;
  168. return { value: undefined, done: true };
  169. }
  170. // return step by kind
  171. if (kind == 'keys') return { value: entry.key, done: false };
  172. if (kind == 'values') return { value: entry.value, done: false };
  173. return { value: [entry.key, entry.value], done: false };
  174. }, IS_MAP ? 'entries' : 'values', !IS_MAP, true);
  175. // add [@@species], 23.1.2.2, 23.2.2.2
  176. setSpecies(CONSTRUCTOR_NAME);
  177. }
  178. };