ByteBlock.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 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::New(Recycler *alloc, const byte * initialContent, int initialContentSize, ScriptContext * requestContext)
  61. {
  62. // initialContent may be 'null' if no data to copy
  63. AssertMsg(initialContentSize > 0, "Must have valid data size");
  64. ByteBlock *newBlock = RecyclerNew(alloc, ByteBlock, initialContentSize, alloc);
  65. //
  66. // Copy any optional data into the block:
  67. // - If initialContent was not provided, the block's contents will be uninitialized.
  68. //
  69. if (initialContent != nullptr)
  70. {
  71. //
  72. // Treat initialContent as array of vars
  73. // Clone vars to the requestContext
  74. //
  75. Var *src = (Var*)initialContent;
  76. Var *dst = (Var*)(byte*)newBlock->m_content;
  77. size_t count = initialContentSize / sizeof(Var);
  78. for (size_t i = 0; i < count; i++)
  79. {
  80. if (TaggedInt::Is(src[i]))
  81. {
  82. dst[i] = src[i];
  83. }
  84. else
  85. {
  86. //
  87. // Currently only numbers are put into AuxiliaryContext data
  88. //
  89. Assert(JavascriptNumber::Is(src[i]));
  90. dst[i] = JavascriptNumber::CloneToScriptContext(src[i], requestContext);
  91. requestContext->BindReference(dst[i]);
  92. }
  93. }
  94. }
  95. return newBlock;
  96. }
  97. }