mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-07 08:15:59 -10:00
* use std::move() for local variables that are copied but not reused Addresses ~140 Coverity "Variable copied when it could be moved" defects across 74 files. Every std::move() targets a local variable whose value is consumed by push_back, emplace, assignment, lambda capture, or a function call and is never read again afterward. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * clang: use std::make_shared * clang: use std::make_unique * address feedback --------- Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
94 lines
2.3 KiB
C++
94 lines
2.3 KiB
C++
|
|
#include "RocketLuaSystemInterface.h"
|
|
|
|
#include "scripting/lua/LuaTable.h"
|
|
|
|
#include <Rocket/Core/ElementDocument.h>
|
|
#include <Rocket/Core/ValueReference.h>
|
|
|
|
namespace scpui {
|
|
|
|
namespace {
|
|
|
|
const char* LuaEnvironmentIdentifier = "document_lua_environment";
|
|
|
|
class LuaTableReference : public Rocket::Core::ValueReference {
|
|
public:
|
|
LuaTableReference(const luacpp::LuaTable& envtable) : _envtable(envtable) {}
|
|
~LuaTableReference() override = default;
|
|
|
|
const luacpp::LuaTable& getEnvTable() const
|
|
{
|
|
return _envtable;
|
|
}
|
|
|
|
private:
|
|
luacpp::LuaTable _envtable;
|
|
};
|
|
|
|
luacpp::LuaTable createEnvironment(lua_State* L)
|
|
{
|
|
// local env = {}
|
|
auto env = luacpp::LuaTable::create(L);
|
|
|
|
// local metatable = {}
|
|
auto metatable = luacpp::LuaTable::create(L);
|
|
|
|
// metatable.__index = _G (non-local lookups go to the global table)
|
|
metatable.pushValue(L);
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "_G");
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
lua_pop(L, 1);
|
|
|
|
// Use that simply metatable for the environment
|
|
env.setMetatable(metatable);
|
|
|
|
return env;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
RocketLuaSystemInterface::RocketLuaSystemInterface() = default;
|
|
RocketLuaSystemInterface::~RocketLuaSystemInterface() = default;
|
|
void RocketLuaSystemInterface::PrepareFunction(lua_State* L, int funcIdx, Rocket::Core::Element* context)
|
|
{
|
|
if (context == nullptr) {
|
|
// Nothing we can do in this case
|
|
return;
|
|
}
|
|
|
|
// To ensure consistent stack indices even for relative funcIdx values
|
|
auto top = lua_gettop(L);
|
|
lua_pushvalue(L, funcIdx);
|
|
|
|
auto envTable =
|
|
static_cast<LuaTableReference*>(context->GetOwnerDocument()->GetUserValue(LuaEnvironmentIdentifier));
|
|
|
|
if (envTable == nullptr) {
|
|
auto reference = std::make_unique<LuaTableReference>(createEnvironment(L));
|
|
envTable = reference.get();
|
|
|
|
context->GetOwnerDocument()->PutUserValue(LuaEnvironmentIdentifier, std::move(reference));
|
|
}
|
|
|
|
// Set the table as the environment of the function
|
|
envTable->getEnvTable().pushValue(L);
|
|
lua_setfenv(L, -2);
|
|
|
|
lua_settop(L, top);
|
|
}
|
|
void RocketLuaSystemInterface::ReportError(lua_State* L, const Rocket::Core::String& location)
|
|
{
|
|
if (!location.Empty()) {
|
|
LuaError(L, "Location: %s", location.CString());
|
|
} else {
|
|
LuaError(L);
|
|
}
|
|
}
|
|
int RocketLuaSystemInterface::ErrorHandler(lua_State* L) {
|
|
return scripting::ade_friendly_error(L);
|
|
}
|
|
|
|
} // namespace scpui
|