#pragma once
#include "Luau/CodeGenCommon.h"
#include "Luau/CodeGenOptions.h"
#include "Luau/LoweringStats.h"
#include <array>
#include <memory>
#include <string>
#include <vector>
#include <stddef.h>
#include <stdint.h>
struct lua_State;
namespace Luau
{
namespace CodeGen
{
enum class CodeGenCompilationResult
{
Success = 0,
NothingToCompile = 1,
NotNativeModule = 2,
CodeGenNotInitialized = 3,
CodeGenOverflowInstructionLimit = 4,
CodeGenOverflowBlockLimit = 5,
CodeGenOverflowBlockInstructionLimit = 6,
CodeGenAssemblerFinalizationFailure = 7,
CodeGenLoweringFailure = 8,
AllocationFailed = 9,
Count = 10,
};
std::string toString(const CodeGenCompilationResult& result);
struct ProtoCompilationFailure
{
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
std::string debugname;
int line = -1;
};
struct CompilationResult
{
CodeGenCompilationResult result = CodeGenCompilationResult::Success;
std::vector<ProtoCompilationFailure> protoFailures;
[[nodiscard]] bool hasErrors() const
{
return result != CodeGenCompilationResult::Success || !protoFailures.empty();
}
};
struct CompilationStats
{
size_t bytecodeSizeBytes = 0;
size_t nativeCodeSizeBytes = 0;
size_t nativeDataSizeBytes = 0;
size_t nativeMetadataSizeBytes = 0;
uint32_t functionsTotal = 0;
uint32_t functionsCompiled = 0;
uint32_t functionsBound = 0;
};
bool isSupported();
class SharedCodeGenContext;
struct SharedCodeGenContextDeleter
{
void operator()(const SharedCodeGenContext* context) const noexcept;
};
using UniqueSharedCodeGenContext = std::unique_ptr<SharedCodeGenContext, SharedCodeGenContextDeleter>;
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext();
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(AllocationCallback* allocationCallback, void* allocationCallbackContext);
[[nodiscard]] UniqueSharedCodeGenContext createSharedCodeGenContext(
size_t blockSize,
size_t maxTotalSize,
AllocationCallback* allocationCallback,
void* allocationCallbackContext
);
void destroySharedCodeGenContext(const SharedCodeGenContext* codeGenContext) noexcept;
void create(lua_State* L);
void create(lua_State* L, AllocationCallback* allocationCallback, void* allocationCallbackContext);
void create(lua_State* L, size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
void create(lua_State* L, SharedCodeGenContext* codeGenContext);
[[nodiscard]] bool isNativeExecutionEnabled(lua_State* L);
void setNativeExecutionEnabled(lua_State* L, bool enabled);
void disableNativeExecutionForFunction(lua_State* L, const int level) noexcept;
using UserdataRemapperCallback = uint8_t(void* context, const char* name, size_t nameLength);
void setUserdataRemapper(lua_State* L, void* context, UserdataRemapperCallback cb);
using ModuleId = std::array<uint8_t, 16>;
CompilationResult compile(lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, unsigned int flags, CompilationStats* stats = nullptr);
CompilationResult compile(lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
CompilationResult compile(const ModuleId& moduleId, lua_State* L, int idx, const CompilationOptions& options, CompilationStats* stats = nullptr);
std::string getAssembly(lua_State* L, int idx, AssemblyOptions options = {}, LoweringStats* stats = nullptr);
using PerfLogFn = void (*)(void* context, uintptr_t addr, unsigned size, const char* symbol);
void setPerfLog(void* context, PerfLogFn logFn);
}
}