utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 getReferenceToBytes(bufferSource) {
  7. // Node.js' Buffer does not allow subclassing for now, so we can get away with a prototype object check for perf.
  8. if (Object.getPrototypeOf(bufferSource) === Buffer.prototype) {
  9. return bufferSource;
  10. }
  11. if (bufferSource instanceof ArrayBuffer) {
  12. return Buffer.from(bufferSource);
  13. }
  14. return Buffer.from(bufferSource.buffer, bufferSource.byteOffset, bufferSource.byteLength);
  15. }
  16. function getCopyToBytes(bufferSource) {
  17. return Buffer.from(getReferenceToBytes(bufferSource));
  18. }
  19. function mixin(target, source) {
  20. const keys = Object.getOwnPropertyNames(source);
  21. for (let i = 0; i < keys.length; ++i) {
  22. if (keys[i] in target) {
  23. continue;
  24. }
  25. Object.defineProperty(target, keys[i], Object.getOwnPropertyDescriptor(source, keys[i]));
  26. }
  27. }
  28. const wrapperSymbol = Symbol("wrapper");
  29. const implSymbol = Symbol("impl");
  30. const sameObjectCaches = Symbol("SameObject caches");
  31. function getSameObject(wrapper, prop, creator) {
  32. if (!wrapper[sameObjectCaches]) {
  33. wrapper[sameObjectCaches] = Object.create(null);
  34. }
  35. if (prop in wrapper[sameObjectCaches]) {
  36. return wrapper[sameObjectCaches][prop];
  37. }
  38. wrapper[sameObjectCaches][prop] = creator();
  39. return wrapper[sameObjectCaches][prop];
  40. }
  41. function wrapperForImpl(impl) {
  42. return impl ? impl[wrapperSymbol] : null;
  43. }
  44. function implForWrapper(wrapper) {
  45. return wrapper ? wrapper[implSymbol] : null;
  46. }
  47. function tryWrapperForImpl(impl) {
  48. const wrapper = wrapperForImpl(impl);
  49. return wrapper ? wrapper : impl;
  50. }
  51. function tryImplForWrapper(wrapper) {
  52. const impl = implForWrapper(wrapper);
  53. return impl ? impl : wrapper;
  54. }
  55. const iterInternalSymbol = Symbol("internal");
  56. const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
  57. function isArrayIndexPropName(P) {
  58. if (typeof P !== "string") {
  59. return false;
  60. }
  61. const i = P >>> 0;
  62. if (i === Math.pow(2, 32) - 1) {
  63. return false;
  64. }
  65. const s = `${i}`;
  66. if (P !== s) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. const supportsPropertyIndex = Symbol("supports property index");
  72. const supportedPropertyIndices = Symbol("supported property indices");
  73. const supportsPropertyName = Symbol("supports property name");
  74. const supportedPropertyNames = Symbol("supported property names");
  75. const indexedGet = Symbol("indexed property get");
  76. const indexedSetNew = Symbol("indexed property set new");
  77. const indexedSetExisting = Symbol("indexed property set existing");
  78. const namedGet = Symbol("named property get");
  79. const namedSetNew = Symbol("named property set new");
  80. const namedSetExisting = Symbol("named property set existing");
  81. const namedDelete = Symbol("named property delete");
  82. module.exports = exports = {
  83. isObject,
  84. getReferenceToBytes,
  85. getCopyToBytes,
  86. mixin,
  87. wrapperSymbol,
  88. implSymbol,
  89. getSameObject,
  90. wrapperForImpl,
  91. implForWrapper,
  92. tryWrapperForImpl,
  93. tryImplForWrapper,
  94. iterInternalSymbol,
  95. IteratorPrototype,
  96. isArrayIndexPropName,
  97. supportsPropertyIndex,
  98. supportedPropertyIndices,
  99. supportsPropertyName,
  100. supportedPropertyNames,
  101. indexedGet,
  102. indexedSetNew,
  103. indexedSetExisting,
  104. namedGet,
  105. namedSetNew,
  106. namedSetExisting,
  107. namedDelete
  108. };