Skip to content

Commit

Permalink
Remove some unnecessary dynamic vectors/strings (#7862)
Browse files Browse the repository at this point in the history
* Eliminate dynamic vector & string storage where there's a simpler alternative.
Code tweak in prepareForEvaluation to use logical OR rather than bitwise and const the initial bool variable.

* Pick the nit.

* Fix issue with constexpr of initializer_list that MSVC doesn't object to.

* Restore named sandboxWhitelist, & sandboxLibraryTables.
More ref usage.
2 functions can be static.

* format

---------

Co-authored-by: David Lowndes <David@DAVIDPCZ>
  • Loading branch information
Dave-Lowndes and David Lowndes authored Nov 22, 2024
1 parent ad9d5f3 commit 12b5da2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
33 changes: 20 additions & 13 deletions src/common/LuaSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int Surge::LuaSupport::parseStringDefiningMultipleFunctions(
// sloppy
int res = 0;
std::vector<std::string> frev(functions.rbegin(), functions.rend());
for (auto functionName : frev)
for (const auto &functionName : frev)
{
lua_getglobal(L, functionName.c_str());
if (lua_isfunction(L, -1))
Expand All @@ -124,7 +124,7 @@ int Surge::LuaSupport::parseStringDefiningMultipleFunctions(
#endif
}

int lua_limitRange(lua_State *L)
static int lua_limitRange(lua_State *L)
{
#if HAS_LUA
auto x = luaL_checknumber(L, -3);
Expand All @@ -137,7 +137,7 @@ int lua_limitRange(lua_State *L)
}

// custom print that outputs limited amount of arguments and restricts use to strings and numbers
int lua_sandboxPrint(lua_State *L)
static int lua_sandboxPrint(lua_State *L)
{
#if HAS_LUA
int n = lua_gettop(L); // number of arguments
Expand Down Expand Up @@ -181,38 +181,45 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L)
lua_setfield(L, eidx, sharedTableName);

// add whitelisted functions and modules
std::vector<std::string> sandboxWhitelist = {"pairs", "ipairs", "unpack",

// clang-format off
static constexpr std::initializer_list<const char *> sandboxWhitelist
{"pairs", "ipairs", "unpack",
"next", "type", "tostring",
"tonumber", "setmetatable", "error"};
// clang-format on

for (const auto &f : sandboxWhitelist)
{
lua_getglobal(L, f.c_str()); // stack: f>t>f
if (lua_isnil(L, -1)) // check if the global exists
lua_getglobal(L, f); // stack: f>t>f
if (lua_isnil(L, -1)) // check if the global exists
{
lua_pop(L, 1);
std::cout << "Error: global not found [ " << f.c_str() << " ]" << std::endl;
std::cout << "Error: global not found [ " << f << " ]" << std::endl;
continue;
}
lua_setfield(L, -2, f.c_str()); // stack: f>t
lua_setfield(L, -2, f); // stack: f>t
}

// add library tables
std::vector<std::string> sandboxLibraryTables = {"math", "string", "table", "bit"};
// clang-format off
static constexpr std::initializer_list<const char *> sandboxLibraryTables = {"math", "string", "table", "bit"};
// clang-format on
for (const auto &t : sandboxLibraryTables)
{
lua_getglobal(L, t.c_str()); // stack: f>t>(t)
lua_getglobal(L, t); // stack: f>t>(t)
int gidx = lua_gettop(L);
if (!lua_istable(L, gidx))
{
lua_pop(L, 1);
std::cout << "Error: not a table [ " << t.c_str() << " ]" << std::endl;
std::cout << "Error: not a table [ " << t << " ]" << std::endl;
continue;
}

// we want to add to a local table so the entries in the global table can't be overwritten
lua_createtable(L, 0, 10); // stack: f>t>(t)>t
lua_setfield(L, eidx, t.c_str());
lua_getfield(L, eidx, t.c_str());
lua_setfield(L, eidx, t);
lua_getfield(L, eidx, t);
int lidx = lua_gettop(L);

lua_pushnil(L);
Expand Down
13 changes: 5 additions & 8 deletions src/common/dsp/modulators/FormulaModulationHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,9 @@ end
{
lua_pushnumber(s.L, i + 1);
lua_gettable(s.L, -2);
bool res = false;
if (lua_isboolean(s.L, -1))
res = lua_toboolean(s.L, -1);
const bool res = (lua_isboolean(s.L, -1)) ? lua_toboolean(s.L, -1) : false;
lua_pop(s.L, 1);
s.subAnyMacro = s.subAnyMacro | res;
s.subAnyMacro = s.subAnyMacro || res;
s.subMacros[i] = res;
}
}
Expand Down Expand Up @@ -832,10 +830,9 @@ std::vector<DebugRow> createDebugDataOfModState(const EvaluatorState &es)
}
};

std::vector<std::string> tablesList = {es.stateName, sharedTableName};
for (const auto &t : tablesList)
for (const auto &t : {es.stateName, sharedTableName})
{
lua_getglobal(es.L, t.c_str());
lua_getglobal(es.L, t);
if (!lua_istable(es.L, -1))
{
lua_pop(es.L, -1);
Expand Down Expand Up @@ -1007,4 +1004,4 @@ end
}

} // namespace Formula
} // namespace Surge
} // namespace Surge
4 changes: 2 additions & 2 deletions src/surge-testrunner/UnitTestsDSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ TEST_CASE("Unison at Different Sample Rates", "[osc]")
}
};

std::vector<int> srs = {{44100, 48000}};
static constexpr std::initializer_list<int> srs{44100, 48000};

SECTION("Wavetable Oscillator")
{
Expand Down Expand Up @@ -746,4 +746,4 @@ TEST_CASE("Oscillator Onset", "[dsp]") // See issue 7570
<< std::endl;*/
}
}
}
}

0 comments on commit 12b5da2

Please sign in to comment.