summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_metatable.h
blob: 04173a2a5631530dba00aa824032835ac6634fca (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#ifndef _LUA_METATABLE_H_
#define _LUA_METATABLE_H_

#include <map>
#include <cstddef>
#include <functional>

#define LUA_TINTEGER LUA_NUMTAGS + 1
#define LUA_TSTRINGA LUA_NUMTAGS + 2
#define LUA_TSTRINGW LUA_NUMTAGS + 3

union MTValue
{
	void *userdata;
	int boolean;
	lua_Integer integer;
	lua_Number  number;
	const char *string;
	const char *stringA;
	const wchar_t *stringW;
	lua_CFunction function;
};

class CMTField
{
	ptrA name;
	int type;

public:
	CMTField(const char *name, int type) :
		name(mir_strdup(name)), type(type) {}

	virtual ~CMTField() {};

	const char* GetName() const { return name; }
	int GetType() const { return type; }

	static int Compare(const CMTField *p1, const CMTField *p2) {
		return mir_strcmp(p1->name, p2->name);
	}

	virtual MTValue GetValue(void *obj) = 0;
};

template <typename R>
class CMTFieldOffset : public CMTField
{
	ptrdiff_t offset;
	size_t size;

public:
	CMTFieldOffset(const char *name, int type, ptrdiff_t offset, size_t size)
		: CMTField(name, type), offset(offset), size(size) {}
	
	virtual MTValue GetValue(void *obj)
	{
		MTValue value;
		memcpy(&value, ((char*)obj + offset), sizeof(R));
		return value;
	}
};

class CMTFieldFunction : public CMTField
{
	lua_CFunction function;
public:

	CMTFieldFunction(const char *name, lua_CFunction function) :
		CMTField(name, LUA_TFUNCTION), function(function) {}

	virtual MTValue GetValue(void*)
	{
		MTValue value;
		value.function = function;
		return value;
	}
};

template <typename T>
class CMTFieldLambda : public CMTField
{
	int type;
	std::function<MTValue(T*)> lambda;
public:

	CMTFieldLambda(const char *name, int type, decltype(lambda) lambda)
		: CMTField(name, type), lambda(lambda), type(type) {}

	virtual MTValue GetValue(void *obj)
	{
		CMTValue result = { type };
		result.Value = lambda((Obj*)obj);
		return result;
	}
};

template<typename T>
class MT
{
private:
	lua_State *L;

	static const char *name;
	static const luaL_Reg events[];
	static OBJLIST<CMTField> fields;

	static T* Init(lua_State *L)
	{
		luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
		return (T*)lua_touserdata(L, 1);
	}

	static int Get(lua_State *L, T* /*obj*/)
	{
		lua_pushnil(L);
		return 1;
	}

	static bool Set(lua_State *L, T* /*obj*/)
	{
		return false;
	}

	static void Free(lua_State* /*L*/, T **obj)
	{
		*obj = NULL;
	}

	static int lua__new(lua_State *L)
	{
		T **udata = (T**)lua_newuserdata(L, sizeof(T*));
		
		*udata = Init(L);
		if (*udata == NULL)
		{
			lua_pushnil(L);
			return 1;
		}

		luaL_setmetatable(L, MT::name);

		return 1;
	}

	static int lua__gc(lua_State *L)
	{
		T **obj = (T**)luaL_checkudata(L, 1, MT::name);
		MT::Free(L, obj);

		return 0;
	}

	static int lua__bnot(lua_State *L)
	{
		T *obj = *(T**)luaL_checkudata(L, 1, MT::name);
		lua_pushlightuserdata(L, obj);

		return 1;
	}

	static int lua__index(lua_State *L)
	{
		T *obj = *(T**)luaL_checkudata(L, 1, MT::name);
		const char *key = lua_tostring(L, 2);

		const void *tmp[2] = { nullptr, key };
		CMTField *field = fields.find((CMTField*)&tmp);
		if (field == nullptr)
			return Get(L, obj);

		MTValue value = field->GetValue(obj);
		int type = field->GetType();
		switch (type) {
		case LUA_TBOOLEAN:
			lua_pushboolean(L, value.boolean);
			break;
		case LUA_TINTEGER:
			lua_pushinteger(L, value.integer);
			break;
		case LUA_TNUMBER:
			lua_pushnumber(L, value.number);
			break;
		case LUA_TSTRING:
			lua_pushstring(L, value.string);
			break;
		case LUA_TSTRINGA:
			lua_pushstring(L, ptrA(mir_utf8encode(value.stringA)));
			break;
		case LUA_TSTRINGW:
			lua_pushstring(L, T2Utf(value.stringW));
			break;
		case LUA_TLIGHTUSERDATA:
			lua_pushlightuserdata(L, value.userdata);
			break;
		case LUA_TFUNCTION:
			lua_pushcfunction(L, value.function);
			break;
		default:
			lua_pushnil(L);
		}

		return 1;
	}

	static int lua__newindex(lua_State *L)
	{
		T *obj = *(T**)luaL_checkudata(L, 1, MT::name);

		if (!Set(L, obj)) {
			const char *key = lua_tostring(L, 2);
			luaL_error(L, "attempt to index a %s value (%s is readonly)", MT::name, key);
		}

		return 0;
	}

	static int lua__tostring(lua_State *L)
	{
		T *obj = *(T**)luaL_checkudata(L, 1, MT::name);
		CMStringA data(MT::name);
		data += "(";

		for (int i = 0; i < fields.getCount(); i++) {
			CMTField &field = fields[i];

			data += field.GetName();
			data += "=";

			MTValue value = field.GetValue(obj);
			int type = field.GetType();
			switch (type) {
			case LUA_TNIL:
				data.Append("nil");
				break;
			case LUA_TBOOLEAN:
				data.Append(value.boolean == 0 ? "false" : "true");
				break;
			case LUA_TINTEGER:
				data.AppendFormat("%d", value.integer);
				break;
			case LUA_TNUMBER:
				data.AppendFormat("%f", value.number);
				break;
			case LUA_TSTRING:
				data.Append(value.string);
				break;
			case LUA_TSTRINGA:
				data.Append(ptrA(mir_utf8encode(value.stringA)));
				break;
			case LUA_TSTRINGW:
				data.Append(T2Utf(value.stringW));
				break;
			default:
				data.AppendFormat("%s(0x%p)", lua_typename(L, type), value.userdata);
				break;
			}
			data += ", ";
		}
		data += ")";
		lua_pushstring(L, data.c_str());
		return 1;
	}

	static int lua__call(lua_State *L)
	{
		luaL_checktype(L, 1, LUA_TTABLE);
		luaL_checkany(L, 2);

		int nres = lua_gettop(L) - 1;

		lua_getfield(L, 1, "new");
		for (int i = 1; i <= nres; i++)
			lua_pushvalue(L, i + 1);
		luaM_pcall(L, nres, 1);

		return 1;
	}

public:
	MT(lua_State *L, const char *tname)
		: L(L)
	{
		MT::name = tname;

		luaL_newmetatable(L, MT::name);
		luaL_setfuncs(L, events, 0);
		lua_pop(L, 1);

		lua_createtable(L, 0, 1);
		lua_pushcfunction(L, lua__new);
		lua_setfield(L, -2, "new");

		lua_createtable(L, 0, 2);
		lua_pushcfunction(L, lua__call);
		lua_setfield(L, -2, "__call");
		lua_setmetatable(L, -2);
		lua_setglobal(L, MT::name);
	}

	template<typename R>
	MT& Field(R T:: *M, const char *name, int type, size_t size = 0)
	{
		if (size == 0)
			size = sizeof(M);
		size_t offset = (size_t)(&(((T*)0)->*M));
		if (type != LUA_TNONE)
			fields.insert(new CMTFieldOffset<R>(name, type, offset, size));
		return *this;
	}

	template<typename L>
	MT& Field(const L &lambda, const char *name, int type)
	{
		if (type != LUA_TNONE)
			fields.insert(new CMTFieldLambda<T>(name, type, lambda));
		return *this;
	}

	MT& Field(const lua_CFunction function, const char *name)
	{
		fields.insert(new CMTFieldFunction(name, function));
		return *this;
	}

	static void Apply(lua_State *L, T *obj)
	{
		if (obj == NULL) {
			lua_pushnil(L);
			return;
		}

		T **udata = (T**)lua_newuserdata(L, sizeof(T));
		*udata = obj;
		luaL_setmetatable(L, MT::name);
	}
};

template<typename T>
const char *MT<T>::name;

template<typename T>
const luaL_Reg MT<T>::events[] = {
	{ "__index", lua__index },
	{ "__newindex", lua__newindex },
	{ "__bnot", lua__bnot },
	{ "__tostring", lua__tostring },
	{ "__gc", lua__gc },

	{ NULL, NULL },
};

template<typename T>
OBJLIST<CMTField> MT<T>::fields(5, &CMTField::Compare);

#endif //_LUA_METATABLE_H_