Repository navigation
Expand file tree
/
Copy pathRocketLuaSystemInterface.cpp
More file actions
93 lines (69 loc) · 2.27 KB
/
Copy pathRocketLuaSystemInterface.cpp
File metadata and controls
93 lines (69 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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