utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. // Returns "Type(value) is Object" in ES terminology.
  3. function isObject(value) {
  4. return typeof value === "object" && value !== null || typeof value === "function";
  5. }
  6. const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
  7. const wrapperSymbol = Symbol("wrapper");
  8. const implSymbol = Symbol("impl");
  9. const sameObjectCaches = Symbol("SameObject caches");
  10. const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");
  11. function getSameObject(wrapper, prop, creator) {
  12. if (!wrapper[sameObjectCaches]) {
  13. wrapper[sameObjectCaches] = Object.create(null);
  14. }
  15. if (prop in wrapper[sameObjectCaches]) {
  16. return wrapper[sameObjectCaches][prop];
  17. }
  18. wrapper[sameObjectCaches][prop] = creator();
  19. return wrapper[sameObjectCaches][prop];
  20. }
  21. function wrapperForImpl(impl) {
  22. return impl ? impl[wrapperSymbol] : null;
  23. }
  24. function implForWrapper(wrapper) {
  25. return wrapper ? wrapper[implSymbol] : null;
  26. }
  27. function tryWrapperForImpl(impl) {
  28. const wrapper = wrapperForImpl(impl);
  29. return wrapper ? wrapper : impl;
  30. }
  31. function tryImplForWrapper(wrapper) {
  32. const impl = implForWrapper(wrapper);
  33. return impl ? impl : wrapper;
  34. }
  35. const iterInternalSymbol = Symbol("internal");
  36. const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
  37. const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);
  38. function isArrayIndexPropName(P) {
  39. if (typeof P !== "string") {
  40. return false;
  41. }
  42. const i = P >>> 0;
  43. if (i === Math.pow(2, 32) - 1) {
  44. return false;
  45. }
  46. const s = `${i}`;
  47. if (P !== s) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. const byteLengthGetter =
  53. Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
  54. function isArrayBuffer(value) {
  55. try {
  56. byteLengthGetter.call(value);
  57. return true;
  58. } catch (e) {
  59. return false;
  60. }
  61. }
  62. function iteratorResult([key, value], kind) {
  63. let result;
  64. switch (kind) {
  65. case "key":
  66. result = key;
  67. break;
  68. case "value":
  69. result = value;
  70. break;
  71. case "key+value":
  72. result = [key, value];
  73. break;
  74. }
  75. return { value: result, done: false };
  76. }
  77. const supportsPropertyIndex = Symbol("supports property index");
  78. const supportedPropertyIndices = Symbol("supported property indices");
  79. const supportsPropertyName = Symbol("supports property name");
  80. const supportedPropertyNames = Symbol("supported property names");
  81. const indexedGet = Symbol("indexed property get");
  82. const indexedSetNew = Symbol("indexed property set new");
  83. const indexedSetExisting = Symbol("indexed property set existing");
  84. const namedGet = Symbol("named property get");
  85. const namedSetNew = Symbol("named property set new");
  86. const namedSetExisting = Symbol("named property set existing");
  87. const namedDelete = Symbol("named property delete");
  88. const asyncIteratorNext = Symbol("async iterator get the next iteration result");
  89. const asyncIteratorReturn = Symbol("async iterator return steps");
  90. const asyncIteratorInit = Symbol("async iterator initialization steps");
  91. const asyncIteratorEOI = Symbol("async iterator end of iteration");
  92. module.exports = exports = {
  93. isObject,
  94. hasOwn,
  95. wrapperSymbol,
  96. implSymbol,
  97. getSameObject,
  98. ctorRegistrySymbol,
  99. wrapperForImpl,
  100. implForWrapper,
  101. tryWrapperForImpl,
  102. tryImplForWrapper,
  103. iterInternalSymbol,
  104. IteratorPrototype,
  105. AsyncIteratorPrototype,
  106. isArrayBuffer,
  107. isArrayIndexPropName,
  108. supportsPropertyIndex,
  109. supportedPropertyIndices,
  110. supportsPropertyName,
  111. supportedPropertyNames,
  112. indexedGet,
  113. indexedSetNew,
  114. indexedSetExisting,
  115. namedGet,
  116. namedSetNew,
  117. namedSetExisting,
  118. namedDelete,
  119. asyncIteratorNext,
  120. asyncIteratorReturn,
  121. asyncIteratorInit,
  122. asyncIteratorEOI,
  123. iteratorResult
  124. };