-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverse-lua.h
299 lines (261 loc) · 9.52 KB
/
traverse-lua.h
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2015 Red Blob Games <[email protected]>
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
/**
* Traverse-Lua extension for Lua 5.2.
*
* Example usage for C++ to Lua:
*
* lua_State* L;
* traverse::LuaWriter luawriter{L};
* visit(luawriter, yourobject);
* // this leaves the object at the top of the lua stack
*
* Example usage for Lua to C++:
*
* // there should be a lua object at the top of the stack
* std::stringstream errors;
* traverse::LuaReader luareader{L, errors};
* visit(luareader, yourobject);
* if (!errors.empty()) { throw "read error"; }
* // the value will be popped off the lua stack
*
*/
#ifndef TRAVERSE_LUA_H
#define TRAVERSE_LUA_H
#include <algorithm>
#include <lua.hpp>
#include "traverse.h"
namespace traverse {
/**
* The LuaWriter will take a value from C++ and push a Lua
* equivalent onto the Lua stack. Ints and floats both become Lua
* numbers. Vectors and structs both become Lua tables.
*/
struct LuaWriter {
lua_State* L;
};
template<typename T> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
visit(LuaWriter& writer, const T& value) {
lua_pushnumber(writer.L, double(value));
}
template <typename T> inline
typename std::enable_if<std::is_enum<T>::value, void>::type
visit(LuaWriter& writer, const T& value) {
visit(writer, typename std::underlying_type<T>::type(value));
}
inline void visit(LuaWriter& writer, const std::string& string) {
lua_pushlstring(writer.L, string.data(), string.size());
}
template<typename Element>
void visit(LuaWriter& writer, const std::vector<Element>& vector) {
lua_createtable(writer.L, vector.size(), 0);
for (size_t i = 0; i != vector.size(); i++) {
visit(writer, vector[i]);
lua_rawseti(writer.L, -2, i+1);
}
}
template<>
struct StructVisitor<LuaWriter> {
const char* name;
LuaWriter& writer;
StructVisitor(const char* name_, LuaWriter& writer_)
: name(name_), writer(writer_) {
lua_newtable(writer.L);
}
template<typename T>
StructVisitor& field(const char* label, const T& value) {
lua_pushstring(writer.L, label);
visit(writer, value);
lua_rawset(writer.L, -3);
return *this;
}
};
/**
* The LuaReader will convert the value at the top of the Lua stack
* to a C++ value, and pop it off the Lua stack. Use lua_pushvalue
* if you need to keep a copy or if you need to convert a value elsewhere
* on the stack.
*
* The reader only checks the structural validity of the data structure; the
* caller must check the semantic validity of the data, e.g.:
*
* - Numbers are in range
* - Enums are valid
* - Strings are valid
* - Vectors have the right length
* - Contents of structs and vectors are valid
*
* If reading untrusted data, write a validation function that runs
* after the LuaReader produces a value.
*
* The reader may not be able to convert some Lua data into C++ data.
* Errors will be written to the 'errors' stream. Check that stream
* before using the value written by the traversal. To ignore a class
* of errors, set the corresponding ignore_ field to true before visit().
*
* - ignore_wrong_type: in case of a type mismatch, leave the value unchanged.
* - ignore_missing_field: if the C++ struct has a field not found in the
* Lua table, leave the value unchanged.
* - ignore_extra_field: if the Lua table has a field not found on the C++
* struct, or if the Lua table has a negative or non-contiguous index
* when converting to a C++ vector, or if the Lua table has a non-numeric
* index when converting to a C++ vector, ignore that field/entry.
*/
struct LuaReader {
lua_State* L;
std::ostream& errors;
bool ignore_wrong_type = false;
bool ignore_missing_field = false;
bool ignore_extra_field = false;
};
template<typename T> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
visit(LuaReader& reader, T& value) {
if (lua_type(reader.L, -1) != LUA_TNUMBER) {
if (!reader.ignore_wrong_type) {
reader.errors << "Error: expected Lua number; skipping" << std::endl;
}
lua_pop(reader.L, 1);
return;
}
if (std::is_integral<T>::value || std::is_enum<T>::value) {
value = T(lua_tointeger(reader.L, -1));
} else {
value = T(lua_tonumber(reader.L, -1));
}
lua_pop(reader.L, 1);
}
template<typename T> inline
typename std::enable_if<std::is_enum<T>::value, void>::type
visit(LuaReader& reader, T& value) {
typename std::underlying_type<T>::type v;
visit(reader, v);
value = T(v);
}
inline void visit(LuaReader& reader, std::string& string) {
if (lua_type(reader.L, -1) != LUA_TSTRING) {
if (!reader.ignore_wrong_type) {
reader.errors << "Error: expected Lua string; skipping" << std::endl;
}
lua_pop(reader.L, 1);
return;
}
size_t size;
const char* data = lua_tolstring(reader.L, -1, &size);
string = std::string(data, size);
lua_pop(reader.L, 1);
}
template<typename Element>
void visit(LuaReader& reader, std::vector<Element>& vector) {
if (!lua_istable(reader.L, -1)) {
if (!reader.ignore_wrong_type) {
reader.errors << "Error: expected Lua array(table); skipping" << std::endl;
}
lua_pop(reader.L, 1);
return;
}
size_t size; // stack: ... input
lua_len(reader.L, -1); // stack: ... input size
visit(reader, size); // stack: ... input
vector.resize(size);
for (size_t i = 0; i < size; i++) {
lua_rawgeti(reader.L, -1, i+1); // stack: ... input input[i+1]
visit(reader, vector[i]); // stack: ... input
}
// Iterate through the table and make sure it doesn't have anything else
lua_pushnil(reader.L); // stack: ... input nil
while (lua_next(reader.L, -2)) { // stack: ... input key value
if (lua_type(reader.L, -2) != LUA_TNUMBER) {
if (!reader.ignore_extra_field) {
reader.errors << "Error: converting Lua table to std::vector, found non-numeric key"
<< std::endl;
}
} else {
lua_Number index = lua_tointeger(reader.L, -2);
if (index < 1 || index > size) {
if (!reader.ignore_extra_field) {
reader.errors << "Error: converting Lua table size=" << size
<< " to std::vector, found key=" << index
<< std::endl;
}
}
}
lua_pop(reader.L, 1); // stack: ... input key
} // stack: ... input
// Remove input from stack
lua_pop(reader.L, 1);
}
template<>
struct StructVisitor<LuaReader> {
const char* name;
LuaReader& reader;
std::vector<std::string> lua_field_names;
bool is_table;
StructVisitor(const char* name_, LuaReader& reader_)
: name(name_), reader(reader_) {
if (!lua_istable(reader.L, -1)) {
if (!reader.ignore_wrong_type) {
reader.errors << "Error: expected Lua object(table) to read into struct "
<< name << "; skipping" << std::endl;
}
is_table = false;
return;
}
is_table = true;
// Iterate through the table to find all the string keys; this
// is used for generating warnings about fields that weren't
// transferred to the C++ side
lua_pushnil(reader.L); // stack: ... input nil
while (lua_next(reader.L, -2)) { // stack: ... input key value
if (lua_type(reader.L, -2) != LUA_TSTRING) {
if (!reader.ignore_extra_field) {
reader.errors << "Error: converting Lua table to " << name
<< ", found non-string key"
<< std::endl;
}
} else {
size_t size = 0;
const char* field = lua_tolstring(reader.L, -2, &size);
lua_field_names.push_back(std::string(field, size));
}
lua_pop(reader.L, 1); // stack: ... input key
} // stack: ... input
}
~StructVisitor() {
lua_pop(reader.L, 1); // stack: ...
if (!reader.ignore_extra_field && !lua_field_names.empty()) {
reader.errors << "Error: Lua object contains extra keys: ";
for (auto field : lua_field_names) {
reader.errors << field << ' ';
}
reader.errors << std::endl;
}
}
template<typename T>
StructVisitor& field(const char* label, T& value) {
if (!is_table) { return *this; }
lua_getfield(reader.L, -1, label); // stack: ... input input[label]
if (lua_isnil(reader.L, -1)) {
if (!reader.ignore_missing_field) {
reader.errors << "Error: Lua object missing field " << label << std::endl;
}
lua_pop(reader.L, 1); // stack: ... input
} else {
if (!reader.ignore_extra_field) {
// To detect this error, we need to track which fields were used
auto it = find(lua_field_names.begin(), lua_field_names.end(), label);
if (it == lua_field_names.end()) {
reader.errors << "Error: lua table lost field during traverse" << std::endl;
} else {
std::swap(*it, lua_field_names.back());
lua_field_names.pop_back();
}
}
visit(reader, value); // stack: ... input
}
return *this;
}
};
}
#endif