StreamWriter.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include "SCACorePch.h"
  6. namespace Js
  7. {
  8. void StreamWriter::Write(const void* pv, size_t cb)
  9. {
  10. ScriptContext* scriptContext = GetScriptContext();
  11. uint32 newSize = UInt32Math::Add((uint32)m_current, (uint32)cb);
  12. if (newSize >= m_capacity)
  13. {
  14. size_t newCapacity = UInt32Math::Add(max(newSize, UInt32Math::Mul((uint32)m_capacity, 2)), 100);
  15. BEGIN_LEAVE_SCRIPT(scriptContext)
  16. {
  17. m_buffer = m_stream->ExtendBuffer(m_buffer, newCapacity, &m_capacity);
  18. }
  19. END_LEAVE_SCRIPT(scriptContext);
  20. }
  21. Assert(m_buffer != nullptr);
  22. js_memcpy_s(m_buffer + m_current, cb, pv, cb);
  23. m_current += cb;
  24. }
  25. void StreamWriter::WriteHostObject(void* data)
  26. {
  27. ScriptContext* scriptContext = GetScriptContext();
  28. BEGIN_LEAVE_SCRIPT(scriptContext)
  29. {
  30. m_stream->WriteHostObject(data);
  31. }
  32. END_LEAVE_SCRIPT(scriptContext);
  33. }
  34. //
  35. // Overload to count for buffer position.
  36. //
  37. scaposition_t StreamWriter::GetPosition() const
  38. {
  39. // If this overflows, we will throw during Flush/RealWrite. So skip checking here.
  40. return static_cast<scaposition_t>(m_current);
  41. }
  42. }