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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include "luacxx.hpp"
extern "C" {
#include <lauxlib.h>
#include <lualib.h>
}
namespace getsuyomi {
using fud::FudStatus;
LuaContext::LuaContext()
{
m_state = luaL_newstate();
if (m_state != nullptr) {
luaL_openlibs(m_state);
}
}
LuaContext::~LuaContext()
{
if (m_state != nullptr) {
lua_close(m_state);
m_state = nullptr;
}
}
LuaContext::LuaContext(LuaContext&& rhs) : m_state{rhs.m_state}
{
rhs.m_state = nullptr;
}
LuaContext& LuaContext::operator=(LuaContext&& rhs)
{
m_state = rhs.m_state;
rhs.m_state = nullptr;
return *this;
}
FudStatus LuaContext::loadFile(const char* filename)
{
if (!valid())
{
return FudStatus::ObjectInvalid;
}
if (luaL_loadfile(m_state, filename) || lua_pcall(m_state, 0, 0, 0)) {
return FudStatus::Failure;
}
return FudStatus::Success;
}
LuaResult<int64_t> LuaContext::getGlobalInteger(const char* name)
{
if (m_state == nullptr) {
return LuaResult<int64_t>::error(FudStatus::ObjectInvalid);
}
int isNumber{};
int64_t result{};
auto luaType = lua_getglobal(m_state, name);
// discard luaType since isNumber will check if variable is integral
static_cast<void>(luaType);
result = lua_tointegerx(m_state, -1, &isNumber);
lua_pop(m_state, 1);
if (!static_cast<bool>(isNumber)) {
return LuaResult<int64_t>::error(FudStatus::Failure);
}
return LuaResult<int64_t>::okay(result);
}
LuaResult<const char*> LuaContext::getGlobalString(const char* name)
{
if (m_state == nullptr) {
return LuaResult<const char*>::error(FudStatus::ObjectInvalid);
}
size_t length;
const char* result{nullptr};
auto luaType = lua_getglobal(m_state, name);
static_cast<void>(luaType);
result = lua_tolstring(m_state, -1, &length);
lua_pop(m_state, 1);
if (result == nullptr) {
return LuaResult<const char*>::error(FudStatus::Failure);
}
return LuaResult<const char*>::okay(result);
}
LuaResult<std::vector<std::string>> LuaContext::getGlobalStringArray(const char* name)
{
using RetType = LuaResult<std::vector<std::string>>;
if (m_state == nullptr) {
return RetType::error(FudStatus::ObjectInvalid);
}
auto luaType = lua_getglobal(m_state, name);
static_cast<void>(luaType);
if (!lua_istable(m_state, -1)) {
lua_pop(m_state, 1);
return RetType::error(FudStatus::Failure);
}
int64_t length;
int isNumber{};
lua_len(m_state, -1);
length = lua_tointegerx(m_state, -1, &isNumber);
lua_pop(m_state, 1);
if (!static_cast<bool>(isNumber) || length < 0) {
lua_pop(m_state, 1);
return RetType::error(FudStatus::Failure);
}
std::vector<std::string> output{};
output.reserve(static_cast<size_t>(length));
for (int64_t index = 1; index <= length; ++index) {
const char* result{nullptr};
lua_pushinteger(m_state, index);
static_cast<void>(lua_gettable(m_state, -2));
result = lua_tolstring(m_state, -1, nullptr);
if (result == nullptr) {
lua_pop(m_state, 1);
return RetType::error(FudStatus::Failure);
}
output.emplace_back(result);
}
lua_pop(m_state, 1);
return RetType::okay(output);
}
} // namespace getsuyomi
|