utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. function hasOwn(obj, prop) {
  7. return Object.prototype.hasOwnProperty.call(obj, prop);
  8. }
  9. const wrapperSymbol = Symbol("wrapper");
  10. const implSymbol = Symbol("impl");
  11. const sameObjectCaches = Symbol("SameObject caches");
  12. const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");
  13. function getSameObject(wrapper, prop, creator) {
  14. if (!wrapper[sameObjectCaches]) {
  15. wrapper[sameObjectCaches] = Object.create(null);
  16. }
  17. if (prop in wrapper[sameObjectCaches]) {
  18. return wrapper[sameObjectCaches][prop];
  19. }
  20. wrapper[sameObjectCaches][prop] = creator();
  21. return wrapper[sameObjectCaches][prop];
  22. }
  23. function wrapperForImpl(impl) {
  24. return impl ? impl[wrapperSymbol] : null;
  25. }
  26. function implForWrapper(wrapper) {
  27. return wrapper ? wrapper[implSymbol] : null;
  28. }
  29. function tryWrapperForImpl(impl) {
  30. const wrapper = wrapperForImpl(impl);
  31. return wrapper ? wrapper : impl;
  32. }
  33. function tryImplForWrapper(wrapper) {
  34. const impl = implForWrapper(wrapper);
  35. return impl ? impl : wrapper;
  36. }
  37. const iterInternalSymbol = Symbol("internal");
  38. const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
  39. function isArrayIndexPropName(P) {
  40. if (typeof P !== "string") {
  41. return false;
  42. }
  43. const i = P >>> 0;
  44. if (i === Math.pow(2, 32) - 1) {
  45. return false;
  46. }
  47. const s = `${i}`;
  48. if (P !== s) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. const byteLengthGetter =
  54. Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
  55. function isArrayBuffer(value) {
  56. try {
  57. byteLengthGetter.call(value);
  58. return true;
  59. } catch (e) {
  60. return false;
  61. }
  62. }
  63. const supportsPropertyIndex = Symbol("supports property index");
  64. const supportedPropertyIndices = Symbol("supported property indices");
  65. const supportsPropertyName = Symbol("supports property name");
  66. const supportedPropertyNames = Symbol("supported property names");
  67. const indexedGet = Symbol("indexed property get");
  68. const indexedSetNew = Symbol("indexed property set new");
  69. const indexedSetExisting = Symbol("indexed property set existing");
  70. const namedGet = Symbol("named property get");
  71. const namedSetNew = Symbol("named property set new");
  72. const namedSetExisting = Symbol("named property set existing");
  73. const namedDelete = Symbol("named property delete");
  74. module.exports = exports = {
  75. isObject,
  76. hasOwn,
  77. wrapperSymbol,
  78. implSymbol,
  79. getSameObject,
  80. ctorRegistrySymbol,
  81. wrapperForImpl,
  82. implForWrapper,
  83. tryWrapperForImpl,
  84. tryImplForWrapper,
  85. iterInternalSymbol,
  86. IteratorPrototype,
  87. isArrayBuffer,
  88. isArrayIndexPropName,
  89. supportsPropertyIndex,
  90. supportedPropertyIndices,
  91. supportsPropertyName,
  92. supportedPropertyNames,
  93. indexedGet,
  94. indexedSetNew,
  95. indexedSetExisting,
  96. namedGet,
  97. namedSetNew,
  98. namedSetExisting,
  99. namedDelete
  100. };