// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details1#pragma once23#include "Luau/ParseOptions.h"4#include "Luau/Location.h"5#include "Luau/StringUtils.h"6#include "Luau/Common.h"78namespace Luau9{10class AstNameTable;11struct ParseResult;12class BytecodeBuilder;13class BytecodeEncoder;1415using CompileConstant = void*;1617// return a type identifier for a global library member18// values are defined by 'enum LuauBytecodeType' in Bytecode.h19using LibraryMemberTypeCallback = int (*)(const char* library, const char* member);2021// setup a value of a constant for a global library member22// use setCompileConstant*** set of functions for values23using LibraryMemberConstantCallback = void (*)(const char* library, const char* member, CompileConstant* constant);2425// Note: this structure is duplicated in luacode.h, don't forget to change these in sync!26struct CompileOptions27{28// 0 - no optimization29// 1 - baseline optimization level that doesn't prevent debuggability30// 2 - includes optimizations that harm debuggability such as inlining31int optimizationLevel = 1;3233// 0 - no debugging support34// 1 - line info & function names only; sufficient for backtraces35// 2 - full debug info with local & upvalue names; necessary for debugger36int debugLevel = 1;3738// type information is used to guide native code generation decisions39// information includes testable typeArguments for function arguments, locals, upvalues and some temporaries40// 0 - generate for native modules41// 1 - generate for all modules42int typeInfoLevel = 0;4344// 0 - no code coverage support45// 1 - statement coverage46// 2 - statement and expression coverage (verbose)47int coverageLevel = 0;4849// alternative global builtin to construct vectors, in addition to default builtin 'vector.create'50const char* vectorLib = nullptr;51const char* vectorCtor = nullptr;5253// alternative vector type name for type tables, in addition to default type 'vector'54const char* vectorType = nullptr;5556// null-terminated array of globals that are mutable; disables the import optimization for fields accessed through these57const char* const* mutableGlobals = nullptr;5859// null-terminated array of userdata typeArguments that will be included in the type information60const char* const* userdataTypes = nullptr;6162// null-terminated array of globals which act as libraries and have members with known type and/or constant value63// when an import of one of these libraries is accessed, callbacks below will be called to receive that information64const char* const* librariesWithKnownMembers = nullptr;65LibraryMemberTypeCallback libraryMemberTypeCb = nullptr;66LibraryMemberConstantCallback libraryMemberConstantCb = nullptr;6768// null-terminated array of library functions that should not be compiled into a built-in fastcall ("name" "lib.name")69const char* const* disabledBuiltins = nullptr;70};7172class CompileError : public std::exception73{74public:75CompileError(const Location& location, std::string message);7677~CompileError() throw() override;7879const char* what() const throw() override;8081const Location& getLocation() const;8283static LUAU_NORETURN void raise(const Location& location, const char* format, ...) LUAU_PRINTF_ATTR(2, 3);8485private:86Location location;87std::string message;88};8990// compiles bytecode into bytecode builder using either a pre-parsed AST or parsing it from source; throws on errors91void compileOrThrow(BytecodeBuilder& bytecode, const ParseResult& parseResult, AstNameTable& names, const CompileOptions& options = {});92void compileOrThrow(BytecodeBuilder& bytecode, const std::string& source, const CompileOptions& options = {}, const ParseOptions& parseOptions = {});9394// compiles bytecode into a bytecode blob, that either contains the valid bytecode or an encoded error that luau_load can decode95std::string compile(96const std::string& source,97const CompileOptions& options = {},98const ParseOptions& parseOptions = {},99BytecodeEncoder* encoder = nullptr100);101102void setCompileConstantNil(CompileConstant* constant);103void setCompileConstantBoolean(CompileConstant* constant, bool b);104void setCompileConstantNumber(CompileConstant* constant, double n);105void setCompileConstantInteger64(CompileConstant* constant, int64_t l);106void setCompileConstantVector(CompileConstant* constant, float x, float y, float z, float w);107void setCompileConstantString(CompileConstant* constant, const char* s, size_t l);108109} // namespace Luau110111112