ByteBlock.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "RuntimeByteCodePch.h"
  6. namespace Js
  7. {
  8. uint ByteBlock::GetLength() const
  9. {
  10. return m_contentSize;
  11. }
  12. const byte* ByteBlock::GetBuffer() const
  13. {
  14. return m_content;
  15. }
  16. byte* ByteBlock::GetBuffer()
  17. {
  18. return m_content;
  19. }
  20. const byte ByteBlock::operator[](uint itemIndex) const
  21. {
  22. AssertMsg(itemIndex < m_contentSize, "Ensure valid offset");
  23. return m_content[itemIndex];
  24. }
  25. byte& ByteBlock::operator[] (uint itemIndex)
  26. {
  27. AssertMsg(itemIndex < m_contentSize, "Ensure valid offset");
  28. return m_content[itemIndex];
  29. }
  30. ByteBlock *ByteBlock::New(Recycler *alloc, const byte * initialContent, int initialContentSize)
  31. {
  32. // initialContent may be 'null' if no data to copy
  33. AssertMsg(initialContentSize > 0, "Must have valid data size");
  34. ByteBlock *newBlock = RecyclerNew(alloc, ByteBlock, initialContentSize, alloc);
  35. //
  36. // Copy any optional data into the block:
  37. // - If initialContent was not provided, the block's contents will be uninitialized.
  38. //
  39. if (initialContent != nullptr)
  40. {
  41. js_memcpy_s(newBlock->m_content, newBlock->GetLength(), initialContent, initialContentSize);
  42. }
  43. return newBlock;
  44. }
  45. ByteBlock *ByteBlock::NewFromArena(ArenaAllocator *alloc, const byte * initialContent, int initialContentSize)
  46. {
  47. // initialContent may be 'null' if no data to copy
  48. AssertMsg(initialContentSize > 0, "Must have valid data size");
  49. ByteBlock *newBlock = Anew(alloc, ByteBlock, initialContentSize, alloc);
  50. //
  51. // Copy any optional data into the block:
  52. // - If initialContent was not provided, the block's contents will be uninitialized.
  53. //
  54. if (initialContent != nullptr)
  55. {
  56. js_memcpy_s(newBlock->m_content, newBlock->GetLength(), initialContent, initialContentSize);
  57. }
  58. return newBlock;
  59. }
  60. ByteBlock * ByteBlock::Clone(Recycler* alloc)
  61. {
  62. return ByteBlock::New(alloc, this->m_content, this->m_contentSize);
  63. }
  64. ByteBlock *ByteBlock::New(Recycler *alloc, const byte * initialContent, int initialContentSize, ScriptContext * requestContext)
  65. {
  66. // initialContent may be 'null' if no data to copy
  67. AssertMsg(initialContentSize > 0, "Must have valid data size");
  68. ByteBlock *newBlock = RecyclerNew(alloc, ByteBlock, initialContentSize, alloc);
  69. //
  70. // Copy any optional data into the block:
  71. // - If initialContent was not provided, the block's contents will be uninitialized.
  72. //
  73. if (initialContent != nullptr)
  74. {
  75. //
  76. // Treat initialContent as array of vars
  77. // Clone vars to the requestContext
  78. //
  79. Var *src = (Var*)initialContent;
  80. Var *dst = (Var*)newBlock->m_content;
  81. size_t count = initialContentSize / sizeof(Var);
  82. for (size_t i = 0; i < count; i++)
  83. {
  84. if (TaggedInt::Is(src[i]))
  85. {
  86. dst[i] = src[i];
  87. }
  88. else
  89. {
  90. //
  91. // Currently only numbers are put into AuxiliaryContext data
  92. //
  93. Assert(JavascriptNumber::Is(src[i]));
  94. dst[i] = JavascriptNumber::CloneToScriptContext(src[i], requestContext);
  95. requestContext->BindReference(dst[i]);
  96. }
  97. }
  98. }
  99. return newBlock;
  100. }
  101. //
  102. // Create a copy of buffer
  103. // Each Var is cloned on the requestContext
  104. //
  105. ByteBlock * ByteBlock::Clone(Recycler* alloc, ScriptContext * requestContext)
  106. {
  107. return ByteBlock::New(alloc, this->m_content, this->m_contentSize, requestContext);
  108. }
  109. }