StringCopyInfo.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // ChakraDiag does not link with Runtime.lib and does not include .cpp files, so this file will be included as a header
  6. #include "RuntimeLibraryPch.h"
  7. #include "DataStructures/LargeStack.h"
  8. namespace Js
  9. {
  10. #pragma region StringCopyInfo
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. inline StringCopyInfo::StringCopyInfo()
  13. {
  14. // This constructor is just to satisfy LargeStack for now, as it creates an array of these. Ideally, it should only
  15. // instantiate this class for pushed items using the copy constructor.
  16. #if DBG
  17. isInitialized = false;
  18. #endif
  19. }
  20. StringCopyInfo::StringCopyInfo(
  21. JavascriptString *const sourceString,
  22. _Inout_count_(sourceString->m_charLength) char16 *const destinationBuffer)
  23. : sourceString(sourceString), destinationBuffer(destinationBuffer)
  24. {
  25. Assert(sourceString);
  26. Assert(destinationBuffer);
  27. #if DBG
  28. isInitialized = true;
  29. #endif
  30. }
  31. JavascriptString *StringCopyInfo::SourceString() const
  32. {
  33. Assert(isInitialized);
  34. return sourceString;
  35. }
  36. char16 *StringCopyInfo::DestinationBuffer() const
  37. {
  38. Assert(isInitialized);
  39. return destinationBuffer;
  40. }
  41. #ifndef IsJsDiag
  42. void StringCopyInfo::InstantiateForceInlinedMembers()
  43. {
  44. // Force-inlined functions defined in a translation unit need a reference from an extern non-force-inlined function in
  45. // the same translation unit to force an instantiation of the force-inlined function. Otherwise, if the force-inlined
  46. // function is not referenced in the same translation unit, it will not be generated and the linker is not able to find
  47. // the definition to inline the function in other translation units.
  48. AnalysisAssert(false);
  49. StringCopyInfo copyInfo;
  50. JavascriptString *const string = nullptr;
  51. char16 *const buffer = nullptr;
  52. (StringCopyInfo());
  53. (StringCopyInfo(string, buffer));
  54. copyInfo.SourceString();
  55. copyInfo.DestinationBuffer();
  56. }
  57. #endif
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. #pragma endregion
  60. #pragma region StringCopyInfoStack
  61. #ifndef IsJsDiag
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. StringCopyInfoStack::StringCopyInfoStack(ScriptContext *const scriptContext)
  64. : scriptContext(scriptContext), allocator(nullptr), stack(nullptr)
  65. {
  66. Assert(scriptContext);
  67. }
  68. StringCopyInfoStack::~StringCopyInfoStack()
  69. {
  70. if (allocator)
  71. {
  72. scriptContext->ReleaseTemporaryAllocator(allocator);
  73. }
  74. }
  75. bool StringCopyInfoStack::IsEmpty()
  76. {
  77. Assert(!allocator == !stack);
  78. return !stack || !!stack->Empty();
  79. }
  80. void StringCopyInfoStack::Push(const StringCopyInfo copyInfo)
  81. {
  82. Assert(!allocator == !stack);
  83. if(!stack)
  84. CreateStack();
  85. stack->Push(copyInfo);
  86. }
  87. const StringCopyInfo StringCopyInfoStack::Pop()
  88. {
  89. Assert(allocator);
  90. Assert(stack);
  91. return stack->Pop();
  92. }
  93. void StringCopyInfoStack::CreateStack()
  94. {
  95. Assert(!allocator);
  96. Assert(!stack);
  97. allocator = scriptContext->GetTemporaryAllocator(_u("StringCopyInfoStack"));
  98. Assert(allocator);
  99. stack = LargeStack<StringCopyInfo>::New(allocator->GetAllocator());
  100. Assert(stack);
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. #endif
  104. #pragma endregion
  105. }