Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/src/NativeProtoExecData.cpp
2746 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/NativeProtoExecData.h"
3
4
#include "Luau/Common.h"
5
6
#include <new>
7
8
namespace Luau
9
{
10
namespace CodeGen
11
{
12
13
[[nodiscard]] static size_t computeNativeExecDataSize(uint32_t bytecodeInstructionCount, uint32_t extraDataCount) noexcept
14
{
15
return sizeof(NativeProtoExecDataHeader) + (bytecodeInstructionCount * sizeof(uint32_t)) + (extraDataCount * sizeof(uint32_t));
16
}
17
18
void NativeProtoExecDataDeleter::operator()(const uint32_t* instructionOffsets) const noexcept
19
{
20
destroyNativeProtoExecData(instructionOffsets);
21
}
22
23
[[nodiscard]] NativeProtoExecDataPtr createNativeProtoExecData(uint32_t bytecodeInstructionCount, uint32_t extraDataCount)
24
{
25
std::unique_ptr<uint8_t[]> bytes = std::make_unique<uint8_t[]>(computeNativeExecDataSize(bytecodeInstructionCount, extraDataCount));
26
new (static_cast<void*>(bytes.get())) NativeProtoExecDataHeader{};
27
return NativeProtoExecDataPtr{reinterpret_cast<uint32_t*>(bytes.release() + sizeof(NativeProtoExecDataHeader))};
28
}
29
30
void destroyNativeProtoExecData(const uint32_t* instructionOffsets) noexcept
31
{
32
const NativeProtoExecDataHeader* header = &getNativeProtoExecDataHeader(instructionOffsets);
33
header->~NativeProtoExecDataHeader();
34
delete[] reinterpret_cast<const uint8_t*>(header);
35
}
36
37
[[nodiscard]] NativeProtoExecDataHeader& getNativeProtoExecDataHeader(uint32_t* instructionOffsets) noexcept
38
{
39
return *reinterpret_cast<NativeProtoExecDataHeader*>(reinterpret_cast<uint8_t*>(instructionOffsets) - sizeof(NativeProtoExecDataHeader));
40
}
41
42
[[nodiscard]] const NativeProtoExecDataHeader& getNativeProtoExecDataHeader(const uint32_t* instructionOffsets) noexcept
43
{
44
return *reinterpret_cast<const NativeProtoExecDataHeader*>(
45
reinterpret_cast<const uint8_t*>(instructionOffsets) - sizeof(NativeProtoExecDataHeader)
46
);
47
}
48
49
} // namespace CodeGen
50
} // namespace Luau
51
52