SCACore.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace SCACore
  9. {
  10. HRESULT ValidateTransferableVars(Var *vars, size_t count)
  11. {
  12. for (size_t i = 0; i < count; i++)
  13. {
  14. Js::TypeId typeId = Js::JavascriptOperators::GetTypeId(vars[i]);
  15. if (typeId != TypeIds_ArrayBuffer)
  16. {
  17. AssertMsg(false, "These should have been filtered out by the host.");
  18. return E_SCA_TRANSFERABLE_UNSUPPORTED;
  19. }
  20. if (Js::JavascriptOperators::IsObjectDetached(vars[i]))
  21. {
  22. return E_SCA_TRANSFERABLE_NEUTERED;
  23. }
  24. }
  25. return S_OK;
  26. }
  27. HRESULT Serializer::SetTransferableVars(Var *vars, size_t count)
  28. {
  29. if (m_transferableVars != nullptr)
  30. {
  31. Assert(false);
  32. return E_FAIL;
  33. }
  34. else if (count > 0)
  35. {
  36. HRESULT hr = ValidateTransferableVars(vars, count);
  37. if (hr != S_OK)
  38. {
  39. return hr;
  40. }
  41. m_transferableVars = vars;
  42. m_cTransferableVars = count;
  43. }
  44. return S_OK;
  45. }
  46. bool Serializer::WriteValue(Var rootObject)
  47. {
  48. ScriptContext *scriptContext = m_streamWriter.GetScriptContext();
  49. BEGIN_JS_RUNTIME_CALL(scriptContext)
  50. {
  51. Js::SCASerializationEngine::Serialize(rootObject, &m_streamWriter, m_transferableVars, m_cTransferableVars, nullptr /*TBD*/);
  52. }
  53. END_JS_RUNTIME_CALL(scriptContext)
  54. return true;
  55. }
  56. bool Serializer::DetachArrayBuffer()
  57. {
  58. Assert(false);
  59. return true;
  60. }
  61. void Serializer::WriteRawBytes(const void* source, size_t length)
  62. {
  63. ScriptContext *scriptContext = m_streamWriter.GetScriptContext();
  64. BEGIN_JS_RUNTIME_CALL(scriptContext)
  65. {
  66. m_streamWriter.Write(source, length);
  67. }
  68. END_JS_RUNTIME_CALL(scriptContext)
  69. }
  70. bool Serializer::Release(byte** data, size_t *dataLength)
  71. {
  72. *data = m_streamWriter.GetBuffer();
  73. *dataLength = m_streamWriter.GetLength();
  74. return true;
  75. }
  76. bool Deserializer::ReadRawBytes(size_t length, void **data)
  77. {
  78. m_streamReader.ReadRawBytes(data, length);
  79. return true;
  80. }
  81. bool Deserializer::ReadBytes(size_t length, void **data)
  82. {
  83. m_streamReader.Read(*data, length);
  84. return true;
  85. }
  86. Var Deserializer::ReadValue()
  87. {
  88. Var returnedValue = nullptr;
  89. ScriptContext *scriptContext = m_streamReader.GetScriptContext();
  90. BEGIN_JS_RUNTIME_CALL(scriptContext)
  91. {
  92. returnedValue = Js::SCADeserializationEngine::Deserialize(&m_streamReader, m_transferableVars, m_cTransferableVars);
  93. }
  94. END_JS_RUNTIME_CALL(scriptContext)
  95. return returnedValue;
  96. }
  97. HRESULT Deserializer::SetTransferableVars(Var *vars, size_t count)
  98. {
  99. if (m_transferableVars != nullptr)
  100. {
  101. Assert(false);
  102. return E_FAIL;
  103. }
  104. else if (count > 0)
  105. {
  106. HRESULT hr = ValidateTransferableVars(vars, count);
  107. if (hr != S_OK)
  108. {
  109. return hr;
  110. }
  111. m_transferableVars = vars;
  112. m_cTransferableVars = count;
  113. }
  114. return S_OK;
  115. }
  116. }
  117. }