Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/src/CodeGenContext.h
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/SharedCodeAllocator.h"
5
6
#include "NativeState.h"
7
8
#include <memory>
9
#include <optional>
10
#include <stdint.h>
11
12
namespace Luau
13
{
14
namespace CodeGen
15
{
16
17
// The "code-gen context" maintains the native code-gen state. There are two
18
// implementations. The StandaloneCodeGenContext is a VM-specific context type.
19
// It is the "simple" implementation that can be used when native code-gen is
20
// used with a single Luau VM. The SharedCodeGenContext supports use from
21
// multiple Luau VMs concurrently, and allows for sharing of executable native
22
// code and related metadata.
23
24
struct ModuleBindResult
25
{
26
CodeGenCompilationResult compilationResult = {};
27
28
uint32_t functionsBound = 0;
29
};
30
31
class BaseCodeGenContext
32
{
33
public:
34
BaseCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
35
virtual ~BaseCodeGenContext();
36
37
[[nodiscard]] bool initHeaderFunctions();
38
39
[[nodiscard]] virtual std::optional<ModuleBindResult> tryBindExistingModule(
40
const ModuleId& moduleId,
41
const std::vector<Proto*>& moduleProtos
42
) = 0;
43
44
[[nodiscard]] virtual ModuleBindResult bindModule(
45
const std::optional<ModuleId>& moduleId,
46
const std::vector<Proto*>& moduleProtos,
47
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
48
const uint8_t* data,
49
size_t dataSize,
50
const uint8_t* code,
51
size_t codeSize
52
) = 0;
53
54
virtual void onCloseState() noexcept = 0;
55
virtual void onDestroyFunction(void* execdata) noexcept = 0;
56
57
CodeAllocator codeAllocator;
58
std::unique_ptr<UnwindBuilder> unwindBuilder;
59
60
uint8_t* gateData_DEPRECATED = nullptr;
61
size_t gateDataSize_DEPRECATED = 0;
62
CodeAllocationData gateAllocationData;
63
64
void* userdataRemappingContext = nullptr;
65
UserdataRemapperCallback* userdataRemapper = nullptr;
66
67
NativeContext context;
68
};
69
70
class StandaloneCodeGenContext final : public BaseCodeGenContext
71
{
72
public:
73
StandaloneCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
74
75
[[nodiscard]] std::optional<ModuleBindResult> tryBindExistingModule(const ModuleId& moduleId, const std::vector<Proto*>& moduleProtos) override;
76
77
[[nodiscard]] ModuleBindResult bindModule(
78
const std::optional<ModuleId>& moduleId,
79
const std::vector<Proto*>& moduleProtos,
80
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
81
const uint8_t* data,
82
size_t dataSize,
83
const uint8_t* code,
84
size_t codeSize
85
) override;
86
87
void onCloseState() noexcept override;
88
void onDestroyFunction(void* execdata) noexcept override;
89
90
private:
91
SharedCodeAllocator sharedAllocator;
92
};
93
94
class SharedCodeGenContext final : public BaseCodeGenContext
95
{
96
public:
97
SharedCodeGenContext(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
98
99
[[nodiscard]] std::optional<ModuleBindResult> tryBindExistingModule(const ModuleId& moduleId, const std::vector<Proto*>& moduleProtos) override;
100
101
[[nodiscard]] ModuleBindResult bindModule(
102
const std::optional<ModuleId>& moduleId,
103
const std::vector<Proto*>& moduleProtos,
104
std::vector<NativeProtoExecDataPtr> nativeExecDatas,
105
const uint8_t* data,
106
size_t dataSize,
107
const uint8_t* code,
108
size_t codeSize
109
) override;
110
111
void onCloseState() noexcept override;
112
void onDestroyFunction(void* execdata) noexcept override;
113
114
private:
115
SharedCodeAllocator sharedAllocator;
116
};
117
118
} // namespace CodeGen
119
} // namespace Luau
120
121