2
0

StringCopyInfo.cpp 4.5 KB

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