Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Config/include/Luau/LuauConfig.h
2727 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/Common.h"
5
#include "Luau/Config.h"
6
#include "Luau/DenseHash.h"
7
#include "Luau/Variant.h"
8
9
#include <string>
10
11
struct lua_State;
12
13
namespace Luau
14
{
15
16
constexpr const char* kLuauConfigName = ".config.luau";
17
18
struct ConfigTableKey : public Variant<std::string, double>
19
{
20
ConfigTableKey() = default;
21
ConfigTableKey(const std::string& s)
22
: Variant(s)
23
{
24
}
25
ConfigTableKey(std::string&& s)
26
: Variant(std::move(s))
27
{
28
}
29
ConfigTableKey(const char* s)
30
: Variant(std::string(s))
31
{
32
}
33
ConfigTableKey(double d)
34
: Variant(d)
35
{
36
}
37
38
std::string toString() const
39
{
40
if (const double* number = get_if<double>())
41
return std::to_string(*number);
42
else if (const std::string* str = get_if<std::string>())
43
return *str;
44
45
LUAU_UNREACHABLE();
46
}
47
};
48
49
template<typename VariantType>
50
struct VariantHashDefault
51
{
52
size_t operator()(const VariantType& variant) const
53
{
54
size_t result = 0;
55
visit(
56
[&result](auto&& value)
57
{
58
result = detail::DenseHashDefault<std::decay_t<decltype(value)>>{}(value);
59
},
60
variant
61
);
62
return result;
63
}
64
};
65
66
// Forward declaration
67
struct ConfigValue;
68
69
struct ConfigTable : public DenseHashMap<ConfigTableKey, ConfigValue, VariantHashDefault<ConfigTableKey>>
70
{
71
ConfigTable()
72
: DenseHashMap<ConfigTableKey, ConfigValue, VariantHashDefault<ConfigTableKey>>({})
73
{
74
}
75
};
76
77
struct ConfigValue : public Variant<std::string, double, bool, ConfigTable>
78
{
79
using Variant::Variant;
80
};
81
82
struct InterruptCallbacks
83
{
84
std::function<void(lua_State*)> initCallback;
85
void (*interruptCallback)(lua_State* L, int gc);
86
};
87
88
// Executes the contents of `source` in a sandboxed Luau VM and extracts the
89
// configuration table that it returns. If extraction fails, std::nullopt is
90
// returned and `error` is populated with the error message, if provided.
91
std::optional<ConfigTable> extractConfig(const std::string& source, const InterruptCallbacks& callbacks, std::string* error = nullptr);
92
93
// Extracts a Luau::Config object into `config` from the configuration code in
94
// `source`, returning an error message if extraction fails.
95
std::optional<std::string> extractLuauConfig(
96
const std::string& source,
97
Config& config,
98
std::optional<ConfigOptions::AliasOptions> aliasOptions,
99
InterruptCallbacks callbacks
100
);
101
102
} // namespace Luau
103
104