Pull whitelists out of functions for easier modification in future
#include <Utils.h>
-LuaSandbox::LuaSandbox(sol::state_view aStateView)
- : m_lua(aStateView)
-{
-}
-
-void LuaSandbox::Initialize(sol::state_view aStateView)
-{
- // initialize state + environment first
- m_lua = aStateView;
- m_env = { aStateView, sol::create };
-
- // copy whitelisted things from global table
- constexpr const char* whitelistedGlobals[] =
+static constexpr const char* GlobalObjectsWhitelist[] =
{
"assert",
"error",
@@ -79,18 +67,59 @@ void LuaSandbox::Initialize(sol::state_view aStateView)
"__Game",
"__Type",
};
- const auto globals = m_lua.globals();
- for (const auto& key : whitelistedGlobals)
- m_env[key].set(globals[key].get<sol::object>());
- // copy whitelisted libs from global table
- constexpr const char* whitelistedTables[] =
+static constexpr const char* GlobalTablesWhitelist[] =
{
"string",
"table",
"math",
};
- for (const auto &tableKey : whitelistedTables)
+
+static constexpr const char* GlobalExtraLibsWhitelist[] =
+{
+ "ImGui",
+ "ImGuiCond",
+ "ImGuiTreeNodeFlags",
+ "ImGuiSelectableFlags",
+ "ImGuiInputTextFlags",
+ "ImGuiColorEditFlags",
+ "ImGuiComboFlags",
+ "ImGuiHoveredFlags",
+ "ImGuiFocusedFlags",
+ "ImGuiPopupFlags",
+ "ImGuiTabItemFlags",
+ "ImGuiWindowFlags",
+ "ImGuiStyleVar",
+ "ImGuiTabBarFlags",
+ "ImGuiSliderFlags",
+ "ImGuiTableFlags",
+ "ImGuiTableColumnFlags",
+ "ImGuiTableRowFlags",
+ "ImGuiTableBgTarget",
+ "ImGuiMouseButton",
+ "ImGuiCol",
+ "ImGuiDir",
+ "json"
+};
+
+LuaSandbox::LuaSandbox(sol::state_view aStateView)
+ : m_lua(aStateView)
+{
+}
+
+void LuaSandbox::Initialize(sol::state_view aStateView)
+{
+ // initialize state + environment first
+ m_lua = aStateView;
+ m_env = { aStateView, sol::create };
+
+ // copy whitelisted things from global table
+ const auto globals = m_lua.globals();
+ for (const auto& key : GlobalObjectsWhitelist)
+ m_env[key].set(globals[key].get<sol::object>());
+
+ // copy whitelisted libs from global table
+ for (const auto &tableKey : GlobalTablesWhitelist)
m_env[tableKey].set(globals[tableKey].get<sol::table>());
// copy safe os functions
@@ -196,34 +225,8 @@ void LuaSandbox::InitializeExtraLibsForSandbox(Sandbox& aSandbox) const
auto& sbEnv = aSandbox.GetEnvironment();
// copy extra whitelisted libs from global table
- constexpr const char* whitelistedTables[] =
- {
- "ImGui",
- "ImGuiCond",
- "ImGuiTreeNodeFlags",
- "ImGuiSelectableFlags",
- "ImGuiInputTextFlags",
- "ImGuiColorEditFlags",
- "ImGuiComboFlags",
- "ImGuiHoveredFlags",
- "ImGuiFocusedFlags",
- "ImGuiPopupFlags",
- "ImGuiTabItemFlags",
- "ImGuiWindowFlags",
- "ImGuiStyleVar",
- "ImGuiTabBarFlags",
- "ImGuiSliderFlags",
- "ImGuiTableFlags",
- "ImGuiTableColumnFlags",
- "ImGuiTableRowFlags",
- "ImGuiTableBgTarget",
- "ImGuiMouseButton",
- "ImGuiCol",
- "ImGuiDir",
- "json"
- };
auto globals = m_lua.globals();
- for (const auto &tableKey : whitelistedTables)
+ for (const auto &tableKey : GlobalExtraLibsWhitelist)
sbEnv[tableKey].set(globals[tableKey].get<sol::table>());
}
