SourceHolder.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class SimpleSourceHolder;
  9. class ISourceHolder : public FinalizableObject
  10. {
  11. private:
  12. static SimpleSourceHolder const emptySourceHolder;
  13. static LPCUTF8 const emptyString;
  14. public:
  15. static ISourceHolder *GetEmptySourceHolder()
  16. {
  17. return (ISourceHolder *)&emptySourceHolder;
  18. }
  19. virtual LPCUTF8 GetSource(const char16* reasonString) = 0;
  20. virtual size_t GetByteLength(const char16* reasonString) = 0;
  21. virtual bool Equals(ISourceHolder* other) = 0;
  22. virtual hash_t GetHashCode() = 0;
  23. virtual bool IsEmpty() = 0;
  24. virtual bool IsDeferrable() = 0;
  25. virtual void Unload() = 0;
  26. };
  27. class SimpleSourceHolder : public ISourceHolder
  28. {
  29. friend class ISourceHolder;
  30. protected:
  31. Field(LPCUTF8) source;
  32. Field(size_t) byteLength;
  33. Field(bool) isEmpty;
  34. SimpleSourceHolder(NO_WRITE_BARRIER_TAG_TYPE(LPCUTF8 source), size_t byteLength, bool isEmpty)
  35. : source(NO_WRITE_BARRIER_TAG(source)),
  36. byteLength(byteLength),
  37. isEmpty(isEmpty)
  38. {
  39. }
  40. public:
  41. SimpleSourceHolder(LPCUTF8 source, size_t byteLength)
  42. : source(source),
  43. byteLength(byteLength),
  44. isEmpty(false)
  45. {
  46. }
  47. virtual LPCUTF8 GetSource(const char16* reasonString) override
  48. {
  49. return source;
  50. }
  51. virtual size_t GetByteLength(const char16* reasonString) override { return byteLength; }
  52. virtual bool Equals(ISourceHolder* other) override
  53. {
  54. const char16* reason = _u("Equal Comparison");
  55. return this == other ||
  56. (this->GetByteLength(reason) == other->GetByteLength(reason)
  57. && (this->GetSource(reason) == other->GetSource(reason)
  58. || memcmp(this->GetSource(reason), other->GetSource(reason), this->GetByteLength(reason)) == 0 ));
  59. }
  60. virtual bool IsEmpty() override
  61. {
  62. return this->isEmpty;
  63. }
  64. virtual hash_t GetHashCode() override
  65. {
  66. Assert(byteLength < MAXUINT32);
  67. return JsUtil::CharacterBuffer<utf8char_t>::StaticGetHashCode(source, (charcount_t)byteLength);
  68. }
  69. virtual void Finalize(bool isShutdown) override
  70. {
  71. }
  72. virtual void Dispose(bool isShutdown) override
  73. {
  74. }
  75. virtual void Mark(Recycler * recycler) override
  76. {
  77. }
  78. virtual void Unload() override { }
  79. virtual bool IsDeferrable() override
  80. {
  81. return CONFIG_FLAG(DeferLoadingAvailableSource);
  82. }
  83. };
  84. class HeapSourceHolder : public SimpleSourceHolder
  85. {
  86. public:
  87. HeapSourceHolder(LPCUTF8 source, size_t byteLength, BYTE* originalSourceBuffer):
  88. SimpleSourceHolder(source, byteLength),
  89. shouldFreeSource(true),
  90. originalSourceBuffer(originalSourceBuffer)
  91. { }
  92. virtual void Unload() override;
  93. virtual void Dispose(bool isShutdown) override;
  94. private:
  95. bool shouldFreeSource;
  96. BYTE* originalSourceBuffer;
  97. };
  98. }