forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteBlock.cpp
More file actions
118 lines (97 loc) · 3.79 KB
/
ByteBlock.cpp
File metadata and controls
118 lines (97 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeByteCodePch.h"
namespace Js
{
uint ByteBlock::GetLength() const
{
return m_contentSize;
}
const byte* ByteBlock::GetBuffer() const
{
return m_content;
}
byte* ByteBlock::GetBuffer()
{
return m_content;
}
const byte ByteBlock::operator[](uint itemIndex) const
{
AssertMsg(itemIndex < m_contentSize, "Ensure valid offset");
return m_content[itemIndex];
}
byte& ByteBlock::operator[] (uint itemIndex)
{
AssertMsg(itemIndex < m_contentSize, "Ensure valid offset");
return m_content[itemIndex];
}
ByteBlock *ByteBlock::New(Recycler *alloc, const byte * initialContent, int initialContentSize)
{
// initialContent may be 'null' if no data to copy
AssertMsg(initialContentSize > 0, "Must have valid data size");
ByteBlock *newBlock = RecyclerNew(alloc, ByteBlock, initialContentSize, alloc);
//
// Copy any optional data into the block:
// - If initialContent was not provided, the block's contents will be uninitialized.
//
if (initialContent != nullptr)
{
js_memcpy_s(newBlock->m_content, newBlock->GetLength(), initialContent, initialContentSize);
}
return newBlock;
}
ByteBlock *ByteBlock::NewFromArena(ArenaAllocator *alloc, const byte * initialContent, int initialContentSize)
{
// initialContent may be 'null' if no data to copy
AssertMsg(initialContentSize > 0, "Must have valid data size");
ByteBlock *newBlock = Anew(alloc, ByteBlock, initialContentSize, alloc);
//
// Copy any optional data into the block:
// - If initialContent was not provided, the block's contents will be uninitialized.
//
if (initialContent != nullptr)
{
js_memcpy_s(newBlock->m_content, newBlock->GetLength(), initialContent, initialContentSize);
}
return newBlock;
}
ByteBlock *ByteBlock::New(Recycler *alloc, const byte * initialContent, int initialContentSize, ScriptContext * requestContext)
{
// initialContent may be 'null' if no data to copy
AssertMsg(initialContentSize > 0, "Must have valid data size");
ByteBlock *newBlock = RecyclerNew(alloc, ByteBlock, initialContentSize, alloc);
//
// Copy any optional data into the block:
// - If initialContent was not provided, the block's contents will be uninitialized.
//
if (initialContent != nullptr)
{
//
// Treat initialContent as array of vars
// Clone vars to the requestContext
//
Var *src = (Var*)initialContent;
Var *dst = (Var*)newBlock->m_content;
size_t count = initialContentSize / sizeof(Var);
for (size_t i = 0; i < count; i++)
{
if (TaggedInt::Is(src[i]))
{
dst[i] = src[i];
}
else
{
//
// Currently only numbers are put into AuxiliaryContext data
//
Assert(JavascriptNumber::Is(src[i]));
dst[i] = JavascriptNumber::CloneToScriptContext(src[i], requestContext);
requestContext->BindReference(dst[i]);
}
}
}
return newBlock;
}
}